def getRackmount(): authInfo = _getUcsAuthInfo((request.headers)) data = [] handle = UcsHandle(*authInfo, secure=False) if handle.login(): try: computeRackUnit = handle.query_children(in_dn="sys", class_id="computeRackUnit") except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: if (type(computeRackUnit) == list): for x in computeRackUnit: server = {} server["name"] = x.rn server["path"] = x.dn server["macs"] = [] try: macs = handle.query_children(in_dn=x.dn, class_id='PciEquipSlot') except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 for y in macs: server["macs"].append(y.mac_left) server["macs"].append(y.mac_right) data.append(server) handle.logout() return data else: handle.logout() return "Couldn't fetch computeRackUnits:", "", 500 else: handle.logout() return 'Forbidden', "", 403
def getChassis(): authInfo = _getUcsAuthInfo((request.headers)) data = [] handle = UcsHandle(*authInfo, secure=False) if handle.login(): try: elememts = handle.query_children(in_dn="sys", class_id="EquipmentChassis") except UcsException as e: return 'Internal Server Error', e.error_descr, 500 else: if (type(elememts) == list): for element in elememts: chassis = {} identifier = element.dn obj = handle.query_dn(dn=identifier) chassis["name"] = obj.rn chassis["path"] = obj.dn chassis["members"] = [] try: blades = handle.query_children(in_dn=identifier, class_id='ComputeBlade') except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: if (type(blades) == list): for x in blades: server = {} server["name"] = x.rn server["path"] = x.dn server["macs"] = [] try: adptares = handle.query_children( in_dn=x.dn, class_id='AdaptorUnit') except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: for x in adptares: server["macs"].append(x.base_mac) chassis["members"].append(server) data.append(chassis) else: handle.logout() return "Couldn't fetch ComputeBlade", "", 500 else: handle.logout() return "Couldn't fetch EquipmentChassis", "", 500 else: handle.logout() return 'Forbidden', "", 403 handle.logout() return data
def updateVlans(module): ucs_ip = module.params['ucs_ip'] ucs_user = module.params['ucs_user'] ucs_pass = module.params['ucs_pass'] vlans = module.params['vlans'] state = module.params['state'] results = {} results['changed'] = False results['created'] = [] results['removed'] = [] # Login to UCS try: handle = UcsHandle(ucs_ip, ucs_user, ucs_pass) except: module.fail_json(msg="Could not login to UCS") try: # Obtain a handle for the LAN Cloud lancloud = handle.query_classid(class_id="FabricLanCloud") defined_vlans = handle.query_children(in_mo=lancloud[0], class_id="FabricVlan") for key, val in vlans.iteritems(): filter_str = '(name, "%s", type="eq") and (id, %s, type="eq")' \ % (key, val) obj = handle.query_children(in_mo=lancloud[0], class_id="FabricVlan", filter_str=filter_str) if state == 'present' and len(obj) > 0: pass elif state == 'present' and len(obj) == 0: vlan = FabricVlan(lancloud[0], name=key, id=str(val)) handle.add_mo(vlan) handle.commit() results['created'].append(key) results['changed'] = True elif state == 'absent' and len(obj) > 0: handle.remove_mo(obj[0]) handle.commit() results['changed'] = True results['removed'].append(key) except Exception, e: module.fail_json(msg="Could not create or validate VLANs; %s" % e)
def systemGetAll(): authInfo = _getUcsAuthInfo((request.headers)) handle = UcsHandle(*authInfo, secure=False) if handle.login(): elements = [{"ciscoXmlName": "EquipmentChassis", "humanReadableName": "Chassis"}, {"ciscoXmlName": "NetworkElement", "humanReadableName": "Fabric Interconnects"}, {"ciscoXmlName": "EquipmentFex", "humanReadableName": "FEX"}, {"ciscoXmlName": "computeRackUnit", "humanReadableName": "Servers"}] finalObjs = {} for x in elements: units = [] try: components = handle.query_children(in_dn="sys", class_id=x["ciscoXmlName"]) except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: if(type(components) == list): for y in components: subElement = {"relative_path": "/" + (vars(y))["dn"]} units.append(subElement) finalObjs[x["humanReadableName"]] = units else: handle.logout() return "Couldn't fetch " + x["ciscoXmlName"], "", 500 handle.logout() return finalObjs else: handle.logout() return 'Forbidden', "", 403
def systemGetAll(host=None, user=None, password=None): handle = UcsHandle(host, user, password, secure=False) if handle.login(): elements = [{ "ciscoXmlName": "EquipmentChassis", "humanReadableName": "Chassis" }, { "ciscoXmlName": "NetworkElement", "humanReadableName": "Fabric Interconnects" }, { "ciscoXmlName": "EquipmentFex", "humanReadableName": "FEX" }, { "ciscoXmlName": "computeRackUnit", "humanReadableName": "Servers" }] finalObjs = {} for x in elements: units = [] components = handle.query_children(in_dn="sys", class_id=x["ciscoXmlName"]) for y in components: subElement = {"relative_path": "/" + (vars(y))["dn"]} units.append(subElement) finalObjs[x["humanReadableName"]] = units handle.logout() return finalObjs
def getRackmount(host=None, user=None, password=None): data = [] handle = UcsHandle(host, user, password, secure=False) if handle.login(): computeRackUnit = handle.query_children(in_dn="sys", class_id="computeRackUnit") for x in computeRackUnit: server = {} server["name"] = x.rn server["path"] = x.dn server["macs"] = [] macs = handle.query_children(in_dn=x.dn, class_id='PciEquipSlot') for y in macs: server["macs"].append(y.mac_left) server["macs"].append(y.mac_right) data.append(server) handle.logout() return data
def updateNtp(module): ucs_ip = module.params['ucs_ip'] ucs_user = module.params['ucs_user'] ucs_pass = module.params['ucs_pass'] ntp = module.params['ntp'] state = module.params['state'] results = {} results['changed'] = False results['created'] = [] results['removed'] = [] # Login to UCS try: handle = UcsHandle(ucs_ip, ucs_user, ucs_pass) except: module.fail_json(msg="Could not login to UCS") try: # Obtain a handle for the Timezone object tz = handle.query_classid(class_id="CommDateTime") if type(ntp) != list: ntp = list(ntp) for server in ntp: filter_str = '(name, "%s", type="eq")' % (server) obj = handle.query_children( in_mo=tz[0], class_id="CommNtpProvider", filter_str=filter_str ) if state == 'present' and len(obj) > 0: pass elif state == 'present' and len(obj) == 0: ntp_server = CommNtpProvider(tz[0], name=server) handle.add_mo(ntp_server) handle.commit() results['created'].append(server) results['changed'] = True elif state == 'absent' and len(obj) > 0: handle.remove_mo(obj[0]) handle.commit() results['changed'] = True results['removed'].append(server) except Exception, e: module.fail_json(msg="Could not create or validate servers; %s" % e)
def getServiceProfile(): authInfo = _getUcsAuthInfo((request.headers)) handle = UcsHandle(*authInfo, secure=False) if not handle.login(): handle.logout() return 'Forbidden', "", 403 rootElements = [{ "ciscoXmlName": "orgOrg", "humanReadableName": "ServiceProfile" }] finalObjs = {} for x in rootElements: subElement = {} try: components = handle.query_classid(x["ciscoXmlName"]) except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: if not (type(components) == list): handle.logout() return "Couldn't fetch " + x["humanReadableName"], "", 500 for y in components: subElement["org"] = y.level subElement["members"] = [] try: lsList = handle.query_children(in_dn="org-root", class_id="lsServer") except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: if not (type(lsList) == list): handle.logout() return "Couldn't fetch Logical Servers", "", 500 for item in lsList: logicalServer = {} identifier = item.dn obj = handle.query_dn(dn=identifier) logicalServer["name"] = obj.rn logicalServer["path"] = obj.dn logicalServer["associatedServer"] = obj.pn_dn logicalServer["assoc_state"] = obj.assoc_state subElement["members"].append(logicalServer) finalObjs[x["humanReadableName"]] = subElement handle.logout() return finalObjs
def getCatalog(identifier=None): authInfo = _getUcsAuthInfo((request.headers)) data = [] handle = UcsHandle(*authInfo, secure=False) if handle.login(): try: elements = (handle.query_children(in_dn=identifier)) elements.append(handle.query_dn(dn=identifier)) for element in elements: if (element): data.append(reduce(element.__dict__)) handle.logout() return data except UcsException as e: handle.logout() return 'Internal Server Error', e.error_descr, 500 else: handle.logout() return 'Forbidden', "", 403
def getCatalog(host=None, user=None, password=None, identifier=None): data = [] handle = UcsHandle(host, user, password, secure=False) if handle.login(): try: elements = handle.query_children(in_dn=identifier) except UcsException as e: handle.logout() return 'Inrternal Server Error', e.error_descr, 500 else: if (type(elements) == list): for element in elements: data.append(reduce(element.__dict__)) handle.logout() return data else: handle.logout() return "Couldn't fetch " + identifier, "", 500 else: handle.logout() return 'Forbiden', "", 403
## The UCS Python SDK Makes Deploying Servers a Breeze ## Exercise 3 # Get a handle and login to UCS Manager from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("198.18.133.91", "admin", "password") handle.login() # Query the computeBlade Class blades = handle.query_classid("computeBlade") # Print computeBlade object attributes for blade in blades: print(blade.dn, blade.total_memory, blade.num_of_cpus, blade.serial) # Print out the computeBlade child object equipmentLocatorLed Class attribute oper_state for blade in blades: leds = handle.query_children(in_dn=blade.dn, class_id="equipmentLocatorLed") for led in leds: print(blade.dn, led.oper_state) # Logout of the UCS Manager handle.logout()
UCS_USER = Connection.UCS_USER UCS_PASS = Connection.UCS_PASS HANDLE = UcsHandle(UCS_HOST, UCS_USER, UCS_PASS) HANDLE.login() BOOT_POLICY = LsbootVirtualMedia( parent_mo_or_dn="org-root/boot-policy-virt-media-add", access="read-only-remote-cimc", order="3" ) HANDLE.add_mo(BOOT_POLICY, True) HANDLE.commit() # Get boot items (children) of Boot Policy OBJECTS = HANDLE.query_children(in_dn="org-root/boot-policy-virt-media-add") # Update boot items order for mo in OBJECTS: if mo.access == "read-only-remote-cimc": mo.order = "1" print(mo) HANDLE.set_mo(mo) elif mo.access == "read-write": storage_object = HANDLE.query_dn( "org-root/boot-policy-virt-media-add/storage/local-storage/local-hdd" ) storage_object.order = str(int(storage_object.order)+1) print(storage_object) HANDLE.set_mo(storage_object) else:
# Create a Service Profile Template and associate to a Server Pool from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsRequirement import LsRequirement mo_sp_template = LsServer(parent_mo_or_dn="org-root", type="initial-template", name="CommanderCode_PY_Mission_Templ") handle.add_mo(mo_sp_template, modify_present=True) handle.commit() filter_exp = '(name,"CommanderCode_PY_Mission_Templ")' mo_sp_templ = handle.query_classid("lsServer",filter_str=filter_exp) mo_sp_templ_ls_requirement = LsRequirement(parent_mo_or_dn=mo_sp_templ[0].dn, name="Python_Heroes_Server_Pool") handle.add_mo(mo_sp_templ_ls_requirement, modify_present=True) # Instantiate a Service Profile from the Service Profile template mo_sp = LsServer(parent_mo_or_dn="org-root", src_templ_name="CommanderCode_PY_Mission_Templ", name="CommanderCode_PY_Mission_Server") handle.add_mo(mo_sp, modify_present=True) handle.commit() # Retrieve the Server Pool and view the Server Pool Server allocation filter_exp = '(name,"Python_Heroes_Server_Pool")' mo_compute_pool = handle.query_classid("ComputePool",filter_str=filter_exp) mo_compute_pooled_slots = handle.query_children(in_mo=mo_compute_pool[0], class_id="ComputePooledSlot") for mo_compute_pooled_slot in mo_compute_pooled_slots: print(mo_compute_pooled_slot.poolable_dn, mo_compute_pooled_slot.assigned_to_dn) # Logout of the UCS Manager handle.logout()
from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("198.18.133.91", "admin", "password") handle.login() # Update the User Label on the Service Profile filter_exp = '(name,"CommanderCode_PY_Mission_Server")' mo_sp = handle.query_classid("LsServer",filter_str=filter_exp) mo_sp[0].usr_lbl = "Quantum Calculations Server" handle.set_mo(mo_sp[0]) handle.commit() input("Service Profile User Label Updated - Press Enter to Continue...") # Delete the Server Pool filter_exp = '(name,"Python_Heroes_Server_Pool")' mo_compute_pool = handle.query_classid("ComputePool",filter_str=filter_exp) handle.remove_mo(mo_compute_pool[0]) handle.commit() # Dis-Associate the Serivce Profile from the assigned Server filter_exp = '(name,"CommanderCode_PY_Mission_Server")' mo_sp = handle.query_classid("LsServer",filter_str=filter_exp) mo_ls_requirement = handle.query_children(in_mo=mo_sp[0],class_id="LsRequirement") handle.remove_mo(mo_ls_requirement[0]) handle.commit() # Logout of the UCS Manager handle.logout()
''' # login into the ucsm, create handle. handle = UcsHandle("192.168.193.138","ucspe","ucspe",secure=False) handle.logout() vars(handle) handle.query_classid('computeBlade') blades = handle.query_classid("computeBlade") print(blades) # get the info for the blades, each at the time itterating for blade in blades: print(blade) # get the specified info from each blade object, in this case we just have the dn(location of blade), CPu and the memory for blade in blades: print(blade.dn, blade.num_of_cpus, blade.available_memory) # query the rack resources compute_resources = handle.query_classids("ComputeBlade", "ComputeRackUnit") # LEDs for locating the physical component in the data center for compute_resource_class in compute_resources: for compute_resource in compute_resources[compute_resource_class]: leds = handle.query_children( in_dn=compute_resource.dn, class_id="equipmentLocatorLed") print('Server Location:' compute_resource.dn, 'is the LED ON?:'leds[0].oper_state)