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); }