
    Vhj                        d Z ddlmZ ddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ dd	lmZ d
dlmZmZmZ  G d de      Z G d de      Zy)z!AWS plugin for integration tests.    )annotationsN   )ApplicationErrordisplay)IntegrationConfig)IntegrationTarget)AnsibleCoreCICloudResource)OriginConfig   )CloudEnvironmentCloudEnvironmentConfigCloudProviderc                  L     e Zd ZdZd fdZd fdZd	 fdZd	dZd
dZ xZ	S )AwsCloudProviderzEAWS cloud provider plugin. Sets up cloud resources before delegation.c                2    t         |   |       d| _        y )NT)super__init__uses_config)selfargs	__class__s     e/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/commands/integration/cloud/aws.pyr   zAwsCloudProvider.__init__)   s        c                `    | j                         }|j                  ryt        |   ||       y)zUFilter out the cloud tests when the necessary config and resources are not available.N)_create_ansible_core_ci	availabler   filter)r   targetsexcludeacir   s       r   r   zAwsCloudProvider.filter.   s)    **,==w(r   c                D   t         |           t        j                  j	                  d      }t        j                  j                  |      r2t        | j                  j                  t              rt        d|z        | j                         s| j                          yy)zKSetup the cloud resource before delegation and register a cleanup callback.z~/.awszDRename "%s" or use the --docker or --remote option to isolate tests.N)r   setupospath
expanduserexists
isinstancer   
controllerr   r   _use_static_config_setup_dynamic)r   aws_config_pathr   s     r   r#   zAwsCloudProvider.setup7   ss    '',,X677>>/*z$)):N:NP\/]"#il{#{||&&(! )r   c                   t        j                  d| j                  z  d       | j                         }| j	                         }|j                         }| j                  j                  sv|d   d   }t        |d   |d   |d   d	
      }t         j                  j                  |d          t         j                  j                  |d          | j                  ||      }| j                  |       y)z<Request AWS credentials through the Ansible Core CI service.z"Provisioning %s cloud environment.r   )	verbosityawscredentials
access_key
secret_keysession_tokenz	us-east-1)
ACCESS_KEY
SECRET_KEYSECURITY_TOKENREGIONr5   r6   N)r   infoplatform_read_config_templater   startr   explaindict	sensitiveadd_populate_config_template_write_config)r   configr!   responser0   valuess         r   r+   zAwsCloudProvider._setup_dynamicC   s    9DMMIUVW++-**,99;yy  "5/-8K&|4&|4*?;"	F !!&"67!!&)9":;33FFCF6"r   c                B    t        | j                  t        d            S )z(Return an AWS instance of AnsibleCoreCI.r/   )r9   )r	   r   r
   )r   s    r   r   z(AwsCloudProvider._create_ansible_core_ci^   s    TYYu(EFFr   )r   r   returnNone)r   ztuple[IntegrationTarget, ...]r    z	list[str]rF   rG   )rF   rG   )rF   r	   )
__name__
__module____qualname____doc__r   r   r#   r+   r   __classcell__)r   s   @r   r   r   &   s     O 
)
"#6Gr   r   c                       e Zd ZdZddZddZy)AwsCloudEnvironmentzTAWS cloud environment plugin. Updates integration test environment after delegation.c                $   t        j                         }|j                  | j                         t	        | j
                  t        j                         j                  dd       }|j                  t	        |j                  d                   t        j                  j                  |j                  d             t        j                  j                  |j                  d             d|vr| j                   |d<   dd	i}t!        ||d
g      S )zRReturn environment configuration for use in the test environment after delegation.r      )resource_prefixtiny_prefixdefaultaws_secret_keysecurity_tokenaws_cleanupANSIBLE_DEBUG_BOTOCORE_LOGSTrueaws_resource_actions)env_varsansible_varscallback_plugins)configparserConfigParserreadconfig_pathr=   rQ   uuiduuid4hexupdateitemsr   r>   r?   getmanagedr   )r   parserr[   rZ   s       r   get_environment_configz*AwsCloudEnvironment.get_environment_configf   s    **,D$$%)- 00

((2.*

 	Di!89:l../?@Al../?@A,.2ll*:L'16:%%45
 	
r   c                h    |s0| j                   r#t        j                  d|j                  z         yyy)z1Callback to run when an integration target fails.zIf %s failed due to permissions, the IAM test policy may need to be updated. https://docs.ansible.com/ansible/devel/collections/amazon/aws/docsite/dev_guidelines.html#aws-permissions-for-integration-testsN)rg   r   noticename)r   targettriess      r   
on_failurezAwsCloudEnvironment.on_failure   s0    NN ]#[[) * &ur   N)rF   r   )rm   r   rn   intrF   rG   )rH   rI   rJ   rK   ri   ro    r   r   rN   rN   c   s    ^
4*r   rN   )rK   
__future__r   r$   ra   r]   typingtutilr   r   rB   r   rm   r   core_cir	   r
   host_configsr    r   r   r   r   rN   rq   r   r   <module>ry      sV    ' " 	   

 :G} :Gz"** "*r   