Cisco UCS Python SDKs John McDonough, Technical Leader – Developer Evangelist DEVNET-2063
Cisco Spark Questions? Use Cisco Spark to chat with the speaker after the session
How 1.
Find this session in the Cisco Live Mobile App
2.
Click “Join the Discussion”
3.
Install Spark or go directly to the space
4.
Enter messages/questions in the space
Cisco Spark spaces will be available until July 3, 2017.
cs.co/ciscolivebot#DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Agenda •
Introduction •
DEVNET-2063 - Cisco UCS Python SDKs
•
UCS Unified API Overview
•
UCS Python SDK •
Install / Get Help / Variable Inspection / Meta Data • Connect / Query / Filter / Dump XML • Configure / Transactions • Compare / Sync / Code Generation •
What else and What's Next / Conclusion
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
UCS Unified API - Overview
Cisco Unified Computing System Cisco UCS™ Manager / IMC / Central
Cisco UCS Fabric Interconnects Cisco UCS I/O modules Cisco UCS Blade Server Chassis
Cisco UCS Blade and Rack Servers
Cisco UCS I/O Adapters
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
6
UCS Management Programmatic Infrastructure Libraries / SDKs
GUI & CLI
3rd Party
Direct
Customer Self Serve portals Management Tools Auditing Tools
XML API Status Inventory Configuration
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
7
UCS XML API Overview
UCS XML API Features • • • • • • • • • •
Communicates over HTTP / HTTPS XML Based, Transactional Standard Request / Response cycle Role Based Authentication Object Model Hierarchy Built-in Object Browser Published XML Schema Full Object Documentation High Availability by default Event Stream
XML API
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
8
UCSM Object Model Documentation
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
9
Management Information Model(MIM) Root
Everything is an object
Objects are hierarchically organized Class identifies object type: Card, Port, VNIC… Class Inheritance Server port is a subclass of port. A server blade is a subclass of compute entity. Set of attributes
MO represents any physical or logical entity Managed Object
Each MO is uniquely identified by DN
UCS XML Database contains comprehensive system information Discovered components System configuration Operational status including statistics and faults DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
10
Distinguished Names Root
rn1 rn2
Slash delimited sequence of RNs Immutable DN Provides a fully qualified path
rn3
dn = {rn1}/{rn2}/{rn3} Example:
DN unambiguously identifies a target object. DN name is composed of sequence of Relative Names (RN).
RN identifies an object within the context of its parent object.
is composed of the following relative names: topSystem MO: rn="sys" equipmentChassis MO: rn="chassis-" computeBlade MO: rn ="blade-<slotId>" adaptorUnit MO: rn="adaptor-" adaptorHostEthIf MO: rn="host-eth-"
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
11
Python SDK
Install
Cisco UCS Python SDKs - Install •
Hosted on Github •
UCS Manager SDK – 0.9.3.1 •
• •
•
UCS IMC SDK – 0.9.2.0 •
•
•
Source – https://github.com/CiscoUcs/ucsmsdk Samples – https://github.com/CiscoUcs/ucsmsdk_samples Documents – https://CiscoUcs.github.io/ucsmsdk_docs
Source – https://github.com/CiscoUcs/imcsdk Documents – https://ciscoucs.github.io/imcsdk_docs
UCS Central Python SDK – 0.9.0.0 •
Source – https://github.com/CiscoUcs/ucscentralsdk
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
14
Cisco UCS Python SDKs - Install •
Install Python 2.7.X or 3.5.X or 3.6.X •
Or both UCS Python SDKs can coexist!
•
pip – Preferred Installer Program (package manager) •
Installs latest “Release” from https://pypi.python.org/pypi pip install ucsmsdk / pip install ucscsdk / pip install imcmsdk
•
git – Install from github.com – works for ucsmsdk, imcsdk, ucscentralsdk •
Installs latest SDK source code from https://github.com/CiscoUcs/ucsmsdk git clone https://github.com/CiscoUcs/ucsmsdk.git cd ucsmsdk C:\Python27\python.exe .\setup.py install Python 2.7 'C:\Program Files\Python35\python.exe' .\setup.py install Python 3.5 DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
15
Get Help / Variable Inspection / Meta Data
Get Help •
from ucsmsdk.ucshandle import UcsHandle
•
help(UcsHandle)
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
17
Variable Inspection - vars •
vars(variable_name)
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
18
Find the Class Id
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
19
MetaData – get_meta_info •
get_meta_info(class_id="ClassName")
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
20
Connect / Query / Filter / Dump XML
Connect & Query •
Connect from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("", "admin", "passsword") handle.login()
•
UcsHandle •
• • • •
handle.query_classid – Query a Specific ClassId handle.query_classids – Query a group of ClassIds handle.query_children – Query a dn children or Specific ClassId children of a dn handle.query_dn – Query a Specific dn handle.query_dns – Query a group of dns
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
22
Connect & Query
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
23
Connect & Query # Returns Object blade_by_dn = handle.query_dn("sys/chassis-5/blade-2") print blade_by_dn # Returns list blades = handle.query_classid("ComputeBlade") print blades
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
24
Connect & Query # Returns dictionary blades_and_chassis = handle.query_classids("ComputeBlade","EquipmentChassis") print blades_and_chassis print blades_and_chassis['ComputeBlade'] for blade in blades_and_chassis['ComputeBlade']: print blade.dn for chassis in blades_and_chassis['EquipmentChassis']: print chassis.dn blade_and_chassis = handle.query_dns('sys/chassis-3/blade-1','sys/chassis-3') print blade_and_chassis print blade_and_chassis['sys/chassis-3/blade-1'].dn
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
25
Query Filters # Regular Expression filter_exp='(model,"UCS*", type="re")' compute_blades = handle.query_classid("ComputeBlade",filter_str=filter_exp) print len(compute_blades), filter_exp
# Not Equal filter_exp='(model,"UCSB-B420-M4", type="ne")' compute_blades = handle.query_classid("ComputeBlade",filter_str=filter_exp) print len(compute_blades) , filter_exp
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
26
Query Filters # Exact Match is Case Sensitive filter_exp='(model,"ucsb-b420-m4", type="eq")' compute_blades = handle.query_classid("ComputeBlade",filter_str=filter_exp) print len(compute_blades), filter_exp
# Exact Match without Case Sensitivity filter_exp='(model,"ucsb-b420-m4", type="re", flag="I")' compute_blades = handle.query_classid("ComputeBlade",filter_str=filter_exp) print len(compute_blades), filter_exp
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
27
Dump XML •
handle.set_dump_xml()
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
28
Query Filters From ucsfilter.py types = {"eq": "ne": "ge": "gt": "le": "lt": "re": }
"EqFilter", "NeFilter", "GeFilter", "GtFilter", "LeFilter", "LtFilter", "WcardFilter"
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
29
Demo
Configure / Transactions
Configure – Create •
Create / Update / Delete •
Create VLAN
1.
import
2.
login
3.
query Parent object
4.
add child object(s)
5.
commit
6.
logout
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
32
Configure – Update •
Create / Update / Delete •
Update VLAN
1.
import
2.
login
3.
query object
4.
set object
5.
commit
6.
logout
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
33
Configure – Delete •
Create / Update / Delete •
Delete VLAN
1.
import
2.
login
3.
query object
4.
remove object
5.
commit
6.
logout
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
34
Transactions •
Transactions are Implicit •
Create VLANs
1.
import
2.
login
3.
query Parent object
4.
add child objects
5.
commit
6.
logout
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
35
Compare / Sync / Code Generation
Compare & Sync •
Use compare_ucs_mo to see the Object differences between two domains.
•
Use sync_ucs_mo with diff object to synchronize the VLANS between two domains. from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.utils import comparesyncmo source_ucs=UcsHandle("192.168.0.1", "admin", "password") target_ucs=UcsHandle("192.168.0.2", "admin", "password") source_ucs.login() target_ucs.login() source_ucs_vlans=source_ucs.query_classid("fabricVlan") target_ucs_vlans=target_ucs.query_classid("fabricVlan") difference_vlans=comparesyncmo.compare_ucs_mo(target_ucs_vlans, source_ucs_vlans) comparesyncmo.write_mo_diff(difference_vlans) # print the difference to the console comparesyncmo.sync_ucs_mo(target_ucs, difference_vlans, delete_not_present=True) delete_not_present=True, Removes VLANs from target_ucs that are not present in source_ucs
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
37
Demo
Code Generation – HTML5 Way
1.
control-option/alt-q (MAC) Ctrl-Alt-q (Windows)
2.
Click Record XML
3.
Do Configuration
4.
Click Stop XML Recording
5.
Download XML file
6.
Feed XML file to UCS Python SDK conversion method
from ucsmsdk.utils.converttopython import convert_to_ucs_python convert_to_ucs_python(xml=True, literal_path="Z:\\Downloads\\vlan-ops_xmlReq.log") DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
39
Demo
What's New What's Next
What's New / What's Next • New • • • •
Ansible Puppet Chef Redfish
• Next • More
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
42
Complete Your Online Session Evaluation •
Give us your feedback to be entered into a Daily Survey Drawing. A daily winner will receive a $750 gift card.
•
Complete your session surveys through the Cisco Live mobile app or on www.CiscoLive.com/us. Don’t forget: Cisco Live sessions will be available for viewing on demand after the event at www.CiscoLive.com/Online.
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Continue Your Education •
DevNet Learning Labs – https://learninglabs.cisco.com
•
DevNet UCS Dev Center – https://developer.cisco.com/site/ucs-dev-center
•
Demos in the Cisco campus
•
Walk-in Self-Paced Labs
•
Lunch & Learn
•
Meet the Engineer 1:1 meetings
•
Related sessions •
DEVNET-2060
DEVNET-2063
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
44
Thank you