Skip to content

Instantly share code, notes, and snippets.

@gigawhitlocks
Created February 12, 2016 18:12
Show Gist options
  • Save gigawhitlocks/45f0ab29a8ce1451d92d to your computer and use it in GitHub Desktop.
Save gigawhitlocks/45f0ab29a8ce1451d92d to your computer and use it in GitHub Desktop.
example of creating/destroying an ESXi instance in VCA
from pyvcloud.vcloudair import VCA
from pprint import pprint
import os
import time
import uuid
class ESXInstance(object):
def __init__(self, password=os.getenv("vca_password"),
username=os.getenv("vca_username"),
vca_uri='https://us-california-1-3.vchs.vmware.com',
org='83ccbb04-6c2c-4868-8e54-d4369e215bbf',
instance='ab72fda9-1d23-43ac-837f-4c1b895daf03',
vdc='VDC1',
template_id="ESXi 6 Nested for Testing",
vapp_name=uuid.uuid4().hex,
catalog_name='vic-catalog'):
self.password = password
self.username = username
self.vca_uri = vca_uri
self.catalog_name = catalog_name
self.org = org
self.instance = instance
self.vdc = "VDC1"
self.template_id = template_id
self.vapp_name = vapp_name
self.vca = None
def login(self):
vca = VCA(self.vca_uri, self.username)
vca.login(self.password)
vca.login_to_instance(password=password, instance=instance, token=None, org_url=None)
vca.login_to_instance(instance=instance, password=None, token=vca.vcloud_session.token,
org_url=vca.vcloud_session.org_url)
self.vca = vca
return vca
def create_esx_instance(self):
try:
self.vca.block_until_completed(
self.vca.create_vapp(self.vdc, self.vapp_name, self.template_id,
self.catalog_name,
poweron='true', deploy='true'))
except Exception as e:
print e.message
self.delete_esx_instance()
def delete_esx_instance(self):
if self.vca == None:
return False
else:
self.vca.block_until_completed(vca.delete_vapp(self.vdc, self.vapp_name))
def main():
esx = ESXInstance() # create a default-settings esx metadata object
esx.login() # create a session on vcloudair
esx.create_esx_instance() # instantiate the instance in vcloudair
esx.delete_esx_instance() # destroy the instance in vcloudair
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment