Using vCloud Director API for ISO/OVA Upload

vCloud Director API

Introduction

The vCloud API offers a number of operations to explore and manipulate your vCloud Director environment. One area that may be of interest to users is its media upload features. The vCloud API allows you to upload OVF packages and ISO-format media images, allowing you to transfer files from your client system to a target catalog in your vCloud Director organization. The vCloud API is available as a REST API that can interact with HTTP requests. A useful resource to get started with the vCloud API is the vCloud API Programming Guide.

In this two-part article, I will illustrate how to explore and use the vCloud API with Python and the Python-Requests package. In the first part of this article, I will demonstrate how to set up your environment and obtain the contents of your organization’s catalogs. In part two of this article, I will illustrate the process of uploading both OVA packages and ISO-format media. The repository for the Python source for this project can be found on Github.

vCloud API Primer

Before going into development detail, let’s start with some background information on the vCloud API, which offers RESTful application development when creating tools which interact with vCloud Director. The following expand on some of the terms used in this article.

  • Objects – The vCloud API defines objects – including organizations, catalogs, virtual data centers, and media – and represents them in XML with additional embedded attributes which can be retrieved from API responses.
  • Links – Link elements provide references to objects and supported actions, providing a mechanism to access and operate on objects such as catalogs and VMs.
  • REST Requests/Responses – Our application client will make HTTP requests using a vCloud API endpoint, then examine the returned response for status and retrieve information from the response body.

Project Environment

This article details developing a Python application on a GNU/Linux platform. The latest versions of Ubuntu, CentOS, Fedora and Redhat Enterprise (RHEL) come with Python 2.7 already installed. Next, virtualenv is a tool to create isolated Python environments, essentially keeping dependencies required by different projects in separate places. In our case, we want to create a new Python environment in which to run our client application. virtutalenv will create a folder in our project directory, containing all the necessary executables and packages that our Python project will need without affecting our system’s existing site-packages.

To begin, let’s install virtualenv via pip:

Next, change directory into your project folder, and create a virtual environment for the project:

This creates a new folder in the current directory, named venv, containing the Python executable files and a copy of the pip library which you can use to install additional packages. The name of the virtual environment, in this case, is venv but can be anything you choose.

To begin using the virtual environment, we need to activate it with:

The name of the current virtual environment appears on the left of the prompt to indicate that it’s active. Any package that you install using pip will be placed in the venv folder, isolated from your system Python installation.

The first package we’ll need is Python Requests, which facilitates HTTP requests to the vCloud Director API. The second package we need is lxml, which will be used to parse XML data from the API responses.

In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages with:

This creates a requirements.txt file, which contains a list of all the packages in the current environment and their respective versions. This is helpful in case you need to re-create the environment, allowing you to install the same packages using the same versions with:

This helps to ensure consistency across different deployments and across developers.

Let’s Get Started

In this article, we will perform the following tasks:

  • Establish a connection to the vCloud API server using Python and the Python-Requests package
  • Make a GET request on the /orgs endpoint of the vCloud API
    • This will return a link corresponding to the organization object
    • Make a GET request on the organization link to obtain catalog information

In order to use the vCloud API, we need the vCloud server hostname (or IP) and user credentials in order to authenticate and obtain a token which enables us to make additional REST calls on the API.

  • vCloud hostname or IP
    • This is the vCloud server to which REST calls will be made
    • If you use the fully-qualified name of your vCloud server, your GNU/Linux localhost needs resolve the IP of the server
    • Using the IP of the vCloud sever works as well
    • User Credentials
      • A user name and password in your vCloud organization with sufficient privileges to log in, view organization info, and access its catalog
      • I used the user role: Organization Administrator
      • User, credentials and access roles can be set up in vCloud Director within your Organization: Administration > Members > Users
      • Given the vCloud organization user credentials, we can establish a Python Requests session with the following:

with the HTTP Accept header set to API version 5.6. Next, our client application must complete a login request to the vCloud API in order to receive an authorization token. This token needs to be included in all subsequent API requests. The login name format used is username@vcd-organization. The login API endpoint is https://vcd-hostname/api/sessions, where vcd-hostname is the fully-qualified name of your vCloud server or IP, and requires a POST call:

Upon a successful response, the returned token can be stored in the session header for subsequent calls.

Now that we have a valid API token, we can make additional calls to the vCloud API. First, let’s make a GET call to the endpoint https://<vcd-hostname>/api/org:

This returns a list of dictionary items with keys: href, type and name. The response body from the API GET call contains XML of the form:

We are interested in the Org href, which references the organization corresponding to the user credentials that were used earlier. Using this href link in a subsequent GET call provides additional information that we are interested in, such as the organization’s catalog.

If we make a GET https://vcd-hostname/api/org/<uuid> API call with this Org href, we will find links to the Org catalogs:

The response body from the GET call contains XML of the form:

From the response, we are interested in links with type application/vnd.vmware.vcloud.catalog+xml. In this case, there are two catalogs for this organization, named pub and Vagrant.

As the last step, let’s perform GET https://vcd-hostname/api/catalog/<uuid> API calls using the Link hrefs corresponding to the above catalog types to obtain the list of files residing in each of the organization’s catalogs. Looping over the list of catalogs returned from the previous code, we have

to yield the href links and names from each catalog. The response body from the GET API call contains XML of the form:

which, in the case shown, corresponds to the Vagrant catalog. The catalog items reside under the <CatalogItem> XML tag, have type application/vnd.vmware.vcloud.catalogItem+xml, and include the name of the catalog item. This same name will appear in the vCloud Director web client.

Part 2 of this article goes through the process of uploading OVF packages and ISO-format media images into our organization catalog.

Ron Hemmers

Ron Hemmers

Software Engineer at Navisite
Originally from Toronto, ON, he has been developing software for over 20 years and enjoys applying software solutions to automation.You can find him attending a Syracuse Chiefs game or discovering something new in the greater CNY area.
Ron Hemmers

Latest posts by Ron Hemmers