vcloud director api

Deploying a VM to vCloud Director using API and Python


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,

def recompose_vapp(self):

    post_headers = self.headers

    xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              <Description> "api deployed vm" </Description>
                  <SourcedItem sourceDelete="false">
                      <Source href="%s"/>
              </RecomposeVAppParams>""" % (self.vapp_template)

    post = + '/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 = + '/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. 🙂