def manage_org(org_op, org_name, org_descr=None): from ucsmsdk.mometa.org.OrgOrg import OrgOrg handle = ucs_login() if org_op.lower() == "add": mo = OrgOrg(parent_mo_or_dn='org-root', name=org_name, descr=org_descr) handle.add_mo(mo, modify_present=True) handle.commit() response = "Org: " + org_name + " Added/Updated." elif org_op.lower() == "remove": mo = handle.query_dn("org-root/org-" + org_name) if mo: handle.remove_mo(mo) handle.commit() response = "Org: " + org_name + " Removed." else: response = "Org: " + org_name + " Not Found." elif org_op.lower() == "update": mo = handle.query_dn("org-root/org-" + org_name) if mo: mo.descr = org_descr handle.add_mo(mo, modify_present=True) handle.commit() response = "Org: " + org_name + " Updated." else: response = "Org: " + org_name + " Not Found: could not update." ucs_logout(handle) print(response) return response
def manage_org(inputs): """ Manage UCS Organizations """ from ucsmsdk.mometa.org.OrgOrg import OrgOrg handle = ucs_login() object_op = inputs[0] org_parent = inputs[1] org_name = inputs[2] if len(inputs) == 4: org_descr = inputs[3] if object_op == "add": parent_org_dn = 'org-' + org_parent.replace('/', '/org-') parent_org = handle.query_dn(parent_org_dn) if parent_org: ucs_mo = OrgOrg(parent_mo_or_dn=parent_org_dn, name=org_name, descr=org_descr) handle.add_mo(ucs_mo, modify_present=True) handle.commit() response = "Org: " + (parent_org_dn + "/org-" + org_name).replace( '/org-', '/').replace('org-', '') + " - Added." else: response = "Org: " + (parent_org_dn).replace('/org-', '/').replace( 'org-', '') + " - Parent Org Not Found: could not Add." elif object_op == "remove": parent_org_dn = 'org-' + org_parent.replace('/', '/org-') ucs_mo = handle.query_dn(parent_org_dn + "/org-" + org_name) if ucs_mo: handle.remove_mo(ucs_mo) handle.commit() response = "Org: " + (parent_org_dn + "/org-" + org_name).replace( '/org-', '/').replace('org-', '') + " - Removed." else: response = "Org: " + (parent_org_dn + "/org-" + org_name).replace( '/org-', '/').replace('org-', '') + " - Not Found: could not remove." elif object_op == "update": parent_org_dn = 'org-' + org_parent.replace('/', '/org-') ucs_mo = handle.query_dn(parent_org_dn + "/org-" + org_name) if ucs_mo: ucs_mo.descr = org_descr handle.add_mo(ucs_mo, modify_present=True) handle.commit() response = "Org: " + (parent_org_dn + "/org-" + org_name).replace( '/org-', '/').replace('org-', '') + " - Updated." else: response = "Org: " + (parent_org_dn + "/org-" + org_name).replace( '/org-', '/').replace('org-', '') + " - Not Found: could not update." ucs_logout(handle) print(response) return response
def test_invalid_server_pool_add_rack_unit(query_mock, add_mo_mock, commit_mock): add_mo_mock.return_value = True commit_mock.return_value = True handle = UcsHandle('169.254.1.1', 'admin', 'password') # Scenario: Invalid Org query_mock.return_value = None # Verify exception was raised for invalid org assert_raises( ValueError, server_pool_add_rack_unit, handle, 16, ) # Scenario: Org is not a ComputePool query_mock.return_value = OrgOrg(parent_mo_or_dn="org-root", name="root") # Verify exception was raised for invalid type assert_raises( TypeError, server_pool_add_rack_unit, handle, 16, )
def org_create(handle, name, descr="", parent_dn="org-root"): """ This method creates sub organization. Args: handle (UcsHandle) name (string): Name of the organization descr (string): Basic description about the org. parent_dn (string): org dn Returns: OrgOrg: Managed Object Raises: ValueError: If OrgOrg is not present Example: org_create(handle, name="sample_org") """ from ucsmsdk.mometa.org.OrgOrg import OrgOrg obj = handle.query_dn(parent_dn) if not obj: raise ValueError("org '%s' does not exist") mo = OrgOrg(parent_mo_or_dn=parent_dn, name=name, descr=descr) handle.add_mo(mo, modify_present=True) handle.commit() return mo
def run_module(): """ Run the module """ module = AnsibleModule(argument_spec=dict( hostname=dict(type='str', required=True), username=dict(type='str', default='admin'), password=dict(type='str', required=True, no_log=True), name=dict(type='str'), descr=dict(type='str'), state=dict( type='str', default='present', choices=['present', 'absent']))) from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.org.OrgOrg import OrgOrg handle = UcsHandle(module.params['hostname'], module.params['username'], module.params['password']) handle.login() ucs_mo = OrgOrg(parent_mo_or_dn='org-root', name=module.params['name'], descr=module.params['descr']) handle.add_mo(ucs_mo, modify_present=True) handle.commit() handle.logout() # TODO: Add delete object code result = dict(changed=True) module.exit_json(**result)
def create_org(handle, org): print "Creating Organization: %s" % org from ucsmsdk.mometa.org.OrgOrg import OrgOrg mo = OrgOrg(parent_mo_or_dn="org-root", name=org, descr="KUBAM org") handle.add_mo(mo, modify_present=True) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\tOrganization already exists."
def test_valid_server_pool_create(query_mock, add_mo_mock, commit_mock): query_mock.return_value = OrgOrg(parent_mo_or_dn="org-root", name="root") add_mo_mock.return_value = True commit_mock.return_value = True handle = UcsHandle('169.254.1.1', 'admin', 'password') # Scenario: Default parameters pool_retval = server_pool_create(handle, 'valid-pool') # Verify we were passed back the correct object type assert isinstance(pool_retval, ComputePool) # Verify the name we gave it was assigned correctly assert pool_retval.name == "valid-pool"
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_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_valid_ipmi_policy_create(query_mock, add_mo_mock, commit_mock): # Create a root org as a return value to query_dn query_mock.return_value = OrgOrg(parent_mo_or_dn="org-root", name="root") add_mo_mock.return_value = True commit_mock.return_value = True handle = UcsHandle('169.254.1.1', 'admin', 'password') # Scenario: Default parameters ipmi_retval = ipmi_policy_create(handle, name="test_ipmi_policy") # Verify we were passed back the correct object type assert isinstance(ipmi_retval, AaaEpAuthProfile) # Verify the name we gave it was assigned correctly assert ipmi_retval.name == "test_ipmi_policy" # Verify the retruned object has no users (children) assert len(ipmi_retval.child) == 0 # Scenario: default user, custom password ipmi_retval = ipmi_policy_create(handle, name="test_ipmi_policy", password="******") user_retval = ipmi_retval.child[0] # Verify the returned policy has a user (child) assert user_retval is not None # Verify the username is the default 'admin' assert user_retval.name is 'admin' # Verify the password was assigned correctly assert user_retval.pwd is 'password' # Scenario: custom user, custom password ipmi_retval = ipmi_policy_create(handle, name="test_ipmi_policy", username="******", password="******") user_retval = ipmi_retval.child[0] # Verify the returned policy has a user (child) assert user_retval is not None # Verify the username was assigned correctly assert user_retval.name is 'myuser' # Verify the password was assigned correctly assert user_retval.pwd is 'password'
def org_create(handle, name, descr="", parent_dn="org-root"): """ This method creates sub organization. Args: handle (UcsHandle) name (string): Name of the organization descr (string): Basic description about the org. parent_dn (string): Returns: OrgOrg: managed object Example: org_create(handle, name="sample_org") """ from ucsmsdk.mometa.org.OrgOrg import OrgOrg mo = OrgOrg(parent_mo_or_dn=parent_dn, name=name, descr=descr) handle.add_mo(mo, modify_present=True) handle.commit() return mo
def run_module(): """ Run the module """ module = AnsibleModule( argument_spec=dict( hostname=dict(type='str', required=True), username=dict(type='str', default='admin'), password=dict(type='str', required=True, no_log=True), name=dict(type='str'), descr=dict(type='str'), state=dict(type='str', default='present', choices=['present', 'absent']) ), required_if=[ ['state', 'present', ['name']], ], supports_check_mode=True ) from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.org.OrgOrg import OrgOrg error = False changed = False kwargs = dict() result = dict() if module.params['descr'] is not None: kwargs['descr'] = module.params['descr'] try: handle = UcsHandle( module.params['hostname'], module.params['username'], module.params['password'] ) handle.login() ucs_mo = handle.query_dn('org-root/org-' + module.params['name']) # Determine state change if ucs_mo: # Object exists, should it exist? has anything changed? if module.params['state'] == 'present': # Any Object properties not match, that is a change if not ucs_mo.check_prop_match(**kwargs): changed = True # Object does not exist but should, that is a change else: if module.params['state'] == 'present': changed = True # Object exists but should not, that is a change if ucs_mo and module.params['state'] == 'absent': changed = True # Apply state if not check_mode if changed and not module.check_mode: if module.params['state'] == 'absent': handle.remove_mo(ucs_mo) else: kwargs['parent_mo_or_dn'] = 'org-root' kwargs['name'] = module.params['name'] if module.params['descr'] is not None: kwargs['descr'] = module.params['descr'] ucs_mo = OrgOrg(**kwargs) handle.add_mo(ucs_mo, modify_present=True) handle.commit() except Exception as e: error = True result['msg'] = "error: %s " % str(e) result['changed'] = changed if error: module.fail_json(**result) module.exit_json(**result)
""" 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()
def set_ucs_server(): from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock from ucsmsdk.mometa.org.OrgOrg import OrgOrg ucsm_login() if status['login'] != "success": message = ("there was an error connecting to the UCS Manager, " + "please check the access credentials or IP address") return message # Create Org DevNet mo_org = OrgOrg(parent_mo_or_dn="org-root", name="DevNet") handle.add_mo(mo_org, modify_present=True) handle.commit() # Create MacPool 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() # Add/Update Service Profile Template mo_sp_template = LsServer(parent_mo_or_dn="org-root/org-DevNet", type="initial-template", name="DevNet_Skill_Template") handle.add_mo(mo_sp_template, modify_present=True) handle.commit() # Retrive the MO for the created/updated Service Profile template filter_exp = '(name,"DevNet_Skill_Template")' mo_sp_templ = handle.query_classid("lsServer", filter_str=filter_exp) # Retrive MOs for any existing Service Profiles filter_exp = '(name,"DevNet_Skill_*", type="re") and (type,"instance")' mo_sp_instances = handle.query_classid("lsServer", filter_str=filter_exp) # Find the highest suffix for existing Service Profiles if len(mo_sp_instances) >= 1: sp_suffixes = [ int(sp_instance.name[sp_instance.name.rindex('_') + 1:]) for sp_instance in mo_sp_instances ] num_sp_instances = max(sp_suffixes) + 1 else: num_sp_instances = 1 # Create the next Service Profile name if num_sp_instances <= 9: service_profile_name = "DevNet_Skill_Server_0" + str(num_sp_instances) else: service_profile_name = "DevNet_Skill_Server_" + str(num_sp_instances) # Find an available compute blade response = handle.query_classid("computeBlade") for blade in response: if blade.admin_state == 'in-service' and blade.availability == 'available': break # Create the Service Profile mo_sp = LsServer(parent_mo_or_dn="org-root/org-DevNet", src_templ_name="DevNet_Skill_Template", name=service_profile_name) mo_sp_templ_ls_binding = LsBinding(parent_mo_or_dn=mo_sp, pn_dn=blade.dn) handle.add_mo(mo_sp, modify_present=True) handle.commit() ucsm_logout() message = ("For the requested UCS Manager Server Provisioning operation," + " server, " + blade.slot_id + ", " + " in chassis, " + blade.chassis_id + ", " + " has been provisioned with service profile, " + service_profile_name.replace('_', ' ') + "," + " in the Dev Net Organization.") return message
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_name=dict(type='str', aliases=['name']), parent_org_path=dict(type='str', default='root'), description=dict(type='str', aliases=['descr']), state=dict(type='str', default='present', choices=['present', 'absent']), delegate_to=dict(type='str', default='localhost'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['org_name']], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. # Imports are below for UCS object creation. ucs = UCSModule(module) from ucsmsdk.mometa.org.OrgOrg import OrgOrg err = False changed = False requested_state = module.params['state'] kwargs = dict() if module.params['description'] is not None: kwargs['descr'] = module.params['description'] try: parent_org_dn = 'org-' + module.params['parent_org_path'].replace( '/', '/org-') dn = parent_org_dn + '/org-' + module.params['org_name'] mo = ucs.login_handle.query_dn(dn) # Determine state change if mo: # Object exists, if it should exist has anything changed? if requested_state == 'present': # Do some or all Object properties not match, that is a change if not mo.check_prop_match(**kwargs): changed = True # Object does not exist but should, that is a change else: if requested_state == 'present': changed = True # Object exists but should not, that is a change if mo and requested_state == 'absent': changed = True # Apply state if not check_mode if changed and not module.check_mode: if requested_state == 'absent': ucs.login_handle.remove_mo(mo) else: kwargs['parent_mo_or_dn'] = parent_org_dn kwargs['name'] = module.params['org_name'] if module.params['description'] is not None: kwargs['descr'] = module.params['description'] mo = OrgOrg(**kwargs) ucs.login_handle.add_mo(mo, modify_present=True) ucs.login_handle.commit() 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 run_module(): argument_spec = ucs_argument_spec argument_spec.update( org_name=dict(type='str', aliases=['name']), parent_org_path=dict(type='str', default='root'), description=dict(type='str', aliases=['descr'], default=''), state=dict(type='str', default='present', choices=['present', 'absent']), delegate_to=dict(type='str', default='localhost'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['org_name']], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. # Imports are below for UCS object creation. ucs = UCSModule(module) from ucsmsdk.mometa.org.OrgOrg import OrgOrg err = False changed = False try: mo_exists = False props_match = False parent_org_dn = 'org-' + module.params['parent_org_path'].replace('/', '/org-') dn = parent_org_dn + '/org-' + module.params['org_name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True if module.params['state'] == 'absent': 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(descr=module.params['description']) if mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # update/add mo mo = OrgOrg(parent_mo_or_dn=parent_org_dn, name=module.params['org_name'], descr=module.params['description']) ucs.login_handle.add_mo(mo, modify_present=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)
# Get a handle and login to UCS Manager from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("198.18.133.91", "admin", "password") handle.login() # Query for compute blades that are not associated blades = handle.query_classid("computeBlade") for blade in blades: if blade.association == "none": print(blade.dn, blade.association) # Create the Python_Heroes Organization from ucsmsdk.mometa.org.OrgOrg import OrgOrg root_org = handle.query_dn("org-root") mo_org = OrgOrg(parent_mo_or_dn=root_org, name="Python_Heroes") handle.add_mo(mo_org, modify_present=True) handle.commit() # Create the CommanderCode_Python_Server Service Profile from ucsmsdk.mometa.ls.LsServer import LsServer sp_org = handle.query_dn("org-root/org-Python_Heroes") mo_sp = LsServer(parent_mo_or_dn=sp_org, name="CommanderCode_Python_Server") handle.add_mo(mo_sp, modify_present=True) handle.commit() # Add a MAC block to the default MAC Pool 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",
VLAN_Name = row i=1 while i < len(VLAN_Name): print VLAN_Name[i] i = i + 1 elif row[0] == "VLAN ID": VLAN_ID = row i=1 while i < len(VLAN_ID): print VLAN_ID[i] i = i + 1 else: print "Bad Robot" #Create Sub Organization mo = OrgOrg(parent_mo_or_dn="org-root", name=my_Org, descr="Sub Organization") handle.add_mo(mo) handle.commit() #Create Production VLANs k = 1 while k < len(VLAN_ID): mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name=VLAN_Name[k], id=VLAN_ID[k], mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit() print k k = k+1 #Create UUID Pool mo = UuidpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", prefix="derived", descr="UUID Pool", assignment_order="sequential", name="UUID_POOL") mo_1 = UuidpoolBlock(parent_mo_or_dn=mo, to="0001-000000000100", r_from="0001-000000000001")
#launch GUI #from ucsmsdk.utils.ucsguilaunch import ucs_gui_launch #ucs_gui_launch(handle) #get ucs commands from the GUI #from ucsmsdk.utils.converttopython import convert_to_ucs_python #convert_to_ucs_python() #Create a Sub Organization ##### Start-Of-PythonScript ##### from ucsmsdk.mometa.org.OrgOrg import OrgOrg mo = OrgOrg(parent_mo_or_dn="org-root", name="Sub_Org_Test", descr="Sub Org Desc") handle.add_mo(mo) handle.commit() ##### End-Of-PythonScript ##### #Remove Sub Organization ##### Start-Of-PythonScript ##### #mo = handle.query_dn("org-root/org-Sub_Org_Name") #handle.remove_mo(mo) #handle.commit() ##### End-Of-PythonScript ##### #create a vlan ##### Start-Of-PythonScript #####