55
66namespace Elasticsearch . Net
77{
8- public class SniffResponse
8+ public static class SniffParser
99 {
10- //internal ctor - so that only Elasticsearch.Net can instantiate it
11- internal SniffResponse ( ) { }
12-
1310 public static Regex AddressRegex { get ; } = new Regex ( @"^((?<fqdn>[^/]+)/)?(?<ip>[^:]+|\[[\da-fA-F:\.]+\]):(?<port>\d+)$" ) ;
11+ public static Uri ParseToUri ( string boundAddress , bool forceHttp )
12+ {
13+ if ( boundAddress == null ) throw new ArgumentNullException ( nameof ( boundAddress ) ) ;
14+ var suffix = forceHttp ? "s" : string . Empty ;
15+ var match = AddressRegex . Match ( boundAddress ) ;
16+ if ( ! match . Success ) throw new Exception ( $ "Can not parse bound_address: { boundAddress } to Uri") ;
17+
18+ var fqdn = match . Groups [ "fqdn" ] . Value ? . Trim ( ) ;
19+ var ip = match . Groups [ "ip" ] . Value ? . Trim ( ) ;
20+ var port = match . Groups [ "port" ] . Value ? . Trim ( ) ;
21+ var host = ! fqdn . IsNullOrEmpty ( ) ? fqdn : ip ;
22+
23+ return new Uri ( $ "http{ suffix } ://{ host } :{ port } ") ;
24+ }
25+ }
26+ internal class SniffResponse
27+ {
1428
29+ // ReSharper disable InconsistentNaming
30+ // this uses simplejsons bindings
1531 public string cluster_name { get ; set ; }
1632
1733 public Dictionary < string , NodeInfo > nodes { get ; set ; }
@@ -20,34 +36,29 @@ public IEnumerable<Node> ToNodes(bool forceHttp = false)
2036 {
2137 foreach ( var kv in nodes . Where ( n => n . Value . HttpEnabled ) )
2238 {
23- yield return new Node ( this . ParseToUri ( kv . Value . http ? . bound_address . FirstOrDefault ( ) , forceHttp ) )
39+ var info = kv . Value ;
40+ var httpEndpoint = info . http ? . publish_address ;
41+ if ( string . IsNullOrWhiteSpace ( httpEndpoint ) )
42+ httpEndpoint = kv . Value . http ? . bound_address . FirstOrDefault ( ) ;
43+ if ( string . IsNullOrWhiteSpace ( httpEndpoint ) )
44+ continue ;
45+
46+ var uri = SniffParser . ParseToUri ( httpEndpoint , forceHttp ) ;
47+ var node = new Node ( uri )
2448 {
2549 Name = kv . Value . name ,
2650 Id = kv . Key ,
2751 MasterEligible = kv . Value . MasterEligible ,
2852 HoldsData = kv . Value . HoldsData ,
2953 HttpEnabled = kv . Value . HttpEnabled
3054 } ;
55+ //TODO selector
56+ yield return node ;
3157 }
3258 }
33-
34- private Uri ParseToUri ( string boundAddress , bool forceHttp )
35- {
36- if ( boundAddress . IsNullOrEmpty ( ) ) return null ;
37- var suffix = forceHttp ? "s" : string . Empty ;
38- var match = AddressRegex . Match ( boundAddress ) ;
39- if ( ! match . Success ) throw new Exception ( $ "Can not parse bound_address: { boundAddress } to Uri") ;
40-
41- var fqdn = match . Groups [ "fqdn" ] . Value ? . Trim ( ) ;
42- var ip = match . Groups [ "ip" ] . Value ? . Trim ( ) ;
43- var port = match . Groups [ "port" ] . Value ? . Trim ( ) ;
44- var host = ! fqdn . IsNullOrEmpty ( ) ? fqdn : ip ;
45-
46- return new Uri ( $ "http{ suffix } ://{ host } :{ port } ") ;
47- }
4859 }
4960
50- public class NodeInfo
61+ internal class NodeInfo
5162 {
5263 public string name { get ; set ; }
5364 public string transport_address { get ; set ; }
@@ -72,8 +83,9 @@ internal bool HttpEnabled
7283 }
7384 }
7485
75- public class NodeInfoHttp
86+ internal class NodeInfoHttp
7687 {
7788 public IList < string > bound_address { get ; set ; }
89+ public string publish_address { get ; set ; }
7890 }
7991}
0 commit comments