
                         J   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 ddlmZ ddlmZ ddlmZ  e
j.                  e
j0                  j2                  e
j0                  j4                         G d de
j6                               Zy)zACommand to deploy an Apigee archive deployment to an environment.    )absolute_import)division)unicode_literals)apigee)waiter)base)archives)defaults)errors)resource_args)labels_util)log)filesc                   B    e Zd ZdZdddZed        Zd Zd Zd Z	d	 Z
y
)Deployz6Deploy an Apigee archive deployment to an environment.a$     {description}

  `{command}` installs an archive deployment in an Apigee environment.

  By default, the archive deployment will be deployed on the remote management
  plane for the specified Apigee organization. To deploy on a locally running
  Apigee emulator, use the `--local` flag.
  a    To deploy the contents of the current working directory as an archive
  deployment to an environment named ``my-test'', given that the Cloud Platform
  project has been set in gcloud settings, run:

    $ {command} --environment=my-test

  To deploy an archive deployment from a local directory other than the current
  working directory, to an environment named ``my-demo'' in an organization
  belonging to a Cloud Platform project other than the one set in gcloud
  settings, named ``my-org'', run:

    $ {command} --organization=my-org --environment=my-demo --source=/apigee/dev

  To deploy the contents of the current working directory as an archive
  deployment, with the user-defined labels ``my-label1=foo'' and
  ``my-label2=bar'', to an environment named ``my-test'', given that the Cloud
  Platform project has been set in gcloud settings, run:

    $ {command} --environment=my-test --labels=my-label1=foo,my-label2=bar
  )DESCRIPTIONEXAMPLESc                 j   t        j                         g}t        j                  | dd|dd       | j	                  dd      }|j                  ddt        j                  d	
       |j                  ddt        j                  d
       | j                  dddd       t        j                  |        y )Nzorganization.environmentz=Apigee environment in which to deploy the archive deployment.FT)resource_path	help_textfallthroughs
positionalrequiredzSource input.)mutexhelpz--sourcez.The source directory of the archive to upload.)r   typer   z--bundle-filez-The zip file containing an archive to upload.z--async
store_trueasync_aP  If set, returns immediately and outputs a description of the long running operation that was launched. Else, `{command}` will block until the archive deployment has been successfully deployed to the specified environment.

To monitor the operation once it's been launched, run `{grandparent_command} operations describe OPERATION_NAME`.)actiondestr   )
r
   !GCPProductOrganizationFallthroughr   AddSingleResourceArgument	add_groupadd_argumentr   ExpandHomeDirr   AddCreateLabelsFlags)parserr   source_input_groups      %lib/surface/apigee/archives/deploy.pyArgszDeploy.ArgsG   s    >>@AL++0!!  ))?)K##  =	 $ ?
 ##  <	 $ >
 L	  	N $$V,    c                     t         j                  j                  |      }d|vrt        j                  d||d      |d   S )a|  Gets the signed URL for uploading the archive deployment.

    Args:
      identifiers: A dict of resource identifers. Must contain "organizationsId"
        and "environmentsId"

    Returns:
      A str of the upload URL.

    Raises:
      googlecloudsdk.command_lib.apigee.errors.RequestError if the "uploadUri"
        field is not included in the GetUploadUrl response.
    	uploadUrigetUploadUrlzPlease try again.)resource_typeresource_identifierbody	user_help)r   ArchivesClientGetUploadUrlr   RequestError)selfidentifiersget_upload_url_resps      r)   _GetUploadUrlzDeploy._GetUploadUrll   sP     !//<<[I--&)"'	) )
 {++r+   c                     t        j                  ||      }|j                  s5t        j                  |j
                  |j                  |j                        y)a\  Issues an HTTP PUT call to the upload URL with the zip file payload.

    Args:
      upload_url: A str containing the full upload URL.
      zip_file_path: A str of the local path to the zip file.

    Raises:
      googlecloudsdk.command_lib.apigee.errors.HttpRequestError if the response
        status of the HTTP PUT call is not 200 (OK).
    N)cmd_libUploadArchiveokr   HttpRequestErrorstatus_codereasoncontent)r6   
upload_urlzip_file_pathupload_archive_resps       r)   _UploadArchivezDeploy._UploadArchive   sU     "//
MJ!!##$7$C$C$7$>$>$7$?$?A A "r+   c                     i }||d<   |r%i |d<   |j                         D ]  \  }}||d   |<    t        j                  j                  ||      }t        j                  j                  |      }|S )aQ  Creates the archive deployment.

    Args:
      identifiers: A dict of resource identifers. Must contain "organizationsId"
        and "environmentsId"
      upload_url: A str containing the full upload URL.
      labels: A dict of the key/value pairs to add as labels.

    Returns:
      A dict containing the operation metadata.
    gcs_urilabels)itemsr   r3   CreateArchiveDeploymentOperationsClient	SplitName)	r6   r7   rB   rH   	post_datakvapi_response	operations	            r)   _DeployArchivezDeploy._DeployArchive   sz     I%Iii,,.$!Q!"	(A !((@@Y L''11,?Ir+   c                    |j                   j                  j                         j                         }t	        j
                  |      }d}	 t        j                  |j                        }|j                  r(|j                  |j                         |j                  }n|j                         }| j                  |      }| j                  ||       | j                  |||      }d|vsd|vr$t        j                   dj#                  |            d|d   v r"|d   d   D ]  }t%        j&                  |        t%        j(                  d|d          |j*                  r"||rt-        |d	      r|j/                          S S S t        j0                  t3        j4                  |d         |d   d
j#                  |d         d       |rt-        |d	      r|j/                          yyy# |rt-        |d	      r|j/                          w w w xY w)zRun the deploy command.NorganizationuuidzUnknown operation response: {}warningsmetadataz$Started archives deploy operation %snameClosez&Waiting for operation [{}] to completei  )messagewait_ceiling_ms)CONCEPTSenvironmentParseAsDictr   GetUpdateLabelsDictFromArgsr;   LocalDirectoryArchivesourcebundle_fileValidateZipFilePathZipr9   rE   rR   r   OperationErrorformatr   warninginfor   hasattrrY   WaitForr   	LROPoller)	r6   argsr7   
labels_arglocal_dir_archiverC   rB   rQ   rh   s	            r)   Runz
Deploy.Run   s   --++113::<K88>J"!77D			--d.>.>?(()--/%%k2j
*m4%%k:zJi	y	(F),C##,33I>@ 	@	y,	, ,Z8G
++g
 9	hh5y7HI	 
w'8'B!  C	 nn


9^4
5
F
:AA! 
w'8'B!  C		w'8'B!  C	s   DG%  AG% %"HN)__name__
__module____qualname____doc__detailed_helpstaticmethodr*   r9   rE   rR   rp    r+   r)   r   r       sA    >!-F "- "-H,.A".!"r+   r   N)rt   
__future__r   r   r   googlecloudsdk.api_libr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   !googlecloudsdk.command_lib.apigeer	   r;   r
   r   r   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   googlecloudsdk.core.utilr   ReleaseTracksReleaseTrackALPHABETADescribeCommandr   rw   r+   r)   <module>r      s~    H &  ' ) . ( A 6 4 ; < # * D%%++T->->-C-CDk"T!! k" Ek"r+   