Register now for better personalized quote!

Python code from UCS PowerTool

May, 20, 2014 Hi-network.com

I|have|been|out|of-LimitScope||college-LimitScope||for-LimitScope |almost 25 years and have been programming ever since.  However I was a programmer before I graduated,I was a programmer when in 7thgrade 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 inline. 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") asf:forlineinf: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-UcsTransactionGet-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-UcsTransactionGet-UcsLanCloud | Get-UcsVlan-Name"jomcdono3335" -LimitScope | Remove-UcsVlanGet-UcsLanCloud | Get-UcsVlan-Name"jomcdono3334" -LimitScope | Remove-UcsVlanGet-UcsLanCloud | Get-UcsVlan-Name"jomcdono3333" -LimitScope | Remove-UcsVlanComplete-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

follow @johnamcdonough


tag-icon Hot Tags : UCS Python PowerTool PowerShell

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.