October 5, 2016


Advanced Quote Ordering
Place an order using a quote with SSH keys and post provisioning scripts. Along with a few examples of how to get the needed data.

import SoftLayer
from pprint import pprint as pp

class example():

    def __init__(self):
        self.client = SoftLayer.Client()

    def orderQuote(self, quote_id, dc_id = None, image_id = None, private_vlan = None, public_vlan = None):
        # If you have more than 1 server in the quote, you will need to append
        # a copy of this for each VSI, with hostnames changed as needed
        guests = {
            'hostname': 'quotetest', 
            'domain': 'example.com'
        }
        if public_vlan:
            guests.update({
                'primaryNetworkComponent': {
                    "networkVlan": {"id": int(public_vlan)}}})
        if private_vlan:
            guests.update({
                "primaryBackendNetworkComponent": {
                    "networkVlan": {"id": int(private_vlan)}}})

        quote = self.client['Billing_Order_Quote']
        quote_container = quote.getRecalculatedOrderContainer(id=quote_id)

        container = quote_container
        container['quantity'] = 1
        container['virtualGuests'] = []
        container['virtualGuests'].append(guests)

        # container['provisionScripts'] = ['https://gist.githubusercontent.com/myscript.py']
        # container['sshKeys'] = [{'sshKeyIds': [660791]} ]

        if image_id is not None:
            container['imageTemplateId'] = image_id

        if dc_id is not None:
            container['location'] = dc_id

        # result = self.client['Product_Order'].verifyOrder(container)
        result = self.client['Product_Order'].placeOrder(container)
        pp(result)

    def listQuotes(self):
        quotes = self.client['SoftLayer_Account'].getActiveQuotes()
        pp(quotes)

    def listLocations(self):
        locations = self.client['SoftLayer_Location'].getDatacenters()
        pp(locations)

    def listSshKeys(self):
        keys = self.client['SoftLayer_Account'].getSshKeys()
        pp(keys)

    def listImageTemplates(self):
        mask = "mask[id,name,note]"
        imageTemplates = self.client['SoftLayer_Account'].getPrivateBlockDeviceTemplateGroups(mask=mask)
        print("ID - Name - Note")
        for template in imageTemplates:
            try:
                print("%s - %s - %s" % (template['id'], template['name'], template['note']))
            except KeyError:
                print("%s - %s - %s" % (template['id'], template['name'], 'None'))

    def listVlansInLocation(self, location_id):
        mask = "mask[id,vlanNumber,primaryRouter[hostname,datacenter[id,name]]]"
        objfilter2 = { "networkVlans":    
                        {"primaryRouter": 
                            {"datacenter": { "id" : {"operation":location_id} } }
                        }
                    }
        subnets = self.client['SoftLayer_Account'].getNetworkVlans(mask=mask,filter=objfilter2)
        for subnet in subnets:
            print("%s, %s, %s" % ( subnet['id'], subnet['vlanNumber'], subnet['primaryRouter']['hostname']))


if __name__ == "__main__":
    quote_id = 1234
    main = example()
    # main.listImageTemplates()
    # main.listQuotes()
    # main.listLocations()
    dal13 = 1854895
    ams03 = 814994
    dal09 = 449494
    # main.listSshKeys()
    # main.listVlansInLocation(dal13)
    backend_vlan = 1400000 # 123, bcr06a.dal09
    front_vlan = 1400001 # 456, fcr06a.dal09
    main.orderQuote(quote_id, dc_id=dal09, private_vlan=backend_vlan,public_vlan=front_vlan )

Feedback?

We would love to hear it
Open an issue

/python/order_quote_advanced/