Reply
Accepted Solution

Python API code for updating a host record's IPv4 address

[ Edited ]
leesweet
Techie
Posts: 5
6894     0

I've been exploring this for several days, and, like many, not getting too far at my first attempt with WAPI programming!  I have demo Python programs working, so, I'm over that hurdle.  It's the depths of the host record that are throwing me.  I've seen some mention of having to read it, write it again, and then update it?

 

What we're doing is trying to create something to use with VMware's SRM, since we need to change DNS host records as VMs are moved from one datacenter to another (and back), and we don't use any of the Orchestration products that seem to have something like this integrated.

 

Anyone have a code sample/fragment they can donate/whip up for this?  All we need is a host record by FQDN in one view, and update the IPv4 adddress.  Thanks much!

 

Edit:  I have seen the sticky, but I'm still unclear on how to change the IP address (never mind actually write anything back out...), and also how to search one view.  I can get the host record, if it's unique (only once in all views...).

Re: Python API code for updating a host record's IPv4 address

Moderator BRampling
Moderator
Posts: 69
6894     0

I'll show you these using curl commands and leave it to you to convert to python. In my system I have two DNS views called "default" and "testview". Each view has a host record called "host.test.com". 

 

First, here's how you get the host record object reference from the "default" view:

 

curl -k1 -u admin:infoblox -H "Content-Type: application/json" -X GET https://10.60.27.4/wapi/v2.5/record:host -d '{"view":"default","name":"host.test.com"}'

[
    {
        "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS50ZXN0Lmhvc3Q:host.test.com/default", 
        "ipv4addrs": [
            {
                "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLnRlc3QuaG9zdC4xLjEuMS4xLg:1.1.1.1/host.test.com/default", 
                "configure_for_dhcp": false, 
                "host": "host.test.com", 
                "ipv4addr": "1.1.1.1"
            }
        ], 
        "name": "host.test.com", 
        "view": "default"
    }
]

Now get the object reference for the host record in the "testview" view:

 

curl -k1 -u admin:infoblox -H "Content-Type: appliction/json" -X GET https://10.60.27.4/wapi/v2.5/record:host -d '{"view":"testview","name":"host.test.com"}'

[
    {
        "_ref": "record:host/ZG5zLmhvc3QkLjEuY29tLnRlc3QuaG9zdA:host.test.com/testview", 
        "ipv4addrs": [
            {
                "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuMS5jb20udGVzdC5ob3N0LjIuMi4yLjIu:2.2.2.2/host.test.com/testview", 
                "configure_for_dhcp": false, 
                "host": "host.test.com", 
                "ipv4addr": "2.2.2.2"
            }
        ], 
        "name": "host.test.com", 
        "view": "testview"
    }
]

Now that I have the object reference, I can make changes. I'll change the IP of the record in the "testview" view to 3.3.3.3:

curl -k1 -u admin:infoblox -H "Content-Type: application/json" -X PUT https://10.60.27.4/wapi/v2.5/record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuMS5jb20udGVzdC5ob3N0LjIuMi4yLjIu:2.2.2.2/host.test.com/testview -d '{"ipv4addr":"3.3.3.3"}'

"record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuMS5jb20udGVzdC5ob3N0LjMuMy4zLjMu:3.3.3.3/host.test.com/testview"

When I run the search again I see the new address:

curl -k1 -u admin:infoblox -H "Content-Type: appliction/json" -X GET https://10.60.27.4/wapi/v2.5/record:host -d '{"view":"testview","name":"host.test.com"}'

[
    {
        "_ref": "record:host/ZG5zLmhvc3QkLjEuY29tLnRlc3QuaG9zdA:host.test.com/testview", 
        "ipv4addrs": [
            {
                "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuMS5jb20udGVzdC5ob3N0LjMuMy4zLjMu:3.3.3.3/host.test.com/testview", 
                "configure_for_dhcp": false, 
                "host": "host.test.com", 
                "ipv4addr": "3.3.3.3"
            }
        ], 
        "name": "host.test.com", 
        "view": "testview"
    }
]  

 

Re: Python API code for updating a host record's IPv4 address

leesweet
Techie
Posts: 5
6894     0

Thanks, but I think I need a lower-level answer.  I have no idea how you would 'move' the answer to the first requests (where you get the _ref string you need to reuse) to the update command.

 

How exactly do you store the answers with the 'keys' or whatever the long unique strings are so you can reuse them in the PUT command when you want to update things like the IP address here?

 

Appreciate the tips, I think that's the only thing missing.  The conversion to Python is googlable, I do believe. Smiley Happy

Re: Python API code for updating a host record's IPv4 address

leesweet
Techie
Posts: 5
6894     0

Why are these being accepted as 'solutions' when there are still open questions by the original submitter?  What was posted is not a complete answer and I sure didn't accept it as a 'solution'.

Re: Python API code for updating a host record's IPv4 address

Adviser
Posts: 213
6894     0

Once you GET the data and have it from your first command, you have to put the data back into the body of the message to PUT (or POST) it.  There is no URL that would allow you to pass all data for all fields.

 

If you look at the WAPI documentation, you'll see an example that shows using curl with the "-d" option.  The data following is either what's new or what's updated.  When you include the JSON syntax for the data, you'll want to pass back the object you are modifying.

 

Re: Python API code for updating a host record's IPv4 address

Adviser
Posts: 63
6894     0

Hi,

 

A bit late to the game, but I think the infoblox-client python module could come handy here :

 

opts = {'host': '192.168.1.10', 'username': 'admin', 'password': 'admin'}

conn = connector.Connector(opts)

my_ip = objects.IP.create(ip='192.168.1.25', mac='aa:bb:cc:11:22:33')

record = objects.HostRecord.create(conn, view='my_dns_view', name='my_host_record.my_zone.com', ip=my_ip)

 

Please see https://infoblox-client.readthedocs.io/en/stable/ for the full documentation.

Re: Python API code for updating a host record's IPv4 address

lgilroy
Techie
Posts: 1
6894     0

All,

 

I have been following this code and I can get the API to add a single record:host at a time, is there any way to send multiple adds as a list in the post data.  Example I was trying .. 

 

So this first eample the payload works and add the record but I was wondering if there is anyway to add 2 or more records with one call ?  I removed the real IPs I am usin for this.

URI = 'record:host'
requestStruct['request_payload'] = {
            "name":"lloydtest1.abc.abc",
            "ipv4addrs":[
                {
                    "ipv4addr":"x.x.x.1"
                    }
                ]
            }

I tried this with 1 payload and it fails with "AdmConProtoError: Input list is not supported"  I can do it one at a time with a loop but I would think 1 cal would be more efficient.  Let me know if this is possible

 

requestStruct['request_payload'] = [
            {
            "name":"lloydtest3.abc.abc",
            "ipv4addrs":[
                {
                    "ipv4addr":"x.x.x.2"
                    }
                ]
            },
            {
            "name":"lloydtest4.abc.abc",
            "ipv4addrs":[
                {
                    "ipv4addr":"x.x.x.3"
                    }
                ]
            }
        ]

Thanks,

 

~Lloyd

Re: Python API code for updating a host record's IPv4 address

sjaiswal
Techie
Posts: 8
6894     0

 

Hi, 

 

I am trying to update 'host record name', and for acheiving the same I am using infoblox-client but I am hitting a roadblock coz of which m not able to modify/update the host record. I have tried via REST API and m able to modify the nam as required but via infoblox-client m kind of stuck. Following are the steps that m trying to update the record:

 

 

 

opts = {'host': '192.168.1.10', 'username': 'admin', 'password': 'admin'}

conn = connector.Connector(opts)

my_ip = objects.IP.create(ip='192.168.1.25')

#to create new host record with parameters

record = objects.HostRecord.create(conn, view='my_dns_view', name='my_host_record.my_zone.com', ip=my_ip)



#to modify m trying following but no help:

#1_both of below snippet adds a new name to record instead of upodating the same
-> result = objects.HostRecord.create(conn, view='my_dns_view', name='new_name.my_zone.com', ip=my_ip, check_if_exists=True, update_if_exists=True)

-> result = objects.ARecordBase.create(conn, ip='192.168.102.14', name='some-new_name.ansible.com', view='default', update_if_exists=True)

#2_I have gone through infoblox-client test case(test_update_fields_on_create) which I have following steps to modify record:
a_record = [{'_ref': 'record:previously_created_record', 'ip': '192.168.1.52', 'name': 'other_name'}]
- conn = connector.Connector(a_record) #this gives me error for host,username missing.
- res = conn.get_object('record:host',a_record) #this results to None

 

so, can u suugest me how can I acheive the task of updating the host record. 

 

Thanks

Re: Python API code for updating a host record's IPv4 address

[ Edited ]
Adviser
Posts: 213
6894     0

If you want to make a change to a record, you need to use a PUT.  With the create operation you are using, that's a POST and it will always work toward creating a new object.

 

Make sure when you attempt to update the object that you also include the _ref of the object you are updating.  That is critical or the operation will fail.

 

Re: Python API code for updating a host record's IPv4 address

sjaiswal
Techie
Posts: 8
6894     0

I am updating the object as follows:

 

opts = {'host': '192.168.1.10', 'username': 'admin', 'password': 'admin'}

conn = connector.Connector(opts)

my_ip = objects.IP.create(ip='192.168.1.25')
#to create new host record with parameters:

record = objects.HostRecord.create(conn, view='my_dns_view', name='my_host_record.my_zone.com', ip=my_ip)

# to update the object:
update_ref = record._ref
conn.update_object(update_ref, {'name': 'my_host_record.my_zone.com'})

#Now, the issue is I am able to update "name" in ARecordBase via:

objects.ARecordBase.create(conn,ip='192.168.1.25', name='new_my_host_record.my_zone.com', view='default',  check_if_exists=True, update_if_exists=True)

#But, I am not able to modify HostRecord, this is again creating new record:
objects.HostRecord.create(conn, ip=my_ip, name='new_my_host_record.my_zone.com', view='default',  check_if_exists=True, update_if_exists=True)

#And, when I am trying as follows I am facing exception:

objects.HostRecord.create(conn, check_if_exists=True, update_if_exists=True, name='new my_host_record.my_zone.com')


error:
infoblox_client.exceptions.InfobloxCannotCreateObject: Cannot create 'record:host' object(s): { "Error": "AdmConProtoError: IP address for the Host record is missing.", 
"code": "Client.Ibap.Proto", 
"text": "IP address for the Host record is missing."

 

Kindly, let me know how to resolve this as I am not able to get through this.

 

Further guidance will be highly appreciated.

 

 

Thanks.

Highlighted

Re: Python API code for updating a host record's IPv4 address

sjaiswal
Techie
Posts: 8
6894     0

I was able to resolve the issue, got mixed up with creating and updating.

 

So, for any one who face similar kind of issue here is the coded solution:

 

opts = {'host': '192.168.1.10', 'username': 'admin', 'password': 'admin'}conn = connector.Connector(opts)my_ip = objects.IP.create(ip='192.168.1.25')
#to create new host record with parametersrecord = objects.HostRecord.create(conn, view='my_dns_view', name='host_record.my_zone.com', ip=my_ip)

#to update the name, directly use update_object:
testConn = conn.update_object(record._ref, {'name': 'new_name_host_record.my_zone.com'})

 

Simple.. I was beating around the bush earlier Smiley Very Happy

Showing results for 
Search instead for 
Do you mean 

Recommended for You