Search code examples
ibm-cloud-infrastructure

How to order an instance with MongoDB installed


I am working with Softlayer Python Client API. I would like to laucn an instance with mongodb installed via API. How can I do it?


Solution

  • To order a virtual guest with a Mongo DB it’s necessary to use the price id of this item. The best way to verify/place an order with the available price items for any product is to review the next method: http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemPrices

    The next script can be used to order a Virtual Guest with MongoDB Community Edition.

    """
    Order Virtual Guest with MongoDB Community Edition.
    
    Important manual pages:
    https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest
    http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder
    http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
    
    License: http://sldn.softlayer.com/article/License
    Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
    """
    import SoftLayer
    from pprint import pprint as pp
    
    USERNAME = 'set me'
    API_KEY = 'set me'
    
    client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
    
    order = {
        'complexType': 'SoftLayer_Container_Product_Order_Virtual_Guest',
        'quantity': 1,
        'virtualGuests': [
            {'hostname': 'test-template', 'domain': 'example.com'}
        ],
        'location': 168642,  # San Jose 1
        'packageId': 46,     # CCI Package
        'prices': [
            {'id': 1640},  # 1 x 2.0 GHz Core
            {'id': 1644},  # 1 GB RAM
            {'id':  905},  # Reboot / Remote Console
            {'id':  272},  # 10 Mbps Public & Private Networks
            {'id':50231},  # 1000 GB Bandwidth
            {'id':   21},  # 1 IP Address
            {'id': 2202},  # 25 GB (SAN)
            {'id':13945},  # CentOS 6.x - Minimal Install (64 bit)
            {'id':   55},  # Host Ping Monitoring
            {'id':   57},  # Email and Ticket Notifications
            {'id':   58},  # Automated Notification Response
            {'id':  420},  # Unlimited SSL VPN Users & 1 PPTP VPN User per account
            {'id':  418},  # Nessus Vulnerability Assessment & Reporting
           {'id':20893}   # MongoDB Community Edition
        ]
    }
    
    try:
           # Replace verifyOrder for placeOrder
           result = client['SoftLayer_Product_Order'].verifyOrder(order)
           pp(result)
    except SoftLayer.SoftLayerAPIError as e:
           pp('Unable to verify/place order faultCode=%s, faultString=%s' 
           % (e.faultCode, e.faultString))
    

    You could review the next link for further information as well: http://sldn.softlayer.com/blog/bpotter/Going-Further-SoftLayer-API-Python-Client-Part-3

    UPDATE

    An object mask would be the best way to retrieve additional object's data, like the item price ids of an instance already created.

    You can use this mask:

    mask[billingItem[orderItem,children[orderItem]]]
    

    Or this one, which is more granular:

    mask[billingItem[id,orderItem[itemPriceId],children[id,orderItem[itemPriceId]]]]
    

    In python you could use these masks in this way:

    """
    Get Virtual Guest and its itme price ids
    
    Important manual pages:
    https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest
    https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/getObject
    https://sldn.softlayer.com/article/object-masks
    
    License: http://sldn.softlayer.com/article/License
    Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
    """
    import SoftLayer
    from pprint import pprint as pp
    
    USERNAME = 'set me'
    API_KEY = 'set me'
    
    virtualGuestId = 25129311
    
    client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
    
    objectMask = 'mask[billingItem[id,orderItem[itemPriceId],children[id,orderItem[itemPriceId]]]]'
    
    try:
        result = client['SoftLayer_Virtual_Guest'].getObject(id=virtualGuestId, mask=objectMask)
        pp(result)
    except SoftLayer.SoftLayerAPIError as e:
        pp('Unable to get virtual guest faultCode=%s, faultString=%s' 
        % (e.faultCode, e.faultString))
    

    Regarding the ssh keys, you just need to add this line into the order object of the above script (the one to order a virtual guest with mongo db)

    'sshKeys': [{'sshKeyIds': [214147, 94206]}]