def main()

in static/watool/utilities/Code/exportAnswersToXLSX.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 upgrade boto3)" % (boto3.__version__, boto3_min_version))
        exit()

    logger.info("Script version %s" % __version__)
    logger.info("Starting Boto %s Session" % boto3.__version__)
    # Create a new boto3 session
    SESSION1 = boto3.session.Session(profile_name=PROFILE)
    # Initiate the well-architected session using the region defined above
    WACLIENT = SESSION1.client(
        service_name='wellarchitected',
        region_name=REGION_NAME,
    )

    # If this is an existing workload, we need to query for the various workload properties
    if WORKLOADID:
        logger.info("User specified workload id of %s" % WORKLOADID)
        workloadJson = GetWorkload(WACLIENT,WORKLOADID)
        LENSES = workloadJson['Lenses']
        logger.info("Lenses for %s: %s" % (WORKLOADID, json.dumps(LENSES)))
        WORKLOADNAME = workloadJson['WorkloadName']
        DESCRIPTION = workloadJson['Description']
        REVIEWOWNER = workloadJson['ReviewOwner']
        ENVIRONMENT= workloadJson['Environment']
        AWSREGIONS = workloadJson['AwsRegions']
        workloadId = WORKLOADID
        workloadARN = workloadJson['WorkloadArn']
    else:
    # In order to gather all of the questions, you must create a TEMP Workload
        logger.info("No workload ID specified, we will create a TEMP workload")
        # Grab all lenses that are currently available
        LENSES = listLens(WACLIENT)
        logger.info("Lenses available: "+json.dumps(LENSES))
        # Set the needed workload variables before we create it
        WORKLOADNAME = 'TEMP DO NOT USE WORKLOAD'
        DESCRIPTION = 'TEMP DO NOT USE WORKLOAD'
        REVIEWOWNER = 'WA Python Script'
        ENVIRONMENT= 'PRODUCTION'
        AWSREGIONS = [REGION_NAME]
        # Creating the TEMP workload
        logger.info("Creating a new workload to gather questions and answers")
        workloadId, workloadARN = CreateNewWorkload(WACLIENT,WORKLOADNAME,DESCRIPTION,REVIEWOWNER,ENVIRONMENT,AWSREGIONS,LENSES,"[]","[]")



    # Create an new xlsx file and add a worksheet.
    logger.info("Creating xlsx file '"+FILENAME+"'")
    workbook = xlsxwriter.Workbook(FILENAME)
    workbook.set_size(2800, 1600)

    # Simple hack to get Wellarchitected base framework first (reverse sort)
    # This will no longer work if we ever have a lens that starts with WB*, X, Y, or Z :)
    LENSES.sort(reverse=True)

    # Iterate over each lens that we either have added or is in the workload
    for lens in LENSES:
        # Grab all questions for a particular lens
        allQuestions = findAllQuestionId(WACLIENT,workloadId,lens)
        if WORKLOADID:
            # If this is an existing workload, just go ahead and create the Tab and cells
            logger.debug("Not answering questions for existing workload")
            lensTabCreation(WACLIENT,workloadId,lens,workbook,allQuestions,WORKLOADNAME,workloadARN,DESCRIPTION)
        else:
            # If this is the TEMP workload, we need to first gather all of the questionIDs possible
            jmesquery = "[*].{QuestionId: QuestionId, PillarId: PillarId, Choices: Choices[].ChoiceId}"
            allQuestionIds = jmespath.search(jmesquery, allQuestions)
            # Next we answer all of the questions across all lenses in the TEMP workload
            for question in allQuestionIds:
                logger.debug("Answering question %s in the %s lens" % (question['QuestionId'], lens))
                updateAnswersForQuestion(WACLIENT,workloadId,lens,question['QuestionId'],question['Choices'],'TEMP WORKLOAD - Added by export script')
            # Once the questions have been answered, we go ahead and create the tab for each
            lensTabCreation(WACLIENT,workloadId,lens,workbook,allQuestions)


    # Close out the workbook file
    logger.info("Closing Workbook File")
    workbook.close()

    # If this is TEMP workload, we may remove it if it has not been set to keep
    if not WORKLOADID:
        if not KEEPTEMP:
            logger.info("Removing TEMP Workload")
            DeleteWorkload(WACLIENT, workloadId)
    logger.info("Done")