Skip to content

Commit 0172e53

Browse files
committed
added functions for Delete, Update, List Network for IBXHelper
1 parent 36aa9aa commit 0172e53

File tree

9 files changed

+169
-57
lines changed

9 files changed

+169
-57
lines changed
File renamed without changes.

IBXHelper.1.0.2.nuspec IBXHelper.1.30.0.nuspec

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
33
<metadata>
44
<id>IBXHelper</id>
5-
<version>1.0.2</version>
5+
<version>1.30.0</version>
66
<authors>Antonio Luevano ([email protected])</authors>
7-
<owners>IBXHelper</owners>
7+
<owners>Genvio Inc.</owners>
88
<requireLicenseAcceptance>false</requireLicenseAcceptance>
99
<description>InfoBlox API C# Library for Azure and Dotnet core 2.1+</description>
10+
<copyright>Copyright ©2019 Genvio Inc.</copyright>
11+
<tags>Infoblox WAPI</tags>
12+
<repository type="git" url="https://github.com/Genvio/InfoBloxHelper" />
1013
<dependencies>
1114
<group targetFramework=".NETCoreApp2.2">
15+
<dependency id="Genvio.Utility" version="1.10.9" exclude="Build,Analyzers" />
1216
<dependency id="Microsoft.Extensions.Configuration" version="2.2.0" exclude="Build,Analyzers" />
1317
<dependency id="Microsoft.Extensions.Configuration.Binder" version="2.2.0" exclude="Build,Analyzers" />
1418
<dependency id="Microsoft.Extensions.Configuration.FileExtensions" version="2.2.0" exclude="Build,Analyzers" />
@@ -18,8 +22,7 @@
1822
</dependencies>
1923
</metadata>
2024
<files>
21-
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Debug\netcoreapp2.2\IBXHelper.dll" target="lib\netcoreapp2.2\IBXHelper.dll" />
22-
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Debug\netcoreapp2.2\Genvio.Utility.dll" target="lib\netcoreapp2.2\Genvio.Utility.dll" />
23-
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Debug\netcoreapp2.2\lib\appsettings.json" target="lib\netcoreapp2.2\appsettings.json" />
25+
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Release\netcoreapp2.2\IBXHelper.dll" target="lib\netcoreapp2.2\IBXHelper.dll" />
26+
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Release\netcoreapp2.2\appsettings.json" target="lib\netcoreapp2.2\appsettings.json" />
2427
</files>
2528
</package>

IBXHelper/Helper.cs

+120-32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using System.IO;
88
using System.Text;
9+
using System.Linq;
910
using System.Globalization;
1011
using System.Collections.Generic;
1112
using System.Threading;
@@ -20,6 +21,7 @@
2021
using Microsoft.Extensions.Configuration;
2122
using Microsoft.Extensions.Configuration.Binder;
2223
using InfoBlox.Automation.Model;
24+
using Genvio.Utility.Network;
2325

2426
#endregion
2527

@@ -28,20 +30,20 @@ namespace InfoBlox.Automation
2830
public sealed partial class Helper
2931
{
3032
//InfoBlox specific options.
31-
public async Task<List<InfobloxNetwork>> GetNetworkListsAsync()
33+
public async Task<InfobloxNetwork> GetNetworkAsync(string cidrNetwork)
3234
{
3335

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
3537

3638
string apifunction = "network";
39+
string apicommand = $"network={cidrNetwork}";
3740

3841
// UriBuilder uriBuilder = new UriBuilder();
3942
uriBuilder.Scheme = scheme;
4043
uriBuilder.Port = port;
4144
uriBuilder.Host = helperConfig.ServerUri;
4245
uriBuilder.Path = $"{helperConfig.ApiRoute}/{helperConfig.ApiVersion}/{apifunction}";
43-
44-
// NewMethod(_acceptInvalidSSL);
46+
uriBuilder.Query = apicommand;
4547

4648
HttpRequestMessage _reqMessage = new HttpRequestMessage();
4749

@@ -58,6 +60,33 @@ public async Task<List<InfobloxNetwork>> GetNetworkListsAsync()
5860
return InfobloxNetwork.FromJson(content);
5961
}
6062

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+
6190
public async Task<IpResult> GetIPAsync(int totalIPRequested = 1)
6291
{
6392
return await GetIPAsync(totalIPRequested, String.Empty);
@@ -69,15 +98,13 @@ public async Task<IpResult> GetIPAsync(int totalIPRequested = 1, string subnetIp
6998

7099
if (totalIPRequested <= 0)
71100
{
72-
return null;
101+
return default(IpResult);
73102
}
74103

75-
76104
string apifunction = "network";
77-
string refnetwork = "ZG5zLm5ldHdvcmskMTAuMTI4LjAuMC8yNC8w"; // TODO - Fix
105+
string refnetwork = FindSubnetBaseRef(subnetIp);
78106
string apicommand = "_function=next_available_ip";
79107

80-
// UriBuilder uriBuilder = new UriBuilder();
81108
uriBuilder.Scheme = scheme;
82109
uriBuilder.Port = port;
83110
uriBuilder.Host = helperConfig.ServerUri;
@@ -103,13 +130,14 @@ public async Task<IpResult> GetIPAsync(int totalIPRequested = 1, string subnetIp
103130

104131
return IpResult.FromJson(content);
105132
}
106-
public async Task<HostRecord> GetHostRecordAsync(string HostName)
133+
134+
public async Task<HostRecord> GetHostRecordAsync(string HostName) //FIX - Needs to fix returned reference object.
107135
{
108136
//https://10.10.10.10/wapi/v2.9/record:host?name~=host.url.path
109137

110138
if (String.IsNullOrEmpty(HostName))
111139
{
112-
return null;
140+
return default(HostRecord);
113141
}
114142

115143
string apifunction = "record:host";
@@ -144,12 +172,12 @@ public async Task<string> CreateHostRecordAsync(string HostName, string HostMac
144172
{
145173
if (String.IsNullOrEmpty(HostName))
146174
{
147-
return null;
175+
return default(string);
148176
}
149177

150-
IpResult nextIP = instance.GetIPAsync(1).Result;
178+
IpResult nextIP = GetIPAsync(1).Result;
151179

152-
return instance.CreateHostRecordAsync(HostName, nextIP.IPAddresses[0], null).Result.ToString();
180+
return (await CreateHostRecordAsync(HostName, nextIP.IPAddresses[0], null));
153181

154182
}
155183
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
158186

159187
if (String.IsNullOrEmpty(HostName) || string.IsNullOrEmpty(Ipv4Address))
160188
{
161-
return null;
189+
return default(string);
162190
}
163191

164192
string apifunction = "record:host";
@@ -201,11 +229,22 @@ public async Task<string> CreateHostRecordAsync(string HostName, string Ipv4Addr
201229
// return IpResult.FromJson(content);
202230
return (content);
203231
}
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+
}
209248

210249
}
211250

@@ -215,29 +254,25 @@ public sealed partial class Helper
215254
#region HttpClient instance variables
216255
private static HttpClient httpClient;
217256
private static HttpClientHandler handler = new HttpClientHandler();
218-
219257
private static AuthenticationHeaderValue httpclientAuthHeaderValue;
220258
private static bool isClientInitialized = false;
221259
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();
226262

227263
//Configuration settings for the Helper
228264
private HelperConfiguration helperConfig;
229-
230265
const string scheme = "https"; //TLS protocol.
231266
const int port = 443; // TLS or SSL default port. Adjust as needed.
232267
private static bool acceptAnySsl = false;
233268

234-
235269
#endregion
236270

237271
#region Singleton member variables
238272

239273
//Singleton Implementation of the Helper
240274
private static Helper instance = null;
275+
private static Assembly assembly;
241276

242277
//Thread Safety object to ensure thread safety.
243278
private static readonly object padlock = new object();
@@ -264,27 +299,29 @@ public static Helper Instance
264299
}
265300
public static string GetVersion()
266301
{
302+
//Reference the local assembly
303+
assembly = Assembly.GetEntryAssembly();
304+
267305
StringBuilder versionBuilder = new StringBuilder();
268306

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");
272310

273311
return versionBuilder.ToString();
274312
}
275313

276314
Helper()
277315
{
278316
RetrieveConfigurationAsync().Wait();
279-
SslBypassCheckAsync().Wait();
280-
RefreshSubnetsAsync().Wait();
317+
//RefreshSubnetsAsync().Wait(); //TODO: review
281318
}
282319

283320
private async Task RefreshSubnetsAsync()
284321
{
285322
try
286323
{
287-
infoBloxSubnets = await this.GetNetworkListsAsync();
324+
infoBloxSubnets = await GetNetworkListsAsync();
288325
if (infoBloxSubnets.Count >= 2)
289326
{
290327
SelectDefaultSubnetAsync(infoBloxSubnets);
@@ -362,6 +399,33 @@ await Task.Run(() =>
362399
CreateAutorizationContextAsync().Wait();
363400

364401
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+
365429
});
366430
}
367431
catch (System.Exception)
@@ -395,5 +459,29 @@ await Task.Run(() =>
395459
});
396460
}
397461

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+
398486
}
399487
}

IBXHelper/IBXHelper.1.30.0.nuspec

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
3+
<metadata>
4+
<id>IBXHelper</id>
5+
<version>1.30.0</version>
6+
<authors>Antonio Luevano ([email protected])</authors>
7+
<owners>Genvio Inc.</owners>
8+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
9+
<description>InfoBlox API C# Library for Azure and Dotnet core 2.1+</description>
10+
<copyright>Copyright ©2019 Genvio Inc.</copyright>
11+
<tags>Infoblox WAPI</tags>
12+
<repository type="git" url="https://github.com/Genvio/InfoBloxHelper" />
13+
<dependencies>
14+
<group targetFramework=".NETCoreApp2.2">
15+
<dependency id="Genvio.Utility" version="1.10.9" exclude="Build,Analyzers" />
16+
<dependency id="Microsoft.Extensions.Configuration" version="2.2.0" exclude="Build,Analyzers" />
17+
<dependency id="Microsoft.Extensions.Configuration.Binder" version="2.2.0" exclude="Build,Analyzers" />
18+
<dependency id="Microsoft.Extensions.Configuration.FileExtensions" version="2.2.0" exclude="Build,Analyzers" />
19+
<dependency id="Microsoft.Extensions.Configuration.Json" version="2.2.0" exclude="Build,Analyzers" />
20+
<dependency id="Newtonsoft.Json" version="12.0.1" exclude="Build,Analyzers" />
21+
</group>
22+
</dependencies>
23+
</metadata>
24+
<files>
25+
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Release\netcoreapp2.2\IBXHelper.dll" target="lib\netcoreapp2.2\IBXHelper.dll" />
26+
<file src="D:\Projects\KPMG\InfoBloxHelper\IBXHelper\bin\Release\netcoreapp2.2\appsettings.json" target="lib\netcoreapp2.2\appsettings.json" />
27+
</files>
28+
</package>

IBXHelper/IBXHelper.csproj

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
<AssemblyVersion>1.0.2.0</AssemblyVersion>
6-
<FileVersion>1.0.2.0</FileVersion>
7-
<Version>1.0.2.0-ktech</Version>
5+
<AssemblyVersion>1.30.0</AssemblyVersion>
6+
<FileVersion>1.30.0</FileVersion>
7+
<Version>1.30.0</Version>
88
<Company>Genvio Inc.</Company>
99
<Authors>Antonio Luevano</Authors>
1010
<PackageProjectUrl></PackageProjectUrl>
1111
<PackageIconUrl></PackageIconUrl>
1212
<PackageTags>Infoblox WAPI</PackageTags>
1313
<Copyright>Copyright ©2019 Genvio Inc.</Copyright>
1414
<Description>InfoBlox API C# Library for Azure and Dotnet core 2.1+</Description>
15-
<PackageLicenseUrl>http://opensource.org/licenses/MS-PL</PackageLicenseUrl>
1615
<NeutralLanguage>en</NeutralLanguage>
1716
<RepositoryUrl>https://github.com/Genvio/InfoBloxHelper</RepositoryUrl>
1817
<RepositoryType>git</RepositoryType>
@@ -38,7 +37,7 @@
3837
</PropertyGroup>
3938

4039
<ItemGroup>
41-
<PackageReference Include="Genvio.Utility" Version="1.10.2" />
40+
<PackageReference Include="Genvio.Utility" Version="1.10.9" />
4241
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
4342
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0" />
4443
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />

0 commit comments

Comments
 (0)