def get_factory_servers()

in source/Tools Integration/MGN-Integration/MGN-automation-scripts/mfcommon.py [0:0]


def get_factory_servers(waveid, token, UserHOST, osSplit = True):
    try:
        linux_exist = False
        windows_exist = False
        auth = {"Authorization": token}
        # Get all Apps and servers from migration factory
        getservers = json.loads(requests.get(UserHOST + serverendpoint, headers=auth).text)
        #print(servers)
        getapps = json.loads(requests.get(UserHOST + appendpoint, headers=auth).text)
        #print(apps)
        servers = sorted(getservers, key = lambda i: i['server_name'])
        apps = sorted(getapps, key = lambda i: i['app_name'])

        # Get Unique target AWS account and region
        aws_accounts = []
        for app in apps:
            if 'wave_id' in app and 'aws_accountid' in app and 'aws_region' in app:
                if str(app['wave_id']) == str(waveid):
                    if len(str(app['aws_accountid']).strip()) == 12:
                        target_account = {}
                        target_account['aws_accountid'] = str(app['aws_accountid']).strip()
                        target_account['aws_region'] = app['aws_region'].lower().strip()
                        if osSplit:
                            target_account['servers_windows'] = []
                            target_account['servers_linux'] = []
                        else:
                            target_account['servers'] = []
                        if target_account not in aws_accounts:
                            aws_accounts.append(target_account)
                    else:
                        msg = "ERROR: Incorrect AWS Account Id Length for app: " + app['app_name']
                        print(msg)
                        sys.exit()
        if len(aws_accounts) == 0:
            msg = "ERROR: Server list for wave " + waveid + " is empty...."
            print(msg)
            sys.exit()

        # Get server list
        for account in aws_accounts:
            print("### Servers in Target Account: " + account['aws_accountid'] + " , region: " + account['aws_region'] + " ###")
            for app in apps:
                if 'wave_id' in app and 'aws_accountid' in app and 'aws_region' in app:
                    if str(app['wave_id']) == str(waveid):
                        if str(app['aws_accountid']).strip() == str(account['aws_accountid']):
                            if app['aws_region'].lower().strip() == account['aws_region']:
                                for server in servers:
                                    if 'app_id' in server:
                                        if server['app_id'] == app['app_id']:
                                            # verify server_os_family attribute, only accepts Windows or Linux
                                            if 'server_os_family' in server:
                                                # Verify server_fqdn, this is mandatory attribute
                                                if 'server_fqdn' in server:
                                                    if osSplit:
                                                        if server['server_os_family'].lower() == 'windows':
                                                            account['servers_windows'].append(server)
                                                        elif server['server_os_family'].lower() == 'linux':
                                                            account['servers_linux'].append(server)
                                                        else:
                                                            print("ERROR: Invalid server_os_family for: " + server['server_name'] + ", please select either Windows or Linux")
                                                            sys.exit()
                                                    else:
                                                        account['servers'].append(server)
                                                    print(server['server_fqdn'])
                                                else:
                                                    print("ERROR: server_fqdn for server: " + server['server_name'] + " doesn't exist")
                                                    sys.exit()
                                            else:
                                                print("ERROR: server_os_family does not exist for: " + server['server_name'])
                                                sys.exit()
            print("")
            if osSplit:
                # Check if the server list is empty for both Windows and Linux
                if len(account['servers_windows']) == 0 and len(account['servers_linux']) == 0:
                    msg = "ERROR: Server list for wave " + waveid + " and account: " + account['aws_accountid'] + " region: " + account['aws_region'] + " is empty...."
                    print(msg)
                    sys.exit()
                if len(account['servers_linux']) > 0:
                   linux_exist = True
                if len(account['servers_windows']) > 0:
                   windows_exist = True
            else:
                if len(account['servers']) == 0:
                    msg = "ERROR: Server list for wave " + waveid + " and account: " + account['aws_accountid'] + " region: " + account['aws_region'] + " is empty...."
                    print(msg)
                    sys.exit()
        if osSplit:
            return aws_accounts, linux_exist, windows_exist
        else:
            return aws_accounts
    except botocore.exceptions.ClientError as error:
        if ":" in str(error):
            err = ''
            msgs = str(error).split(":")[1:]
            for msg in msgs:
                err = err + msg
            msg = "ERROR: " + err
            print(msg)
            sys.exit()
        else:
            msg = "ERROR: " + str(error)
            print(msg)
            sys.exit()