def main()

in static/watool/utilities/Code/duplicateWAFR.py [0:0]


def main():
    boto3_min_version = "1.16.38"
    # Verify if the version of Boto3 we are running has the wellarchitected APIs included
    if (packaging.version.parse(boto3.__version__) < packaging.version.parse(boto3_min_version)):
        logger.error("Your Boto3 version (%s) is less than %s. You must ugprade to run this script (pip3 install boto3 --upgrade --user)" % (boto3.__version__, boto3_min_version))
        exit()

    # STEP 1 - Configure environment
    logger.info("Starting Boto %s Session" % boto3.__version__)
    # Create a new boto3 session
    if FROM_ACCOUNT:
        SESSION1 = boto3.session.Session(profile_name=FROM_ACCOUNT)
    else:
        SESSION1 = boto3.session.Session()
    if TO_ACCOUNT:
        SESSION2 = boto3.session.Session(profile_name=TO_ACCOUNT)
    else:
        SESSION2 = boto3.session.Session()
    # Initiate the well-architected session using the region defined above
    WACLIENT = SESSION1.client(
        service_name='wellarchitected',
        region_name=REGION_NAME,
    )

    WACLIENT_TO = SESSION2.client(
        service_name='wellarchitected',
        region_name=TO_REGION_NAME,
    )



    logger.info("Copy WorkloadID '%s' from '%s:%s' to '%s:%s'" % (FROM_WORKLOADID,REGION_NAME,FROM_ACCOUNT,TO_REGION_NAME,TO_ACCOUNT))
    # Ignoring milestones for now, will add later if interested
    workloadId = FROM_WORKLOADID
    # Find out what lenses apply to the from workloadid
    workloadJson = GetWorkload(WACLIENT,workloadId)
    WorkloadARN = workloadJson['WorkloadArn']
    # For each of the optional variables, lets check and see if we have them first:
    Notes = workloadJson['Notes'] if "Notes" in workloadJson else ""
    nonAwsRegions = workloadJson['NonAwsRegions'] if "NonAwsRegions" in workloadJson else []
    architecturalDesign = workloadJson['ArchitecturalDesign'] if "ArchitecturalDesign" in workloadJson else ""
    industryType = workloadJson['IndustryType'] if "IndustryType" in workloadJson else ""
    industry = workloadJson['Industry'] if "Industry" in workloadJson else ""
    accountIds = workloadJson['AccountIds'] if "AccountIds" in workloadJson else []
    tagresponse = WACLIENT.list_tags_for_resource(WorkloadArn=WorkloadARN)
    tags = tagresponse['Tags'] if "Tags" in tagresponse else []

    # Create the new workload to copy into
    toWorkloadId,toWorkloadARN = CreateNewWorkload(WACLIENT_TO,
    (workloadJson['WorkloadName']),
    workloadJson['Description'],
    workloadJson['ReviewOwner'],
    workloadJson['Environment'],
    workloadJson['AwsRegions'],
    workloadJson['Lenses'],
    tags,
    workloadJson['PillarPriorities'],
    Notes,
    nonAwsRegions,
    architecturalDesign,
    industryType,
    industry,
    accountIds
    )
    logger.info("New workload id: %s (%s)" % (toWorkloadId,toWorkloadARN))

    # Iterate over each lens and copy all of the answers
    for lens in workloadJson['Lenses']:
        logger.info("Retrieving all answers for lens %s" % lens)
        answers = listAllAnswers(WACLIENT,workloadId,lens)
        # Ensure the lens is attached to the new workload
        associateLens(WACLIENT_TO,toWorkloadId,[lens])
        logger.info("Copying answers into new workload for lens %s" % lens)
        for answerCopy in answers:
            notesField = ''
            notesField = getNotesForQuestion(WACLIENT,workloadId,lens,answerCopy['QuestionId'])
            updateAnswersForQuestion(WACLIENT_TO,toWorkloadId,lens,answerCopy['QuestionId'],answerCopy['SelectedChoices'],notesField)

    logger.info("Copy complete - exiting")