o
    .&ßaT  ã                   @   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-bucketTzàThe 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ÚrequiredÚ	help_textz	sns-topiczÔThe 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-rolez¥The 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   úO/usr/lib/python3/dist-packages/awscli/customizations/configservice/subscribe.pyÚregister_subscribe*   ó   r   c                 K   s   t |ƒ| d< d S )NÚ	subscribe)ÚSubscribeCommand)Ú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   zÕSubcribes 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   ÚroleARN)ÚConfigurationRecorder)r   Ús3BucketNameÚsnsTopicARNÚs3KeyPrefix)ÚDeliveryChannel)ÚConfigurationRecorderNamezSubscribe succeeded:

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

zDelivery Channels: ÚDeliveryChannelsÚ
r   )Ú_setup_clientsÚS3BucketHelperr   Úprepare_bucketÚ	s3_bucketÚSNSTopicHelperr   Úprepare_topicÚ	sns_topicr   Úput_configuration_recorderÚiam_roleÚput_delivery_channelÚstart_configuration_recorderÚsysÚstdoutÚwriteÚ describe_configuration_recordersÚjsonÚdumpsÚdescribe_delivery_channels)r   Úparsed_argsÚparsed_globalsÚs3_bucket_helperÚbucketÚprefixÚsns_topic_helperÚsns_topic_arnr   Ú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)ÚverifyÚregion_nameÚs3ÚsnsÚendpoint_urlÚconfig)rH   )rI   )rK   )Ú
verify_sslÚregionÚ_sessionÚcreate_clientr   r   rJ   r   )r   r=   Úclient_argsr   r   r   r*   x   s   þ

ÿzSubscribeCommand._setup_clients)Ú__name__Ú
__module__Ú__qualname__ÚNAMEÚDESCRIPTIONÚ	S3_BUCKETÚ	SNS_TOPICÚIAM_ROLEÚ	ARG_TABLEr   rE   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   Ú	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_bucketr5   r6   r7   )r   Ús3_pathr?   ÚkeyÚ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 )NÚBucketÚLocationConstraintz	us-east-1ÚCreateBucketConfigurationr   )r   ÚmetarG   Úcreate_bucket)r   r?   rG   ÚparamsÚbucket_configr   r   r   r_   –   s   
ÿzS3BucketHelper._create_bucketN)rQ   rR   rS   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   Ú
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)ÚNameÚTopicArnzUsing new SNS topic: %s
zUsing existing SNS topic: %s
)Ú_check_is_arnr   Úcreate_topicr5   r6   r7   )r   r0   rB   rD   r   r   r   r/   ¥   s   
ÿzSNSTopicHelper.prepare_topicc                 C   s   d|v S )Nú:r   )r   r0   r   r   r   rm   °   s   zSNSTopicHelper._check_is_arnN)rQ   rR   rS   r   r/   rm   r   r   r   r   r.   ¡   s    r.   )r9   r5   Úawscli.customizations.commandsr   Úawscli.customizations.utilsr   Úawscli.customizations.s3.utilsr   rV   rW   rX   r   r	   r   Úobjectr+   r.   r   r   r   r   Ú<module>   s&   ÿÿÿS