in static/watool/utilities/Code/exportImportWAFR.py [0:0]
def main():
""" Main program run """
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))
sys.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,
)
# This will setup a blank dict we can use to export to a JSON file
exportObject = {
"workload": [],
"lenses": [],
"lens_review": [],
}
if exportWorkload:
logger.info("Exporting workload '%s' to file %s" % (WORKLOADID, FILENAME))
workloadJson = GetWorkload(WACLIENT,WORKLOADID)
exportObject['workload'].append(workloadJson)
# Iterate over each lens and copy all of the answers
for lens in workloadJson['Lenses']:
logger.info("Gathering overall review for lens %s" % lens)
lensReview = getWorkloadLensReview(WACLIENT,WORKLOADID,lens)
exportObject['lens_review'].append({lens: lensReview})
logger.info("Retrieving all answers for lens %s" % lens)
answers = listAllAnswers(WACLIENT,WORKLOADID,lens)
exportObject['lenses'].append({lens: answers})
with open(FILENAME, 'w') as outfile:
json.dump(exportObject, outfile, indent=4, cls=DateTimeEncoder)
logger.info("Export completed to file %s" % FILENAME)
if importWorkload:
logger.info("Creating a new workload from file %s" % FILENAME)
with open(FILENAME) as json_file:
importObject = json.load(json_file)
workloadJson = importObject['workload'][0]
# 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 []
tags = workloadJson['Tags'] if "Tags" in workloadJson else []
# Create the new workload to copy into
toWorkloadId,toWorkloadARN = CreateNewWorkload(WACLIENT,
(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']:
# We need to verify the lens version first
logger.info("Verifying lens version before restoring answers")
lensReview = getWorkloadLensReview(WACLIENT,toWorkloadId,lens)
importLensVersion = jmespath.search("[*]."+lens+".LensVersion", importObject['lens_review'])[0]
# ************************************************************************
# There is no ability to restore to a specific lens version
# in the API at this time, so we just have to error out if
# the version has changed.
# ************************************************************************
if lensReview['LensVersion'] != importLensVersion:
logger.error("Version of the lens %s does not match the new workload" % lens)
logger.error("Import Version: %s" % importLensVersion)
logger.error("New Workload Version: %s" % lensReview['LensVersion'])
logger.error("You may need to delete the workload %s" % toWorkloadId)
sys.exit()
else:
logger.info("Versions match (%s)" % importLensVersion)
logger.info("Retrieving all answers for lens %s" % lens)
answers = jmespath.search("[*]."+lens+"[]", importObject['lenses'])
associateLens(WACLIENT,toWorkloadId,[lens])
logger.info("Copying answers into new workload for lens %s" % lens)
for answerCopy in answers:
notesField = answerCopy['Notes'] if "Notes" in answerCopy else ""
updateAnswersForQuestion(WACLIENT,toWorkloadId,lens,answerCopy['QuestionId'],answerCopy['SelectedChoices'],notesField)
logger.info("Copy complete - exiting")