B t…ƒ]³0ã@s’dZddlmZddlZddlmZddlmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlmZe e¡ZdZdZGd d „d eƒZdS) uT TODO: * Async mode – take a wait condition handle as an input, increases max timeout to 12 hours * Idempotency – If a duplicate request comes in (say there was a network error in signaling back to cfn) the subsequent request should return the already created response, will need a persistent store of some kind... * Functional tests é)Úprint_functionN)Ú_send_response)Ú log_helper)ÚsleepÚSUCCESSÚFAILEDc@seZdZdCdd„Zdd„Zefd d „Zd d „Zd d„Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*efd+d,„Zd-d.„Zd/d0„Zed1d2„ƒZd3d4„Zd5d6„Zd7d8„Zd9d:„Zd;d<„Z d=d>„Z!d?d@„Z"dAdB„Z#d)S)DÚ CfnResourceFÚDEBUGÚERRORéc CsJd|_d|_d|_d|_d|_d|_d|_d|_||_||_ ||_ d|_ ||_ d|_ d|_d|_d|_d|_d|_i|_i|_d|_d|_t d¡|_t d¡|_yf|jsàtjd|jd|_tjd|jd|_tjd|jd|_|röt j!||d d nt j!|d|d Wn<t"k rD}zt#j$|d d | %|¡Wdd}~XYnXdS)NFÚZ AWS_SAM_LOCALZ AWS_REGIONÚlambda)Z region_nameÚeventsZlogsZ ContainerInit)Ú boto_levelÚ RequestType)Ú formatter_clsrT)Úexc_info)&Ú _create_funcÚ _update_funcÚ _delete_funcÚ_poll_create_funcÚ_poll_update_funcÚ_poll_delete_funcÚ_timerÚ _init_failedÚ _json_loggingÚ _log_levelÚ _boto_levelrÚ_polling_intervalÚStatusÚReasonÚPhysicalResourceIdÚStackIdÚ RequestIdÚLogicalResourceIdÚDataÚ_eventÚ_contextÚ _response_urlÚosÚgetenvÚ _sam_localZ_regionÚboto3ÚclientÚ_lambda_clientÚ_events_clientZ _logs_clientrÚsetupÚ ExceptionÚloggerÚerrorÚ init_failure)ÚselfZ json_loggingZ log_levelrZpolling_intervalÚe©r7úr%Ú_setup_pollingÚ_remove_pollingr)r5rEr7r7r8r?„s   zCfnResource._polling_initcCs’|js(d| ¡kr(t d¡|d|_n^|jr8|jdkr†d| ¡krNt d¡t d¡|d d¡dd|d d| d ¡|_| ¡dS) Nr!zÚsplitÚ _rand_stringrB)r5rEr7r7r8rA”s     .zCfnResource._cfn_responsecCst|d |jd ¡¡ƒS)Nz _poll_{}_funcr)ÚgetattrÚformatr&Úlower)r5r7r7r8r=¢szCfnResource._poll_enabledcCs ||_|S)N)r)r5Úfuncr7r7r8Úcreate¥szCfnResource.createcCs ||_|S)N)r)r5r^r7r7r8Úupdate©szCfnResource.updatecCs ||_|S)N)r)r5r^r7r7r8Údelete­szCfnResource.deletecCs ||_|S)N)r)r5r^r7r7r8Ú poll_create±szCfnResource.poll_createcCs ||_|S)N)r)r5r^r7r7r8Ú poll_updateµszCfnResource.poll_updatecCs ||_|S)N)r)r5r^r7r7r8Ú poll_delete¹szCfnResource.poll_deletec Csfy|r||j|jƒnd|_WnDtk r`}z&tjt|ƒddt|ƒ|_t|_ Wdd}~XYnXdS)Nr T)r) r&r'r!r1r2r3rCr rr)r5r^r6r7r7r8rQ½s  zCfnResource._wrap_functioncCst d¡| td¡dS)Nz7Execution is about to time out, sending failure messagezExecution timed out)r2r3rBr)r5r7r7r8Ú_timeoutÅs zCfnResource._timeoutcCs,t |j ¡dd|j¡|_|j ¡dS)Ng@@gà?)Ú threadingÚTimerr'rLrerÚstart)r5r7r7r8rPÉs zCfnResource._set_timeoutcCs4d}d|j ¡krd|}t|| |jd ¡¡ƒS)Nz_{}_funcrSÚ_pollr)r&rOr[r\r])r5Z request_typer7r7r8rRÎszCfnResource._get_funcNr cCsºttt|jƒƒƒdkrñsz+CfnResource._rand_string..)ÚjoinÚrange)Úlr7r7r8rZïszCfnResource._rand_stringcCs2|jd| d¡}|jj|jj|dd|d|S)Nr$rXzlambda:InvokeFunctionzevents.amazonaws.com)Ú FunctionNameÚ StatementIdZActionZ PrincipalZ SourceArn)r&rZr.Zadd_permissionr'Ú function_name)r5Úrule_arnÚsidr7r7r8Ú_add_permissionószCfnResource._add_permissioncCs2|jj|jd| d¡d |j¡dd}|dS)Nr$rXzrate({} minutes)ZENABLED)ÚNameZScheduleExpressionZStateZRuleArn)r/Zput_ruler&rZr\r)r5Úresponser7r7r8Ú _put_ruleþs  zCfnResource._put_rulecCsŽ|jd d¡d}|jd d¡d}|jd d¡d}|jd d¡d}t |j¡|jj|dd||||ft |j¡d œgd dS) Nrpú:éérJrVÚ1zarn:%s:lambda:%s:%s:function:%s)ZIdZArnZInput)ÚRuleZTargets)r&rYr2r;r/Z put_targetsÚjsonÚdumps)r5Ú func_nameZregionZ account_idÚ partitionZ rule_namer7r7r8Ú _put_targetss zCfnResource._put_targetscCs |jj| d¡ddgddS)NrVrJr‰)rŠZIds)r/Zremove_targetsrY)r5r€r7r7r8Ú_remove_targetss zCfnResource._remove_targetscCs|jj|jj|ddS)N)r}r~)r.Zremove_permissionr'r)r5rr7r7r8Ú_remove_permissionszCfnResource._remove_permissioncCs|jj| d¡dddS)NrVrJ)rƒ)r/Z delete_rulerY)r5r€r7r7r8Ú _delete_rule#szCfnResource._delete_rulecCsL|j|jd<d|jd<| ¡|jd<| |jd¡|jd<| |jj¡dS)NrNTrSrpro)r%r&r…r‚rr'r)r5r7r7r8rT(s   zCfnResource._setup_pollingcCs¶d|j ¡kr|j d¡d|j ¡kr4|j d¡d|j ¡krT| |jd¡n t d¡d|j ¡kr~| |jd¡n t d¡d|j ¡kr¨| |jd¡n t d¡dS)NrNr!rpzECannot remove CloudWatch events rule, Rule arn not available in eventrozLCannot remove lambda events permission, permission id not available in eventzGCannot remove CloudWatch events target, Rule arn not available in event) r&rOÚpopr%rr2r3r‘r’)r5r7r7r8rU/s    zCfnResource._remove_polling)Fr r r )$Ú__name__Ú __module__Ú __qualname__r9rGrr@r:r<r?rAr=r_r`rarbrcrdrQrerPrRrrBr4rrÚ staticmethodrZr‚r…rrr‘r’rTrUr7r7r7r8rs< '   r)Ú__doc__Ú __future__rrfZcrhelper.utilsrZcrhelperrÚloggingrsr,rur‹r)ÚtimerÚ getLoggerr”r2rrÚobjectrr7r7r7r8Ús