Marodr
Marodr

Reputation: 177

Writing a Python script to add a host using infoblox WAPI

I am trying to create a host if it does not exist. Having trouble with the post command. What I have done so far:

# prompt user for credentials to infoblox
if sys.stdin.isatty():
print "Enter credentials to access infoblox"
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
else:
username = sys.stdin.readline().rstrip()
password = sys.stdin.readline().rstrip()

# creates session to infoblox
session = requests.Session()
session.auth = (username, password)
session.verify = False
url = infobloxURL 

# silence http warnings
urllib3.disable_warnings()
 
ipAddress = "10.1.1.0"
hostName = "zocalo"

# searches the internal DNS to see if host exists 
response = session.get(url + 'record:host?name~=^%s.mydomain.net$' % hostName)

# if there is a response, response.content will contain a json object with the properties for that host
hostExists = "%s.mydomain.net" % (hostName) in response.content

if hostExists == True:
print ("The hostname already exists in infoblox. Please select a new hostname and try again")
sys.exit(1)
else:
print("Success! The host does not exist in infoblox")

# add a host into infoblox
host = { "name":"%s.mydomain.net$" % hostName,
   "ipv4addrs":[
      {
          "ipv4addr":"%s" % ipAddress
      }
      ]
   }

response = session.post(url + 'record:host', data=host )

if response.status_code == 201:
print("successfully created a new host record in infoblox")
else:
print("failure, host not added to infoblox")
print (response.status_code)

Output of code above:

Enter credentials to access infoblox
Username: xxxxx
Password: 
Success! The host does not exist in infoblox
{ "Error": "AdmConProtoError: List value expected for field: ipv4addrs", 
"code": "Client.Ibap.Proto", 
"text": "List value expected for field: ipv4addrs"
}
400

While I'm able to perform a get function to search for existing hosts, I am getting a status_code of 400 when I am trying to add a new host using post.

Upvotes: -1

Views: 1296

Answers (1)

Marodr
Marodr

Reputation: 177

this may be helpful for someone else. Here is a working solution to my problem:

session = requests.Session()
session.auth = ('yourusername', 'yourpassword')
session.verify = False
url = infobloxURL
# silence http warnings
urllib3.disable_warnings()
ipAddress = "10.10.10.5"
hostName = "zocalo"

# searches the internal DNS to see if host exists 
response = session.get(url + 'record:host?name~=^%s.mydomain.net$' % hostName)

# if there is a response, response.content will contain a json object with the properties for that host
hostExists = "%s.mydomain.net" % (hostName) in response.content

if hostExists == True:
    print ("The hostname already exists in infoblox. Please select a new hostname and try again")
    sys.exit(1)
else:
    print("Success! The host does not exist in infoblox")

# add a host into infoblox
host = '{ "ipv4addrs": [{"configure_for_dhcp": false, "ipv4addr": "10.10.10.5"}], "name": "zocalo.mydomain.net", "view": "internal"}'

response = session.post(url + 'record:host', data=host )
# shows you the host that was created
print (response.content)
#if code 201 is received then post was successful
print (response.status_code)

Upvotes: 0

Related Questions