def test_compare_org_hierarchy(): from ucsmsdk.mometa.org.OrgOrg import OrgOrg from ucsmsdk.mometa.ls.LsServer import LsServer static_setup() ref_org = OrgOrg(parent_mo_or_dn="org-root", name="org_same", descr="") ref_org._handle = ref_handle ref_same_sp = LsServer(ref_org, name="same") ref_same_sp._handle = ref_handle ref_remove = LsServer(ref_org, name="remove_from_ref") ref_remove._handle = ref_handle ref_mos = [ref_org, ref_same_sp, ref_remove] diff_org = OrgOrg(parent_mo_or_dn="org-root", name="org_same", descr="diff") diff_org._handle = diff_handle diff_same_sp = LsServer(diff_org, name="same", usr_lbl="diff") diff_same_sp._handle = diff_handle diff_add = LsServer(diff_org, name="add_to_ref") diff_add._handle = diff_handle diff_mos = [diff_org, diff_same_sp, diff_add] difference = compare_ucs_mo(ref_mos, diff_mos) write_mo_diff(difference) assert_equal(len(difference), 4)
def test_compare_remove_obj_from_ref_exist_only_in_ref(): from ucsmsdk.mometa.ls.LsServer import LsServer static_setup() ref_mo = LsServer(parent_mo_or_dn="org-root", name="remove_from_ref", usr_lbl="") ref_mo._handle = ref_handle ref_mos = [ref_mo] diff_mos = [] difference = compare_ucs_mo(ref_mos, diff_mos) write_mo_diff(difference) assert_equal(len(difference), 1)
def sp_create(ucs_handle, name, src_templ_name, parent_dn='org-root'): """ This method creates Service Profiles from existing templates. Args: ucs_handle (ucs_handle) name(string): Name of SP src_templ_name (string): Name of Source template parent_dn= Parent DN Returns: Service Profile: Managed Object Raises: ValueError: If OrgOrg is not present Example: sp_create(ucs_handle, name='Test-1', src_templ_name='TestTemplate') """ from ucsmsdk.mometa.ls.LsServer import LsServer obj = ucs_handle.query_dn(parent_dn) if not obj: raise ValueError("org '%s' does not exist." % parent_dn) mo = LsServer(parent_mo_or_dn=obj, name=name, src_templ_name=src_templ_name, type='instance', uuid='derived') ucs_handle.add_mo(mo, True) ucs_handle.commit() return mo
def setup_sp(server, module): from ucsmsdk.mometa.ls.LsServer import LsServer ansible = module.params args_mo = _get_mo_params(ansible) changed = False for sp in args_mo['sp_list']: mo = server.query_dn(args_mo['org_dn'] + '/ls-' + sp['name']) if mo: exists = True else: exists = False if ansible['state'] == 'absent': if exists: changed = True if not module.check_mode: server.remove_mo(mo) server.commit() else: if not exists: changed = True if not module.check_mode: # create if mo does not already exist mo = LsServer(parent_mo_or_dn=args_mo['org_dn'], name=sp['name'], src_templ_name=sp['src_templ_name'], type='instance', uuid='derived') server.add_mo(mo, True) server.commit() return changed
def test_002_create_specific_obj(): # Create an object of type LsServer with parent dn specified # check if the object has the right values populated from ucsmsdk.mometa.ls.LsServer import LsServer obj = LsServer("org-root", "temp_sp") assert_equal(obj.name, "temp_sp") assert_equal(obj.rn, "ls-temp_sp") assert_equal(obj.dn, "org-root/ls-temp_sp")
def test_001_add_sp(): from ucsmsdk.mometa.ls.LsServer import LsServer sp = LsServer(parent_mo_or_dn="org-root", name="test_sp") add_mo(mo=sp) xml_str = commit() print(xml_str) expected = b'''<configConfMos cookie="cookie" inHierarchical="false"><inConfigs><pair key="org-root/ls-test_sp"><lsServer dn="org-root/ls-test_sp" name="test_sp" status="created" /></pair></inConfigs></configConfMos>''' assert_equal(xml_str, expected)
def setup_module(): from ucsmsdk.mometa.ls.LsServer import LsServer global obj global handle handle = custom_setup() obj = LsServer("org-root", "test", usr_lbl="sample") handle.add_mo(obj, True) handle.commit()
def setup_module(): from ucsmsdk.ucseventhandler import UcsEventHandle from ucsmsdk.mometa.ls.LsServer import LsServer global handle, sp, ueh handle = custom_setup() ueh = UcsEventHandle(handle) org = handle.query_dn("org-root") sp = LsServer(org, name="eventhandle", descr="") handle.add_mo(sp, True) handle.commit()
def test_005_get_org_and_add_sp_set_mo_org(): from ucsmsdk.mometa.ls.LsServer import LsServer org = handle.query_dn("org-root") sp = LsServer(parent_mo_or_dn=org, name="test_sp") set_mo(org) xml_str = commit() print(xml_str) expected = b'''<configConfMos cookie="cookie" inHierarchical="false"><inConfigs><pair key="org-root"><orgOrg dn="org-root" status="modified"><lsServer dn="org-root/ls-test_sp" name="test_sp" /></orgOrg></pair></inConfigs></configConfMos>''' assert_equal(xml_str, expected)
def test_002_add_hierarchy(): from ucsmsdk.mometa.org.OrgOrg import OrgOrg from ucsmsdk.mometa.ls.LsServer import LsServer org = OrgOrg(parent_mo_or_dn="org-root", name="test_org") sp = LsServer(parent_mo_or_dn=org, name="test_sp") add_mo(mo=org) xml_str = commit() print(xml_str) expected = b'''<configConfMos cookie="cookie" inHierarchical="false"><inConfigs><pair key="org-root/org-test_org"><orgOrg dn="org-root/org-test_org" name="test_org" status="created"><lsServer dn="org-root/org-test_org/ls-test_sp" name="test_sp" /></orgOrg></pair></inConfigs></configConfMos>''' assert_equal(xml_str, expected)
def test_001_sp_minimal(): from ucsmsdk.mometa.ls.LsServer import LsServer mo = LsServer(parent_mo_or_dn="org-root", vmedia_policy_name="", ext_ip_state="none", bios_profile_name="", mgmt_fw_policy_name="", agent_policy_name="", mgmt_access_policy_name="", dynamic_con_policy_name="", kvm_mgmt_policy_name="", sol_policy_name="", uuid="0", descr="", stats_policy_name="default", policy_owner="local", ext_ip_pool_name="ext-mgmt", boot_policy_name="", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="default", src_templ_name="", local_disk_policy_name="", scrub_policy_name="", power_policy_name="default", maint_policy_name="", name="test_sp", resolve_remote="yes") handle.add_mo(mo) handle.commit() ########################################################## # Modify a single property in the Sp created above # and genertate XML with DIRTY option set ########################################################## import ucsmsdk.ucsxmlcodec as xc from ucsmsdk.ucscoremeta import WriteXmlOption obj = handle.query_dn("org-root/ls-test_sp") obj.usr_lbl = "new_label" print(xc.to_xml_str(obj.to_xml(option=WriteXmlOption.DIRTY))) print(xc.to_xml_str(obj.to_xml(option=WriteXmlOption.ALL_CONFIG))) print(xc.to_xml_str(obj.to_xml())) ########################################################## # Delete the SP ########################################################## obj = handle.query_dn("org-root/ls-test_sp") handle.remove_mo(obj) handle.commit()
def setup_module(): from ucsmsdk.mometa.ls.LsServer import LsServer global obj global handle handle = custom_setup() if not handle: msg = get_skip_msg() raise SkipTest(msg) obj = LsServer("org-root", "test", usr_lbl="sample") handle.add_mo(obj, True) handle.commit()
def test_power_on_chassis1_blade1_pass(mock_login, mock_query_dn, mock_commit): from ucsmsdk.mometa.compute.ComputeBlade import ComputeBlade from ucsmsdk.mometa.ls.LsServer import LsServer mock_login.return_value = True server_mo = Mock() server_mo.assigned_to_dn = "org-root/ls-testsp" sp_mo = LsServer("org-root", name="testsp") mock_query_dn.side_effect = [server_mo, sp_mo] mock_commit.return_value = None assert server_power_on(handle, chassis_id=1, blade_id=1) is None
def setup_module(): from ucsmsdk.ucseventhandler import UcsEventHandle from ucsmsdk.mometa.ls.LsServer import LsServer global handle, sp, ueh handle = custom_setup() if not handle: msg = get_skip_msg() raise SkipTest(msg) ueh = UcsEventHandle(handle) org = handle.query_dn("org-root") sp = LsServer(org, name="eventhandle-test", descr="") handle.add_mo(sp, True) handle.commit()
def setup_module(): from ucsmsdk.mometa.ls.LsServer import LsServer global handle handle = custom_setup() org = handle.query_dn("org-root") sp_TEST = LsServer(org, name="TEST", usr_lbl="TEST") handle.add_mo(sp_TEST, True) sp_list.append(sp_TEST) sp_test = LsServer(org, name="test", usr_lbl="test", descr="test") handle.add_mo(sp_test, True) sp_list.append(sp_test) sp_test11 = LsServer(org, name="test11", usr_lbl="test11") handle.add_mo(sp_test11, True) sp_list.append(sp_test11) sp_test12 = LsServer(org, name="test12", usr_lbl="test12") handle.add_mo(sp_test12, True) sp_list.append(sp_test12) handle.commit()
def create_service_profile_template(handle, org): from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.vnic.VnicConnDef import VnicConnDef from ucsmsdk.mometa.ls.LsRequirement import LsRequirement # from ucsmsdk.mometa.lstorage.LstorageProfileBinding import LstorageProfileBinding print "Creating KUBAM Service Profile Template" mo = LsServer( parent_mo_or_dn=org, policy_owner="local", name="KUBAM", descr="KUBAM Service Profile Template", type="updating-template", # Boot using Kubernetes Boot policy: local Disk, then Remote DVD boot_policy_name="kubam", # Default Maintenance Policy maint_policy_name="default", # scrub policy scrub_policy_name="kubam", # Bios Policy bios_profile_name="kubam", # UUID Pool ident_pool_name="kubam", # disks we use. local_disk_policy_name="kubam", # storage_profile_name="kubam", # virtual media policy vmedia_policy_name="kubam" ) # Create vNIC Connection Policy VnicConnDef(parent_mo_or_dn=mo, lan_conn_policy_name="kubam") # Create server pool and add to template. LsRequirement(parent_mo_or_dn=mo, name="kubam") # Add storage profile. # LstorageProfileBinding(parent_mo_or_dn=mo, storage_profile_name="kubam") handle.add_mo(mo, True) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\talready exists" else: return 1, err.error_descr except Exception as e: return 1, e return 0, None
def test_compare_same_obj_with_diff_props_include_operational(): from ucsmsdk.mometa.ls.LsServer import LsServer static_setup() ref_mo = LsServer(parent_mo_or_dn="org-root", name="same", usr_lbl="") ref_mo.__dict__['config_state'] = "applied" ref_mo._handle = ref_handle ref_mos = [ref_mo] diff_mo = LsServer(parent_mo_or_dn="org-root", name="same", usr_lbl="xxx") diff_mo.__dict__['config_state'] = "applying" diff_mo._handle = diff_handle diff_mos = [diff_mo] difference = compare_ucs_mo(ref_mos, diff_mos, include_operational=True) write_mo_diff(difference) assert_equal(len(difference), 1) assert_equal(len(difference[0].diff_property), 2)
def create_service_profile(name, handle): # Service Profile sp = LsServer(parent_mo_or_dn="org-root", name=name) # Vnic eth0 vnic_eth0 = VnicEther(parent_mo_or_dn=sp, name="eth0") VnicEtherIf(parent_mo_or_dn=vnic_eth0, default_net="yes", name="default") # Vnic eth1 vnic_eth1 = VnicEther(parent_mo_or_dn=sp, name="eth1") VnicEtherIf(parent_mo_or_dn=vnic_eth1, default_net="yes", name="default") VnicFcNode(parent_mo_or_dn=sp, ident_pool_name="", addr="pool-derived") handle.add_mo(sp) try: handle.commit() except: print "Error creating Service Profile" sys.exit(1) handle.logout()
def test_unknown_property_noversionfilter(): from ucsmsdk.mometa.ls.LsServer import LsServer static_setup() ref_mo = LsServer(parent_mo_or_dn="org-root", name="ra_ref", usr_lbl="") ref_mo._handle = ref_handle ref_mo.unknown = "" ref_mos = [ref_mo] diff_mo = LsServer(parent_mo_or_dn="org-root", name="ra_ref", usr_lbl="xxx") diff_mo._handle = diff_handle diff_mo.unknown = "yyy" diff_mos = [diff_mo] difference = compare_ucs_mo(ref_mos, diff_mos, version_filter=False) write_mo_diff(difference) assert_equal(len(difference), 1) assert_equal(len(difference[0].diff_property), 2)
def test_003_get_then_set_sp(): from ucsmsdk.mometa.ls.LsServer import LsServer sp = LsServer(parent_mo_or_dn="org-root", name="test_sp") handle.add_mo(sp) handle.commit() test_sp = handle.query_dn("org-root/ls-test_sp") test_sp.descr = "change" set_mo(test_sp) xml_str = commit() print(xml_str) handle.remove_mo(sp) handle.commit() expected = b'''<configConfMos cookie="cookie" inHierarchical="false"><inConfigs><pair key="org-root/ls-test_sp"><lsServer descr="change" dn="org-root/ls-test_sp" status="modified" /></pair></inConfigs></configConfMos>''' assert_equal(xml_str, expected)
def main(): handle = UcsHandle("192.168.254.200","ucspe","ucspe", secure=False) handle.login() ## Acknolwedge Chassis mo = EquipmentChassis(parent_mo_or_dn="sys", admin_state="re-acknowledge", id="5") handle.add_mo(mo, True) handle.commit() ## Update MAC Pool mo = MacpoolBlock(parent_mo_or_dn="org-root/mac-pool-default", r_from="00:25:B5:00:00:00", to="00:25:B5:00:00:C7") handle.add_mo(mo) handle.commit() ## Update UUID Pools handle.query_dn("org-root/uuid-pool-default") mo = UuidpoolBlock(parent_mo_or_dn="org-root/uuid-pool-default", r_from="0000-000000000001", to="0000-0000000000C8") handle.add_mo(mo) handle.commit() ## Setup Fabric Ethernet Uplink A Side mo = FabricEthLan(parent_mo_or_dn="fabric/lan", id="A") mo_1 = FabricEthLanEp(parent_mo_or_dn=mo, admin_speed="10gbps", admin_state="enabled", auto_negotiate="yes", eth_link_profile_name="default", fec="auto", flow_ctrl_policy="default", name="", port_id="1", slot_id="1", usr_lbl="") handle.add_mo(mo, True) handle.commit() ## Setup Fabric Ethernet Uplink A Side mo = FabricEthLan(parent_mo_or_dn="fabric/lan", id="B") mo_1 = FabricEthLanEp(parent_mo_or_dn=mo, admin_speed="10gbps", admin_state="enabled", auto_negotiate="yes", eth_link_profile_name="default", fec="auto", flow_ctrl_policy="default", name="", port_id="1", slot_id="1", usr_lbl="") handle.add_mo(mo, True) handle.commit() ## Setup Service Profile Template mo = LsServer(parent_mo_or_dn="org-root", ident_pool_name="default", name="globotemplate", type="initial-template", uuid="00000000-0000-0000-0000-000000000000") handle.add_mo(mo) handle.commit() handle.logout()
def configure_service_profile_template(ucs, module): from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.vnic.VnicConnDef import VnicConnDef from ucsmsdk.mometa.vnic.VnicIScsiNode import VnicIScsiNode from ucsmsdk.mometa.ls.LsRequirement import LsRequirement from ucsmsdk.mometa.ls.LsPower import LsPower from ucsmsdk.mometa.lstorage.LstorageProfileBinding import LstorageProfileBinding from ucsmsdk.mometa.mgmt.MgmtInterface import MgmtInterface from ucsmsdk.mometa.mgmt.MgmtVnet import MgmtVnet from ucsmsdk.mometa.vnic.VnicIpV4MgmtPooledAddr import VnicIpV4MgmtPooledAddr if not module.check_mode: try: # create if mo does not already exist mo = LsServer( parent_mo_or_dn=module.params['org_dn'], bios_profile_name=module.params['bios_policy'], boot_policy_name=module.params['boot_policy'], descr=module.params['description'], ext_ip_state='pooled', ext_ip_pool_name=module.params['mgmt_ip_pool'], # graphics_card_policy_name=module.params['graphics_card_policy'], host_fw_policy_name=module.params['host_firmware_package'], ident_pool_name=module.params['uuid_pool'], kvm_mgmt_policy_name=module.params['kvm_mgmt_policy'], local_disk_policy_name=module.params['local_disk_policy'], maint_policy_name=module.params['maintenance_policy'], mgmt_access_policy_name=module.params['ipmi_access_profile'], name=module.params['name'], power_policy_name=module.params['power_control_policy'], power_sync_policy_name=module.params['power_sync_policy'], scrub_policy_name=module.params['scrub_policy'], sol_policy_name=module.params['sol_policy'], stats_policy_name=module.params['threshold_policy'], type=module.params['template_type'], usr_lbl=module.params['user_label'], vmedia_policy_name=module.params['vmedia_policy'], ) if module.params['storage_profile']: # Storage profile mo_1 = LstorageProfileBinding( parent_mo_or_dn=mo, storage_profile_name=module.params['storage_profile'], ) if module.params['mgmt_interface_mode']: # Management Interface mo_1 = MgmtInterface( parent_mo_or_dn=mo, mode=module.params['mgmt_interface_mode'], ip_v4_state='pooled', ) mo_2 = MgmtVnet( parent_mo_or_dn=mo_1, id='1', name=module.params['mgmt_vnet_name'], ) VnicIpV4MgmtPooledAddr( parent_mo_or_dn=mo_2, name=module.params['mgmt_inband_pool_name'], ) # LAN/SAN connectivity policy mo_1 = VnicConnDef( parent_mo_or_dn=mo, lan_conn_policy_name=module.params['lan_connectivity_policy'], san_conn_policy_name=module.params['san_connectivity_policy'], ) if module.params['iqn_pool']: # IQN pool mo_1 = VnicIScsiNode( parent_mo_or_dn=mo, iqn_ident_pool_name=module.params['iqn_pool'] ) # power state admin_state = 'admin-' + module.params['power_state'] mo_1 = LsPower( parent_mo_or_dn=mo, state=admin_state, ) if module.params['server_pool']: # server pool mo_1 = LsRequirement( parent_mo_or_dn=mo, name=module.params['server_pool'], qualifier=module.params['server_pool_qualification'], ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() except Exception as e: # generic Exception handling because SDK can throw a variety of exceptions ucs.result['msg'] = "setup error: %s " % str(e) module.fail_json(**ucs.result) ucs.result['changed'] = True
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str', required=True), source_template=dict(type='str', required=True), user_label=dict(type='str', default=''), power_state=dict(type='str', choices=['up', 'down']), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) err = False # UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below. from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsPower import LsPower changed = False try: mo_exists = False props_match = False dn = module.params['org_dn'] + '/ls-' + module.params['name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() changed = True else: if mo_exists: # check top-level mo props kwargs = dict(src_templ_name=module.params['source_template']) kwargs['usr_lbl'] = module.params['user_label'] # service profiles are of type 'instance' kwargs['type'] = 'instance' if mo.check_prop_match(**kwargs): # top-level props match if module.params.get('power_state'): child_dn = dn + '/power' mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: kwargs = dict(state=module.params['power_state']) if mo_1.check_prop_match(**kwargs): props_match = True else: # no power state provided, use existing state as match props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist mo = LsServer( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], src_templ_name=module.params['source_template'], type='instance', usr_lbl=module.params['user_label'], ) if module.params.get('power_state'): admin_state = 'admin-' + module.params['power_state'] mo_1 = LsPower( parent_mo_or_dn=mo, state=admin_state, ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() changed = True except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) ucs.result['changed'] = changed if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)
def sp_template_create(ucs_handle, name, type, resolve_remote, descr="", usr_lbl="", src_templ_name="", ext_ip_state="pooled", ext_ip_pool_name="", ident_pool_name="", agent_policy_name="", bios_profile_name="", boot_policy_name="", dynamic_con_policy_name="", host_fw_policy_name="", kvm_mgmt_policy_name="", lan_conn_policy_name="", local_disk_policy_name="", maint_policy_name="", mgmt_access_policy_name="", mgmt_fw_policy_name="", power_policy_name="", san_conn_policy_name="", scrub_policy_name="", sol_policy_name="", stats_policy_name="", vcon_profile_name="", vmedia_policy_name="", server_pool_name="", adapter_profile_name="", vnic_template_name="", vnic_name="", vnic_order="", storage_profile_name="", parent_dn="org-root"): """ This method creates Service profile template. Args: ucs_handle (ucs_handle) name(string): Name of SP template type : "initial-template", "updating-template" resolve_remote : "no", "yes" descr (string): Basic description src_templ_name (string): Name of Source template ext_ip_state= :none", "pooled", "static" ext_ip_pool_name (string): Name of IP Pool ident_pool_name (string): Name of Ident pool agent_policy_name (string): Name of agent policy bios_profile_name (string): Name of bios profile boot_policy_name (string): Name of boot policy dynamic_con_policy_name (string): Name of dynamic connection policy host_fw_policy_name (string): Name of Host firmware. kvm_mgmt_policy_name (string): KVM management policy lan_conn_policy_name (string): LAN connection policy local_disk_policy_name (string): Local disk policy maint_policy_name (string): Maintenance policy mgmt_access_policy_name(string): Access Management policy mgmt_fw_policy_name (string): Firmware management policy power_policy_name (string): Power policy san_conn_policy_name (string): SAN connection policy scrub_policy_name (string): Scrub policy sol_policy_name (string): SOL policy stats_policy_name (string): Statistics Policy vcon_profile_name (string): Virtual Connection profile policy vmedia_policy_name (string): Virtual media policy server_pool_name (string): Server Pool parent_dn= Parent DN Returns: Service Profile: Managed Object Raises: ValueError: If OrgOrg is not present Example: sp_template_create(ucs_handle, name="sample_temp", type="initial-template", resolve_remote="yes",local_disk_policy="sample_local") """ from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.vnic.VnicConnDef import VnicConnDef from ucsmsdk.mometa.ls.LsRequirement import LsRequirement from ucsmsdk.mometa.lstorage.LstorageProfileBinding import LstorageProfileBinding from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode from ucsmsdk.mometa.vnic.VnicFc import VnicFc from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf from ucsmsdk.mometa.fabric.FabricVCon import FabricVCon from ucsmsdk.mometa.ls.LsPower import LsPower obj = ucs_handle.query_dn(parent_dn) if not obj: raise ValueError("org '%s' does not exist." % parent_dn) mo = LsServer(parent_mo_or_dn=obj, name=name, type=type, resolve_remote=resolve_remote, descr=descr, usr_lbl=usr_lbl, src_templ_name=src_templ_name, ext_ip_state=ext_ip_state, ext_ip_pool_name=ext_ip_pool_name, ident_pool_name=ident_pool_name, vcon_profile_name=vcon_profile_name, agent_policy_name=agent_policy_name, bios_profile_name=bios_profile_name, boot_policy_name=boot_policy_name, dynamic_con_policy_name=dynamic_con_policy_name, host_fw_policy_name=host_fw_policy_name, kvm_mgmt_policy_name=kvm_mgmt_policy_name, local_disk_policy_name=local_disk_policy_name, maint_policy_name=maint_policy_name, mgmt_access_policy_name=mgmt_access_policy_name, mgmt_fw_policy_name=mgmt_fw_policy_name, power_policy_name=power_policy_name, scrub_policy_name=scrub_policy_name, sol_policy_name=sol_policy_name, stats_policy_name=stats_policy_name, vmedia_policy_name=vmedia_policy_name ) if(storage_profile_name <> ""): mo_1 = LstorageProfileBinding(parent_mo_or_dn=mo, storage_profile_name=storage_profile_name) if(vnic_name <> "" and vnic_template_name <> ""): mo_1 = VnicEther(parent_mo_or_dn=mo, adaptor_profile_name=adapter_profile_name, order=vnic_order, name=vnic_name, nw_templ_name=vnic_template_name) if(server_pool_name <> ""): mo_1 = LsRequirement(parent_mo_or_dn=mo, name=server_pool_name) # Add vNIC Connection Policy to template VnicConnDef(parent_mo_or_dn=mo, lan_conn_policy_name=lan_conn_policy_name, san_conn_policy_name=san_conn_policy_name) ucs_handle.add_mo(mo, True) ucs_handle.commit() return mo
from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.org.OrgOrg import OrgOrg HANDLE = UcsHandle( "sandbox-ucsm1.cisco.com", "admin", "password" ) HANDLE.login() ORG_ORG = OrgOrg( parent_mo_or_dn='org-root', name="devnet", ) HANDLE.add_mo(ORG_ORG, modify_present=True) HANDLE.commit() SP_TEMPLATE = LsServer( parent_mo_or_dn='org-root/org-devnet', name="devcore_template", type="updating-template" ) HANDLE.add_mo(SP_TEMPLATE, modify_present=True) HANDLE.commit() HANDLE.logout()
Cisco Systems, Inc. """ from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.org.OrgOrg import OrgOrg HANDLE = UcsHandle("sandbox-ucsm1.cisco.com", "admin", "password") HANDLE.login() ORG_ORG = OrgOrg( parent_mo_or_dn='org-root', name="devnet", ) HANDLE.add_mo(ORG_ORG, modify_present=True) HANDLE.commit() SP_FROM_TEMPLATE = LsServer(parent_mo_or_dn='org-root/org-devnet', name="devcore-server-01", src_templ_name="devcore_template", type="instance") LsBinding(parent_mo_or_dn=SP_FROM_TEMPLATE, pn_dn="sys/chassis-7/blade-3") HANDLE.add_mo(SP_FROM_TEMPLATE, modify_present=True) HANDLE.commit() HANDLE.logout()
def run(job, resource, *args, **kwargs): username = conn.username password = conn.password organization = "{{ organization }}" service_profile_template = "{{ service_profile_template }}" create_sp_from_sp_template = "{{ create_sp_from_sp_template }}" service_profile_name = "{{ service_profile_name }}" service_profile_description = "{{ service_profile_description }}" use_blade_servers = "{{ use_blade_servers }}" chassis = "{{ chassis }}" ucs_server_dn = "{{ ucs_server_dn }}" ucs_rack_server = "{{ rack_server }}" mac_pool_name = "{{ mac_pool_name }}" service_profile_server_dn, _ = CustomField.objects.get_or_create( name="service_profile_server_dn", label="Service Profile Server DN", type="STR", show_on_servers=True) handler.login() ucs_event_handler = UcsEventHandle(handler) # create SP in an org set_progress(f"Creating service profile named {service_profile_name}") if create_sp_from_sp_template == "True": dn_set = ucscbasetype.DnSet() dn_set.child_add(Dn(value=f"{service_profile_name}")) xml_element = mf.ls_instantiate_n_named_template( cookie=handler.cookie, dn=service_profile_template, in_error_on_existing="true", in_name_set=dn_set, in_target_org=organization) handler.process_xml_elem(xml_element) else: mo = LsServer(parent_mo_or_dn=organization, vmedia_policy_name="", ext_ip_state="none", bios_profile_name="", mgmt_fw_policy_name="", agent_policy_name="", mgmt_access_policy_name="", dynamic_con_policy_name="", kvm_mgmt_policy_name="", sol_policy_name="", uuid="0", descr=service_profile_description, stats_policy_name="default", policy_owner="local", ext_ip_pool_name="ext-mgmt", boot_policy_name="", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="", src_templ_name="", local_disk_policy_name="", scrub_policy_name="", power_policy_name="default", maint_policy_name="", name=service_profile_name, resolve_remote="yes") mo_1 = LsbootDef(parent_mo_or_dn=mo, descr="", reboot_on_update="no", adv_boot_order_applicable="no", policy_owner="local", enforce_vnic_name="yes", boot_mode="legacy") mo_1_1 = LsbootStorage(parent_mo_or_dn=mo_1, order="1") mo_1_1_1 = LsbootLocalStorage(parent_mo_or_dn=mo_1_1, ) mo_1_1_1_1 = LsbootDefaultLocalImage(parent_mo_or_dn=mo_1_1_1, order="1") mo_2 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="eth0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="", ident_pool_name=mac_pool_name, order="unspecified", nw_templ_name="", addr="derived") mo_2_1 = VnicEtherIf(parent_mo_or_dn=mo_2, default_net="yes", name="default") mo_3 = VnicFcNode(parent_mo_or_dn=mo, ident_pool_name="", addr="pool-derived") handler.add_mo(mo) # Associate a server to a service profile. if use_blade_servers == "True": set_progress( f"Associating service profile {service_profile_name} with {ucs_server_dn}" ) mo = LsBinding( parent_mo_or_dn=f"{organization}/ls-{service_profile_name}", pn_dn=ucs_server_dn, restrict_migration="no") else: set_progress( f"Associating service profile {service_profile_name} with {ucs_rack_server}" ) mo = LsBinding( parent_mo_or_dn=f"{organization}/ls-{service_profile_name}", pn_dn=ucs_rack_server, restrict_migration="no") handler.add_mo(mo) handler.commit() mo = handler.query_dn(f"{organization}/ls-{service_profile_name}") # Save the service profile dn resource.service_profile_server_dn = f"{organization}/ls-{service_profile_name}" resource.name = service_profile_name resource.save() return "SUCCESS", f"Created service profile named {service_profile_name}", ""
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), service_profile_name=dict(type='str', required=True), server_assignment=dict(type='str', choices=['server', 'pool']), server_dn=dict(type='str'), server_pool_name=dict(type='str'), restrict_migration=dict(type='str', default='no', choices=['yes', 'no']), state=dict(default='present', choices=['present', 'absent'], type='str'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['server_assignment']], ['server_assignment', 'server', ['server_dn']], ['server_assignment', 'pool', ['server_pool_name']], ], mutually_exclusive=[ ['server_dn', 'server_pool_name'], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. ucs = UCSModule(module) err = False from ucsmsdk.mometa.ls.LsRequirement import LsRequirement from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.ls.LsServer import LsServer changed = False ucs.result['assign_state'] = 'unassigned' ucs.result['assoc_state'] = 'unassociated' try: ls_mo_exists = False pn_mo_exists = False pn_req_mo_exists = False props_match = False # logical server distinguished name is <org>/ls-<name> and physical node dn appends 'pn' or 'pn-req' ls_dn = module.params['org_dn'] + '/ls-' + module.params['service_profile_name'] ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ls_mo_exists = True pn_dn = ls_dn + '/pn' pn_mo = ucs.login_handle.query_dn(pn_dn) if pn_mo: pn_mo_exists = True pn_req_dn = ls_dn + '/pn-req' pn_req_mo = ucs.login_handle.query_dn(pn_req_dn) if pn_req_mo: pn_req_mo_exists = True if module.params['state'] == 'absent': if ls_mo_exists and ls_mo.assign_state != 'unassigned': if pn_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_mo) ucs.login_handle.commit() changed = True elif pn_req_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_req_mo) ucs.login_handle.commit() changed = True elif ls_mo_exists: # check if logical server is assigned and associated ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state if module.params['server_assignment'] == 'pool' and pn_req_mo_exists: # check the current pool kwargs = dict(name=module.params['server_pool_name']) kwargs['restrict_migration'] = module.params['restrict_migration'] if pn_req_mo.check_prop_match(**kwargs): props_match = True elif pn_mo_exists: kwargs = dict(pn_dn=module.params['server_dn']) kwargs['restrict_migration'] = module.params['restrict_migration'] if pn_mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist in desired state mo = LsServer( parent_mo_or_dn=module.params['org_dn'], name=module.params['service_profile_name'], ) if module.params['server_assignment'] == 'pool': if pn_mo_exists: ucs.login_handle.remove_mo(pn_mo) mo_1 = LsRequirement( parent_mo_or_dn=mo, name=module.params['server_pool_name'], restrict_migration=module.params['restrict_migration'], ) else: mo_1 = LsBinding( parent_mo_or_dn=mo, pn_dn=module.params['server_dn'], restrict_migration=module.params['restrict_migration'], ) ucs.login_handle.add_mo(mo_1, True) ucs.login_handle.commit() pn_req_mo = ucs.login_handle.query_dn(pn_req_dn) if pn_req_mo: # profiles from templates will add a server pool, so remove and add the server again ucs.login_handle.remove_mo(pn_req_mo) ucs.login_handle.add_mo(mo_1, True) ucs.login_handle.commit() ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state changed = True except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) ucs.result['changed'] = changed if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)
def test_002_sp_expert(): ''' This case is generated based on SP expert mode creation wizard. ''' from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsVConAssign import LsVConAssign from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf from ucsmsdk.mometa.vnic.VnicFc import VnicFc from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode from ucsmsdk.mometa.storage.StorageIniGroup import StorageIniGroup from ucsmsdk.mometa.vnic.VnicFcGroupDef import VnicFcGroupDef from ucsmsdk.mometa.storage.StorageInitiator import StorageInitiator from ucsmsdk.mometa.ls.LsPower import LsPower from ucsmsdk.mometa.fabric.FabricVCon import FabricVCon mo = LsServer(parent_mo_or_dn="org-root", vmedia_policy_name="", ext_ip_state="none", bios_profile_name="SRIOV", mgmt_fw_policy_name="", agent_policy_name="", mgmt_access_policy_name="", dynamic_con_policy_name="", kvm_mgmt_policy_name="", sol_policy_name="", uuid="00000000-0000-0000-0000-0000000000bb", descr="", stats_policy_name="default", policy_owner="local", ext_ip_pool_name="ext-mgmt", boot_policy_name="default", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="", src_templ_name="", local_disk_policy_name="default", scrub_policy_name="", power_policy_name="default", maint_policy_name="", name="test_sp", resolve_remote="yes") mo_1 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="1", transport="ethernet", vnic_name="eth0") mo_2 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="2", transport="fc", vnic_name="fc0") mo_3 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="eth0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="", ident_pool_name="default", order="1", nw_templ_name="", addr="derived") mo_3_1 = VnicEtherIf(parent_mo_or_dn=mo_3, default_net="yes", name="default") mo_4 = VnicFc(parent_mo_or_dn=mo, addr="derived", name="fc0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", pers_bind="disabled", pers_bind_clear="no", qos_policy_name="", adaptor_profile_name="", ident_pool_name="", order="2", nw_templ_name="", max_data_field_size="2048") mo_4_1 = VnicFcIf(parent_mo_or_dn=mo_4, name="default") mo_5 = VnicFcNode(parent_mo_or_dn=mo, ident_pool_name="", addr="20:00:00:25:B5:00:00:00") mo_6 = StorageIniGroup(parent_mo_or_dn=mo, name="test", descr="", group_policy_name="", policy_name="", policy_owner="local", rmt_disk_cfg_name="") mo_6_1 = VnicFcGroupDef(parent_mo_or_dn=mo_6, storage_conn_policy_name="", policy_owner="local", name="", descr="", stats_policy_name="default") mo_6_2 = StorageInitiator(parent_mo_or_dn=mo_6, policy_owner="local", name="fc0", descr="") mo_7 = LsPower(parent_mo_or_dn=mo, state="admin-up") mo_8 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="1", inst_type="auto") mo_9 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="2", inst_type="auto") mo_10 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="3", inst_type="auto") mo_11 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="4", inst_type="auto") handle.add_mo(mo) handle.commit() obj = handle.query_dn("org-root/ls-test_sp") handle.remove_mo(obj) handle.commit()
from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("198.18.133.91", "admin", "password") handle.login() # Create a MAC Pool Block from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock mac_pool_default = handle.query_dn("org-root/mac-pool-default") mo_mac_pool_block = MacpoolBlock(parent_mo_or_dn=mac_pool_default,r_from="00:25:B5:00:00:AA",to="00:25:B5:00:00:D9") handle.add_mo(mo_mac_pool_block, modify_present=True) handle.commit() # 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()