Using Python with UCS Director

Version 2

    I recently updated my Python library and utilities for interfacing with CECS to add some additional capabilities for UCS Director (it was originally focused on UCSD).  All the code is available on GitHub at https://github.com/hpreston/cisco_cloud and I’d love to see others pull down the code, add features, fix bugs, etc.

     

    I focused on some initial use cases for working with UCSD as a developer looking to order and manage their VMs.  Rather than list out what is possible… here’s a narrated example of using the code.  Happy to hear from everyone on ideas on how to improve!


    Example… I’m a Cloud Consumer working on a Customer Portal project. 

    “What was the name of my vDC again?” 


    hpreston-imac:lab-automation hpreston$ ./ucsd_vdc_list.py -g "Cloud Users"

    [{u'Active_VMs': 2,

      u'Approvers': u'',

      u'Cloud': u'cloud-vcenter',

      u'Custom_Categories': 3,

      u'Group': u'Cloud Users',

      u'Lock_State': u'Unlocked',

      u'Status': u'',

      u'Tag': u'',

      u'Total_VMs': 2,

      u'Type': u'Standard',

      u'vDC': u'prj-CustomerPortal-u892',

      u'vDC_Description': u'Desc',

      u'vDC_ID': 57}]


    “That’s right… “prj-CustomerPortal-u892”… what VMs are in there already?”


    hpreston-imac:lab-automation hpreston$ ./ucsd_vm_list.py -k vDC -v prj-CustomerPortal-u892 -f VM_Name -f VM_ID -f Category

    [{'Category': u'Web Server', 'VM_ID': 239, 'VM_Name': u'gencld-u895'},

    {'Category': u'Web Server', 'VM_ID': 240, 'VM_Name': u'gencld-u899'},

    {'Category': u'Application Server', 'VM_ID': 241, 'VM_Name': u'gencld-u906'}]


    “Okay… I’ve got 2 Web Servers and an Application Server.  I’m going to need 2 more Web Servers and a Data Server.  What are those catalogs?” 


    hpreston-imac:lab-automation hpreston$ ./ucsd_catalog_list.py -g "Cloud Users" -f "Catalog_Name"

    [{'Catalog_Name': u'General Linux Web Server'},

    {'Catalog_Name': u'General Linux App Server'},

    {'Catalog_Name': u'General Linux Data Server'},

    {'Catalog_Name': u'General Windows Web Server'},

    {'Catalog_Name': u'General Windows App Server'},

    {'Catalog_Name': u'General Windows Data Server'}]


    “Alright… Let’s do a bulk order.  Let’s use the Python libraries rather than CLI utilities…"


    >>> from ucsd_library import *

    >>> from pprint import pprint

    >>>

    >>> # Notes about my project

    >>> vdc = "prj-CustomerPortal-u892"

    >>> catalog_web = "General Linux Web Server"

    >>> catalog_data = "General Linux Data Server"

    >>> group = "Cloud Users"

    >>>

    >>> # Create a list of the servers I need

    >>> # My list will be one item per request in format ["Catalog", "VDC", "Comment"]

    >>> servers = [[catalog_web, vdc, "1st new web server"],

    ... [catalog_web, vdc, "2nd new web server"],

    ... [catalog_data, vdc, "data server"]]

    >>>

    >>> # What's the format for the catalog_order() function?

    >>> help(catalog_order)

    Help on function catalog_order in module ucsd_library:


    catalog_order(catalog, vdc, group, comment='', vmname='', vcpus='0', vram='0', datastores='', vnics='')


    >>> # loop through the servers and order each that I need

    >>> for server in servers:

    ...     order = catalog_order(server[0], server[1], group, server[2])

    ...     print order

    ...

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIVMWareProvisionRequest', u'serviceResult': 919, u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIVMWareProvisionRequest', u'serviceResult': 920, u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIVMWareProvisionRequest', u'serviceResult': 921, u'serviceError': None}


    “Okay… ordered… what new VMs do I have?"


    >>> srs = ["919", "920", "921"]

    >>> for sr in srs:

    ...     pprint(sr_vms(sr))


    [{u'cloudName': u'cloud-vcenter',

      u'hostName': u'gencld-u919',

      u'imageId': u'gencld-u919',

      u'instanceId': u'gencld-u919',

      u'ipAddress': u'10.139.17.37',

      u'powerStatus': u'ON',

      u'statusMessage': u'poweredOn',

      u'vmId': 247,

      u'vmType': u'VMWare'}]

    [{u'cloudName': u'cloud-vcenter',

      u'hostName': u'gencld-u920',

      u'imageId': u'gencld-u920',

      u'instanceId': u'gencld-u920',

      u'ipAddress': u'10.139.17.38',

      u'powerStatus': u'ON',

      u'statusMessage': u'poweredOn',

      u'vmId': 246,

      u'vmType': u'VMWare'}]

    [{u'cloudName': u'cloud-vcenter',

      u'hostName': u'gencld-u921',

      u'imageId': u'gencld-u921',

      u'instanceId': u'gencld-u921',

      u'ipAddress': u'10.139.19.37',

      u'powerStatus': u'ON',

      u'statusMessage': u'poweredOn',

      u'vmId': 248,

      u'vmType': u'VMWare'}]


    “Wait… its 5… time to go home.  Let’s shutdown my servers till tomorrow.”


    >>> vms = vm_list(result_filter={"vDC":"prj-CustomerPortal-u892"})

    >>> # To power off the VMs, I need teh VM_ID only...

    >>> for vm in vms:

    ...     vm_poweroff(str(vm["VM_ID"]))

    ...

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'239-140-14d44d6fd91', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'240-141-14d44d6ff91', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'241-142-14d44d700ef', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'242-143-14d44d702c6', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'244-144-14d44d704de', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'246-145-14d44d70671', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'247-146-14d44d707fb', u'serviceError': None}

    {u'serviceName': u'InfraMgr', u'opName': u'userAPIExecuteVMAction', u'serviceResult': u'248-147-14d44d7097c', u'serviceError': None}

    >>>

    >>> # Give them time to shut down and check status again... grabbing a snack for the road

    >>> vms = vm_list(result_filter={"vDC":"prj-CustomerPortal-u892"}, key_filter=["VM_Name", "Power_State"])

    >>> pprint(vms)

    [{'Power_State': u'OFF', 'VM_Name': u'gencld-u895'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u899'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u906'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u910'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u915'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u920'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u919'},

    {'Power_State': u'OFF', 'VM_Name': u'gencld-u921'}]


    “I’m outta here!”