Cisco Blogs


Cisco Blog > Data Center and Cloud

Python code from UCS PowerTool

May 20, 2014
at 9:25 am PST

I have been out of college for almost 25 years and have been programming ever since.  However I was a programmer before I graduated, I was a programmer when in 7th grade I got to bring home a Commodore PET (Personal Electronic Transactor), for a week at a time. I thought that everyone would want to bring it home, I got to school early every Friday morning to signup to bring it home the next week. Turns out I was always the only one in line. Then my dad bought me a Timex Sinclair 1000, and I was hooked.

Oddly enough I started out studying engineering in,1984, I didn’t think that my computer hobby was a pursuable major. In most classes I just felt I didn’t get it and those around me did, maybe engineering was not for me. Then I took my first programming course, Fortran, I got it, but very few others did, it was then that I realized that I was a programmer.

In “The Mythical Man Month” by Fred Brooks, in the very first chapter, The Tar Pit, there is a description of the joys of the craft and it captured why I program, perfectly.

The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.

Written in the 1970s, I couldn’t believe how succinct and right on-target was the statement. My early languages were C, Pascal, PL/1, ada. And after I graduated C++, Java, and object oriented programming became the rage. I did come to like Java and augmented my programming skills with Perl and was perfectly happy to continue along with those, that was until I met Python

I’m not the kind of person to get into debates about what programming language is better and why.  I’m an average guy programmer, if it works, great! And Python works great.

For starters python fits, it works on multiple OSes and code is portable from OS to OS.  Cisco has python interpreters embedded on several of its Nexus devices. There is a python SDK in development for UCS  (beta version) Also a major Open Source Cloud Computing platform is written in python. Python is free! Finally Python embodies, IMO, the second sentence from the quote above, Python as a media of creation is flexible, easy to polish and rework and very capable of realizing grand conceptual structures.

Python is concise – First program – Hello World

print "hello world"

 

Python is eloquent – File handling – Open / Read / Print / Close file

with open("/Users/jomcdono/Desktop/test.txt") as f:
    for line in f:
        print line

The “with” is a context manager in python it replaces the “try:except:finally:” The keyword “with” is not just for files, context managers can be written for any object.

Python is extensive

The included python libraries are extensive, but if you cannot find what you need in what is included get it from The Python Package Index, https://pypi.python.org/pypi

Python is integrated into UCS PowerTool

UCS PowerTool cmdlet ConvertTo-UcsCmdlet generates Python code for the UCS Python SDK!

You already know how ConvertTo-UcsCmdlet works. Connect to a UCS, start a UCS Manager GUI session, run ConvertToUcsCmdlet.  For example in the UCS Manager GUI I created and then deleted three VLANs with ConvertToUcsCmdlet running in my PowerTool session.

PS C:\> ConvertTo-UcsCmdlet

Create VLANs in UCS Manager and get the ConvertTo-UcsCmdlet PowerShell output below

Start-UcsTransaction
Get-UcsLanCloud | Add-UcsVlan -CompressionType "included" -DefaultNet "no" -Id 3335 -Name "jomcdono3335" -PolicyOwner "local" -Sharing "none" -XtraProperty @{McastPolicyName=""; PubNwName=""; }
Get-UcsLanCloud | Add-UcsVlan -CompressionType "included" -DefaultNet "no" -Id 3334 -Name "jomcdono3334" -PolicyOwner "local" -Sharing "none" -XtraProperty @{McastPolicyName=""; PubNwName=""; }
Get-UcsLanCloud | Add-UcsVlan -CompressionType "included" -DefaultNet "no" -Id 3333 -Name "jomcdono3333" -PolicyOwner "local" -Sharing "none" -XtraProperty @{McastPolicyName=""; PubNwName=""; }
Complete-UcsTransaction

 

Delete VLANs in UCS Manager and get the ConvertTo-UcsCmdlet PowerShell output below

Start-UcsTransaction
Get-UcsLanCloud | Get-UcsVlan -Name "jomcdono3335" -LimitScope | Remove-UcsVlan
Get-UcsLanCloud | Get-UcsVlan -Name "jomcdono3334" -LimitScope | Remove-UcsVlan
Get-UcsLanCloud | Get-UcsVlan -Name "jomcdono3333" -LimitScope | Remove-UcsVlan
Complete-UcsTransaction

Then I did the same operations in the GUI but this time when I started ConvertToUcsCmdlet I added the -Python command line switch.

PS C:\> ConvertTo-UcsCmdlet -Python

Create VLANs in UCS Manager and get the ConvertTo-UcsCmdlet Python output below

handle.StartTransaction()
obj = handle.GetManagedObject(None, None, {"Dn":"fabric/lan"})
handle.AddManagedObject(obj, "fabricVlan", {"DefaultNet":"no", "PubNwName":"", "Dn":"fabric/lan/net-jomcdono3335", "PolicyOwner":"local", "CompressionType":"included", "Name":"jomcdono3335","Sharing":"none", "McastPolicyName":"", "Id":"3335"})
obj = handle.GetManagedObject(None, None, {"Dn":"fabric/lan"})
handle.AddManagedObject(obj, "fabricVlan", {"DefaultNet":"no", "PubNwName":"", "Dn":"fabric/lan/net-jomcdono3334", "PolicyOwner":"local", "CompressionType":"included", "Name":"jomcdono3334","Sharing":"none", "McastPolicyName":"", "Id":"3334"})
obj = handle.GetManagedObject(None, None, {"Dn":"fabric/lan"})
handle.AddManagedObject(obj, "fabricVlan", {"DefaultNet":"no", "PubNwName":"", "Dn":"fabric/lan/net-jomcdono3333", "PolicyOwner":"local", "CompressionType":"included", "Name":"jomcdono3333","Sharing":"none", "McastPolicyName":"", "Id":"3333"})
handle.CompleteTransaction()

 

Delete VLANs in UCS Manager and get the ConvertTo-UcsCmdlet Python output below

handle.StartTransaction()
obj = handle.GetManagedObject(None, None, {"Dn":"fabric/lan/net-jomcdono3335"})
handle.RemoveManagedObject(obj)
obj = handle.GetManagedObject(None, None, {"Dn":"fabric/lan/net-jomcdono3334"})
handle.RemoveManagedObject(obj)
obj = handle.GetManagedObject(None, None, {"Dn":"fabric/lan/net-jomcdono3333"})
handle.RemoveManagedObject(obj)
handle.CompleteTransaction()

Pretty cool. You will need to parameterize the code and put it in a control structure, but now you can easily translate operations in UCS Manager into Python using UCS PowerTool.

How about writing a context manager to handle connections to your UCS domains, manage your initial authentications and refreshes, catch exceptions, and ensure session logout.

More reasons why Python is Awesome from Python core developer Raymond Hettinger

I leave that one to you. As always I hope this helps in your scripting and automation efforts. Script formatting was provided by, http://hilite.me

Tags:

In an effort to keep conversations fresh, Cisco Blogs closes comments after 60 days. Please visit the Cisco Blogs hub page for the latest content.

6 Comments.


  1. Stephen Speirs

    Glad to see someone who know about/has used a Commodore PET :-) … must be an age thing John :-)

       0 likes

    • Stephen Speirs

      meant to say … was my first computer/programming experience too! (hence age comment applies to me!)

         0 likes

  2. May 21, 2014 at 6:55 am

    Age applies to me as well, but programming is a great way to stay mentally active.

       1 like

  3. Great read John. My first and last programming experience was FORTRAN which I excelled. My new found love is Python, which I agree is awesome and does the job.

       0 likes

  4. May 30, 2014 at 12:35 pm

    Thanks a lot for a very good write up, John! With the impending advent of SDN/NFV I personally had to brush up my Python skills majorly :-), especially client-server programming. For someone who has largely been a network engineer since 1999, I had some catching up to do. :-) But, on the positive side, it did not take me too long to come upto some sort of speed.

    I have a query: is a version of the UCS Powertool available for non-Cisco partners please? For example, same as UCS has an emulator available for all.

    BTW, I also recall Commodore 64′s, VAX machines:-), 5¼-inch diskettes and believe it or not punch cards…even though I never really worked with those. An age thing I am sure :-)

    Thank you for a very trendy and thorough discussion!

    Best Regards

    santanu

       0 likes

  5. May 30, 2014 at 1:35 pm

    Hi Santanu,

    UCS PowerTool is available on cisco.com at, http://software.cisco.com/download/type.html?mdfid=283850978&flowid=25021

    You do need an account on cisco.com

    Regards,
    John

       0 likes