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 gen_surl(bucketname, keyname): url = s3client.generate_presigned_url(ClientMethod='get_object', Params={ 'Bucket': bucketname, 'Key': keyname}) return url def b_putpriv_v1(bucket, key, acl): srep = s3client.put_object_acl(ACL=acl, Bucket=bucket, Key=key) #print(srep) return srep def b_putpriv(bucket, key, ContentType,body): srep = s3client.put_object( ACL='private', Body=body, Bucket=bucket, Key=key, ContentType=ContentType,) #print(srep) return srep def b_get_obj(bucket, bkey): object = s3client.get_object(Bucket=bucket, Key=bkey) return(object['Body'].read().decode('utf-8')) def lgetattgrp(event,attgrp): rep = appr_client.get_attribute_group(attributeGroup=attgrp) jatt={} try: jatt = json.dumps(rep['attributes']) #print(jatt) except Exception as e: h=1 #print(rep['attributes']) out = {'id':attgrp,'name':rep['name'],'description':rep['description'],'attributes':rep['attributes']} return out def lassocappgroup(event,applicationid): rep = appr_client.list_associated_attribute_groups(application=applicationid) return rep['attributeGroups'] def lapplications(event): res = appr_client.list_applications() apps=[] for app in res['applications']: attgrp=lassocappgroup(event,app['id']) ag=[] for ap in attgrp: ag.append(lgetattgrp(event,ap)) apps.append({'id':app['id'],'name':app['name'],'description':app['description'],'attributeGroups':ag}) return {'applications':apps} def l_mkneptunecsv(event,apregjson): o_app='"~id",name,description,~label\n' o_appgroup ='"~id",name,description,~label\n' o_attb ='"~id",name,~label\n' o_c_attb ='"~id",~label,~from,~to\n' o_cbine = '"~id",~label,~from,~to\n' bucket = event['ResourceProperties']['bucket'] prefix = event['ResourceProperties']['Prefix'] appId=1 atb=1 Atgrpid= len(apregjson['applications']) +1 atb=Atgrpid +200 for app in apregjson['applications']: o_app += f"{app['id']},{app['name']},{app['description']},Application\n" for attribgroup in app['attributeGroups']: o_appgroup += f"{attribgroup['id']},{attribgroup['name']},{attribgroup['description']},Attributegroups\n" o_cbine += f"App{appId}Attg{Atgrpid},Attgroup,{app['id']},{attribgroup['id']}\n" if 'attributes' in attribgroup: ajson = attribgroup['attributes'].replace('AWS::','').replace('{','').replace('}','').replace('[','').replace(']','').replace('"','').split(',') atb_count=1 for _at in ajson: ##_at = _at.replace(' ','').replace(':',' ') _at = _at.replace(' ','') o_attb += f"{atb},{_at},Attributes\n" o_c_attb += f"{atb}{atb_count},Jattributes,{attribgroup['id']},{atb}\n" atb =atb +1 atb_count = atb_count +1 Atgrpid = Atgrpid+1 appId = appId+1 print(o_attb) Applicationkey = f'{prefix}Application.csv' Attgroupkey = f'{prefix}Attgroup.csv' ApplicationAttgroupappskey = f'{prefix}Application_Attgroup_Application_edges.csv' Attribute2attributekey = f'{prefix}Attributegroup_attribute_Attributegroup_edges.csv' _dataloadkey = f'{prefix}outloadme.sh' _justfile = _dataloadkey.split('/')[2] Attbkey = f'{prefix}Attributes.csv' b_putpriv(bucket, Applicationkey, 'text/text',o_app) b_putpriv(bucket, Attgroupkey, 'text/text',o_appgroup) b_putpriv(bucket, ApplicationAttgroupappskey, 'text/text',o_cbine) b_putpriv(bucket, Attbkey, 'text/text',o_attb) b_putpriv(bucket, Attribute2attributekey, 'text/text',o_c_attb) mes = f'{Applicationkey},{Attgroupkey},{ApplicationAttgroupappskey} created in {bucket}' neplkey= f'{prefix}neptune_load.sh' region = os.environ['AWS_REGION'] _nepload = b_get_obj(bucket,neplkey) _Attbkeyload = _nepload.replace('_s3','s3://'+bucket+'/'+Attbkey).replace('_region',region).replace('_rolearn',event['ResourceProperties']['iamRoleArn']).replace('_nependpoint','https://'+event['ResourceProperties']['endpoint']) _appload = _nepload.replace('_s3','s3://'+bucket+'/'+Applicationkey).replace('_region',region).replace('_rolearn',event['ResourceProperties']['iamRoleArn']).replace('_nependpoint','https://'+event['ResourceProperties']['endpoint']) _Attgroupload = _nepload.replace('_s3','s3://'+bucket+'/'+Attgroupkey).replace('_region',region).replace('_rolearn',event['ResourceProperties']['iamRoleArn']).replace('_nependpoint','https://'+event['ResourceProperties']['endpoint']) _ApplicationAttgrload = _nepload.replace('_s3','s3://'+bucket+'/'+ApplicationAttgroupappskey).replace('_region',region).replace('_rolearn',event['ResourceProperties']['iamRoleArn']).replace('_nependpoint','https://'+event['ResourceProperties']['endpoint']) _Attgrattrbload = _nepload.replace('_s3','s3://'+bucket+'/'+Attribute2attributekey).replace('_region',region).replace('_rolearn',event['ResourceProperties']['iamRoleArn']).replace('_nependpoint','https://'+event['ResourceProperties']['endpoint']) _neploadsh = f'#!/bin/bash -x\n{_Attbkeyload}{_appload}{_Attgroupload}{_ApplicationAttgrload}{_Attgrattrbload}' b_putpriv(bucket, _dataloadkey, 'text/text',_neploadsh) _loads3cp = f'aws s3 cp s3://{bucket}/{_dataloadkey} {_justfile}\n' _loads3cp += f'chmod u+x {_justfile}\n' _loads3cp += f'./{_justfile}\n' print(' ') print(_loads3cp) print(' ') return {'mes':_loads3cp,'_loads3cp':_loads3cp} def S3updateobjectsPub(event): Bucket = event['bucket'] Prefix = event['prefix'] objs = s3client.list_objects(Bucket=Bucket,Delimiter=',',Prefix=Prefix) objects=[] for _key in objs['Contents']: objects.append(_key['Key']) b_putpriv_v1(Bucket, _key['Key'], 'public-read') return objects def updateweb(event): bucket = event['ResourceProperties']['S3Bucket'] _api = event['ResourceProperties']['req_apistage'] _visreportkey = f"content/appreg/report/visreport.html" _visreporttext = b_get_obj(bucket, _visreportkey) _visreporttext = _visreporttext.replace('__api__',_api) b_putpriv(bucket, _visreportkey, 'text/html',_visreporttext) print('setting to pup') prefix = f"content/appreg/report/" oevent ={'bucket':bucket,'prefix':prefix} objs = S3updateobjectsPub(oevent) def lambda_handler(event, context): print(json.dumps(event)) _ret={} _ret['Message']='Loading..' timer = threading.Timer((context.get_remaining_time_in_millis() / 1000.00) - 0.5, timeout, args=[event, context]) timer.start() status = 'SUCCESS' try: if event['RequestType'] == 'Create': if event['ResourceProperties']['InitialSetup'] == 'yes': pubobjs = updateweb(event) print('create step') _ret['Message'] = 'Setting up' except Exception as e: logging.error('Exception: %s' % e, exc_info=True) status = 'FAILED' finally: timer.cancel() print(json.dumps(_ret)) cfnresponse(event, context, status,_ret) return _ret