Using the CloudStack API From Python

I've recently been playing around with the Private Cloud at work. They're using VMWare and they've provided a CloudStack API layer that allowed me write my own command line tools in Python.

A couple of things you'll need to get started -
  • You'll need your 'API key' and your 'Secret'. These should be available through whatever UI your infrastructure provides
  • You will need the CloudStack client for Python
  • I'm also using the Termcolor package to prettify my console output in the examples below
I've created two files with Constants. The first, is called nikkeys.py. You will need to replace the examples below with your own keys. This file contains just two constants -

apikey = 'YOUR_API_KEY'
secret = 'YOUR_SECRET_KEY'

The second file is called cloudconstants.py and contains information about the cloud environment. Eventually you would want to get rid of this file otherwise you would need to update it regularly. These constants are the GUIDs for the various resources (the GUIDs below are fake they've been replaced by random GUIDs) -

api = 'http://your.cloudstack.url.com/client/api'
 
# Services
svc_micro_hourly = '1de5ca9f-df86-bc16-4d74-3a5a953b0eb4'
svc_micro_daily = 'decb027e-0847-5417-7d8b-e937f92edd24'
svc_small_hourly = 'fcc6567e-7eed-4c39-9254-b35311215419'
svc_small_daily = '266a32a4-2f94-f528-8a96-5b789a4fd96d'
svc_medium_hourly = 'decb027e-0847-5417-7d8b-e937f92edd24'
svc_medium_daily = '593f5d52-c5a3-422c-aac0-54abf33b46fa'
svc_large_hourly = '1de5ca9f-df86-bc16-4d74-3a5a953b0eb4'
 
# Templates
tmpl_ol631_tomcat = '239c347a-f63b-4995-ab6f-ec31ced0f6ca'
tmpl_ol631_httpd = 'decb027e-0847-5417-7d8b-e937f92edd24'
tmpl_win2k8r2_ent_iis = '1de5ca9f-df86-bc16-4d74-3a5a953b0eb4'
tmpl_win2k8r2_std_iis = '266a32a4-2f94-f528-8a96-5b789a4fd96d'
tmpl_win2k8r2_ent = '8bfd166a-b6c8-4259-893e-9a8f31f9b102'
tmpl_win2k8r2_std = '80497396-288a-4e08-bcfb-0589b11b8611'
 
# Zones
zone_america1 = 'decb027e-0847-5417-7d8b-e937f92edd24'
zone_europe1 = '266a32a4-2f94-f528-8a96-5b789a4fd96d'
 
# Networks
net_america1 = '266a32a4-2f94-f528-8a96-5b789a4fd96d'
net_europe1 = 'decb027e-0847-5417-7d8b-e937f92edd24'
 
# Projects
proj_testautomation = '266a32a4-2f94-f528-8a96-5b789a4fd96d'
proj_qareporting = 'decb027e-0847-5417-7d8b-e937f92edd24'



Here is how you can create VMs, destroy VMs and List VMs  -

Comments

Sri said…
NIce Post, I tried using the same way but ended up with the below error.

cloudstack.cloud_exceptions.CloudException: Account error

Can you please suggest.

Code:

import cloudstack
from cloudstack import Client

api = 'xxx'
apikey = 'yyy'
secretkey = 'zzz'
cloud = cloudstack.Client(api, apikey, secretkey)
woodlouse said…
Hi Sri. The code only works with Python 2.x. Aside from that I've never encountered this error before.

I'm not really an expert but 'account error' seems to suggest that API access might not have been set up correctly. You're better of posting to a public forum.

Popular posts from this blog

The Forecaster Brown Fan Club

How to Create a Pentaho Report Using the REST Client

Automated Testing with vncdotool (Not Headless, but Hairless)