
    x                         d 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 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  G d de      Zy)z1Support library to handle the deploy subcommands.    )absolute_import)division)unicode_literals)client_util)automation_util)custom_target_type_util)deploy_policy_util)manifest_util)target_util)logc                   0    e Zd ZdZddZd Zd Zd Zd Zy)	DeployClientzHClient for managing Cloud Deploy delivery pipeline and target resources.Nc                     |xs t        j                         | _        t        j                         | _        |xs t        j
                  |      | _        | j                  j                  | _        y)zInitialize a deploy.DeployClient.

    Args:
      client: base_api.BaseApiClient, the client class for Cloud Deploy.
      messages: module containing the definitions of messages for Cloud Deploy.
    N)	r   GetClientInstanceclientOperationsClientoperation_clientGetMessagesModulemessages$projects_locations_deliveryPipelines_pipeline_service)selfr   r   s      0lib/googlecloudsdk/api_lib/clouddeploy/deploy.py__init__zDeployClient.__init__!   sS     ;K99;DK'88:DE = =f EDM![[MMD    c                 \   t        j                  | j                  ||      }d}|t         j                  j                     D ci c]  }|j
                  | j                  |        }}| j                  j                  ||       |t         j                  j                     D ci c]"  }|j
                  t        j                  |      $ }}| j                  j                  ||       |t         j                  j                     D ci c]"  }|j
                  t        j                  |      $ }}| j                  j                  ||       |t         j                  j                     D ci c]"  }|j
                  t!        j"                  |      $ }}| j                  j                  ||       |t         j                  j$                     D ci c]"  }|j
                  t'        j(                  |      $ }}| j                  j                  ||       yc c}w c c}w c c}w c c}w c c}w )zCreates Cloud Deploy resources.

    Asynchronously calls the API then iterate the operations
    to check the status.

    Args:
     manifests: the list of parsed resource yaml definitions.
     region: location ID.
    z"Created Cloud Deploy resource: {}.N)r
   ParseDeployConfigr   ResourceKindDELIVERY_PIPELINEnameCreateDeliveryPipeliner   CheckOperationStatusTARGETr   PatchTarget
AUTOMATIONr   PatchAutomationCUSTOM_TARGET_TYPEr   PatchCustomTargetTypeDEPLOY_POLICYr	   PatchDeployPolicy)r   	manifestsregionresource_dictmsg_templateresourceoperation_dicts          r   CreateResourceszDeployClient.CreateResources-   s+    "33y&M 8L &&&88

H 	t228<<
   	..~|L &m&@&@&G&GHHH 	{..x88H   	..~|L
 &m&@&@&K&KLLH 	66x@@L   	..~|L
 &&&99

H 	.DDXNN
   	..~|L
 &m&@&@&N&NOOH 	);;HEEO   	..~|LGs   #H#'H'H+'H$'H)c                    t        j                  | j                  ||      }d}|t         j                  j                     D ci c],  }|j
                  t        j                  |j
                        . }}| j                  j                  ||       |t         j                  j                     D ci c],  }|j
                  t        j                  |j
                        . }}| j                  j                  ||       |t         j                  j                     D ci c],  }|j
                  t        j                  |j
                        . }}| j                  j                  ||       |t         j                  j                      D ci c]  }|j
                  | j#                  ||      ! }}| j                  j                  ||       |t         j                  j$                     D ci c],  }|j
                  t'        j(                  |j
                        . }}| j                  j                  ||       yc c}w c c}w c c}w c c}w c c}w )ap  Delete Cloud Deploy resources.

    Asynchronously calls the API then iterate the operations
    to check the status.

    Args:
     manifests: [str], the list of parsed resource yaml definitions.
     region: str, location ID.
     force: bool, if true, the delivery pipeline with sub-resources will be
       deleted and its sub-resources will also be deleted.
    z"Deleted Cloud Deploy resource: {}.N)r
   r   r   r   r#   r    r   DeleteTargetr   r"   r'   r   DeleteCustomTargetTyper%   r   DeleteAutomationr   DeleteDeliveryPipeliner)   r	   DeleteDeployPolicy)r   r+   r,   forcer-   r.   r/   r0   s           r   DeleteResourceszDeployClient.DeleteResources`   sB    "33y&M 8L &m&@&@&G&GHHH 	{//>>H   	..~|L &&&99
	
H 	.EEMM
 	

	   	..~|L &m&@&@&K&KLLH 	77FFL   	..~|L &&&88

H 	t228UCC
   	..~|L &m&@&@&N&NOOH 	)<<X]]KKO   	..~|LEs   1H>11I1I$I.1Ic                     t        j                  dt        |      z          | j                  j	                  | j
                  j                  |d|j                  t        j                              S )zCreates a delivery pipeline resource.

    Args:
      pipeline_config: apitools.base.protorpclite.messages.Message, delivery
        pipeline message.

    Returns:
      The operation message.
    zCreating delivery pipeline: T)deliveryPipelineallowMissingr    
updateMask)
r   debugreprr   Patchr   9ClouddeployProjectsLocationsDeliveryPipelinesPatchRequestr    r
   PIPELINE_UPDATE_MASK)r   pipeline_configs     r   r!   z#DeployClient.CreateDeliveryPipeline   sd     II,tO/DDE!!''OO, %%$99	 	P 	
 r   c                     t        j                  dt        |      z          | j                  j	                  | j
                  j                  d|j                  |            S )az  Deletes a delivery pipeline resource.

    Args:
      pipeline_config: apitools.base.protorpclite.messages.Message, delivery
        pipeline message.
      force: if true, the delivery pipeline with sub-resources will be deleted
        and its sub-resources will also be deleted.

    Returns:
      The operation message. It could be none if the resource doesn't exist.
    zDeleting delivery pipeline: T)r<   r    r8   )r   r>   r?   r   Deleter   :ClouddeployProjectsLocationsDeliveryPipelinesDeleteRequestr    )r   rC   r8   s      r   r6   z#DeployClient.DeleteDeliveryPipeline   sY     II,tO/DDE!!((PPO$8$8 	Q 	
 r   )NN)	__name__
__module____qualname____doc__r   r1   r9   r!   r6    r   r   r   r      s#    P
N1Mf2Mh(r   r   N)rJ   
__future__r   r   r   "googlecloudsdk.api_lib.clouddeployr   !googlecloudsdk.command_lib.deployr   r   r	   r
   r   googlecloudsdk.corer   objectr   rK   r   r   <module>rQ      s6    8 &  ' : = E @ ; 9 #[6 [r   