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 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 test_unknown_mo(): from ucsmsdk.ucsmo import GenericMo static_setup() ref_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="") ref_mo._handle = ref_handle ref_mos = [ref_mo] diff_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="yyy") diff_mo._handle = diff_handle diff_mos = [diff_mo] difference = compare_ucs_mo(ref_mos, diff_mos) write_mo_diff(difference) assert_equal(len(difference), 0)
def test_unknown_mo(): from ucsmsdk.ucsmo import GenericMo static_setup() ref_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="") ref_mo._handle = ref_handle ref_mos = [ref_mo] diff_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="yyy") diff_mo._handle = diff_handle diff_mos = [diff_mo] difference = compare_ucs_mo(ref_mos, diff_mos) write_mo_diff(difference) assert_equal(len(difference), 0)
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 test_unknown_mo_noversionfilter(): from ucsmsdk.ucsmo import GenericMo static_setup() ref_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="") ref_mo._handle = ref_handle ref_mos = [ref_mo] diff_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="yyy") diff_mo._handle = diff_handle 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), 1)
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 test_unknown_mo_noversionfilter(): from ucsmsdk.ucsmo import GenericMo static_setup() ref_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="") ref_mo._handle = ref_handle ref_mos = [ref_mo] diff_mo = GenericMo(class_id="unknown", parent_mo_or_dn="parent_dn", dn="sys/unknown", unknown="yyy") diff_mo._handle = diff_handle 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), 1)
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_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)
from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.utils import comparesyncmo # Login to a SOURCE UCS and a TARGET UCS SOURCE_UCS = UcsHandle("13.58.22.56", "admin", "password") TARGET_UCS = UcsHandle("18.217.19.216", "admin", "password") SOURCE_UCS.login() TARGET_UCS.login() # Query the SOURCE UCS VLANS and the TARGET UCS VLANS SOURCE_UCS_VLANS = SOURCE_UCS.query_classid("fabricVlan") TARGET_UCS_VLANS = TARGET_UCS.query_classid("fabricVlan") # Compare the TARGET VLANS with the SOURCE VLANS DIFFERENCE_VLANS = comparesyncmo.compare_ucs_mo(TARGET_UCS_VLANS, SOURCE_UCS_VLANS) print("difference is %s" % DIFFERENCE_VLANS) if DIFFERENCE_VLANS: # Display the difference comparesyncmo.write_mo_diff(DIFFERENCE_VLANS) # Sync - apply the difference to the TARGET_UCS, remove from the # TARGET_UCS any objects that are not present on the SOURCE_UCS # This action is not done through the HANDLE, the commit() is implicit comparesyncmo.sync_ucs_mo(TARGET_UCS, DIFFERENCE_VLANS, delete_not_present=True) # Logout
# Combined Dictionary to keep track of DN/VLAN mapping per UCS Domain (Left/Right) dn_to_vlan_mapping = {} dn_to_vlan_mapping["left"] = {} dn_to_vlan_mapping["right"] = {} # Loop through the data we previously collected with the query_classid lookups. # Doing this for speed instead of having to make subsequent query lookups per VLAN/DN # Reduced from 99 seconds down to 7 seconds for element in left_ucs_vlans: dn_to_vlan_mapping["left"][element.dn] = element.id for element in right_ucs_vlans: dn_to_vlan_mapping["right"][element.dn] = element.id # Using the built in compare function, identify what MO's have differences. Returns an object difference_vlans = compare_ucs_mo(left_ucs_vlans, right_ucs_vlans, include_equal=True) # Built in function to print the differences, with some information, however no real logic applied #write_mo_diff(difference_vlans) difference_in_vlan = [] left_only_vlans = [] # VLANs found to only exist in one UCS Domain, i.e Left right_only_vlans = [ ] # VLANs found to only exist in one UCS Domain, i.e. Right common_vlans = [ ] # VLANs found to exist in BOTH UCS Domains and are the SAME, i.e. VLAN NAME and VLAN ID for item in difference_vlans: if item.diff_property == None: if item.side_indicator == "<=":
def main(): """ Main routine to be executed """ # Get the CLI arguements args = get_parser().parse_args() section_platform_left = args.platform[0] section_platform_right = args.platform[1] parser = SafeConfigParser() # Since when the script executes, it is difficult to assume the relative path - as depends on where you run your Python script from. # Especially if calling it via absolute path and you're not in the actual directory # Hence, use the following to effectively obtain the config file as if it were '../my_credentials.ini' parent_configFilePath = os.path.join( os.path.abspath(os.path.dirname(__file__)), '..', MY_CONFIG_FILE) local_configFilePath = os.path.join( os.path.abspath(os.path.dirname(__file__)), '.', MY_CONFIG_FILE) # Check if MY_CONFIG_FILE is in the current local directory, otherwise check the parent directory. If missing in both, then fail if os.path.exists(local_configFilePath): # Use the local directory # print ("Found in local directory") configFilePath = local_configFilePath elif os.path.exists(parent_configFilePath): # Use the parent directory # print ("Found in parent directory") configFilePath = parent_configFilePath else: # Missing completely. Fail print( "Unable to locate your Config File: {}. Please ensure that you have it created and available" .format(MY_CONFIG_FILE)) print("Not found in the local directory: {}".format( local_configFilePath)) print("Not found in the Parent directory: {}".format( parent_configFilePath)) exit(0) if args.debug: print("Config File: {}".format(configFilePath)) # Attempt to read the configuration file try: result = parser.read(configFilePath) except Exception as e: print(str(e)) exit(0) if (section_platform_left not in parser.sections()) or (section_platform_right not in parser.sections()): print( "Please ensure you select a Platform that you have defined in your {} file" .format(MY_CONFIG_FILE)) print("Current Platforms defined: " + str(parser.sections())) exit(0) else: try: UCSM_LEFT = parser.get(section_platform_left, 'UCSM') USER_LEFT = parser.get(section_platform_left, 'USER') PASS_LEFT = parser.get(section_platform_left, 'PASS') UCSM_RIGHT = parser.get(section_platform_right, 'UCSM') USER_RIGHT = parser.get(section_platform_right, 'USER') PASS_RIGHT = parser.get(section_platform_right, 'PASS') except Exception as e: print(str(e), ' could not read configuration file') for section_name in parser.sections(): print 'Section:', section_name print ' Options:', parser.options(section_name) if args.debug: for name, value in parser.items(section_name): print ' %s = %s' % (name, value) print # Exit due to exception exit(0) # Rest of normal flow would take place here start_time = time.time() left_ucs = UcsHandle(UCSM_LEFT, USER_LEFT, PASS_LEFT, secure=False) right_ucs = UcsHandle(UCSM_RIGHT, USER_RIGHT, PASS_RIGHT, secure=False) left_ucs.login() right_ucs.login() #print (" Left UCS Name: " + left_ucs.ucs) #print ("Right UCS Name: " + right_ucs.ucs) #print "=" * 80 left_ucs_vlans = left_ucs.query_classid("fabricVlan") right_ucs_vlans = right_ucs.query_classid("fabricVlan") # Combined Dictionary to keep track of DN/VLAN mapping per UCS Domain (Left/Right) dn_to_vlan_mapping = {} dn_to_vlan_mapping["left"] = {} dn_to_vlan_mapping["right"] = {} # Loop through the data we previously collected with the query_classid lookups. # Doing this for speed instead of having to make subsequent query lookups per VLAN/DN # Reduced from 99 seconds down to 7 seconds for element in left_ucs_vlans: dn_to_vlan_mapping["left"][element.dn] = element.id for element in right_ucs_vlans: dn_to_vlan_mapping["right"][element.dn] = element.id # Using the built in compare function, identify what MO's have differences. Returns an object difference_vlans = compare_ucs_mo(left_ucs_vlans, right_ucs_vlans, include_equal=True) # Built in function to print the differences, with some information, however no real logic applied #write_mo_diff(difference_vlans) difference_in_vlan = [] left_only_vlans = [ ] # VLANs found to only exist in one UCS Domain, i.e Left right_only_vlans = [ ] # VLANs found to only exist in one UCS Domain, i.e. Right common_vlans = [ ] # VLANs found to exist in BOTH UCS Domains and are the SAME, i.e. VLAN NAME and VLAN ID for item in difference_vlans: if item.diff_property == None: if item.side_indicator == "<=": # lov is left only vlan lov = dn_to_vlan_mapping["left"][item.dn] left_only_vlans.append((item.dn, lov)) elif item.side_indicator == "=>": # rov is right only vlan rov = dn_to_vlan_mapping["right"][item.dn] right_only_vlans.append((item.dn, rov)) else: # Extremely slow. Need to figure out how to speed up # Since the same, doesn't matter which UCS Domain we query # cv is common vlan cv = dn_to_vlan_mapping["left"][item.dn] common_vlans.append((item.dn, cv)) else: ''' # item.diff_property != None therefore has some different property values print "DN: " + item.dn print "Class ID: " + str(item.input_object.get_class_id()) print "Side indicator: " + item.side_indicator print "Diff Property: " + str(item.diff_property) for key, value in item.diff_prop_values.iteritems(): print "Key: " + key print "Value: " + value ''' left_ucs_query = query_dn_for_vlan_id(left_ucs, item.dn) right_ucs_query = query_dn_for_vlan_id(right_ucs, item.dn) difference_in_vlan.append( (item.input_object.get_class_id(), item.dn, left_ucs.ucs, left_ucs_query, right_ucs_query, right_ucs.ucs)) # Display the various data in table format print("") print("=" * 80) print("") print("VLAN/s unique to the Left UCS Domain: " + left_ucs.ucs) print("") print( tabulate(left_only_vlans, headers=["DN", "VLAN ID"], tablefmt="simple")) print("") print("=" * 80) print("") print("VLAN/s unique to the Right UCS Domain: " + right_ucs.ucs) print("") print( tabulate(right_only_vlans, headers=["DN", "VLAN ID"], tablefmt="simple")) print("") print("=" * 80) print("") print("Common VLANs, configured the same in both UCS Domains: " + left_ucs.ucs + " // " + right_ucs.ucs) print("") print(tabulate(common_vlans, headers=["DN", "VLAN ID"], tablefmt="simple")) print("") print("=" * 80) print("") print( "VLAN/s by the same name exist on both UCS Domains however there is a difference in the VLAN ID" ) print("") print( tabulate(difference_in_vlan, headers=[ "ClassID", "Affected DN", "Left UCS", "VLAN ID", "VLAN ID", "Right UCS" ], tablefmt="simple")) print("") print("=" * 80) print("=" * 80) print("Summary:") print("Left UCS: %s has %s unique VLANs" % (left_ucs.ucs, len(left_only_vlans))) print("Right UCS: %s has %s unique VLANs" % (right_ucs.ucs, len(right_only_vlans))) print("There are %s common VLANs" % (len(common_vlans))) print("There is potential issues with %s VLANs due to differences" % (len(difference_in_vlan))) print("=" * 80) print("--- Execution Time: %s seconds ---" % (time.time() - start_time)) left_ucs.logout() right_ucs.logout()