
    #                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJ	r	  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\5      rS rS r " S S5      rg)z=Helper functions for working with Apigee archive deployments.    )absolute_import)division)print_function)unicode_literalsN)apigee)errors)log)requests)resource_projector)archive)files)urllibc                       \ rS rSrSrSr/ SQr\R                  R                  SSS5      r
S rS	 rS
 rS rS rS rS rS rSrg)LocalDirectoryArchive#   zManages a local zip archive.zapigee_archive_deployment.zip)z.graphqlz.jarz.javaz.jsz.jscz.jsonz.oasz.propertiesz.pyz.securityPolicyz.wsdlz.xmlz.xsdz.xslz.yamlz.ymlsrcmainr   c                    U R                  U5        U(       a:  [        R                  R                  U5      (       d  [        R
                  " U5      eUb  UO[        R                  " 5       U l        [        R                  " 5       U l
        g N)_CheckIfPathExistsospathisdirr   SourcePathIsNotDirectoryErrorr   GetCWD_src_dirTemporaryDirectory_tmp_dir)selfsrc_dirs     1lib/googlecloudsdk/command_lib/apigee/archives.py__init__LocalDirectoryArchive.__init__<   sZ    G$rww}}W--0099&2GDM,,.DM    c                     U(       aJ  [         R                  R                  U5      (       d%  [        R                  " SR                  U5      5      egg)z'Checks that the given file path exists.z-Path to archive deployment does not exist: {}N)r   r   existsr   MissingFileErrorformat)r   r   s     r!   r   (LocalDirectoryArchive._CheckIfPathExistsD   sA    BGGNN4((""
9
@
@
FH H )tr$   c                    UR                  U R                  5      (       d  g[        R                  R	                  U5      u  p#[        R                  R                  U R                  U5      n[        R                  R                  U5      R                  S5      (       a  g[        R                  R                  U5      (       a  g[        R                  R                  U5      (       a  UR                  5       U R                  ;   a  gg)zEFilter all files in the archive directory to only allow Apigee files.F.T)
startswith_ARCHIVE_ROOTr   r   splitextjoinr   basenamer   isfilelower_APIGEE_ARCHIVE_FILE_EXTENSIONS)r   	file_name_ext	full_paths        r!   _ZipFileFilter$LocalDirectoryArchive._ZipFileFilterJ   s     2 233WWi(FAT]]I6I 
ww	"--c22 
ww}}Y
y!!		t;;;r$   c                     [         R                  R                  U R                  R                  U R                  5      n[
        R                  " XR                  U R                  5        U$ )z1Creates a zip archive of the specified directory.)	r   r   r/   r   _ARCHIVE_FILE_NAMEr   MakeZipFromDirr   r8   )r   dst_files     r!   ZipLocalDirectoryArchive.Zipa   sE    ww||DMM..0G0GHH8]]D4G4GHOr$   c                     U R                  U5        [        R                  " U5      (       d  [        R                  " U5      eg)zCChecks that the zip file path exists and the file is a zip archvie.N)r   zipfile
is_zipfiler   BundleFileNotValidError)r   zip_paths     r!   ValidateZipFilePath)LocalDirectoryArchive.ValidateZipFilePathg   s7    H%h''**844 (r$   c                 6    U R                   R                  5       $ )z&Deletes the local temporary directory.)r   Closer   s    r!   rH   LocalDirectoryArchive.Closem   s    ==  r$   c                     U $ r    rI   s    r!   	__enter__LocalDirectoryArchive.__enter__q   s    Kr$   c                 `     U R                  5         g !   [        R                  " S5         g= f)Nz1Temporary directory was not successfully deleted.T)rH   r	   warning)r   exc_typevaltbs       r!   __exit__LocalDirectoryArchive.__exit__t   s'    
jjl	kkEFs    -)r   r   N)__name__
__module____qualname____firstlineno____doc__r;   r3   r   r   r/   r-   r"   r   r8   r>   rE   rH   rM   rT   __static_attributes__rL   r$   r!   r   r   #   sV    $ 7%!$ '',,ufh7-/H.5!r$   r   c                     [         R                  R                  U 5      nUR                  R	                  S5      nUS   $ )a}  Helper function to extract the upload file id from the signed URL.

Archive deployments must be uploaded to a provided signed URL in the form of:
https://storage.googleapis.com/<bucket id>/<file id>.zip?<additional headers>
This function extracts the file id from the URL (e.g., <file id>.zip).

Args:
  upload_url: A string of the signed URL.

Returns:
  A string of the file id.
/)r   parseurlparser   split)
upload_urlurl
split_paths      r!   GetUploadFileIdre   |   s4     	j)#xx~~c"*	Br$   c                     [         R                  " 5       nSSS.n[        R                  " U5       nUR	                  XUS9nSSS5        U$ ! , (       d  f       W$ = f)a  Uploads the specified zip file with a PUT request to the provided URL.

Args:
  upload_url: A string of the URL to send the PUT request to. Required to be a
    signed URL from GCS.
  zip_file: A string of the file path to the zip file to upload.

Returns:
  A requests.Response object.
zapplication/zipz0,1073741824)zcontent-typezx-goog-content-length-range)dataheadersN)r
   
GetSessionr   BinaryFileReaderput)rb   zip_filesessrh   rg   responses         r!   UploadArchivero      s]     
			$ (%3' h'4xx
wx?H (	/ ('	/s   A
Ac                   *    \ rS rSrSrS rS rS rSrg)ListArchives   z5Adds additional helpful fields to a list of archives.c                     Xl         [        R                  " U5      U l        SU l        SU l        SU l        SU l        SU l        SU l	        g )NDeployedzIn ProgressFailedz	Not FoundUnknownMissing)
_orgr   	LROPoller_lro_helper_deployed_status_inprogress_status_failed_status_not_found_status_unknown_status_missing_status)r   orgs     r!   r"   ListArchives.__init__   sJ    I'',D&D+D"D(D$D$Dr$   c                 |   [        US SS9nSn[        U5       H  u  pE[        R                  " U5      nU(       a  U R                  US'   OSU;   a  [
        R                  R                  SUS   05      S   n [
        R                  R                  U R                  US	.5      nU R                  U5      n	U	S
   US'   U	S
   U R                  :X  a  XbU'   Us  $ SU	;   a  U	S   US'   OU R                  US'   XbU'   M     U$ ! [        R                   a    U R                  US'    N/  SnU R                  US'    NC= f)zGiven a list of archives, extends them with a status and error field where needed.

Args:
  archives: A list of archives to extend with a status and potential error.

Returns:
  A list of archives with their associated status.
c                     U S   $ )N	createdAtrL   )ks    r!   <lambda>/ListArchives.ExtendedArchives.<locals>.<lambda>   s    +r$   T)keyreverseFoperationStatus	operationnameuuid)organizationsIdoperationsIdstatuserror)sorted	enumerater   MakeSerializabler   r   OperationsClient	SplitNameDescriberx   _StatusFromOperationr{   r   EntityNotFoundErrorr~   r   )
r   archivesextended_archivescascade_unknownidxaserilized_archiver   opr   s
             r!   ExtendedArchivesListArchives.ExtendedArchives   sv    .> O-.,==a@	/3/C/C+,!&&00&!K. 23 44:<	F&&//!%"1 " ,,R0&171A
-
.H!6!66%6c"$$& )/g& 04/C/C+,07 /8  )) 	H151G1G
-
.	F /151E1E
-
.s   :ADD#D;'D;c                     0 n U R                   R                  U5      nU(       a  U R                  US'   U$ U R                  US'    U$ ! [        R
                   a    U R                  US'   US   S   US'    U$ f = f)zGathers given an LRO, determines the associated archive status.

Args:
  op: An Apigee LRO

Returns:
  A dict in the format of
    {"status": "{status}", "error": "{error if present on LRO}"}.
r   r   message)rz   IsDoner{   r|   r   RequestErrorr}   )r   r   r   is_dones       r!   r   !ListArchives._StatusFromOperation   s     F/  ''+g	  00x M  22x M  /,,fX 7I.fWoM/s   1A	 A	 	.A;:A;)r{   r}   r|   rz   r   r~   rx   r   N)	rV   rW   rX   rY   rZ   r"   r   r   r[   rL   r$   r!   rq   rq      s    =%0dr$   rq   )rZ   
__future__r   r   r   r   r   rA   googlecloudsdk.api_libr   !googlecloudsdk.command_lib.apigeer   googlecloudsdk.corer	   r
   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   r   	six.movesr   objectr   re   ro   rq   rL   r$   r!   <module>r      sZ    D &  % ' 	  ) 4 # ( ; , * VF Vr$,Y Yr$   