Reply
Accepted Solution

How to create static DHCP record with API?

[ Edited ]
darkmallard
Techie
Posts: 7
5673     0

I am using PowerShell with a POST method with JSON, but I think the syntax may not be correct, because I only get 400 Bad Request. I'm able to use JSON to create a NON-DHCP host record without a problem.

 

I need to be able to:

 

create a host record with an IP address

enable it for DHCP

set the MAC address

set the name

 

Here's are just a couple examples I've tried:

 

{
    "ipv4addrs":  [
                      {
                          "ipv4addr":  "10.252.240.12",
                          "configure_for_dhcp":  true,
                          "mac":  "ABCDEFGHIJ123456"    <----- I've tried with colons also
                      }
                  ],
    "name":  "test3.nowhere.com",
    "configure_for_dns":  false
}

 

And another one without any hostname still doesn't work:

 

{
    "ipv4addrs":  [
                      {
                          "ipv4addr":  "10.252.240.12",
                          "configure_for_dhcp":  true,
                          "mac":  "AB:CD:EF:GH:IJ:12:34:56"   <--- I've tried without the colons also
                      }
                  ]
}

 

Any help would be appreciated!

Re: How to create static DHCP record with API?

Adviser
Posts: 357
5673     0

MAC addresses are only 48 bits, not 64. So, your MAC address should only have 6 segments, not 8.

DISREGARD: How to create static DHCP record with API?

darkmallard
Techie
Posts: 7
5673     0

It was just a typo in the MAC address. The JSON works fine. I wish I could delete forum posts. Smiley Happy

Re: DISREGARD: How to create static DHCP record with API?

Adviser
Posts: 43
5673     0

Powershell doesn't readily show you the actual error response output from Infoblox.  It only tells you there is a 400 error, which makes it hard to debug errors like this.

 

To get the error response, you can wrap your InvokeRest call in a try/catch block and output the error that gets returned in the 400 response:
E.g.

 

try {

 

  $RestResult= Invoke-RestMethod $Url -Method $Method -Credential $Cred -Verbose 

 

} catch {

    # Dig into the exception to get the Response details.

    # Note that value__ is not a typo.

$error= $_

Write-Host "Error -" $error

# Find and output actual Infoblox error

        if ($error.Exception.Response) {

        $InfobloxError = $error.Exception.Response.GetResponseStream()

        $reader = New-Object System.IO.StreamReader($InfobloxError)

        $responseBody = $reader.ReadToEnd();

        Write-Host $responseBody

        }

 

Re: DISREGARD: How to create static DHCP record with API?

darkmallard
Techie
Posts: 7
5673     0

Thanks for the error extrapolation idea! I should have checked there first.

Re: DISREGARD: How to create static DHCP record with API?

Adviser
Posts: 43
5673     0

No problem.  Took me a while to figure out how to do this in Powershell, so glad to have an opportunity to share it.

Highlighted

Re: DISREGARD: How to create static DHCP record with API?

ljohnston11
Techie
Posts: 1
5673     0

Could you share your entire power shell code? how to build the $URI string and the Invoke-RestMethod call?

 

Thanks

Re: DISREGARD: How to create static DHCP record with API?

[ Edited ]
Adviser
Posts: 43
5674     0

I am by no means a Powershell developer, so comments/suggestions on improving the code below are greatly appreciated.

 

Here's the function call:

 

 

 Function CallRestMethod {

# Crafts the InvokeRestMethod call based on whether it is an Infoblox record create, read, update, or delete 

 

Param ($Url, $Method, $Data, $ContentType)

 

# Convert User Credentials to Object for use with Invoke Rest Method

 

$Cred= New-Object PSCredential -ArgumentList $username,$secPw

 

# Try to make REST call and catch if Fails

try {

if (!$Data) {

Write-Host "Data Not Found"

 

            $RestResult = Invoke-RestMethod $Url -Method $Method -Credential $Cred 

}

Else

{

 

 

Write-Host "Data Found"

            $RestResult = Invoke-RestMethod $Url -Method $Method -Credential $Cred -Body $Data -ContentType $ContentType

 

}

 

} catch {

    # Dig into the exception to get the Response details.

    # Note that value__ is not a typo.

$error= $_

Write-Host "Error on API call -" $error

# Find and output actual Infoblox error

        if ($error.Exception.Response) {

        $InfobloxError = $error.Exception.Response.GetResponseStream()

        $reader = New-Object System.IO.StreamReader($InfobloxError)

        $responseBody = $reader.ReadToEnd();

    Write-Host $responseBody

        }

}

 

return $RestResult

 

I set the $username and $secPW in my script via a prompt before I call the function

          $username = Read-Host -Prompt 'Admin User Name'

            $secPw = Read-Host 'Password' -AsSecureString 

 

And I use these functions to make the rest call with different parameters depending on whether it is create, read, update, or delete

 

 Function ReadIBRecord {

# Retrieve Infoblox Record from Grid Master

 

Param ($RecordType,$QueryString)

if ($QueryString) {

$Query= "?" + $QueryString

}

$method= "Get"

#Write-Host "Getting Infoblox Record Type"$RecordType" with Query ?"$QueryString

$url= "https://" + $gridmaster + "/wapi/" + $WAPIversion + "/" + $RecordType + $Query

$result= CallRestMethod $url $method

return ,$result

}

 

Function CreateIBRecord {

# Create Infoblox Record

 

Param ($RecordType,$QueryString,$contentType,$data)

if ($QueryString) {

$Query= "?" + $QueryString

}

$method= "Post"

$url= "https://" + $gridmaster + "/wapi/" + $WAPIversion + "/" + $RecordType + $Query

Write-Host "Creating Infoblox Record Type"$RecordType" with Content-Type:"$contentType" and Data "$data

$result= CallRestMethod $url $method $data $contenttype 

return ,$result

}

 

Function UpdateIBRecord {

# Update Infoblox Record

 

Param ($Record,$QueryString,$contentType,$data)

if ($QueryString) {

$Query= "?" + $QueryString

}

$method= "Put"

$url= "https://" + $gridmaster + "/wapi/" + $WAPIversion + "/" + $Record + $Query

Write-Host "Updating Infoblox Record" $Record" with Content-Type:"$contentType" and Data "$data

$result= CallRestMethod $url $method $data $contenttype

return ,$result

}

 

Function DeleteIBRecord {

# Delete Infoblox Record

 

Param ($Record)

$method = "Delete"

Write-Host "Deleting Infoblox Record:" $Record

$url= "https://" + $gridmaster + "/wapi/" + $WAPIversion + "/" + $Record 

$result= CallRestMethod $url $method

return ,$result

 

An example of calling one of the above functions is

 

 #Get Host Records 

$record = "record:host"

$q = "name=linux-12.cloud.zone"

$newresult = ReadIBRecord $record $q

write $newresult._ref 

 

with the following global variables:

 

$gridmaster = "10.10.100.51"

$WAPIversion= "v1.4.2"

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You