February 24, 2012

Classes
Tags blog

Command Line Simplicity
<p>Sometimes when putting together a script for the API I find myself wanting to remove as much complication from the pr

Sometimes when putting together a script for the API I find myself wanting to remove as much complication from the process as possible. Maybe in need of troubleshooting simplicity to rule out the plethora of idiosyncrasies which may be causing an API call to fail, or possibly just the need to walk my way through an idea and worry about the logic later.

I’m sure we all have our own process to filter out all of the complications the technologies we use may cause; for me in the case of the SLAPI I fall back to a Bash shell, curl and our REST endpoint. This trio allows me to make calls to the API without having to be concerned with remembering to define my array outside of a loop.

Get a list of CCIs and their primary IP addresses

curl "https://[apiUser]:[apiKey]@api.softlayer.com/rest/v3/SoftLayer_Account/VirtualGuests.xml?objectMask=virtualGuests.id;virtualGuests.primaryIpAddress"

Create a CNAME record

First we need to create our POST data which will contain one element named “parameters” which contains our SoftLayer_Dns_Domain_ResourceRecord templateObject (type).

{
    "parameters" : [
        {
            "data": "www.example.com.",
            "domainId": 1117134,
            "host": "atest.example.com",
            "ttl": 86400,
            "type": "cname"
 
    ]
}

I have used -d to specify this request as POST and @cname.json to tell curl to look in file cname.json for the data

curl -d @cname.json https://[apiUser]:[apiKey]@api.softlayer.com/rest/v3/SoftLayer_Dns_Domain_ResourceRecord.json

Data Formats

The SoftLayer API will provide responses in XML or JSON however the ease of viewing from your Bash prompt will vary. In general XML will display in an nice easy to read format, while JSON likes to live all on one line. A nifty trick from fellow SLDN blogger Scott T is to pipe your JSON response into python -mjson.tool or tidy-xml should you find your XML unwieldy.

Our next example we will be requesting JSON response data. We have used a resultLimit too keep the number of responses manageable.

Raw JSON

curl "https://[apiUser]:[apiKey]@api.softlayer.com/rest/v3/SoftLayer_Product_Package/46/getItems.json?resultLimit=1"

Output:

{"capacity":"250","description":"250 GB (LOCAL)","id":3916,"softwareDescriptionId":null,"units":"GB","upgradeItemId":null,"prices":[{"currentPriceFlag":null,"hourlyRecurringFee":".02","id":13958,"itemId":3916,"laborFee":"0","onSaleFlag":null,"oneTimeFee":"0","recurringFee":"14","setupFee":"0","sort":0,"accountRestrictions":[{"accountId":34455,"id":3089,"itemPriceId":13958}]}]}

Data Formatting Magic

curl "https://[apiUser]:[apiKey]@api.softlayer.com/rest/v3/SoftLayer_Product_Package/46/getItems.json?resultLimit=1" | python -mjson.tool

Output:

{
    "capacity": "40", 
    "description": "40 GB NAS", 
    "id": 41, 
    "prices": [
        {
            "accountRestrictions": [], 
            "currentPriceFlag": null, 
            "hourlyRecurringFee": ".04", 
            "id": 47, 
            "itemId": 41, 
            "laborFee": "0", 
            "onSaleFlag": null, 
            "oneTimeFee": "0", 
            "recurringFee": "20", 
            "setupFee": "0", 
            "sort": 0
        }
    ], 
    "softwareDescriptionId": null, 
    "units": "GIGABYTE", 
    "upgradeItemId": 42
}

If the commandline is not your style don't fret! We have a tool for creating REST calls on our github.


Feedback?

We would love to hear it
Open an issue