The Cisco UCS Object Model is a representation of all UCS objects logical or physical. The objects are all very similar, so much so that the code that makes up the UCS Python SDK and the UCS PowerTool Suite are ninety-nine percent generated. 99% is a lot of similarities but you would never mistake a VLAN object for a Service Profile object, or a Boot Policy object for a MAC Pool object. The similarities come in the form of structure, properties, privileges, processing, etc. These similarities and the generated UCS Python SDK code are what I am going to focus on to show you how to write less code.
Mostly we write code that is object-specific. For example, if you want to add a VLAN to a UCS Manager using the UCS Python SDK you need to:
Query for the FabricLanCloud so you know where to put the new VLAN
Import the FabricVlan module
instantiate an instance of the FabricVlan class, minimally setting these attributes
id
name
Add the VLAN object instance to the UCS connection handle
Commit the handle
If all went well the new VLAN is added to the Fabric LAN Cloud. The code below shows this process including the UCS handle creation code.
>from ucsmsdk.ucshandle import UcsHandle
>from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan
>handle = UcsHandle("192.168.220.201", "admin", "password")
>handle.login()
>fabric_lan_cloud = handle.query_dn("fabric/lan")
>vlan_100 = FabricVlan(parent_mo_or_dn=fabric_lan_cloud,
name="vlan100",
id="100")
>handle.add_mo(vlan_100)
>handle.commit()
>handle.logout()
There is nothing wrong with this code. It could be enhanced with exception handling and parameterization of username, password, UCS IP, VLAN ID, and VLAN Name. You could also add validation of those parameters as well as some command line options for the parameters. Then, after you did all that, you would have a Python program that adds a VLAN to UCS Manager. Considering there are thousands and thousands of UCS Manager objects you better stock up on whatever fuels your development sessions.