Last active
February 17, 2021 08:48
-
-
Save bockor/6d326773706a7d03d5462a817e072c26 to your computer and use it in GitHub Desktop.
Infoblox notes // WAPI (Web API) Sample Code for NIOS 8.4.4 using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Ref: https://community.infoblox.com/t5/DNS-DHCP-IPAM/WAPI-Web-API-Sample-Code-for-NIOS/m-p/105/highlight/true#M12 | |
Sample -- sample python application for WAPI | |
''' | |
import sys | |
import json | |
import ssl #https://stackoverflow.com/questions/5319430/how-do-i-have-python-httplib-accept-untrusted-certs | |
from httplib import HTTPSConnection | |
USER_NAME = 'admin' | |
PASSWORD = 'infoblox' | |
ADDRESS = '192.168.1.222' | |
VERSION = '2.10.3' | |
PATH = '/wapi/v' + VERSION + '/' | |
DEFAULT_OBJECT_TYPE = 'network' | |
JSON = 'application/json' | |
URLENCODED = 'application/x-www-form-urlencoded' | |
DEFAULT_CONTENT_TYPE = URLENCODED | |
def perform_request(operation, ref='', params='', fields='', \ | |
object_type=DEFAULT_OBJECT_TYPE, \ | |
content_type=DEFAULT_CONTENT_TYPE): | |
''' | |
Send an HTTPS request to the Web API server. | |
''' | |
# Create connection and request header. | |
# This class does not perform any verification of the server`s certificate. | |
conn = HTTPSConnection(ADDRESS, timeout=5, context=ssl._create_unverified_context()) | |
auth_header = 'Basic %s' % (':'.join([USER_NAME, PASSWORD]) | |
.encode('Base64').strip('\r\n')) | |
request_header = {'Authorization':auth_header, | |
'Content-Type': content_type} | |
if ref: | |
url = PATH + ref | |
else: | |
url = PATH + object_type | |
if params: | |
url += params | |
conn.request(operation, url, fields, request_header) | |
response = conn.getresponse(); | |
if response.status >= 200 and response.status < 300: | |
return handle_success(response) | |
else: | |
return handle_exception(response) | |
def handle_exception(response): | |
''' | |
If there was encountered an error while performing requested action, | |
print response code and error message. | |
''' | |
print 'Request finished with error, response code: %i %s'\ | |
% (response.status, response.reason) | |
json_object = json.loads(response.read()) | |
print 'Error message: %s' % json_object['Error'] | |
return json_object | |
def handle_success(response): | |
''' | |
If the action requested by the client was received, understood, accepted | |
and processed successfully, print response code and return response body. | |
''' | |
print 'Request finished successfully with response code: %i %s'\ | |
% (response.status, response.reason) | |
return response.read() | |
def network_example(): | |
''' | |
Perform sample operations with networks. | |
''' | |
# Create 1.0.0.0/24 network | |
response = perform_request('POST', fields='network=1.0.0.0/24') | |
first_network = json.loads(response) | |
print 'Created with ref: %s' % first_network | |
# Try to create the same 1.0.0.0/24 network | |
#perform_request('POST', fields='network=1.0.0.0/24') | |
# Add a new comment | |
perform_request('PUT', ref=first_network, fields='comment=new') | |
# Add a new extensible attribute | |
perform_request('PUT', first_network, fields=\ | |
'{"extattrs":{"Site": {"value": "OIZY"}}}', \ | |
content_type=JSON) | |
# Read this network with extensible attributes and comment return fields | |
response = perform_request('GET', first_network, params=\ | |
'?_return_fields=extattrs,comment') | |
print 'Object read: %s' % response | |
# Parse JSON object and check its comment field | |
json_object = json.loads(response) | |
print 'New comment: "%s"' % json_object['comment'] | |
print 'Extensible attribute "Site": "%s"'\ | |
% json_object['extattrs']['Site']['value'] | |
# Create another network | |
response = perform_request('POST', fields='network=2.0.0.0/24') | |
second_network = json.loads(response) | |
print 'Created with ref: %s' % second_network | |
# Get all networks from the response | |
print 'Read all objects: %s' % perform_request('GET') | |
""" | |
# Delete 1.0.0.0/24 network | |
perform_request('DELETE', ref=first_network) | |
# Get all networks, check that one of the networks was deleted | |
print 'Read all objects: %s' % perform_request('GET') | |
# Delete 2.0.0.0/24 network | |
perform_request('DELETE', ref=second_network) | |
# Check that there are no networks | |
print 'Read all objects: %s' % perform_request('GET') | |
""" | |
def host_example(): | |
''' | |
Perform sample create operation with Host. | |
''' | |
""" | |
# Create 1.0.0.0/24 network | |
response = perform_request('POST', fields='network=1.0.0.0/24') | |
network = json.loads(response) | |
print 'Created with ref: %s' % network | |
""" | |
# Create Host with Host Address 1.0.0.1 under this network | |
response = perform_request('POST', object_type='record:host', \ | |
content_type=JSON, fields=\ | |
'{"ipv4addrs": [{"ipv4addr": "1.0.0.1"}],\ | |
"name": "host_record",\ | |
"configure_for_dns": false}') | |
host = json.loads(response) | |
print 'Created with ref: %s' % host | |
""" | |
# Delete 1.0.0.0/24 network | |
perform_request('DELETE', ref=network) | |
# Note that host was also deleted: | |
perform_request('GET', ref=host) | |
""" | |
def main(): | |
''' | |
Main function: run network and host examples. | |
''' | |
network_example() | |
host_example() | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment