
    Vh                        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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y)z(CloudStack plugin for integration tests.    )annotationsN   )ApplicationErrordisplay)IntegrationConfig)docker_exec)run_support_containerwait_for_file   )CloudEnvironmentCloudEnvironmentConfigCloudProviderc                  H     e Zd ZdZd fdZd fdZddZddZd	dZ xZ	S )
CsCloudProviderzLCloudStack cloud provider plugin. Sets up cloud resources before delegation.c                    t         |   |       t        j                  j	                  dd      | _        d| _        d| _        d| _        d| _	        y )NANSIBLE_CLOUDSTACK_CONTAINERz/quay.io/ansible/cloudstack-test-container:1.7.0 r   T)
super__init__osenvirongetimagehostportuses_dockeruses_config)selfargs	__class__s     d/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/commands/integration/cloud/cs.pyr   zCsCloudProvider.__init__&   sF    ZZ^^$BDuv
		    c                    t         |           | j                         r| j                          y| j	                          y)zKSetup the cloud resource before delegation and register a cleanup callback.N)r   setup_use_static_config_setup_static_setup_dynamic)r   r    s    r!   r$   zCsCloudProvider.setup0   s0    ""$ !r"   c                D   t        j                         }|j                  | j                         |j	                  dd      }t
        j                  j                  |      }|j                  | _	        | j                  st        d|z        |j                  r|j                  | _        n<|j                  dk(  rd| _        n%|j                  dk(  rd| _        nt        d|z        t        j                  d	| j                  | j                  | j                  fz  d
       y)z=Configure CloudStack tests for use with static configuration.
cloudstackendpointz*Could not determine host from endpoint: %shttpP   httpsi  z*Could not determine port from endpoint: %sz-Read cs host "%s" and port %d from config: %sr   )	verbosityN)configparserConfigParserreadconfig_static_pathr   urllibparseurlparsehostnamer   r   r   schemer   info)r   parserr*   partss       r!   r&   zCsCloudProvider._setup_static9   s    **,D++,::lJ7%%h/NN	yy"#ORZ#Z[[::

DI\\V#DI\\W$DI"#ORZ#Z[[D		SWS\S\^b^u^uGvv  CD  	Er"   c                   | j                         }d| _        | j                  g}t        | j                  | j                  | j
                  d|      }|syt        | j                  |j                  g dd       | j                  j                  r+t        | j                  t        | j                              }no| j                  |j                        }t        |j                  t        | j                        |d   |d	   
      }t        j                  j                  |d          | j!                  ||      }| j#                  |       y)z+Create a CloudStack simulator using docker.i"  zcloudstack-simN)findz/var/lib/mysqlz-typefz-exectouchz{};T)capture)HOSTPORTapikey	secretkey)rA   rB   KEYSECRETrF   )_read_config_templater   r	   r   platformr   r   nameexplaindictr   str_get_credentialsr   	sensitiveadd_populate_config_template_write_config)r   configports
descriptorvaluescredentialss         r!   r'   zCsCloudProvider._setup_dynamicR   s   ++-	 II
 +IIMMJJ

  	DIIz0u  @D  	E99YY^F
 //
@K__^)";/	F !!&"23//?6"r"   c                j    dd}t        | j                  |ddd|      }t        j                  |      S )z8Wait for the CloudStack simulator to return credentials.c                N    	 t        j                  |        y# t        $ r Y yw xY w)zMReturn True if the given configuration is valid JSON, otherwise return False.FT)jsonloads	Exception)values    r!   checkz/CsCloudProvider._get_credentials.<locals>.check   s,    

5!   s    	$$z/var/www/html/admin.json
      )sleeptriesr]   )returnbool)r
   r   rY   rZ   )r   container_namer]   stdouts       r!   rM   z CsCloudProvider._get_credentials   s4    	 tyy.:T\^fhpuvzz&!!r"   )r   r   rb   None)rb   rf   )rd   rL   rb   zdict[str, t.Any])
__name__
__module____qualname____doc__r   r$   r&   r'   rM   __classcell__)r    s   @r!   r   r   #   s!    V "E2,#\"r"   r   c                      e Zd ZdZddZy)CsCloudEnvironmentz[CloudStack cloud environment plugin. Updates integration test environment after delegation.c                T   t        j                         }|j                  | j                         t	        |j                  d            }t	        |d   |d   |d   |d         }t        j                  j                  |d          t	        | j                        }t        ||	      S )
zRReturn environment configuration for use in the test environment after delegation.defaultr*   keysecrettimeout)CLOUDSTACK_ENDPOINTCLOUDSTACK_KEYCLOUDSTACK_SECRETCLOUDSTACK_TIMEOUTru   )cs_resource_prefix)env_varsansible_vars)r/   r0   r1   config_pathrK   itemsr   rN   rO   resource_prefixr   )r   r9   rR   rx   ry   s        r!   get_environment_configz)CsCloudEnvironment.get_environment_config   s    **,D$$%fll9-. &z 2!%=$X.%i0	
 	h':;<#33
 &%
 	
r"   N)rb   r   )rg   rh   ri   rj   r}    r"   r!   rm   rm      s
    e
r"   rm   )rj   
__future__r   rY   r/   r   urllib.parser3   typingtutilr   r   rR   r   docker_utilr   
containersr	   r
   r   r   r   r   r   rm   r~   r"   r!   <module>r      sT    . "   	  

 l"m l"^
) 
r"   