Jul 14 2016

How to use Python with Nutanix REST API – Part 3

This is the 3rd 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 and Part 2.

Picking up from I left off in the 2nd part… When retrieving data from Nutanix using REST you will be commonly left with a json object containing list entries and properties. However, unless we are trying to list all objects and their properties you will often need to select specific entries from the json object. In the example below I demonstrate the simplest way to iterate the son object using key and value.


json_object = __request_vm_by_name('vmname')

# iterate json result and add vm details to list
for key, value in json_object.items():
    # do not display certain keys
    if key not in KEY_IGNORE_VM:
        # Display property on screen procedure


Inside the for loop there;s a conditional operations to ignore properties in the json object that do not match KEY_IGNORE_VM, and therefore will not have the procedure execute to display on-screen. This type of approach is good for eliminating unnecessary properties from being utilized during iterations. Here is a list of virtual machine properties that can be used, but I would recommend you making a simple GET call and look at all the available properties; for the most part they are self-describing.


# Filter ignore Keys
KEY_IGNORE_VM = {'vmDisks', 'vmNics', 'containerIds', 'vmId', 'virtualNicIds', 'vdiskFilePaths', 'stats', 'uuid', 'nutanixVirtualDisks', 'nutanixVirtualDisksIds', 'nutanixVirtualDiskUuids',
                 'nutanixVirtualDiskIds', 'hostId', 'hostUuid', 'clusterUuid', 'usageStats', 'virtualNicUuids', 'containerUuids', 'nutanixGuestTools', 'runningOnNdfs', 'vdiskNames', 'displayable', 'guestOperatingSystem', 'acropolisVm', 'powerState', 'onDiskDedup', 'fingerPrintOnWrite', 'controllerVm', 'numNetworkAdapters', 'memoryReservedCapacityInBytes', 'cpuReservedInHz'}


Understanding the resulting json object is key for a proper iteration. As a Mac user I like to use CocoaRestClient, but Advanced REST client is also a good tool for Google Chrome users.

In the next post we will dissect a more complex procedure that I use to trigger the creation of a virtual machine snapshots using snapshot specifications snapshotSpecs. Of course, you can always use PRISM or PowerShell to simplify the entire snapshot process, but the objective here is to be as granular as possible for Python with Nutanix API.


def __snapshot_vm(name):

    # request saved config data
    ntnxapi_data = __retrieve_config_data()

    # request vm uuid
    uuid = __request_vm_uuid(name)

    # request vm vmId
    vmuuid = __request_vm_vmuuid(name)

    # retrieve timestamp for use in snapshotName
    snapshottimestamp = datetime.datetime.fromtimestamp(
        time.time()).strftime('%Y-%m-%d %H:%M:%S')

    # build snapshotSpecs payload
    children = []
    children.append({"vmUuid": vmuuid,
                     'snapshotName': snapshottimestamp,
                     "uuid": uuid})
    container = {}
    container['snapshotSpecs'] = children
    json_data = json.dumps(container)

    base_url = "https://" + ntnxapi_data['cluster'] + API_AHV + "/snapshots/"

    # request http post
    __http_post(base_url, json_data)


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

Older posts «