in functions/source/register/register.py [0:0]
def stackset_check(messages):
cloudFormationClient = session.client('cloudformation')
sqsClient = session.client('sqs')
snsClient = session.client('sns')
newRelicRegisterSNS = os.environ['newRelicRegisterSNS']
newRelicDLQ = os.environ['newRelicDLQ']
for stackSetName, params in messages.items():
logger.info("Checking stack set instances: {} {}".format(stackSetName, params['OperationId']))
try:
stackset_status = cloudFormationClient.describe_stack_set_operation(
StackSetName=stackSetName,
OperationId=params['OperationId']
)
if 'StackSetOperation' in stackset_status:
if stackset_status['StackSetOperation']['Status'] in ['RUNNING','STOPPING','QUEUED',]:
logger.info("Stackset operation still running")
messageBody = {}
messageBody[stackSetName] = {'OperationId': params['OperationId']}
try:
logger.info("Sleep and wait for 20 seconds")
time.sleep(20)
snsResponse = snsClient.publish(
TopicArn=newRelicRegisterSNS,
Message = json.dumps(messageBody))
logger.info("Re-queued for registration: {}".format(snsResponse))
except Exception as snsException:
logger.error("Failed to send queue for registration: {}".format(snsException))
elif stackset_status['StackSetOperation']['Status'] in ['SUCCEEDED']:
logger.info("Start registration")
cloudFormationPaginator = cloudFormationClient.get_paginator('list_stack_set_operation_results')
stackset_iterator = cloudFormationPaginator.paginate(
StackSetName=stackSetName,
OperationId=params['OperationId']
)
newRelicSecret = os.environ['newRelicSecret']
newRelicAccId = os.environ['newRelicAccId']
newRelicAccessKey = get_secret_value(newRelicSecret)
newRelicIntegrationList = newrelic_get_schema(newRelicAccessKey)
if newRelicAccessKey:
for page in stackset_iterator:
if 'Summaries' in page:
for operation in page['Summaries']:
if operation['Status'] in ('SUCCEEDED'):
targetAccount = operation['Account']
newrelic_registration(targetAccount, newRelicAccessKey, newRelicAccId, newRelicIntegrationList)
elif stackset_status['StackSetOperation']['Status'] in ['FAILED','STOPPED']:
logger.warning("Stackset operation failed/stopped")
messageBody = {}
messageBody[stackSetName] = {'OperationId': params['OperationId']}
try:
sqsResponse = sqsClient.send_message(
QueueUrl=newRelicDLQ,
MessageBody=json.dumps(messageBody))
logger.info("Sent to DLQ: {}".format(sqsResponse))
except Exception as sqsException:
logger.error("Failed to send to DLQ: {}".format(sqsException))
except Exception as e:
logger.error(e)