Infoblox Exchange Cybersecurity Roadshow 2020 – Join us!
North America | Europe | Middle East/Africa | Asia-Pacific

API & Integration

Reply
Accepted Solution

Convert leased IPv4 address in DHCP range to Fixedaddress object

[ Edited ]
GregP
Techie
Posts: 6
6426     0

I'm trying to write a function to "convert" and existing DHCP lease to a fixed address.  There seems to be a few posts on this that are 'close' to what I'm wanting to do, but not exactly the same.  I'm not trying to create a net "new" fixed address, but rather "convert" a DHCP lease to a fixed address.  The use case here is automated server provisioning.  We leave the servers configured for DHCP and then use DHCP fixed addresses to essentially force the server's IP to be static, even though the server is configured for DHCP.

 

In the GUI, this is a simple click on the gear icon next to the IP address and choosing "convert -> fixed address"  This action retains the MAC address that was already in the lease, so effectively this IP address is now "reserved" only for that MAC address (server).  The server will in effect have a static IP, though we don't have to customize it's internal IP configuration post-deployment.

 

I'm testing this in Postman, and I have done GET's before and after conversion to see what fields changed:

 

 
[
    {
        "_ref": "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuNS4wLjE4NS8w:10.5.0.185",
        "dhcp_client_identifier": "\"\\001\\000PV\\262\\\"X\"",
        "ip_address": "10.5.0.185",
        "is_conflict": false,
        "lease_state": "ACTIVE",
        "mac_address": "00:50:56:b2:22:58",
        "names": [
            "prdadfs01.test.com",
            "prdadfs01"
        ],
        "network": "10.5.0.0/24",
        "network_view": "default",
        "objects": [
            "record:a/ZG5zLmJpbmRfYSQuX2RlZmF1bHQubG9jYWwud3d0LHByZGFkZnMwMSwxMC41LjAuMTg1Smiley Tonguerdadfs01.test.com/Internal",
            "lease/ZG5zLmxlYXNlJDEvMTAuNS4wLjE4NS8wLw:10.5.0.185/default",
            "lease/ZG5zLmxlYXNlJDIvMTAuNS4wLjE4NS8wLw:10.5.0.185/default",
            "recordSmiley Tonguetr/ZG5zLmJpbmRfcHRyJC5fZGVmYXVsdC5hcnBhLmluLWFkZHIuMTAuNS4wLjE4NS5wcmRhZGZzMDEud3d0LmxvY2Fs:185.0.5.10.in-addr.arpa/Internal"
        ],
        "status": "USED",
        "types": [
            "A",
            "LEASE",
            "PTR",
            "DHCP_RANGE"
        ],
        "usage": [
            "DNS",
            "DHCP"
        ]
    }
]
 
 
NOTE: Changing the DHCP lease to a fixed address requires hup'ing DHCPD... will need to issue this via REST as well
 
Here is what the same GET returns after conversion:
 
[
    {
        "_ref": "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuNS4wLjE4NS8w:10.5.0.185",
        "ip_address": "10.5.0.185",
        "is_conflict": false,
        "lease_state": "FREE",
        "mac_address": "00:50:56:b2:22:58",
        "names": [
            "unknown",
            "prdadfs01.test.com"
        ],
        "network": "10.5.0.0/24",
        "network_view": "default",
        "objects": [
            "fixedaddress/ZG5zLmZpeGVkX2FkZHJlc3MkMTAuNS4wLjE4NS4wLi4:10.5.0.185/default",
            "record:a/ZG5zLmJpbmRfYSQuX2RlZmF1bHQubG9jYWwud3d0LHByZGFkZnMwMSwxMC41LjAuMTg1Smiley Tonguerdadfs01.test.com/Internal",
            "recordSmiley Tonguetr/ZG5zLmJpbmRfcHRyJC5fZGVmYXVsdC5hcnBhLmluLWFkZHIuMTAuNS4wLjE4NS5wcmRhZGZzMDEud3d0LmxvY2Fs:185.0.5.10.in-addr.arpa/Internal"
        ],
        "status": "USED",
        "types": [
            "FA",
            "A",
            "PTR",
            "DHCP_RANGE"
        ],
        "usage": [
            "DHCP",
            "DNS"
        ]
    }
]

 

Do I need to change each of the fields independantly or is there a master "convert" function (e.g. PUT) that can just switch the root record type from a lease in a DHCP range to a fixed address in a DHCP range?  I'm thinking there should be a fairly simple way to do this in the API, since the GUI already supports it.

 

Any suggestions appreciated!

 

Best regards,

Greg

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

[ Edited ]
GHorne Community Manager
Community Manager
Posts: 248
6427     0

Really, you don't need to do anything with the lease. 

Once you create the fixed address; the next time the client renews it's address, the fixed address setting will override anything else and the lease state will just update with a long (infinite lease time).

 

You don't need the lease to be 'free' for this to work. We just do that as part of the GUI conversion process because, well, we can

 

You should just be able to do the following:

 - GET /lease to get the lease object for the address

 - scrape the MAC and any other useful info from the lease

 - POST /fixedaddress to create the fixed addr

 - restart services (which you don't need to do with later versions of NIOS 8)

 

 

 

 

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

[ Edited ]
GregP
Techie
Posts: 6
6427     0

I eventually answered my own question, but for the benefit of others who are new to the WAPI and get stuck on this same terminology confusion, I will post my solution. The TLDR here is I was not able to find a WAPI call to "convert" a DHCP lease to a DHCP fixed address, even though this exact feature is available in the GUI. It turns out that just creating a new fixed address and feeding it the same IP and MAC that were associated with the lease will actually leave you with the same net effect as doing the conversion operation in the GUI (I did before/after comparisions and recevied the exact same response data).

 

Before delving into the code, I will also state that myself and a number of other sysadmins in the industry get tripped up on another unconventional use of terminology with regards to DHCP in NIOS. That being the terms "Fixed Address" and "DHCP Reservation." Many of us "old-school" guys consider a DHCP reservation to be what Infoblox calls a "Fixed Address"--meaning the assignment of a "permanent" lease to one specific MAC address, so that device always receives the same IP address when it does a DHCP request. The term "Fixed Address" to us sounds like it means "Static IP", or DHCP exclusion -- which is exactly what NIOS means by a DHCP Reservation. The odd use of the word reservation here really means the opposite of what you'd think: It's not "reserved" for DHCP, it's actually reserved for a statically assigned address! None of this is a big deal, once you are used to it. Just be aware of these terms before you start searching for WAPI examples. Trust me, it will save lots of time!

 

Without any further ado, here is how I implemented what is effectively a "conversion" of a DHCP Lease issued to a given MAC to a DHCP "Fixed Address": In my use case, I was developing code to be used as part of automated VM server builds. Our design deploys the VM via PXE-boot from Razor, and our intent is to leave the server configured for DHCP, but essentially give it a static IP that is controlled by Infoblox. To do that, we wanted convert the DHCP lease the new VM just obtained to a fixed address, which we already knew we could do in the GUI.

 

In my use case, I was developing code to be used as part of automated VM server builds.  Our design deploys the VM via PXE-boot from Razor, and our intent is to leave the server configured for DHCP, but essentially give it a static IP that is controlled by Infoblox. To do that, we wanted convert the DHCP lease the new VM just obtained to a fixed address, which we already knew we could do in the GUI.

 

I accomplished this by writing three PowerShell functions, which basically do a Invoke-RESTmethod passing the URI and (if needed) the BODY back to /wapi on the grid master.

 

 

 

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

GregP
Techie
Posts: 6
6427     0

LOL!  Thanks GHorne, I assume you were posting while I was still wrestling with the editor to post my update, but yes, I ended up doing exactly what you suggested.  It just wasn't obvious to me not being all that familiar with the API documentation.

 

 

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

[ Edited ]
GregP
Techie
Posts: 6
6427     0

N.B.: Had some challenges with the post editor. Will try to outline my REST calls here in case they help someone in the future....

 

I accomplished this by writing three PowerShell functions, which basically do a Invoke-RESTmethod passing the URI and (if needed) the BODY back to /wapi on the grid master.

 

Step 1: Create fixed address:

 

            URI:     https://wapi.test.com/wapi/v2.2/fixedaddress

 

            BODY: {

                        "ipv4addr":"10.5.0.185",

                        "mac":"00:50:56:b2:22:58",

                        "match_client":"MAC_ADDRESS",

                        "name":"server.test.com"

                       }

 

Step 2: Restart IB DHCP services (this is triggered when you make this change since the dhcpd.conf file must be changed and re-read).  To do this we need to first get the _ref to our particular Grid to use in the restart function call:

            2a) Get Grid object reference:

 

            URI:  https://wapi.test.com/wapi/v2.2/grid

 

            This returns something like: "_ref": "grid/a25lLmNsdXN0ZXIkMN:YOURGRIDNAME"

 

            I return that in a variable and use in the next step:

 

           

            2b) Restart DHCP services. Since this particular syntax will only restart services that the grid deems to need restarting, it can be used for any change that requires a service restart:

 

            URI: This is created in my function by concatenating the URI in step 2A with the return value from step 2A.

 

https://wapi.test.com/wapi/v2.2/grid/a25lLmNsdXN0ZXIkMN:YOURGRIDNAME?_function=restartservices

 

                BODY: {

                                "restart_option":"RESTART_IF_NEEDED",

                                "service_option":"ALL",

                                "member_order":"SEQUENTIALLY",

                                "sequential_delay":10

                              }

 

 

 

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

[ Edited ]
GregP
Techie
Posts: 6
6427     0

Here is the PowerShell code for the steps above, in case it helps someone learn to use the WAPI in PowerShell.  There are three functions for clarity, but the one that you would normally call would be Set-IBfixedAddress{}, and it calls the other two where appropriate.  There is little or no error-checking here, the intent is to keep the code simple just to highilght what is necessary to make the REST calls shown above:

 

 

function Get-IBgrid{
#Returns _ref object representing this IB's grid ID
#Required by Restart-IBservices()
    Param
    (
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        $infobloxHost,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        $infobloxCreds
    )

    $URI = "https://$($infobloxHost)/wapi/v2.2/grid"

    $GridRef = Invoke-RestMethod -Method Get -Uri $URI -Credential $infobloxCreds

    return $GridRef._ref

}



function Restart-IBservices{ # Restarts ALL IB services that need a HUP signal # Restarts the service ONLY if it has changed config data to read. Param ( [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] $infobloxHost, [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] $infobloxCreds, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $GridRef ) $URI = "https://$($infobloxHost)/wapi/v2.2/$($GridRef)?_function=restartservices" $BODY = @" { "restart_option":"RESTART_IF_NEEDED", "service_option":"ALL", "member_order":"SEQUENTIALLY", "sequential_delay":10 } "@ $SVCstatus = Invoke-RestMethod -Uri $URI -Method Post -Body $BODY -Credential $infobloxCreds -ContentType 'application/json' return $SVCstatus }

function Set-IBfixedAddress{ # Converts a DHCP address to a Fixed Address # Requirements: Get-IBgrid{}, Restart-IBservices{} from this module. Param ( [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] $infobloxHost, [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] $infobloxCreds, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $IP, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $MAC, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $VMhostname ) $URI = "https://$($infobloxHost)/wapi/v2.2/fixedaddress" $BODY = @" { "ipv4addr":"$IP", "mac":"$MAC", "match_client":"MAC_ADDRESS", "name":"$VMhostname" } "@ ## Convert DHCP lease to fixed address on the grid $FAstatus = Invoke-RestMethod -Uri $URI -Method Post -Body $BODY -Credential $infobloxCreds -ContentType 'application/json' ## Get Grid obj reference needed for grid services rstart $Grid = Get-IBgrid -infobloxHost $infobloxHost -infobloxCreds $infobloxCreds ## Give the grid time to sync after making the changes so the correct config is re-read. start-sleep 10 ## Restart grid services that have updated config files to read. $SVCrestart = Restart-IBservices -infobloxHost $infobloxHost -infobloxCreds $infobloxCreds -GridRef $Grid return $FAstatus }

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

GregP
Techie
Posts: 6
6427     0

Thanks so much GHorne!  I actually came to this same conclusion late last night, and I should have just tried it earlier in the day.  It wasn't readily apparent (at least to me) that the "convert" operation in the GUI wasn't represented directly by an API call.

 

Your steps are more complete than the ones I listed below, but in my case, I am doing this as part of a much larger script which is creating the server, so my script already has the $MAC, $IP, and $HOSTNAME of the new server, which is the same data I would scrape from the lease, so I basically just overwrite it with those values I already have in memory.

Re: Convert leased IPv4 address in DHCP range to Fixedaddress object

Posts: 1
6427     0

@GregP, Can you please clarify this.

When I say reserve a static IP address, does it mean dhcp reservation or a fixed address reservation in infoblox term?

Showing results for 
Search instead for 
Do you mean 

Recommended for You