o
    !dU                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ ddddZd	dd
dZ	ddddZ
dd Zdd ZG dd deZG dd deZG dd deZdS )    N)BasicCommand)s3_bucket_exists)find_bucket_keyz	s3-bucketTzThe S3 bucket that the AWS Config delivery channel will use. If the bucket does not exist, it will be automatically created. The value for this argument should follow the form bucket/prefix. Note that the prefix is optional.)namerequiredZ	help_textz	sns-topiczThe SNS topic that the AWS Config delivery channel will use. If the SNS topic does not exist, it will be automatically created. Value for this should be a valid SNS topic name or the ARN of an existing SNS topic.ziam-rolezThe IAM role that the AWS Config configuration recorder will use to record current resource configurations. Value for this should be the ARN of the desired IAM role.c                 C   s   |  dt d S )Nz$building-command-table.configservice)registeradd_subscribe)cli r
   Qusr/lib/python3.10/site-packages/awscli/customizations/configservice/subscribe.pyregister_subscribe*      r   c                 K   s   t || d< d S )N	subscribe)SubscribeCommand)Zcommand_tablesessionkwargsr
   r
   r   r   .   r   r   c                       s>   e Zd ZdZdZeeegZ fddZ	dd Z
dd Z  ZS )	r   r   zSubscribes user to AWS Config by creating an AWS Config delivery channel and configuration recorder to track AWS resource configurations. The names of the default channel and configuration recorder will be default.c                    s&   d | _ d | _d | _tt| | d S N)
_s3_client_sns_client_config_clientsuperr   __init__)selfr   	__class__r
   r   r   :   s   zSubscribeCommand.__init__c                 C   s  |  | t| j}||j\}}t| j}||j}d}| j	j
||jdd |||d}	|r6||	d< | j	j|	d | j	j|d tjd tjd	 | j	 }
tjtj|
d
 dd tjd tjd | j	 }
tjtj|
d dd tjd dS )Ndefault)r   ZroleARN)ZConfigurationRecorder)r   Zs3BucketNameZsnsTopicARNZs3KeyPrefix)ZDeliveryChannel)ZConfigurationRecorderNamezSubscribe succeeded:

zConfiguration Recorders: ZConfigurationRecorders   )indentz

zDelivery Channels: ZDeliveryChannels
r   )_setup_clientsS3BucketHelperr   prepare_bucketZ	s3_bucketSNSTopicHelperr   prepare_topic	sns_topicr   Zput_configuration_recorderZiam_roleZput_delivery_channelZstart_configuration_recordersysstdoutwriteZ describe_configuration_recordersjsondumpsZdescribe_delivery_channels)r   Zparsed_argsparsed_globalsZs3_bucket_helperbucketprefixZsns_topic_helpersns_topic_arnr   Zdelivery_channelresponser
   r
   r   	_run_main@   sF   


	

zSubscribeCommand._run_mainc                 C   sX   |j |jd}| jjdi || _| jjdi || _|j|d< | jjdi || _d S )	N)Zverifyregion_names3snsendpoint_urlconfig)r1   )r2   )r4   )Z
verify_sslZregionZ_sessionZcreate_clientr   r   r3   r   )r   r*   Zclient_argsr
   r
   r   r   x   s   

zSubscribeCommand._setup_clients)__name__
__module____qualname__NAMEZDESCRIPTION	S3_BUCKET	SNS_TOPICIAM_ROLEZ	ARG_TABLEr   r/   r   __classcell__r
   r
   r   r   r   2   s    
8r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r    c                 C   
   || _ d S r   )r   )r   Z	s3_clientr
   r
   r   r         
zS3BucketHelper.__init__c                 C   sT   t |\}}| |}|s| | tjd|  ||fS tjd|  ||fS )NzUsing new S3 bucket: %s
zUsing existing S3 bucket: %s
)r   _check_bucket_exists_create_bucketr%   r&   r'   )r   Zs3_pathr+   keyZbucket_existsr
   r
   r   r!      s   

zS3BucketHelper.prepare_bucketc                 C   s   t | j|S r   )r   r   )r   r+   r
   r
   r   r?      s   z#S3BucketHelper._check_bucket_existsc                 C   s@   | j jj}d|i}d|i}|dkr||d< | j jdi | d S )NZBucketZLocationConstraintz	us-east-1ZCreateBucketConfigurationr
   )r   metar0   Zcreate_bucket)r   r+   r0   paramsZbucket_configr
   r
   r   r@      s   
zS3BucketHelper._create_bucketN)r5   r6   r7   r   r!   r?   r@   r
   r
   r
   r   r       s
    
r    c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r"   c                 C   r=   r   )r   )r   Z
sns_clientr
   r
   r   r      r>   zSNSTopicHelper.__init__c                 C   sL   |}|  |s| jj|d}|d }tjd|  |S tjd|  |S )N)NameZTopicArnzUsing new SNS topic: %s
zUsing existing SNS topic: %s
)_check_is_arnr   Zcreate_topicr%   r&   r'   )r   r$   r-   r.   r
   r
   r   r#      s   
zSNSTopicHelper.prepare_topicc                 C   s   d|v S )N:r
   )r   r$   r
   r
   r   rE      s   zSNSTopicHelper._check_is_arnN)r5   r6   r7   r   r#   rE   r
   r
   r
   r   r"      s    r"   )r(   r%   Zawscli.customizations.commandsr   Zawscli.customizations.utilsr   Zawscli.customizations.s3.utilsr   r9   r:   r;   r   r   r   objectr    r"   r
   r
   r
   r   <module>   s&   S