Skip to content

Instantly share code, notes, and snippets.

@rhodgin
Last active August 29, 2015 14:19
Show Gist options
  • Save rhodgin/e3a828198a2e6e299301 to your computer and use it in GitHub Desktop.
Save rhodgin/e3a828198a2e6e299301 to your computer and use it in GitHub Desktop.
Code to list SoftLayer virtual instances (VSIs) that were created by specific user ID
'''
Code example of how to list virtual instances from a SoftLayer account
You can call the script from the command and provide the user ID you
want to check. The script expects that you have the SoftLayer CLI
installed and have it configured with the credentials you'd like
to use with SoftLayer.
You can run the script with the user ID you'd like to check under
the account you have configured:
list_my_active_softlayer_vms userid
The output of the script is in a csv format with tabs as the
column separator.
'''
from __future__ import print_function
import sys
import SoftLayer
def list_my_vms(creator_id=None):
"""
Get the list of active VMs based on the creator ID
Keyword arguments:
creator_id -- the SoftLayer ID of the VM's creator (default: None)
"""
# create a client to the SoftLayer_Account API service.
client = SoftLayer.Client()
# number of results that we expect at a time
chunk = 200
mask = "id,fullyQualifiedDomainName,primaryIpAddress,createDate,billingItem[orderItem[description, order[userRecord[username], id]]]"
# get the data from the SoftLayer APIs
vms = client.iter_call("Account", "getVirtualGuests", chunk=chunk, mask=mask)
if creator_id is None:
# if the creator wasn't provided use the
# user provided to the API call as the creator
creator_id = client.auth.username
# add table header
print('VM ID', '\t', 'Host', '\t', 'Public IP', '\t', 'Create Date')
for vsi in vms:
# navigate to the creator property under the VMs billing item
if 'billingItem' in vsi.keys():
billing_item = vsi['billingItem']
if 'orderItem' in billing_item.keys():
vm_creator_userid = billing_item['orderItem']['order']['userRecord']['username']
if creator_id == vm_creator_userid:
print(str(vsi['id']), '\t',
vsi['fullyQualifiedDomainName'], '\t',
vsi.get('primaryIpAddress', 'N/A'), '\t', vsi['createDate'])
if __name__ == '__main__':
if len(sys.argv) == 1:
list_my_vms()
else:
list_my_vms(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment