VMstore T7000 Series Icon

VMstore T7000 Series

On-prem VMstore platform.

Tintri Cloud Platform

Managed infrastructure powered by Tintri.

Tintri Cloud Engine

Container-driven VMstore platform.

All Die Hard Tintri News & Events Perspectives Products & Solutions Technical

Tintri Python SDK Released

As part of Tintri’s cloud initiative, we are pleased to announce that a Python SDK (PySDK) for Tintri REST APIs is now available on GitHub. Tintri’s PySDK is a thin layer over its REST APIs with the following features:

  • auto pagination
  • automatic re-login after time-out
  • includes all v310.51 APIs which maps to Tintri OS 4.2 and Tintri Global Center (TGC) 3.0
  • works with Python 2.7
  • object oriented

PySDK versus tintri_1.1.py

If you have been using tintri_1.1.py library from GitHub, you will find the PySDK an improvement. The image below shows the difference.

As you can see from the above image, PowerShell is a thicker library. The PowerShell Toolkit provides convenience cmdlets; for example, PowerShell has Enable-TintriMaintenanceMode and Disable-TintriMaintenanceMode, whereas the PySDK uses update_appliance method. See the code example set_maintenance_mode.py for details. Also the PowerShell toolkit can accept hypervisor objects, while PySDK cannot.

PowerShell is great for Windows clients, whereas Python is can be used for non-Windows clients as well. Python is now even more with the PySDK.

How to Use

To use the PySDK, follow the README file and install the wheel. A short tutorial is included in the documentation. My future Python coding examples will be using the PySDK.

If you do a code comparison, the difference between using tintri_1.1.py and the PySDK, I think you’ll want to use the PySDK. The first code snippet is using tintri_1.1.py. Note that the URL needs to be specified and the results need to be placed into a dictionary.


    # Get a list of VMs, but only return a page size
    url = "/v310/vm"
    r = tintri.api_get(server_name, url, session_id)

    vm_paginated_result = r.json()
    num_vms = int(vm_paginated_result["filteredTotal"])
    if num_vms == 0:
        raise tintri.TintriRequestsException("No VMs present")

    print_info(str(num_vms) + " VMs present")

 

But with the PySDK, a get_vms method is called instead of a generic get, and the results are returned in an object with typed properties. Also the session_id is not needed because the PySDK object keeps track of it.


    # Get VMs with page size filter.
    vms = tintri.get_vms(filters = vm_filter_spec)
    num_vms = vms.filteredTotal
    if num_vms == 0:
        raise TintriServerError(0, cause="No VMs present")

    print_info(str(num_vms) + " VMs present")

 

The PySDK has a method call for each REST API. There are more code examples in the examples directory.

In this update example of the difference, the first code snippet shows how to set the VMstore maintenance mode.


    if (new_is_enabled):
        # Create the maintenance mode DTO for enabling.
        new_maint_mode_info = 
            {"typeId": "com.tintri.api.rest.v310.dto.domain.beans.hardware.ApplianceMaintenanceMode",
             "endTime"   : add_6_str,
             "isEnabled" : new_is_enabled,
             "startTime" : now_str
            }
    else:
        # Create the maintenance mode DTO for disabling.
        new_maint_mode_info = 
            {"typeId": "com.tintri.api.rest.v310.dto.domain.beans.hardware.ApplianceMaintenanceMode",
             "isEnabled" : new_is_enabled,
            }

    # Create the Appliance object wit the new ApplianceDns DTO.
    new_appliance = 
        {"typeId": "com.tintri.api.rest.v310.dto.domain.Appliance",
         "maintenanceMode": new_maint_mode_info
        }

    # Create the Request object with the Appliance DTO.
    request = 
        {"typeId": "com.tintri.api.rest.v310.dto.Request",
         "objectsWithNewValues": [new_appliance],
         "propertiesToBeUpdated": ["maintenanceMode"]
        }

    # Invoke the appliance API to set the maintenance mode.
    url = APPLIANCE_URL
    r = tintri.api_put(server_name, url, request, session_id)

 

Note how the typeId needs to be specified for every DTO above, but the PySDK takes care of that for you in the Python object. Python dictionaries are used with the old library, while properties of objects are used in PySDK.


    # Create the appliance maintenance mode object.
    new_maint_mode_info = ApplianceMaintenanceMode()

    if (new_is_enabled):
        # Add attributes for enabling.
        new_maint_mode_info.isEnabled =  new_is_enabled
        new_maint_mode_info.endTime =  add_6_str
        new_maint_mode_info.startTime =  now_str
    else:
        # Add attributes for disabling.
        new_maint_mode_info.isEnabled =  new_is_enabled

    # Call the update appliance method to set the maintenance mode.
    server.update_appliance(None, "default", maintenance_mode = new_maint_mode_info)

 

The advantages of coding using the PySDK, are:

  • less coding
  • focus on job at hand, and not on Tintri API details
  • better code readability

A Python SDK (PySDK) for Tintri REST APIs is now available. See below for code comparisons and a link to our GitHub.

This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.