Sometimes I just can't be bothered to do things in the "normal" way. Now, I can enjoy a good ole zen activity like the occasional manual data entry, but in general if I have to repeat a task more than a few times I get antsy. And I am not completely unsure that someone smart and sucessful said something good about lazy people. So in my quest for maximum return for minimal effort I find myself collecting various useful snippets and scripts. Today we will go through one in my collection.
Upgrading a CCI through the SLAPI is a fairly straight forward process which invloves creating an order container of type SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade. In addition to the usual suspects this container will also require a property by the name of MAINTENANCE_WINDOW
.
Import the SLAPI, datetime which will provide the current time stamp for our maintenance window and pretty printer because no one likes ugly print...
import SoftLayer.API import datetime from pprint import pprint as pp
This helper function takes in the amount of RAM you wish set for your CCI as an int and returns the prices array needed for the order container. Take note of the anonmyous function on the len() check...nested foreach loops are cool and all, but this is much more fun.
# Find the priceId for the RAM item matching our defined memory allotment def getMemoryPrice(memoryCapacity): packageClient = SoftLayer.API.Client('SoftLayer_Product_Package', 46, apiUsername, apiKey) objectMask = "mask[capacity;prices.id;categories[name,id]]" packageClient.set_object_mask(objectMask) items = packageClient.getItems() for item in items: if len(filter(lambda x: x.get('id') == 3, item['categories'])) \\ and item.get('capacity') == str(memoryCapacity): return item['prices'][0]['id']
This function puts all our data together in the needed format then sends it off to processing where the unicorns churn it into server dust.
# Create the upgrade order container and send the order to the API. Replace # verifyOrder() with placeOrder() after testing def upgradeOrder(virtualGuestId, memoryPriceId): orderClient = SoftLayer.API.Client('SoftLayer_Product_Order', None, apiUsername, apiKey) orderContainer = {} orderContainer['complexType'] = 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' orderContainer['virtualGuests'] = [{'id': virtualGuestId}] orderContainer['prices'] = [{'id': memoryPriceId}] orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', 'value': str(datetime.datetime.now())}] return orderClient.verifyOrder(orderContainer)
To wrap up: our implimentation of argsparse to handle the command line arguments, our API username and API key and our call to upgradeOrder which sets the process in motion.
# Define our arguments and start the process if __name__ == "__main__": import argparse argsparse = argparse.ArgumentParser(description='Upgrade a CCI\\'s Memory') argsparse.add_argument('virtualGuestId', metavar='Virtual Guest ID', type=int, help='ID of the target CCI.') argsparse.add_argument('memoryCapacity', metavar='Ram GBs', type=int, help='Amount of RAM in GB.') args = argsparse.parse_args() apiUsername = '' apiKey = '' pp(upgradeOrder(args.virtualGuestId, getMemoryPrice(args.memoryCapacity)))
Now with the syntax python upgradeCCI.py
we can easily place a RAM upgrade order. Remember to reboot your CCI after the order is complete to take advantage of your newly aquired memory.
-Phil