source/Tools Integration/MGN-Integration/MGN-automation-scripts/3-Verify-instance-status.py [31:87]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
serverendpoint = mfcommon.serverendpoint
appendpoint = mfcommon.appendpoint

with open('FactoryEndpoints.json') as json_file:
    endpoints = json.load(json_file)

def assume_role(account_id, region):

    sts_client = boto3.client('sts')
    role_arn = 'arn:aws:iam::' + account_id + ':role/Factory-Automation'
    # Call the assume_role method of the STSConnection object and pass the role
    # ARN and a role session name.
    try:
        user = sts_client.get_caller_identity()['Arn']
        sessionname = user.split('/')[1]
        response = sts_client.assume_role(RoleArn=role_arn, RoleSessionName=sessionname)
        credentials = response['Credentials']
        session = boto3.Session(
            region_name = region,
            aws_access_key_id=credentials['AccessKeyId'],
            aws_secret_access_key=credentials['SecretAccessKey'],
            aws_session_token=credentials['SessionToken']
        )
        return session
    except botocore.exceptions.ClientError as e:
        print(str(e))

def GetInstanceId(serverlist):
        for account in serverlist:
            target_account_session = assume_role(str(account['aws_accountid']), account['aws_region'])
            print("")
            print("Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'])
            mgn_client = target_account_session.client("mgn", account['aws_region'])
            mgn_sourceservers = mgn_client.describe_source_servers(filters={})
            for factoryserver in account['servers']:
                if 'server_fqdn' not in factoryserver:
                    print("ERROR: server_fqdn does not exist for server: " + factoryserver['server_name'])
                    sys.exit()
                else:
                    sourceserver = mfcommon.get_MGN_Source_Server(factoryserver, mgn_sourceservers['items'])
                    if sourceserver is not None:
                        # Get target instance Id for the source server in Application Migration Service
                        if sourceserver['isArchived'] == False:
                            if 'launchedInstance' in sourceserver:
                                if 'ec2InstanceID' in sourceserver['launchedInstance']:
                                    factoryserver['target_ec2InstanceID'] = sourceserver['launchedInstance']['ec2InstanceID']
                                    print(factoryserver['server_name'] + " : " + factoryserver['target_ec2InstanceID'])
                                else:
                                    factoryserver['target_ec2InstanceID'] = ''
                                    print("ERROR: target instance Id does not exist for server: " + factoryserver['server_name'] + ", please wait for a few minutes")
                            else:
                                factoryserver['target_ec2InstanceID'] = ''
                                print("ERROR: target instance does not exist for server: " + factoryserver['server_name'] + ", please wait for a few minutes")
                        else:
                            print("ERROR: Server: " + factoryserver['server_name'] + " is archived in Application Migration Service (Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'] + "), Please install the agent")
                            sys.exit()
        return serverlist
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



source/Tools Integration/MGN-Integration/MGN-automation-scripts/4-Get-instance-IP.py [30:86]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
serverendpoint = mfcommon.serverendpoint
appendpoint = mfcommon.appendpoint

with open('FactoryEndpoints.json') as json_file:
    endpoints = json.load(json_file)

def assume_role(account_id, region):

    sts_client = boto3.client('sts')
    role_arn = 'arn:aws:iam::' + account_id + ':role/Factory-Automation'
    # Call the assume_role method of the STSConnection object and pass the role
    # ARN and a role session name.
    try:
        user = sts_client.get_caller_identity()['Arn']
        sessionname = user.split('/')[1]
        response = sts_client.assume_role(RoleArn=role_arn, RoleSessionName=sessionname)
        credentials = response['Credentials']
        session = boto3.Session(
            region_name = region,
            aws_access_key_id=credentials['AccessKeyId'],
            aws_secret_access_key=credentials['SecretAccessKey'],
            aws_session_token=credentials['SessionToken']
        )
        return session
    except botocore.exceptions.ClientError as e:
        print(str(e))

def GetInstanceId(serverlist):
        for account in serverlist:
            target_account_session = assume_role(str(account['aws_accountid']), account['aws_region'])
            print("")
            print("Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'])
            mgn_client = target_account_session.client("mgn", account['aws_region'])
            mgn_sourceservers = mgn_client.describe_source_servers(filters={})
            for factoryserver in account['servers']:
                if 'server_fqdn' not in factoryserver:
                    print("ERROR: server_fqdn does not exist for server: " + factoryserver['server_name'])
                    sys.exit()
                else:
                    sourceserver = mfcommon.get_MGN_Source_Server(factoryserver, mgn_sourceservers['items'])
                    if sourceserver is not None:
                        # Get target instance Id for the source server in Application Migration Service
                        if sourceserver['isArchived'] == False:
                                if 'launchedInstance' in sourceserver:
                                    if 'ec2InstanceID' in sourceserver['launchedInstance']:
                                        factoryserver['target_ec2InstanceID'] = sourceserver['launchedInstance']['ec2InstanceID']
                                        print(factoryserver['server_name'] + " : " + factoryserver['target_ec2InstanceID'])
                                    else:
                                        factoryserver['target_ec2InstanceID'] = ''
                                        print("ERROR: target instance Id does not exist for server: " + factoryserver['server_name'] + ", please wait for a few minutes")
                                else:
                                    factoryserver['target_ec2InstanceID'] = ''
                                    print("ERROR: target instance does not exist for server: " + factoryserver['server_name'] + ", please wait for a few minutes")
                        else:
                            print("ERROR: Server: " + factoryserver['server_name'] + " is archived in Application Migration Service (Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'] + "), Please install the agent")
                            sys.exit()
        return serverlist
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



