Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created June 25, 2010 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softlayer/453040 to your computer and use it in GitHub Desktop.
Save softlayer/453040 to your computer and use it in GitHub Desktop.
"""
Order a Cloud Computing Instance based on an existing CCI
Build a SoftLayer_Container_Product_Order_Virtaul_Guest object for a new
CloudLayer Computing Instance based on an existing CCI and disk template image
on your account and pass it to the SoftLayer_Product_Order API service to order
it. See below for more details.
This assumes you're using the SoftLayer API Python client
<http://github.com/softlayer/softlayer-api-python-client>. It's been written
for Python 2.x. If you're using Python 3 then please adjust print() usage and
exception handling accordingly.
Important manual pages:
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Virtual_Guest
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Virtual_Guest
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item_Price
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
# So we can talk to the SoftLayer API:
import SoftLayer.API
# For nice debug output:
import pprint
# Your SoftLayer API username and key.
#
# Generate an API key at the SoftLayer Customer Portal:
# https://manage.softlayer.com/Administrative/apiKeychain
apiUsername = 'set me!'
apiKey = 'set me too!'
# The id number of the CCI you wish to duplicate.
#
# Grab a list of your account's CCIs by calling the getVirtualGuests() method
# in the SoftLayer_Account API service if you don't know your CCI's id.
cciId = 1234
# The type of CCI you wish to create.
#
# Set this to either "HOURLY" or "MONTHLY" depending on how you'd like to be
# billed for your new CCI.
billingType = 'HOURLY'
# The disk template you'd like to install on your CCI.
#
# Your order template will set the physical aspects of your new CCI. It sets
# things like the number of cores, amount of RAM, network speeds, and the like.
# The contents of the CCI are determined by your disk template. If you don't
# know your template id call the getBlockDeviceTemplateGroups() in the
# SoftLayer_Account API service to get your saved templates.
imageTemplateId = 1234
# The id of the datacenter you wish to provision your new CCI in.
#
# Specify the id of the datacenter you want your instance to go in. You can get
# a list of these from the getDatacenters() method in the
# SoftLayer_Location_Datacenter API service or use the following:
# 3: Dallas
# 18171: Seattle
# 37473: Washington DC
locationId = 3
# The hostname and domain names of the new CCIs you wish to order.
#
# You can create multiple CCIs from your instance. Define one dictionary for
# each new CCI you wish to order.
newCcis = [
{
'hostname' : 'testhost',
'domain' : 'example.org'
}
]
# Connect to the SoftLayer_Virtual_Guest API service.
client = SoftLayer.API.Client('SoftLayer_Virtual_Guest', cciId, apiUsername, apiKey)
# Retrieve the order used to create your CCI.
try:
orderTemplate = client.getOrderTemplate(billingType)
except Exception, e:
print 'Unable to retrieve order template. ', e
exit()
# Modify our order template with our new CCIs' information.
del orderTemplate['sourceVirtualGuestId']
orderTemplate['imageTemplateId'] = imageTemplateId
orderTemplate['location'] = locationId
orderTemplate['quantity'] = len(newCcis)
orderTemplate['virtualGuests'] = newCcis
# Declare the template as a SoftLayer_Container_Product_Order_Virtual_Guest
# type, so the API knows we're trying to place an order for a virtual guest.
orderTemplate['complexType'] = 'SoftLayer_Container_Product_Order_Virtual_Guest'
# Re-Declare the API client to use the SoftLayer_Product_Order API service.
client = SoftLayer.API.Client('SoftLayer_Product_Order', None, apiUsername, apiKey)
# verifyOrder() will check your order for errors. Replace this with a call to
# placeOrder() when you're ready to order. Both calls return a receipt object
# that you can use for your records.
#
# Once your order is placed it'll go through SoftLayer's provisioning process.
# When it's done you'll have a new SoftLayer_Virtual_Guest object and CCI ready
# to use.
try:
receipt = client.verifyOrder(orderTemplate)
pprint.pprint(receipt)
except Exception, e:
print 'There was an error in your order. ', e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment