o
    !d&                     @   s   d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
 d dlmZmZmZ d dlmZ e eZdZdZG dd	 d	e
ZdS )
    N)&DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME'DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME%DATAPIPELINE_DEFAULT_SERVICE_ROLE_ARN&DATAPIPELINE_DEFAULT_RESOURCE_ROLE_ARN/DATAPIPELINE_DEFAULT_SERVICE_ROLE_ASSUME_POLICY0DATAPIPELINE_DEFAULT_RESOURCE_ROLE_ASSUME_POLICY)BasicCommand)display_responsedict_to_string
get_region)ClientErrora  
Support for this command has been deprecated and may fail to create these roles
if they do not already exist. For more information on managing these policies
manually see the following documentation:

https://docs.aws.amazon.com/datapipeline/latest/DeveloperGuide/dp-iam-roles.html
a-  
NOTE: {}

Creates the default IAM role "{}" and "{}" which are used while creating an EMR
cluster.

If these roles do not exist, create-default-roles will automatically create
them and set their policies.

If these roles have already been created create-default-roles will not update
their policies.
c                       s   e Zd ZdZdZeeee	Z
d fdd	Zdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )CreateDefaultRoleszcreate-default-rolesTNc                    s   t t| | d S N)superr   __init__)selfZsession	formatter	__class__ Yusr/lib/python3.10/site-packages/awscli/customizations/datapipeline/createdefaultroles.pyr   A   s   zCreateDefaultRoles.__init__c                 K   sH   t | j|| _|j| _| jjd| j| j|jd| _t	t
 | ||S )zCall to run the commandsZiam)Zregion_nameendpoint_urlZverify)r   _sessionZ_regionr   Z_endpoint_urlZcreate_clientZ
verify_ssl_iam_clientwarningswarn_DEPRECATION_NOTICE_create_default_roles)r   parsed_argsparsed_globalskwargsr   r   r   	_run_mainD   s   
zCreateDefaultRoles._run_mainc                 C   sb   d}d}|  |rtd| d  ||fS td| d |  | |||}| |}||fS )z[Method to create a role for a given role name and arn
        if it does not exist
        NzRole  exists.z0 does not exist. Creating default role for EC2: )_check_if_role_existsLOGdebug_create_role_with_role_policy_get_role_policy)r   	role_namerole_arnZrole_policyZrole_resultZrole_policy_resultr   r   r   _create_roleQ   s   


zCreateDefaultRoles._create_rolec                 C   s$   g }|  ||| |  ||| |S )znMethod to create a resultant list of responses for create roles
        for service and resource role
        ))_construct_role_and_role_policy_structure)r   Zdpl_default_resultZdpl_default_policyZdpl_default_res_resultZdpl_default_res_policyresultr   r   r   _construct_resultf   s   z$CreateDefaultRoles._construct_resultc           	      C   s   |  ttt\}}|  ttt\}}t}| |r#t	d| d  nt	d| d |  | 
|| | ||||}t| jd|| dS )NzInstance Profile r"   z2does not exist. Creating default Instance Profile create_roler   )r*   r   r   r   r   r   r   !_check_if_instance_profile_existsr$   r%   "_create_instance_profile_with_roler-   r	   r   )	r   r   r   Zdatapipline_default_resultZdatapipline_default_policyZ#datapipline_default_resource_resultZ#datapipline_default_resource_policyinstance_profile_namer,   r   r   r   r   w   s@   

z(CreateDefaultRoles._create_default_rolesc                 C   s2   | j j|d}| j j||d d d}|d d S )zvMethod to get the Policy for a particular ARN
        This is used to display the policy contents to the user
        )	PolicyArnPolicyZDefaultVersionId)r2   Z	VersionIdZPolicyVersionZDocument)r   Z
get_policyZget_policy_version)r   ZarnZpol_detZpolicy_version_detailsr   r   r   r'      s
   z#CreateDefaultRoles._get_role_policyc                 C   s(   | j j|t|d}| j j||d |S )z]Method to create role with a given rolename, assume_role_policy
        and role_arn
        )RoleNameZAssumeRolePolicyDocument)r2   r4   )r   r.   r
   Zattach_role_policy)r   r(   Zassume_role_policyr)   Zcreate_role_responser   r   r   r&      s   z0CreateDefaultRoles._create_role_with_role_policyc                 C   s4   |dur|d dur| |d |d |S dS dS )z;Method to construct the message to be displayed to the userNRole)r5   Z
RolePolicy)append)r   Zlist_valresponsepolicyr   r   r   r+      s   z<CreateDefaultRoles._construct_role_and_role_policy_structurec              
   C   R   z
| j j|d W dS  ty( } z|jd d dkr"W Y d}~dS |d}~ww ),Method to verify if a particular role existsInstanceProfileNameErrorCodeNoSuchEntityNFT)r   Zget_instance_profiler   r7   )r   r1   er   r   r   r/      s   z4CreateDefaultRoles._check_if_instance_profile_existsc              
   C   r9   )r:   )r4   r=   r>   r?   NFT)r   Zget_roler   r7   )r   r(   r@   r   r   r   r#      s   z(CreateDefaultRoles._check_if_role_existsc                 C   s"   | j j|d | j j||d dS )z3Method to create the instance profile with the roler;   )r<   r4   N)r   Zcreate_instance_profileZadd_role_to_instance_profile)r   r1   r(   r   r   r   r0      s   
z5CreateDefaultRoles._create_instance_profile_with_roler   )__name__
__module____qualname__NAMEZ_UNDOCUMENTED_DESCRIPTIONformatr   r   r   ZDESCRIPTIONr   r!   r*   r-   r   r'   r&   r+   r/   r#   r0   __classcell__r   r   r   r   r   7   s&    #	
r   )loggingr   Z,awscli.customizations.datapipeline.constantsr   r   r   r   r   r   Zawscli.customizations.commandsr   Z-awscli.customizations.datapipeline.translatorr	   r
   r   Zbotocore.exceptionsr   	getLoggerrA   r$   r   rE   r   r   r   r   r   <module>   s    
