Jul 13 2016

How to use Python with Nutanix REST API – Part 2

This is the 2nd part of my brief tutorial on how to use Python with Nutanix REST API. In case you missed, How to use Python with Nutanix REST API – Part 1.

Picking up from I left off in the 1st part… In order to make changes or trigger actions on a Nutanix object we need to execute a http_post operation while sending the base_url and the correct data in json format. The snippet below demonstrates a function to execute a http_post. Note that I am passing username and password to the Nutanix cluster inside the function, but I recommend using a pre-defined global variable or opt for entering them also as parameters.

def __http_post(base_url, json_data):
# supress the security warnings
__supress_security()

s = requests.Session()
s.auth = ('username', 'password')
s.headers.update({'Content-Type': 'application/json; charset=utf-8'})
s.post(base_url, data=json_data, verify=False)

return 0

 

The __http_post function is commonly called from within another function. The example below demonstrates how to place a AHV host in maintenance mode. First we create the json_data object containing the model schema and it’s properties. Most http_post actions require a different model schema and the best way to learn about the correct model schemas is using the REST API Explorer Live! (see previous post). The model schema provides the necessary information to build the json_data object. Selecting the model option the API Explorer provides detailed information about each property.

Next we generate the base_url object containing the full URI. The URI can also be attained via REST API Explorer Live!. You can also try to simulate the call and get the entire URL ready to be consumed.

def __enter_maintenance_mode_host(uuid):

# POWER_OFF, COLD_MIGRATE, LIVE_MIGRATE
json_data = json.dumps(
{"evacuationOption": 'LIVE_MIGRATE',
"logicalTimestamp": ''}, sort_keys=True)

base_url = "https://" + \
cluster_ip_address + API_AHV + \
"/hosts/" + uuid + "/enter_maintenance_mode"

# request http post
__http_post(base_url, json_data)

 

The __enter_maintenance_mode_host function requires the AHV host uuid as input parameter. Unless you already know the host uuid you will need to search for it using another function call. There are multiple ways to search for the uuid, but in the example below I demonstrate how to search by name – there are no hosts with same name in a Nutanix cluster.

def __request_host_uuid_new(host_name):

base_url = "https://" + cluster_ip + API_PRISM + \
"/hosts/?searchString=" + host_name
# check if json_object has no entities before proceeding
if json.loads(__htpp_request(base_url))['metadata']['count'] > 0:
if host_name == str(json.loads(__htpp_request(base_url))['entities'][0]['name']):
break

return str(json.loads(__htpp_request(base_url))['entities'][0]['uuid'])

 

Finally, the snippet below is responsible for initiating the entire procedure. First the AHV host uuid is retrieved using __request_host_uuid_new and stored on uuid object, and second the __enter_maintenance_mode_host function place the host in maintenance using the uuid as input..

# uuid variable
uuid = __request_host_uuid_new('hostname')

# __enter_maintenance_mode_host
__enter_maintenance_mode_host(uuid)

 

In the next article I will explain how to parse json responses from Nutanix.

 

How to use Python with Nutanix REST API – Part 3 has already been published here.

 

This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net.

Older posts «

» Newer posts