Skip to content

Instantly share code, notes, and snippets.

@underscorephil
Created September 24, 2012 17:32
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 underscorephil/3777178 to your computer and use it in GitHub Desktop.
Save underscorephil/3777178 to your computer and use it in GitHub Desktop.
Command Line Order CCI
import SoftLayer.API
import pprint
def table_print(data, title_row):
max_widths = {}
data_copy = [dict(title_row)] + list(data)
for col in data_copy[0].keys():
max_widths[col] = max([len(str(row[col])) for row in data_copy])
cols_order = [tup[0] for tup in title_row]
def custom_just(col, value):
if type(value) == int:
return str(value).rjust(max_widths[col])
else:
return value.ljust(max_widths[col])
for row in data_copy:
row_str = " | ".join([custom_just(col, row[col]) for col in cols_order])
print "| %s |" % row_str
if data_copy.index(row) == 0:
underline = "-+-".join(['-' * max_widths[col] for col in cols_order])
print '+-%s-+' % underline
class virtualGuest(object):
def __init__(self, hostname, domain):
self.hostname = hostname
self.domain = domain
def __str__(self):
return '.'.join([self.hostname, self.domain])
def orderable(self):
return {"hostname": self.hostname, "domain": self.domain}
class orderTemplate(object):
def __init__(self, virtualGuestId, imageId, templateId, hostname, domain):
self.template = templateId
self.imageid = imageId
self.quantity = 0
self.location = "265592"
self._complexType = "SoftLayer_Container_Product_Order_Virtual_Guest"
self.virtual_guests = []
self.hostname = hostname
self.domain = domain
def set_quantity(self, num):
self.quantity = int(num)
self.__set_virtual_guests()
def __set_virtual_guests(self):
self.virtual_guests = []
for x in xrange(self.quantity):
hostname = self.hostname % x
self.virtual_guests.append(virtualGuest(hostname, self.domain))
def get_template(self):
self.template.update({
"location": self.location,
"complexType": self._complexType,
"virtualGuests": [x.orderable() for x in self.virtual_guests],
"quantity": self.quantity,
"imageTemplateId": self.imageid,
})
return self.template
class orderCloud(object):
def __init__(self, username=None, apikey=None):
self.apiUsername = username
self.apiKey = apikey
self.orderClient = SoftLayer.API.Client('SoftLayer_Product_Order',
None, self.apiUsername, self.apiKey)
self.guestClient = SoftLayer.API.Client('SoftLayer_Virtual_Guest',
None, self.apiUsername, self.apiKey)
self.accountClient = SoftLayer.API.Client('SoftLayer_Account',
None, self.apiUsername, self.apiKey)
self.templateIds = {}
# XXX should probably make these configurable
self.hostname = "server-%d"
self.domain = "example.com"
def listInstances(self):
self.accountClient.set_object_mask("mask[operatingSystem.passwords.password]")
guests = self.accountClient.getVirtualGuests()
showGuest = []
for guest in guests:
if not "appster" in guest['hostname'] or not len(guest['operatingSystem']['passwords']):
continue
showGuest.append(
dict(ip=guest['primaryIpAddress'],
password=guest['operatingSystem']['passwords'][0]['password'],
hostname=guest['hostname']
)
)
titles = [('hostname', 'Host Name'), ('ip', 'Ip Address'), ('password', 'Root Password')]
table_print(showGuest, titles)
def getOrderTemplate(self, virtualGuestId):
if virtualGuestId in self.templateIds:
return self.templateIds[virtualGuestId]
self.guestClient.set_init_parameter(virtualGuestId)
self.templateIds[virtualGuestId] = \
self.guestClient.getOrderTemplate('HOURLY')
return self.templateIds[virtualGuestId]
def buildOrderContainer(self, numServers, virtualGuestId, imageTemplateId):
template = orderTemplate(
virtualGuestId,
imageTemplateId,
self.getOrderTemplate(virtualGuestId),
self.hostname,
self.domain)
template.set_quantity(numServers[0])
return template
def order(self, numServers, virtualGuestId, imageTemplateId):
container = self.buildOrderContainer(
numServers, virtualGuestId, imageTemplateId)
orderReceipt = self.orderClient.placeOrder(container.get_template())
return orderReceipt
if __name__ == "__main__":
import argparse
argsparse = argparse.ArgumentParser(description='Order Drupal 8 Servers')
argsparse.add_argument('--numServers', metavar='numServers', type=int,
nargs=1, help='Number of Drupal 8 servers to provision.')
argsparse.add_argument('--virtualGuestId', metavar='virtualGuestId',
type=int, help='Id of template virtual guest.', default=1)
argsparse.add_argument('--imageTemplateId', metavar='imageTemplateId',
type=int, help='Image template ID.', default=1)
argsparse.add_argument('--list', action='store_true',
help='List drupal8 instances', default=False)
args = argsparse.parse_args()
apiUsername = ""
apiKey = ""
order = orderCloud(username=apiUsername, apikey=apiKey)
if args.list > 0:
order.listInstances()
else:
pprint.pprint(
order.order(
args.numServers,
args.virtualGuestId,
args.imageTemplateId
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment