o
    .&ßa=  ã                   @   sH   d dl Z d dlmZ d dlmZ d dlmZmZmZ G dd„ deƒZ	dS )é    N)ÚClientError)ÚBasicCommand)Úvalidate_regionÚvalidate_instance_nameÚINSTANCE_NAME_ARGc                   @   s^   e Zd ZdZdZedddddœgZdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )Ú
DeregisterÚ
deregisterz½Removes any tags from the on-premises instance; deregisters the on-premises instance from AWS CodeDeploy; and, unless requested otherwise, deletes the IAM user for the on-premises instance.zno-delete-iam-userÚ
store_trueFzMOptional. Do not delete the IAM user for the registered on-premises instance.)ÚnameÚactionÚdefaultÚ	help_textc              
   C   sì   |}| j |_t||ƒ t|ƒ | j jd|j|j|jd�| _| j jd|jd�| _	z-|  
|¡ |jr5|  |¡ |  |¡ |jsL|  |¡ |  |¡ |  |¡ tj d¡ W d S  tyu } ztj ¡  tj d |¡¡ W Y d }~d S d }~ww )NÚ
codedeploy)Úregion_nameÚendpoint_urlÚverifyÚiam)r   znRun the following command on the on-premises instance to uninstall the codedeploy-agent:
aws deploy uninstall
z´ERROR
{0}
Deregister the on-premises instance by following the instructions in "Configure Existing On-Premises Instances by Using AWS CodeDeploy" in the AWS CodeDeploy User Guide.
)Ú_sessionÚsessionr   r   Úcreate_clientÚregionr   Ú
verify_sslr   r   Ú_get_instance_infoÚtagsÚ_remove_tagsÚ_deregister_instanceÚno_delete_iam_userÚ_delete_user_policyÚ_delete_access_keyÚ_delete_iam_userÚsysÚstdoutÚwriteÚ	ExceptionÚflushÚstderrÚformat)ÚselfÚparsed_argsÚparsed_globalsÚparamsÚe© r,   úM/usr/lib/python3/dist-packages/awscli/customizations/codedeploy/deregister.pyÚ	_run_main-   sD   
üþ






ÿ
ú€þzDeregister._run_mainc                 C   sº   t j d¡ | jj|jd�}|d d |_|j d¡d }|j|d … |_|d d |_	t j d 
|j¡¡ |j	r[t j d	¡ |j	D ]}t j d
 
|d |d ¡¡ qBt j d¡ d S d S )Nz/Retrieving on-premises instance information... ©ÚinstanceNameÚinstanceInfoÚ
iamUserArnú/é   r   zDONE
IamUserArn: {0}
zTags:z Key={0},Value={1}ÚKeyÚValueÚ
)r    r!   r"   r   Úget_on_premises_instanceÚinstance_nameÚiam_user_arnÚrfindÚ	user_namer   r&   )r'   r*   ÚresponseÚstartÚtagr,   r,   r-   r   W   s,   ÿÿþ
ÿúzDeregister._get_instance_infoc                 C   s2   t j d¡ | jj|j|jgd� t j d¡ d S )Nz/Removing tags from the on-premises instance... )r   ÚinstanceNamesúDONE
)r    r!   r"   r   Ú&remove_tags_from_on_premises_instancesr   r9   ©r'   r*   r,   r,   r-   r   n   s   þzDeregister._remove_tagsc                 C   s,   t j d¡ | jj|jd� t j d¡ d S )Nz*Deregistering the on-premises instance... r/   rA   )r    r!   r"   r   Úderegister_on_premises_instancer9   rC   r,   r,   r-   r   v   s
   ÿzDeregister._deregister_instancec              
   C   s¢   t j d¡ | j d¡}z|j|jd�D ]}|d D ]}| jj|j|d� qqW n  tyH } z|j	 
di ¡ 
d¡dkr>|‚W Y d }~nd }~ww t j d	¡ d S )
Nz"Deleting the IAM user policies... Úlist_user_policies©ÚUserNameÚPolicyNames)rG   Ú
PolicyNameÚErrorÚCodeÚNoSuchEntityrA   )r    r!   r"   r   Úget_paginatorÚpaginater<   Údelete_user_policyr   r=   Úget)r'   r*   rE   r=   Úpolicy_namer+   r,   r,   r-   r   }   s(   
ÿþÿþÿ€ÿzDeregister._delete_user_policyc              
   C   s¦   t j d¡ | j d¡}z|j|jd�D ]}|d D ]}| jj|j|d d� qqW n  tyJ } z|j	 
di ¡ 
d¡d	kr@|‚W Y d }~nd }~ww t j d
¡ d S )Nz%Deleting the IAM user access keys... Úlist_access_keysrF   ÚAccessKeyMetadataÚAccessKeyId)rG   rT   rJ   rK   rL   rA   )r    r!   r"   r   rM   rN   r<   Údelete_access_keyr   r=   rP   )r'   r*   rR   r=   Ú
access_keyr+   r,   r,   r-   r   �   s(   
ÿþÿþÿ€ÿzDeregister._delete_access_keyc              
   C   sz   t j d |j¡¡ z
| jj|jd� W n  ty4 } z|j 	di ¡ 	d¡dkr*|‚W Y d }~nd }~ww t j d¡ d S )NzDeleting the IAM user ({0})... rF   rJ   rK   rL   rA   )
r    r!   r"   r&   r<   r   Údelete_userr   r=   rP   )r'   r*   r+   r,   r,   r-   r   �   s   
ÿÿ€ÿzDeregister._delete_iam_userN)Ú__name__Ú
__module__Ú__qualname__ÚNAMEÚDESCRIPTIONr   Ú	ARG_TABLEr.   r   r   r   r   r   r   r,   r,   r,   r-   r      s$    ÿûþ*r   )
r    Úbotocore.exceptionsr   Úawscli.customizations.commandsr   Ú&awscli.customizations.codedeploy.utilsr   r   r   r   r,   r,   r,   r-   Ú<module>   s
   