6
6
using System . Reflection ;
7
7
using System . IO ;
8
8
using System . Text ;
9
+ using System . Linq ;
9
10
using System . Globalization ;
10
11
using System . Collections . Generic ;
11
12
using System . Threading ;
20
21
using Microsoft . Extensions . Configuration ;
21
22
using Microsoft . Extensions . Configuration . Binder ;
22
23
using InfoBlox . Automation . Model ;
24
+ using Genvio . Utility . Network ;
23
25
24
26
#endregion
25
27
@@ -28,20 +30,20 @@ namespace InfoBlox.Automation
28
30
public sealed partial class Helper
29
31
{
30
32
//InfoBlox specific options.
31
- public async Task < List < InfobloxNetwork > > GetNetworkListsAsync ( )
33
+ public async Task < InfobloxNetwork > GetNetworkAsync ( string cidrNetwork )
32
34
{
33
35
34
- // https://10.10.10.10/wapi/v2.9/network
36
+ // https://10.10.10.10/wapi/v2.9/network?network=10.10.10.10/8
35
37
36
38
string apifunction = "network" ;
39
+ string apicommand = $ "network={ cidrNetwork } ";
37
40
38
41
// UriBuilder uriBuilder = new UriBuilder();
39
42
uriBuilder . Scheme = scheme ;
40
43
uriBuilder . Port = port ;
41
44
uriBuilder . Host = helperConfig . ServerUri ;
42
45
uriBuilder . Path = $ "{ helperConfig . ApiRoute } /{ helperConfig . ApiVersion } /{ apifunction } ";
43
-
44
- // NewMethod(_acceptInvalidSSL);
46
+ uriBuilder . Query = apicommand ;
45
47
46
48
HttpRequestMessage _reqMessage = new HttpRequestMessage ( ) ;
47
49
@@ -58,6 +60,33 @@ public async Task<List<InfobloxNetwork>> GetNetworkListsAsync()
58
60
return InfobloxNetwork . FromJson ( content ) ;
59
61
}
60
62
63
+ public async Task < InfobloxNetworks > GetNetworkListsAsync ( )
64
+ {
65
+
66
+ // https://10.10.10.10/wapi/v2.9/network
67
+
68
+ string apifunction = "network" ;
69
+
70
+ uriBuilder . Scheme = scheme ;
71
+ uriBuilder . Port = port ;
72
+ uriBuilder . Host = helperConfig . ServerUri ;
73
+ uriBuilder . Path = $ "{ helperConfig . ApiRoute } /{ helperConfig . ApiVersion } /{ apifunction } ";
74
+
75
+ HttpRequestMessage _reqMessage = new HttpRequestMessage ( ) ;
76
+
77
+ _reqMessage . Headers . Authorization = httpclientAuthHeaderValue ;
78
+
79
+ _reqMessage . RequestUri = uriBuilder . Uri ;
80
+ _reqMessage . Method = HttpMethod . Get ;
81
+
82
+ HttpResponseMessage _httpResponse = await httpClient . SendAsync ( _reqMessage ) ;
83
+
84
+ //Get the response back
85
+ string content = await _httpResponse . Content . ReadAsStringAsync ( ) ;
86
+ _httpResponse . EnsureSuccessStatusCode ( ) ;
87
+ return InfobloxNetworks . FromJson ( content ) ;
88
+ }
89
+
61
90
public async Task < IpResult > GetIPAsync ( int totalIPRequested = 1 )
62
91
{
63
92
return await GetIPAsync ( totalIPRequested , String . Empty ) ;
@@ -69,15 +98,13 @@ public async Task<IpResult> GetIPAsync(int totalIPRequested = 1, string subnetIp
69
98
70
99
if ( totalIPRequested <= 0 )
71
100
{
72
- return null ;
101
+ return default ( IpResult ) ;
73
102
}
74
103
75
-
76
104
string apifunction = "network" ;
77
- string refnetwork = "ZG5zLm5ldHdvcmskMTAuMTI4LjAuMC8yNC8w" ; // TODO - Fix
105
+ string refnetwork = FindSubnetBaseRef ( subnetIp ) ;
78
106
string apicommand = "_function=next_available_ip" ;
79
107
80
- // UriBuilder uriBuilder = new UriBuilder();
81
108
uriBuilder . Scheme = scheme ;
82
109
uriBuilder . Port = port ;
83
110
uriBuilder . Host = helperConfig . ServerUri ;
@@ -103,13 +130,14 @@ public async Task<IpResult> GetIPAsync(int totalIPRequested = 1, string subnetIp
103
130
104
131
return IpResult . FromJson ( content ) ;
105
132
}
106
- public async Task < HostRecord > GetHostRecordAsync ( string HostName )
133
+
134
+ public async Task < HostRecord > GetHostRecordAsync ( string HostName ) //FIX - Needs to fix returned reference object.
107
135
{
108
136
//https://10.10.10.10/wapi/v2.9/record:host?name~=host.url.path
109
137
110
138
if ( String . IsNullOrEmpty ( HostName ) )
111
139
{
112
- return null ;
140
+ return default ( HostRecord ) ;
113
141
}
114
142
115
143
string apifunction = "record:host" ;
@@ -144,12 +172,12 @@ public async Task<string> CreateHostRecordAsync(string HostName, string HostMac
144
172
{
145
173
if ( String . IsNullOrEmpty ( HostName ) )
146
174
{
147
- return null ;
175
+ return default ( string ) ;
148
176
}
149
177
150
- IpResult nextIP = instance . GetIPAsync ( 1 ) . Result ;
178
+ IpResult nextIP = GetIPAsync ( 1 ) . Result ;
151
179
152
- return instance . CreateHostRecordAsync ( HostName , nextIP . IPAddresses [ 0 ] , null ) . Result . ToString ( ) ;
180
+ return ( await CreateHostRecordAsync ( HostName , nextIP . IPAddresses [ 0 ] , null ) ) ;
153
181
154
182
}
155
183
public async Task < string > CreateHostRecordAsync ( string HostName , string Ipv4Address , string HostMac = null )
@@ -158,7 +186,7 @@ public async Task<string> CreateHostRecordAsync(string HostName, string Ipv4Addr
158
186
159
187
if ( String . IsNullOrEmpty ( HostName ) || string . IsNullOrEmpty ( Ipv4Address ) )
160
188
{
161
- return null ;
189
+ return default ( string ) ;
162
190
}
163
191
164
192
string apifunction = "record:host" ;
@@ -201,11 +229,22 @@ public async Task<string> CreateHostRecordAsync(string HostName, string Ipv4Addr
201
229
// return IpResult.FromJson(content);
202
230
return ( content ) ;
203
231
}
204
- public async Task UpdateHostRecordAsync ( )
205
- { }
206
- public async Task DeleteHostRecordAsync ( )
207
- { }
208
-
232
+ public async Task < bool > UpdateHostRecordAsync ( HostRecordPost host ) //TODO: Implement
233
+ {
234
+ return false ;
235
+ }
236
+ public async Task < bool > DeleteHostRecordAsync ( HostRecordPost host ) //TODO: Implement
237
+ {
238
+ return false ;
239
+ }
240
+ public async Task < bool > UpdateHostRecordAsync ( string hostName )
241
+ {
242
+ return ( await UpdateHostRecordAsync ( new HostRecord ( ) { Name = hostName } ) ) ;
243
+ }
244
+ public async Task < bool > DeleteHostRecordAsync ( string hostName )
245
+ {
246
+ return ( await DeleteHostRecordAsync ( new HostRecord ( ) { Name = hostName } ) ) ; ;
247
+ }
209
248
210
249
}
211
250
@@ -215,29 +254,25 @@ public sealed partial class Helper
215
254
#region HttpClient instance variables
216
255
private static HttpClient httpClient ;
217
256
private static HttpClientHandler handler = new HttpClientHandler ( ) ;
218
-
219
257
private static AuthenticationHeaderValue httpclientAuthHeaderValue ;
220
258
private static bool isClientInitialized = false ;
221
259
private static UriBuilder uriBuilder = new UriBuilder ( ) ;
222
-
223
- private static List < InfobloxNetwork > infoBloxSubnets ;
224
-
225
- private static InfobloxNetwork defaultSubnet ;
260
+ private static List < InfobloxNetwork > infoBloxSubnets = new List < InfobloxNetwork > ( ) ;
261
+ private static InfobloxNetwork defaultSubnet = new InfobloxNetwork ( ) ;
226
262
227
263
//Configuration settings for the Helper
228
264
private HelperConfiguration helperConfig ;
229
-
230
265
const string scheme = "https" ; //TLS protocol.
231
266
const int port = 443 ; // TLS or SSL default port. Adjust as needed.
232
267
private static bool acceptAnySsl = false ;
233
268
234
-
235
269
#endregion
236
270
237
271
#region Singleton member variables
238
272
239
273
//Singleton Implementation of the Helper
240
274
private static Helper instance = null ;
275
+ private static Assembly assembly ;
241
276
242
277
//Thread Safety object to ensure thread safety.
243
278
private static readonly object padlock = new object ( ) ;
@@ -264,27 +299,29 @@ public static Helper Instance
264
299
}
265
300
public static string GetVersion ( )
266
301
{
302
+ //Reference the local assembly
303
+ assembly = Assembly . GetEntryAssembly ( ) ;
304
+
267
305
StringBuilder versionBuilder = new StringBuilder ( ) ;
268
306
269
- versionBuilder . AppendFormat ( $ "IBXHelper Assembly Version: { Assembly . GetEntryAssembly ( ) . GetName ( ) . Version . ToString ( ) } \n ") ;
270
- versionBuilder . AppendFormat ( $ "IBXHelper File Version: { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) . Version } \n ") ;
271
- versionBuilder . AppendFormat ( $ "IBXHelper Assembly Informational Version: { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) . InformationalVersion } \n ") ;
307
+ versionBuilder . AppendFormat ( $ "IBXHelper Assembly Version: { assembly . GetName ( ) . Version . ToString ( ) } \n ") ;
308
+ versionBuilder . AppendFormat ( $ "IBXHelper File Version: { assembly . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) . Version } \n ") ;
309
+ versionBuilder . AppendFormat ( $ "IBXHelper Assembly Informational Version: { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) . InformationalVersion } \n ") ;
272
310
273
311
return versionBuilder . ToString ( ) ;
274
312
}
275
313
276
314
Helper ( )
277
315
{
278
316
RetrieveConfigurationAsync ( ) . Wait ( ) ;
279
- SslBypassCheckAsync ( ) . Wait ( ) ;
280
- RefreshSubnetsAsync ( ) . Wait ( ) ;
317
+ //RefreshSubnetsAsync().Wait(); //TODO: review
281
318
}
282
319
283
320
private async Task RefreshSubnetsAsync ( )
284
321
{
285
322
try
286
323
{
287
- infoBloxSubnets = await this . GetNetworkListsAsync ( ) ;
324
+ infoBloxSubnets = await GetNetworkListsAsync ( ) ;
288
325
if ( infoBloxSubnets . Count >= 2 )
289
326
{
290
327
SelectDefaultSubnetAsync ( infoBloxSubnets ) ;
@@ -362,6 +399,33 @@ await Task.Run(() =>
362
399
CreateAutorizationContextAsync ( ) . Wait ( ) ;
363
400
364
401
acceptAnySsl = helperConfig . AcceptAnySsl ;
402
+
403
+ SslBypassCheckAsync ( ) . Wait ( ) ;
404
+
405
+ infoBloxSubnets = GetNetworkListsAsync ( ) . Result ;
406
+
407
+ if ( ! String . IsNullOrEmpty ( helperConfig . DefaultNetworkCIDR ) )
408
+ {
409
+ if ( NetworkUtilities . ValidateCidrIp ( helperConfig . DefaultNetworkCIDR ) . Value )
410
+ {
411
+
412
+ //defaultSubnet = instance.GetNetworkAsync(helperConfig.DefaultNetworkCIDR).Result;
413
+ var _subnetResult = from subnet in infoBloxSubnets
414
+ where ( subnet . Network == helperConfig . DefaultNetworkCIDR )
415
+ select subnet ;
416
+
417
+ defaultSubnet = _subnetResult . FirstOrDefault ( ) ;
418
+ }
419
+ }
420
+ else
421
+ {
422
+ //defaultSubnet = instance.GetNetworkAsync(helperConfig.DefaultNetworkCIDR).Result;
423
+ var _subnetResult = ( from subnet in infoBloxSubnets
424
+ select subnet ) . Take ( 1 ) ;
425
+
426
+ defaultSubnet = _subnetResult . FirstOrDefault ( ) ;
427
+ }
428
+
365
429
} ) ;
366
430
}
367
431
catch ( System . Exception )
@@ -395,5 +459,29 @@ await Task.Run(() =>
395
459
} ) ;
396
460
}
397
461
462
+ private string FindSubnetBaseRef ( string subnetIp )
463
+ {
464
+ if ( String . IsNullOrEmpty ( subnetIp ) && String . IsNullOrEmpty ( defaultSubnet . Network ) )
465
+ {
466
+
467
+ throw new Exception ( "No default subnet in the InfoBlox server. Cannot continue. " ) ;
468
+ }
469
+ else
470
+ {
471
+ if ( String . IsNullOrEmpty ( subnetIp ) )
472
+ {
473
+ return defaultSubnet . BaseRef ;
474
+ }
475
+ else
476
+ {
477
+ var searchBaseRef = ( from subnet in infoBloxSubnets
478
+ where ( subnet . Network == subnetIp )
479
+ select subnet . BaseRef ) . FirstOrDefault ( ) ;
480
+ return searchBaseRef ;
481
+ }
482
+ }
483
+
484
+ }
485
+
398
486
}
399
487
}
0 commit comments