import json import logging import urllib3 import threading import boto3 import time import random import os localtime = time.asctime( time.localtime(time.time()) ).replace(" "," ").replace(" ","-").replace(":","-") http = urllib3.PoolManager() appr_client = boto3.client('servicecatalog-appregistry') s3client = boto3.client('s3') #logger = logging.getLogger() def cfnresponse(event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False): responseBody = {} responseBody['Status'] = responseStatus responseBody['Reason'] = 'See the details in CloudWatch Log Stream: ' + context.log_stream_name responseBody['PhysicalResourceId'] = physicalResourceId or context.log_stream_name responseBody['StackId'] = event['StackId'] responseBody['RequestId'] = event['RequestId'] responseBody['LogicalResourceId'] = event['LogicalResourceId'] responseBody['NoEcho'] = noEcho responseBody['Data'] = responseData json_responseBody = json.dumps(responseBody) headers = {'content-type' : '','content-length' : str(len(json_responseBody))} try: response = http.request('PUT',event['ResponseURL'],body=json_responseBody.encode('utf-8'),headers=headers) logger.debug('Status code: ' + response.reason) except Exception as e: logger.error('cfnresponse(..) failed executing requests.put(..): ' + str(e)) def timeout(event, context): logging.error('Execution is about to time out, sending failure response to CloudFormation') cfnresponse(event, context, 'FAILED',{}) def cr_application(eapp): rand = random.randint(130, 2400) clientToken = f'ctok{rand}' rep = appr_client.create_application(name=eapp['name'],description=eapp['description'],clientToken=clientToken) return rep def cr_attributegroup(eapp): rand = random.randint(130, 2400) attributes = json.dumps(eapp['attributes']) clientToken = f'ctok{rand}' response = appr_client.create_attribute_group(name=eapp['name'],description=eapp['description'], attributes=attributes,clientToken=clientToken ) def cr_attributegroup(eapp): rand = random.randint(130, 2400) attributes = json.dumps(eapp['attributes']) clientToken = f'ctok{rand}' response = appr_client.create_attribute_group(name=eapp['name'],description=eapp['description'], attributes=attributes,clientToken=clientToken ) def asso_attributegroup(eapp): rand = random.randint(130, 2400) response = appr_client.associate_attribute_group(application=eapp['application'],attributeGroup=eapp['attributegroup']) return response def asso_app_stack(eapp): response = appr_client.associate_resource(application=eapp['name'],resourceType='CFN_STACK',resource=eapp['cfn_name']) def l_stack_res(stackid): client = boto3.client('cloudformation') s_dec = client.list_stack_resources(StackName=stackid) #print(s_dec) #return 'a' buf=[] for l in s_dec['StackResourceSummaries']: #buf = buf + "%s-%s-%s\n" % (l['LogicalResourceId'],l['ResourceType'],l['PhysicalResourceId']) if l['PhysicalResourceId'].find('$LATEST') < 0: buf.append(l['ResourceType']) buf = list(dict.fromkeys(buf)) return buf def killEmAll(usure): if usure == 'YeS': print('CleaningUp') myapps = appr_client.list_applications() for app in myapps['applications']: #if app['name'].find('CC_Recommendation_App') >-1 : appid = app['id'] appgroups = appr_client.list_associated_attribute_groups(application=appid) #print(app['name']) for agrp in appgroups['attributeGroups']: #print('\t'+agrp) appr_client.disassociate_attribute_group(application=appid, attributeGroup=agrp) appr_client.delete_attribute_group(attributeGroup=agrp) asores = appr_client.list_associated_resources(application=appid) for _asor in asores['resources']: #print('\t'+_asor['name']) appr_client.disassociate_resource(application=appid,resourceType='CFN_STACK',resource=_asor['arn']) evntdel={'applicationId':app['id']} del_application(evntdel) def listAll(eapps): print('Listing....') myapps = appr_client.list_applications() for app in myapps['applications']: appid = app['id'] appgroups = appr_client.list_associated_attribute_groups(application=appid) print(app['name']) for agrp in appgroups['attributeGroups']: print('\t'+agrp) asores = appr_client.list_associated_resources(application=appid) for _asor in asores['resources']: print(_asor) print('\tres-'+_asor['name']) def del_application(eapp): response = appr_client.delete_application(application=eapp['applicationId']) return response def loadfromJson(event): _ret = {} print('Loading...') for eapp in event['application']: _ret[eapp['name']]=cr_application(eapp) for acfn in eapp['cfns']: _ret = l_stack_res(acfn) _abname = f'STACK_{acfn}' _abdescription = f'Description STACK_{acfn}' evnt={'application':eapp['name'],'name':_abname,'description':_abdescription,'attributes':_ret} _r = cr_attributegroup(evnt) evnt['attributegroup'] = _abname _rr = asso_attributegroup(evnt) evstack={'name':eapp['name'],'cfn_name':acfn} _rrr = asso_app_stack(evstack) _ret = evnt for eapp in event['attribute-groups']: _ret[eapp['name']]=cr_attributegroup(eapp) evnt={'application':eapp['application'],'attributegroup':eapp['name']} _ret[eapp['name']] = asso_attributegroup(evnt) x=42 def lambda_handler(event, context): #print(json.dumps(event)) killEmAll('YeS') #_r =l_stack_res('SCloftlab') #return _r _ret ={} #listAll('s') #return '2' loadfromJson(event) listAll('s') return _ret