public async Task <bool> DeleteHostRecordAsync(HostRecordPost hostToDelete) { //Validations (3) // This area perform validations to the information provided to the function. // Check#1 - Validate input to ensure that there is a host object to delete if (hostToDelete is null) { return(false); } // Check#2 - Ensure that the host is already in the DNS registry var existingHost = await GetHostRecordAsync(hostToDelete.Name); if (existingHost is null) { throw new ArgumentException($"The host {hostToDelete.Name} is does not exist in the server or is invalid. Check your values and try again."); } // Check#3 - Validate the existing host has a valid Base Reference string baseReferenceToValidate = existingHost.BaseRef; if (String.IsNullOrEmpty(baseReferenceToValidate)) { throw new ArgumentOutOfRangeException($"The record doesn't have a valid Base Reference or doesn't exists. Check your values and try again."); } //If everything is good so far... let's go ahead and prepare the info for the HostRecord! string apifunction = "record:host"; string refhost = existingHost.BaseRef; string apipath = $"{helperConfig.ApiRoute}/{helperConfig.ApiVersion}/{apifunction}/{refhost}"; string content = await IBXCallApi(HttpMethod : HttpMethod.Delete, ApiFunction : apifunction, ApiPath : apipath); return(!String.IsNullOrEmpty(content)); }
public async Task <HostRecord> CreateHostRecordAsync(string HostName, string Ipv4Address, string HostMac = null) { // https://10.10.10.10/wapi/v2.9/record:host //Validations (4) // This area perform validations to the information provided to the function. // Check#1 - Validate input to ensure that thereis a hostname and an Ipv4Address if (String.IsNullOrEmpty(HostName) || string.IsNullOrEmpty(Ipv4Address)) { return(default(HostRecord)); } // Check#2 - Ensure that the host is not already in the DNS registry var hostAlreadyExists = await GetHostRecordAsync(HostName); if (hostAlreadyExists != null) { return(hostAlreadyExists); //record for that hostname already exists, return the existing record } // Check#3 - Validate the ipV4 address sent is a valid IP address if ((NetworkUtilities.ParseSingleIPv4Address(Ipv4Address).Value.ToString()) != Ipv4Address) { throw new ArgumentException($"The value of {Ipv4Address} is invalid. Check your values and try again."); } // Check#4 - Validate the ipV4 address is in one of the managed Ip Subnets in the InfoBlox API if (!IsIpv4AddressInSubnetsRange(Ipv4Address)) { throw new ArgumentException($"The value of {Ipv4Address} is not within the range of the subnets managed by the InfoBlox Grid. Check your values and try again."); } //If everything is good so far... let's go ahead and make us a HostRecord! //Add spices to the recipe //Single line model construct. Pick your poison by uncommenting the chosen method. ;-) //HostRecord newHost = new HostRecord() { Name = HostName, Ipv4Addresses = new Ipv4Address[] { new Ipv4Address() { Value = Ipv4Address } } }; //Multi-line model construct. Pick your poison by uncommenting the chosen method. ;-) HostRecordPost newHost = new HostRecordPost(); newHost.Name = HostName; newHost.Ipv4Addresses = new Ipv4AddressPost[] { new Ipv4AddressPost() { Value = Ipv4Address } }; //return newHost.ToJson(); string apifunction = "record:host"; string apipath = $"{helperConfig.ApiRoute}/{helperConfig.ApiVersion}/{apifunction}"; string requestcontent = newHost.ToJson(); string content = await IBXCallApi(HttpMethod : HttpMethod.Post, ApiFunction : apifunction, ApiPath : apipath, RequestContent : requestcontent); HostRecord createdHostRecord = await GetHostRecordAsync(HostName); return(createdHostRecord); }
public async Task <HostRecord> UpdateHostRecordAsync(string HostName, HostRecordPost changedRecord) { // https://10.10.10.10/wapi/v2.9/record:host //Validations (4) // This area perform validations to the information provided to the function. // Check#1 - Validate input to ensure that thereis a hostname and Host object to update if (String.IsNullOrEmpty(HostName) || (changedRecord is null)) { return(default(HostRecord)); } // Check#2 - Ensure that the host is already in the DNS registry var hostAlreadyExists = await GetHostRecordAsync(HostName); if (hostAlreadyExists is null) { throw new ArgumentException($"The hostname {HostName} is does not exist in the server or is invalid. Check your values and try again."); } // Check#3 - Validate the ipV4 address sent is a valid IP address string ipv4AddressToValidate = changedRecord.Ipv4Addresses[0].Value; if ((NetworkUtilities.ParseSingleIPv4Address(ipv4AddressToValidate).Value.ToString()) != ipv4AddressToValidate) { throw new ArgumentException($"The value of {ipv4AddressToValidate} is invalid. Check your values and try again."); } // Check#4 - Validate the ipV4 address is in one of the managed Ip Subnets in the InfoBlox API if (!IsIpv4AddressInSubnetsRange(ipv4AddressToValidate)) { throw new ArgumentException($"The value of {ipv4AddressToValidate} is not within the range of the subnets managed by the InfoBlox Grid. Check your values and try again."); } //If everything is good so far... let's go ahead and prepare the info for the HostRecord! string apifunction = "record:host"; string refhost = hostAlreadyExists.BaseRef; string apipath = $"{helperConfig.ApiRoute}/{helperConfig.ApiVersion}/{apifunction}/{refhost}"; string requestcontent = changedRecord.ToJson(); string content = await IBXCallApi(HttpMethod : HttpMethod.Put, ApiFunction : apifunction, ApiPath : apipath, RequestContent : requestcontent); HostRecord createdHostRecord = await GetHostRecordAsync(changedRecord.Name); return(createdHostRecord); }
static void Main(string[] args) { Console.WriteLine("Welcome InfoBlox Helper Test Console"); Console.WriteLine(Helper.GetVersion()); var _nets = RetrieveNetworks().Result; Console.WriteLine(JsonConvert.SerializeObject(_nets)); Console.ReadKey(); var _subnet = ibxHelper.GetNetworkAsync("10.128.0.0/24").Result; Console.WriteLine(JsonConvert.SerializeObject(_subnet)); Console.ReadKey(); //RetrieveIP().Wait(); var _ip = RetrieveIP().Result; Console.WriteLine(_ip); Console.ReadKey(); Console.Write("Please enter a name for the host to be created: -> "); Console.WriteLine(); string _hostname = $"{Console.ReadLine()}.url.goes.here"; //Write a Record. HostRecord _newRecord = AddNewRecord(_hostname).Result; Console.WriteLine(JsonConvert.SerializeObject(_newRecord)); Console.ReadKey(); //Read a host record. HostRecord _retrieveRecord = GetHostRecord(_hostname).Result; Console.WriteLine(JsonConvert.SerializeObject(_retrieveRecord)); Console.ReadKey(); //Search a host record by IP address. string _ipv4ToSearch = _retrieveRecord.Ipv4Addresses[0].Value; HostRecord _retrieveIpRecord = GetHostByIP(_ipv4ToSearch).Result; Console.WriteLine(JsonConvert.SerializeObject(_retrieveRecord)); Console.ReadKey(); //Update the host record with a new IP address. string _ipv4ToUpdate = ibxHelper.GetIPAsync(1).Result.IPAddresses[0]; HostRecordPost _recordToChange = new HostRecordPost() { Name = _retrieveIpRecord.Name, Ipv4Addresses = new Ipv4AddressPost[] { new Ipv4AddressPost() { Value = _ipv4ToUpdate } } }; HostRecord _updatedRecord = ibxHelper.UpdateHostRecordAsync(_retrieveIpRecord.Name, _recordToChange).Result; Console.WriteLine(JsonConvert.SerializeObject(_updatedRecord)); Console.ReadKey(); //Delete the host record bool _isRecordDeleted = ibxHelper.DeleteHostRecordAsync(_recordToChange).Result; Console.WriteLine(JsonConvert.SerializeObject(_isRecordDeleted)); Console.ReadKey(); //Delete a record that has already been removed (deleted). _isRecordDeleted = ibxHelper.DeleteHostRecordAsync(_recordToChange).Result; Console.WriteLine(JsonConvert.SerializeObject(_isRecordDeleted)); Console.ReadKey(); Console.WriteLine("InfoBlox Helper Test Completed. Press any key to end."); Console.ReadKey(); }