Deploying a VM to vCloud Director using API and Python

pyton_api_1

Further to my other post about getting started with vCloud Director API using Python, I thought I would write down how I deployed a VM to a vApp.

Once you have got a login function working by obtaining the x-vcloud-authorization token, you need to use the recomposeVApp REST API call, which as outlined by the documentation is done by using appending this URI, /vApp/{id}/action/recomposeVApp, to your endpoint. NOte, your endpoint is your API URL.

Here is my example code, which can be found on my github account, https://github.com/oliverleach/vcloud-autodeploy/blob/master/vCloudDeployVm.py


def recompose_vapp(self):

    post_headers = self.headers
    post_headers['Content-Type']='application/vnd.vmware.vcloud.recomposeVAppParams+xml'

    xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
             <RecomposeVAppParams
              xmlns="http://www.vmware.com/vcloud/v1.5"
              xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
              xmlns:environment_1="http://schemas.dmtf.org/ovf/environment/1">
              <Description> "api deployed vm" </Description>
                  <SourcedItem sourceDelete="false">
                      <Source href="%s"/>
                  </SourcedItem>
              <AllEULAsAccepted>true</AllEULAsAccepted>
              </RecomposeVAppParams>""" % (self.vapp_template)

    post = requests.post(self.vapp + '/action/recomposeVApp', data=xml, headers=post_headers)
    result = ET.fromstring(post.text)

Firstly, you can see I am setting the post_headers value to self.headers – these headers include the x-vcloud-authorization token. You also need to add the content type which is outlined in the documentation. It just tells the API what XML to expect, which in this case is recomposeVAppParams+xml.

Next, I have specified the XML file and I’ve cut down what I have specified and kept it simple. The only thing I have provided is the vapp_template value, which is something I have obtain earlier in my API script.

Finally, I then post this request as shown again below:

post = requests.post(self.vapp + '/action/recomposeVApp', data=xml, headers=post_headers)

Here I am using the python requests library and adding the required parameers, which in cludes the self.vapp URL, again obtained earlier in my API script, which appends /action/recomposeVApp. I then provide the XML which is contained in the function and add the post_headers.

You need to look at the whole API script to understand how I have built up the post request, including how I have obtained the various requirements such as vapp href and the vapp_template href.

Please do post a comment if you would like more info or use the contact me link on my blog.

Happy scripting. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s