o
    !d:                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZmZ e eZdd Zd	d
 Zdd Zdd ZG dd deZdS )    N)CLIOperationCaller)get_policy_arn_suffix)BasicCommand)IAM)	RESOURCES$LIFECYCLE_DEFAULT_ROLE_ASSUME_POLICYPOLICY_ARN_PATTERNRESOURCE_TYPE_SNAPSHOTRESOURCE_TYPE_IMAGEc                 C   s.   | dd  |  dd  d|i}||  |S )NZResponseMetadataZ
RolePolicy)popupdate)create_role_responseget_policy_responseresult r   Ousr/lib/python3.10/site-packages/awscli/customizations/dlm/createdefaultrole.py_construct_result   s
   
r   c                 C   s&   |d urt | }|||| d S d S N)r   Z_display_response)sessionZoperation_namer   parsed_globalsZcli_operation_callerr   r   r   display_response'   s   r   c                 C   s   t | }t||}|S r   )r   r   format)regionZpolicy_nameZregion_suffixZrole_arnr   r   r   get_policy_arn1   s   r   c                 C   s   |j }|d u r| d}|S )Nr   )r   Zget_config_variable)r   r   r   r   r   r   
get_region8   s   
r   c                       s\   e Zd ZdZdZdddddeeegdeeef d	gZ fd
dZdd Z	dd Z
  ZS )CreateDefaultRolezcreate-default-rolezCreates the default IAM role  which will be used by Lifecycle service.
If the role does not exist, create-default-role will automatically create it and set its policy. If the role has been already created, create-default-role will not update its policy.
ziam-endpointTz<p>The IAM endpoint to call for creating the roles. This is optional and should only be specified when a custom endpoint should be called for IAM operations.</p>)nameZno_paramfile	help_textzresource-typez<p>The resource type for which the role needs to be created. The available options are '%s' and '%s'. This parameter defaults to '%s'.</p>)r   defaultchoicesr   c                    s   t t| | d S r   )superr   __init__)selfr   	__class__r   r   r!   \   s   zCreateDefaultRole.__init__c                 C   s\   t | j|| _|j| _|j| _t| jjd| j| j|j	d| _
| |}t| jd|| dS )zCall to run the commandsZiam)Zregion_nameZendpoint_urlZverifyZcreate_roler   )r   _sessionZ_regionZiam_endpointZ_endpoint_urlZresource_type_resource_typer   Zcreate_clientZ
verify_ssl_iam_client"_create_default_role_if_not_existsr   )r"   Zparsed_argsr   r   r   r   r   	_run_main_   s"   

zCreateDefaultRole._run_mainc                 C   s   t | j d }t}| j|rtd| dS td| t| j|}|du r+t	dt
|t | j d }| j|sCtd| dS td| | j||}| j|| | j|}t||S )	zXMethod to create default lifecycle role
            if it doesn't exist already
        Zdefault_role_namezRole %s existsNz;Role %s does not exist. Creating default role for LifecyclezYYou must specify a region. You can also configure your region by running "aws configure".Zdefault_policy_namez!Managed Policy %s does not exist.zManaged Policy %s exists.)r   r&   r   r'   Zcheck_if_role_existsLOGdebugr   r%   
ValueErrorr   Zcheck_if_policy_existsZcreate_role_with_trust_policyZattach_policy_to_roleZ
get_policyr   )r"   r   Z	role_nameZassume_role_policyr   Zmanaged_policy_arnr   r   r   r   r   r(   w   s<   
z4CreateDefaultRole._create_default_role_if_not_exists)__name__
__module____qualname__NAMEZDESCRIPTIONr	   r
   Z	ARG_TABLEr!   r)   r(   __classcell__r   r   r#   r   r   ?   s(    	r   )loggingZawscli.clidriverr   Zawscli.customizations.utilsr   Zawscli.customizations.commandsr   Zawscli.customizations.dlm.iamr   Z#awscli.customizations.dlm.constantsr   r   r   r	   r
   	getLoggerr-   r*   r   r   r   r   r   r   r   r   r   <module>   s   
	
