def test_get_central_instances(nickname, server, all_profiles_dict, org_vm,
                               reference_direction='dmtf'):
    """Test get central instances"""
    good_rtns = []
    error_rtns = []

    for inst in server.profiles:
        try:
            prof = profile_name(org_vm, inst, short=True)
            prof_long = profile_name(org_vm, inst)
            if prof not in all_profiles_dict:
                PROFILES_WITH_NO_DEFINITIONS[nickname] = prof
                continue

            prof_def = all_profiles_dict[prof]
            print('%s: PROFILE get_central_instances: %s inst.path=%s\n   '
                  'central_class=%s,scoping_class=%s, scoping_path=%s' %
                  (nickname, prof_long, inst.path, prof_def.central_class,
                   prof_def.scoping_class, prof_def.scoping_path))

            # If profile has ElementConformsToProfile, ignore other
            # definition parameters
            ci_paths = server.conn.AssociatorNames(
                ObjectName=inst.path,
                AssocClass="CIM_ElementConformsToProfile",
                ResultRole="ManagedElement")
            if ci_paths:
                try:
                    central_paths = server.get_central_instances(
                        inst.path,
                        central_class=prof_def.central_class,
                        scoping_class=prof_def.scoping_class,
                        scoping_path=prof_def.scoping_path,
                        reference_direction=reference_direction)
                except pywbem.Error as er:
                    print('GET_CENTRAL_INSTANCES Exception %s:%s' %
                          (er.__class__.__name__, er))
                    if server.conn.debug:
                        print('LAST_REQUEST\n%s' % server.conn.last_request)
                        print('LAST_REPLY\n%s' % server.conn.last_reply)
                    continue

                for path in central_paths:
                    path.host = None
                    path.namespace = None
                good_rtns.append((prof_long, central_paths))

            # Otherwise test only if definition complete. Note this
            # code requires the direction param
            else:
                # Test that central class and scoping class are defined. Also
                # test scoping path. If None, this is an error, unless
                # central and scoping classes are the same
                if prof_def.scoping_class and prof_def.central_class:
                    if prof_def.scoping_class.lower() == \
                            prof_def.central_class.lower() or \
                            prof_def.scoping_path:
                        try:
                            central_paths = server.get_central_instances(
                                inst.path,
                                central_class=prof_def.central_class,
                                scoping_class=prof_def.scoping_class,
                                scoping_path=prof_def.scoping_path,
                                reference_direction=reference_direction)
                        except pywbem.Error as er:
                            print('GET_CENTRAL_INSTANCES Exception %s:%s' %
                                  (er.__class__.__name__, er))
                            if server.conn.debug:
                                print('LAST_REQUEST\n%s' %
                                      server.conn.last_request)
                                print('LAST_REPLY\n%s' %
                                      server.conn.last_raw_reply)
                            continue
                        for path in central_paths:
                            path.host = None
                            path.namespace = None
                        good_rtns.append((prof_long, central_paths))
                    else:
                        print('INVALID_SCOPING_PATH %s %s %s' % (
                            prof_def.scoping_class, prof_def.central_class,
                            prof_def.scoping_path))
                        PROFILES_WITH_NO_DEFINITIONS[nickname] = prof
                else:
                    PROFILES_WITH_NO_DEFINITIONS[nickname] = prof

        except ValueError as ve:
            print("ValueError: %s: %s\n  inst_path %s\n   exception %s" %
                  (nickname, profile_name(org_vm, inst), inst.path, ve))
            if VERBOSE:
                if server.conn.debug:
                    print('LAST_REQUEST\n%s' % server.conn.last_request)
                    print('LAST_REPLY\n%s' % server.conn.last_reply)
            error_rtns.append((prof_long, ve))
        except pywbem.Error as er:
            print("pywbem.Error: %s: %s\n  inst_path=%s\n   "
                  "server=%s\n   Exception=%s"
                  % (nickname, profile_name(org_vm, inst, short=True),
                     inst.path, server, er))
            if VERBOSE:
                if server.conn.debug:
                    print('LAST_REQUEST\n%s' % server.conn.last_request)
                    print('LAST_REPLY\n%s' % server.conn.last_reply)
            error_rtns.append((prof_long, er))

    print('%s: Successful_Profiles=%s Error=%s\n' % (nickname, len(good_rtns),
                                                     len(error_rtns)))

    title = '%s: Successful returns from get_central_instances' % nickname
    headers = ['Profile', 'Rtn Paths']
    rows = []
    for data in good_rtns:
        # TODO str(p) to fold_path(str(p), 20)
        paths_str = '\n'.join([str(p) for p in data[1]])
        rows.append([data[0], paths_str])
    print_table(title, headers, rows)

    title = '%s: Error returns from get_central_instances' % nickname
    headers = ['Profile', 'Error rtn']
    rows = [[data[0], textwrap.fill(str(data[1]), 80)] for data in error_rtns]
    print_table(title, headers, rows, sort_columns=0)

    # set the data into the lists for display at end of the test
    GET_CENTRAL_INST_ERRS[nickname] = error_rtns
    GET_CENTRAL_INST_ROWS.append([nickname, len(server.profiles),
                                  len(good_rtns), len(error_rtns)])
def test_get_central_instances ( nickname , server , all_profiles_dict , org_vm , reference_direction = 'dmtf' ) : good_rtns = [ ] error_rtns = [ ] for inst in server . profiles : try : prof = profile_name ( org_vm , inst , short = True ) prof_long = profile_name ( org_vm , inst ) if prof not in all_profiles_dict : PROFILES_WITH_NO_DEFINITIONS [ nickname ] = prof continue prof_def = all_profiles_dict [ prof ] print ( '%s: PROFILE get_central_instances: %s inst.path=%s\n   ' 'central_class=%s,scoping_class=%s, scoping_path=%s' % ( nickname , prof_long , inst . path , prof_def . central_class , prof_def . scoping_class , prof_def . scoping_path ) ) ci_paths = server . conn . AssociatorNames ( ObjectName = inst . path , AssocClass = "CIM_ElementConformsToProfile" , ResultRole = "ManagedElement" ) if ci_paths : try : central_paths = server . get_central_instances ( inst . path , central_class = prof_def . central_class , scoping_class = prof_def . scoping_class , scoping_path = prof_def . scoping_path , reference_direction = reference_direction ) except pywbem . Error as er : print ( 'GET_CENTRAL_INSTANCES Exception %s:%s' % ( er . __class__ . __name__ , er ) ) if server . conn . debug : print ( 'LAST_REQUEST\n%s' % server . conn . last_request ) print ( 'LAST_REPLY\n%s' % server . conn . last_reply ) continue for path in central_paths : path . host = None path . namespace = None good_rtns . append ( ( prof_long , central_paths ) ) else : if prof_def . scoping_class and prof_def . central_class : if prof_def . scoping_class . lower ( ) == prof_def . central_class . lower ( ) or prof_def . scoping_path : try : central_paths = server . get_central_instances ( inst . path , central_class = prof_def . central_class , scoping_class = prof_def . scoping_class , scoping_path = prof_def . scoping_path , reference_direction = reference_direction ) except pywbem . Error as er : print ( 'GET_CENTRAL_INSTANCES Exception %s:%s' % ( er . __class__ . __name__ , er ) ) if server . conn . debug : print ( 'LAST_REQUEST\n%s' % server . conn . last_request ) print ( 'LAST_REPLY\n%s' % server . conn . last_raw_reply ) continue for path in central_paths : path . host = None path . namespace = None good_rtns . append ( ( prof_long , central_paths ) ) else : print ( 'INVALID_SCOPING_PATH %s %s %s' % ( prof_def . scoping_class , prof_def . central_class , prof_def . scoping_path ) ) PROFILES_WITH_NO_DEFINITIONS [ nickname ] = prof else : PROFILES_WITH_NO_DEFINITIONS [ nickname ] = prof except ValueError as ve : print ( "ValueError: %s: %s\n  inst_path %s\n   exception %s" % ( nickname , profile_name ( org_vm , inst ) , inst . path , ve ) ) if VERBOSE : if server . conn . debug : print ( 'LAST_REQUEST\n%s' % server . conn . last_request ) print ( 'LAST_REPLY\n%s' % server . conn . last_reply ) error_rtns . append ( ( prof_long , ve ) ) except pywbem . Error as er : print ( "pywbem.Error: %s: %s\n  inst_path=%s\n   " "server=%s\n   Exception=%s" % ( nickname , profile_name ( org_vm , inst , short = True ) , inst . path , server , er ) ) if VERBOSE : if server . conn . debug : print ( 'LAST_REQUEST\n%s' % server . conn . last_request ) print ( 'LAST_REPLY\n%s' % server . conn . last_reply ) error_rtns . append ( ( prof_long , er ) ) print ( '%s: Successful_Profiles=%s Error=%s\n' % ( nickname , len ( good_rtns ) , len ( error_rtns ) ) ) title = '%s: Successful returns from get_central_instances' % nickname headers = [ 'Profile' , 'Rtn Paths' ] rows = [ ] for data in good_rtns : paths_str = '\n' . join ( [ str ( p ) for p in data [ 1 ] ] ) rows . append ( [ data [ 0 ] , paths_str ] ) print_table ( title , headers , rows ) title = '%s: Error returns from get_central_instances' % nickname headers = [ 'Profile' , 'Error rtn' ] rows = [ [ data [ 0 ] , textwrap . fill ( str ( data [ 1 ] ) , 80 ) ] for data in error_rtns ] print_table ( title , headers , rows , sort_columns = 0 ) GET_CENTRAL_INST_ERRS [ nickname ] = error_rtns GET_CENTRAL_INST_ROWS . append ( [ nickname , len ( server . profiles ) , len ( good_rtns ) , len ( error_rtns ) ] )
Test get central instances