Skip to content

Instantly share code, notes, and snippets.

@nacx
Last active August 11, 2017 12:34
Show Gist options
  • Save nacx/97fe2c3a5449332bc7b6ae17c139d31d to your computer and use it in GitHub Desktop.
Save nacx/97fe2c3a5449332bc7b6ae17c139d31d to your computer and use it in GitHub Desktop.
Script to provision a tenant in Abiquo and create an initial virtual datacenter
#!/usr/bin/env python
# Python script to provision a tenant.
#
# It uses the Abiquo API python bindings. It can be installed as follows:
# $ virtualenv /opt/abiquo
# $ source /opt/abiquo/bin/activate
# $ pip install abiquo-api
import json
from abiquo.client import Abiquo
from requests.auth import AuthBase
########################
# Script configuration #
########################
API_URL = 'https://<abiquo url>/api'
AUTH_TOKEN = 'openid access token'
VERIFY_SSL_CERTIFICATES = False
tenant_info = {
'name': 'Tenant From Script'
}
nsx_properties = {
'nsx.gateway.network-id': '10008',
'nsx.gateway.network': '10.0.0.0/24'
}
virtualdatacenter_info = {
'name': 'VDC From Script',
'hypervisorType': 'VMX_04',
'network': {
'name': 'Default Private Network',
'type': 'INTERNAL',
'address': '192.168.0.0',
'mask': 24,
'gateway': '192.168.0.1',
'primaryDNS': '10.60.1.4',
'secondaryDNS': '8.8.8.8',
'sufixDNS': 'abiquo.com'
}
}
############################
# END Script configuration #
############################
# Helper class to implement the OpenID token based authentication
class BearerTokenAuth(AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, request):
request.headers['Authorization'] = "Bearer %s" % self.token
return request
api = Abiquo(API_URL, verify=VERIFY_SSL_CERTIFICATES, auth=BearerTokenAuth(AUTH_TOKEN))
if not VERIFY_SSL_CERTIFICATES:
import urllib3
urllib3.disable_warnings()
# Helper method to raise a proper exception upon failure
def check_response(expected_code, code, errors):
if code != expected_code:
try:
first_error = errors.json['collection'][0]
except:
# If it is not an Abiquo controlled error, throw a generic error
raise Exception("HTTP(%s) Operation failed!" % code)
# If it is an Abiquo error, properly show the error code and error details
raise Exception("HTTP(%s) %s: %s" % (code, first_error['code'], first_error['message']))
# ----- 1. Tenant creation ----- #
print "Creating tenant: %s..." % tenant_info['name']
# Run the create operation (POST) on the /admin/enterprises resource
code, tenant = api.admin.enterprises.post(data=json.dumps(tenant_info),
headers={'content-type': 'application/vnd.abiquo.enterprise+json',
'accept': 'application/vnd.abiquo.enterprise+json'})
# We expect an HTTP 201 (Created) response for create (POST) operations
check_response(201, code, tenant)
print "Configuring NSX properties..."
# We 'follow' the 'properties' link from the tenant object and execute the update (PUT) operation there
code, props = tenant.follow('properties').put(data=json.dumps({'properties': nsx_properties}),
headers={'content-type': 'application/vnd.abiquo.enterpriseproperties+json',
'accept': 'application/vnd.abiquo.enterpriseproperties+json'})
# We expect an HTTP 200 (OK) response for update (PUT) operations
check_response(200, code, props)
# ----- 2. Datacenter assignment ----- #
# We'll get all datacenters and assign them to the tenant so it can use them
code, datacenters = api.admin.datacenters.get(headers={'accept': 'application/vnd.abiquo.datacenters+json'})
for dc in datacenters:
dc_edit_link = dc._extract_link('edit')
assignment = {
# To assign a DC to a tenant, we'll create an assignment using the link that references the datacenter,
# and configuring the 'rel' attribute as 'location'.
'links': [{'rel': 'location', 'href': dc_edit_link['href']}]
}
print "Allowing tenant to use Datacenter: %s..." % dc.name
code, allowed = tenant.follow('limits').post(data=json.dumps(assignment),
headers={'content-type': 'application/vnd.abiquo.limit+json',
'accept': 'application/vnd.abiquo.limit+json'})
check_response(201, code, allowed)
# ----- 3. Virtual datacenter creation ----- #
# To create a Virtual Datacener we need two links:
# - A link with rel 'enterprise' that points to the tenant URL.
# - A link with rel 'location' that points to the Datacenter where the VDC will be created.
tenant_ref = {'rel': 'enterprise', 'href': tenant._extract_link('edit')['href']}
datacenter_ref = dc._extract_link('location')
virtualdatacenter_info['links'] = [ datacenter_ref, tenant_ref ]
print "Creating virtual datacenter '%s' in datacenter '%s'..." % (virtualdatacenter_info['name'], dc.name)
code, vdc = api.cloud.virtualdatacenters.post(data=json.dumps(virtualdatacenter_info),
headers={'content-type': 'application/vnd.abiquo.virtualdatacenter+json',
'accept': 'application/vnd.abiquo.virtualdatacenter+json'})
check_response(201, code, vdc)
print "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment