3 =\@sZddlZddlZddlZddlmZddlmZddlmZddl m Z GdddZ dS)N) ClientError)PrintMsg) CommonTools)CfnResourceToolsc@s4eZdZddZddZddZddZd d Zd S) CfnLogToolscCs ||_dS)N) _boto_client)selfZ boto_clientr m/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/taskcat/taskcat/cfn_logutils.py__init__ szCfnLogTools.__init__cCs|jjd|}g}yL|j|d}|j|dx,d|krZ|j|d|d}|j|dq0WWn@tk r}z$tdjtjt |t ||WYdd}~XnX|S)a Given a stack name and the region, this function returns the event logs of the given stack, as list. :param self: :param stackname: Name of the stack :param region: Region stack belongs to :return: Event logs of the stack Zcloudformation) StackNameZ StackEvents NextToken)r r zC{} Error trying to get the events for stack [{}] in region [{}] {}N) rgetZdescribe_stack_eventsextendrprintformatrERRORstr)r stacknameregionZ cfn_client stack_eventsresponseer r r get_cfn_stack_eventss  z CfnLogTools.get_cfn_stack_eventscCs~ttjd|d|j||}g}xR|D]J}|d|d|d|dd}d|krd|d|d<nd |d<|j|q,W|S) z This function returns the event logs of the given stack in a specific format. :param stackname: Name of the stack :param region: Region stack belongs to :return: Event logs of the stack zCollecting logs for z" Z TimestampResourceStatus ResourceTypeLogicalResourceId)Z TimeStamprrrResourceStatusReason)rrINFOrappend)rrrreventseventZ event_detailsr r r get_cfnlogs+s   zCfnLogTools.get_cfnlogsc Cstdjtjxp|D]h}xb|jD]V}t|dj}t|d}t|d}d}dj|||d|} |jt|d| q$WqWdS) z This function creates the CloudFormation log files. :param testdata_list: List of TestData objects :param logpath: Log file path :return: z {}Collecting CloudFormation LogsZStackId stack_namerz.txtz {}/{}-{}-{}{}cfnlogsN) rrrrZget_test_stacksrparse_stack_infor write_logs) rZ testdata_listlogpathteststack stackinforr extensionZ test_logpathr r r createcfnlogsGs   zCfnLogTools.createcfnlogsc Cst|j}t|d}t|d}|j||}t|dkr |dddkrnd|dkrh|dd}qrd}nd}td |td |td |td ttjjjd tdtdtt j t|dtdt |d}|j d|j d|d|j d|d|j d|j d|j t j t|dd|j d|j d|j d|j tj|dd|j d|j d|j dtjjjd d|j d|jWdQRXt|jj||d d!} x<| D]$} | d"d#kr|j| d$|qWnttjd%dS)&z This function writes the event logs of the given stack and all the child stacks to a given file. :param stack_id: Stack Id :param logpath: Log file path :return: r$rrrZCREATE_COMPLETErUnknownzStack launch was successfulz |StackName: z |Region: z |Logging to: z |Tested on: z%A, %d. %B %Y %I:%M%pzZ------------------------------------------------------------------------------------------zResourceStatusReason: UzZ==========================================================================================azN----------------------------------------------------------------------------- zRegion:  z StackName: zN***************************************************************************** zResourceStatusReason: z Events: keys)headerszO ***************************************************************************** z Tested on: zO----------------------------------------------------------------------------- NT)Zinclude_stacksZ resourceTypezAWS::CloudFormation::StackZ physicalIdzBNo event logs found. Something went wrong at describe event call. )rr&rr#lenrdatetimenowstrftimetextwrapfillopenwrite writelinestabulatecloserr get_resourcesr'rr) rZstack_idr(r+rrr%reasonZ log_output resourcesresourcer r r r'^sV                  zCfnLogTools.write_logsN)__name__ __module__ __qualname__r rr#r-r'r r r r r s r) r5r8r=Zbotocore.exceptionsrZtaskcat.colored_consolerZtaskcat.common_utilsrZtaskcat.cfn_resourcesrrr r r r s