
    Vh                        d Z ddlm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mZmZ  G d de      Z G d de      ZddZy)z#Azure plugin for integration tests.    )annotationsN   )ApplicationErrordisplay)IntegrationConfig)IntegrationTarget)AnsibleCoreCICloudResource   )CloudEnvironmentCloudEnvironmentConfigCloudProviderc                  X     e Zd ZdZd fdZd	 fdZd
 fdZd
 fdZd
dZddZ	 xZ
S )AzureCloudProviderzGAzure cloud provider plugin. Sets up cloud resources before delegation.c                @    t         |   |       d | _        d| _        y )NT)super__init__aciuses_config)selfargs	__class__s     g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/commands/integration/cloud/azure.pyr   zAzureCloudProvider.__init__#   s    .2    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excluder   r   s       r   r   zAzureCloudProvider.filter*   s)    **,==w(r   c                    t         |           | j                         s| j                          t	        | j
                         y)zKSetup the cloud resource before delegation and register a cleanup callback.N)r   setup_use_static_config_setup_dynamic
get_configconfig_pathr   r   s    r   r"   zAzureCloudProvider.setup3   s2    &&(!4##$r   c                n    | j                   r| j                   j                          t        |           y)zWClean up the cloud resource and any temporary configuration files after tests complete.N)r   stopr   cleanupr'   s    r   r*   zAzureCloudProvider.cleanup<   s!    88HHMMOr   c           	     *   t        j                  d| j                  z  d       | j                         }i }| j	                         }|j                         }| j                  j                  s|d   }|| _        | j                  j                  sst        |d   |d   |d   |d   |d	   d
   |d	   d         t         j                  j                  d          dj                  fdt              D              }d|z   }| j                  |       y)z2Request Azure credentials through ansible-core-ci.z"Provisioning %s cloud environment.r   )	verbosityazureclientIdclientSecretsubscriptionIdtenantIdresourceGroupNamesr   )AZURE_CLIENT_IDAZURE_SECRETAZURE_SUBSCRIPTION_IDAZURE_TENANTRESOURCE_GROUPRESOURCE_GROUP_SECONDARYr4   
c              3  2   K   | ]  }|d |     yw)z: N ).0keyvaluess     r   	<genexpr>z4AzureCloudProvider._setup_dynamic.<locals>.<genexpr>^   s     W3s<Ws   z
[default]
N)r   infoplatform_read_config_templater   startr   explainr   dict	sensitiveaddjoinsorted_write_config)r   configresponser   
aci_resultr>   s        @r   r$   z!AzureCloudProvider._setup_dynamicC   s   9DMMIUVW++-**,YY[
yy  !'*HDHyy   ( 4%n5&./?&@%j1'(<=a@)12F)G)JF !!&"89YYWvWWF"V+F6"r   c                B    t        | j                  t        d            S )z*Return an Azure instance of AnsibleCoreCI.r-   )rA   )r	   r   r
   )r   s    r   r   z*AzureCloudProvider._create_ansible_core_cid   s    TYYw(GHHr   )r   r   returnNone)r   ztuple[IntegrationTarget, ...]r    z	list[str]rO   rP   )rO   rP   )rO   r	   )__name__
__module____qualname____doc__r   r   r"   r*   r$   r   __classcell__)r   s   @r   r   r       s&    Q )%#BIr   r   c                       e Zd ZdZddZddZy)AzureCloudEnvironmentzVAzure cloud environment plugin. Updates integration test environment after delegation.c                   t        | j                        }t        j                  j	                  |j                  d             t        j                  j	                  |j                  d             t        | j                        }|j                  t        d |j                         D                     t        ||      S )zRReturn environment configuration for use in the test environment after delegation.r4   AZURE_PASSWORD)resource_prefixc              3  F   K   | ]  \  }}|j                         |f  y wN)lowerr<   r=   values      r   r?   z?AzureCloudEnvironment.get_environment_config.<locals>.<genexpr>w   s      Y*#u#))+u!5 Y   !)env_varsansible_vars)r%   r&   r   rF   rG   getrE   rZ   updateitemsr   )r   ra   rb   s      r   get_environment_configz,AzureCloudEnvironment.get_environment_configl   s    d../hll>:;hll+;<= 00
 	D YHX YYZ%%
 	
r   c                h    |s0| j                   r#t        j                  d|j                  z         yyy)z1Callback to run when an integration target fails.zHIf %s failed due to permissions, the test policy may need to be updated.N)managedr   noticename)r   targettriess      r   
on_failurez AzureCloudEnvironment.on_failure~   s+    NNehnhshsst &ur   N)rO   r   )rk   r   rl   intrO   rP   )rQ   rR   rS   rT   rf   rm   r;   r   r   rW   rW   i   s    `
$ur   rW   c           
        t        j                         }|j                  |        t        d |j	                  d      D              d}d}d}t        fd|D              }t        fd|D              }t        fd|D              }|s&t        d	d
j                  t        |            z        |sC|sAt        dd
j                  t        |            dd
j                  t        |                  S )zKReturn a configuration dictionary parsed from the given configuration path.c              3  F   K   | ]  \  }}|j                         |f  y wr\   )upperr^   s      r   r?   zget_config.<locals>.<genexpr>   s     Q:3399;&Qr`   default)r7   r8   )r3   r4   r5   r6   )AZURE_AD_USERrY   r5   c              3  &   K   | ]  }|v  
 y wr\   r;   r<   varrK   s     r   r?   zget_config.<locals>.<genexpr>        1#v1   c              3  &   K   | ]  }|v  
 y wr\   r;   ru   s     r   r?   zget_config.<locals>.<genexpr>   rw   rx   c              3  &   K   | ]  }|v  
 y wr\   r;   ru   s     r   r?   zget_config.<locals>.<genexpr>   rw   rx   z(Resource groups must be defined with: %sz, z=Credentials must be defined using either:
Service Principal: z
Active Directory: )	configparserConfigParserreadrE   re   allr   rH   rI   )	r&   parserrg_varssp_varsad_varsrg_oksp_okad_okrK   s	           @r   r%   r%      s    &&(F
KKQi9PQQFG
GG 111E111E111EIDIIV\]dVeLffggIIfWo&		&/(B D E 	E Mr   )r&   strrO   zdict[str, str])rT   
__future__r   r{   typingtutilr   r   rK   r   rk   r   core_cir	   r
    r   r   r   r   rW   r%   r;   r   r   <module>r      sT    ) "  

 FI FIRu, u6$r   