
    !                     Z   S 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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  \R2                  \R4                  " \R6                  R8                  5       " S S\R:                  5      5       5       rg)zIImplements the command to create nand upload attachments to a repository.    N)transfer)
exceptions)waiter)arg_parsers)base)docker_util)flags)requests)util)log)
properties)	resources)files)scaled_integerc                   N    \ rS rSrSrSrSSS.r\S 5       rS r	S	 r
S
 rS rSrg)Create%   z8Creates an Artifact Registry attachment in a repository.v1z{description}aH      To create an attachment for target `projects/myproject/locations/us-central1/packages/mypackage/versions/sha256:123` using a file located in `/path/to/file/sbom.json`:

        $ {command} --target=projects/myproject/locations/us-central1/packages/mypackage/versions/sha256:123
          --files=/path/to/file/sbom.json
    )DESCRIPTIONEXAMPLESc                    [         R                  " 5       R                  U 5        U R                  SSSSS9  U R                  SSSSS9  U R                  S	S
SSS9  U R                  SSS[        R
                  " 5       SS9  g)zOSet up arguments for this command.

Args:
  parser: An argparse.ArgumentPaser.
z--targetTARGETTz@Target of the attachment, should be fully qualified version name)metavarrequiredhelpz--attachment-typeATTACHMENT_TYPEzType of the attachmentz--attachment-namespaceATTACHMENT_NAMESPACEFzNamespace of the attachmentz--filesFILESz>Comma-seperated list of files that are part of this attachment)r   r   typer   N)r	   GetRequiredAttachmentFlagAddToParseradd_argumentr   ArgList)parsers    +lib/surface/artifacts/attachments/create.pyArgsCreate.Args6   s     
##%11&9
O	   !%	    &*	     "M      c           	         [         R                  " 5       nUR                  nUR                  R                  R                  5       n[        R                  " UR                  5      nUR                  R                  R                  UR                  :w  aB  [        R                  " SR                  UR!                  5       UR#                  5       5      5      e[        R$                  " UR                  R                  R&                  5      nXdR(                  :w  aB  [        R                  " SR                  UR!                  5       UR#                  5       5      5      eUR                  R                  R*                  UR,                  :w  aB  [        R                  " SR                  UR!                  5       UR#                  5       5      5      e/ nUR.                   H4  nU R1                  XX4R3                  5       5      n	UR5                  U	5        M6     UR7                  UR9                  UR#                  5       UR:                  UR<                  US9UR3                  5       R!                  5       UR>                  S9n
UR@                  RC                  U
5      n[D        RF                  RI                  URJ                  SS9n[L        RN                  " [L        RP                  " URR                  5      US5      nU$ )	a#  Run the attachment create command.

Args:
  args: an argparse namespace. All the arguments that were provided to this
    command invocation.

Returns:
  Result of CreateAttachment operation.

Raises:
  InvalidInputValueError: when target and attachment
  project/location/repository match.
z7Attachment {} must be in the same project as target {}.z8Attachment {} must be in the same location as target {}.z:Attachment {} must be in the same repository as target {}.)targetr   attachmentNamespacer   )
attachmentparentattachmentId.artifactregistry.projects.locations.operations
collectionzCreating Attachment)*r
   	GetClientMESSAGES_MODULECONCEPTSr,   Parser   ParseDockerVersionStrr*   imagedocker_repoproject
projectsIdar_exceptionsInvalidInputValueErrorformatRelativeNameGetVersionNameRemoveEndpointPrefixlocationlocationsIdreporepositoriesIdr   upload_fileParentappendEArtifactregistryProjectsLocationsRepositoriesAttachmentsCreateRequest
Attachmentattachment_typeattachment_namespaceattachmentsId+projects_locations_repositories_attachmentsr   r   REGISTRYParseRelativeNamenamer   WaitForCloudOperationPollerNoResourcesprojects_locations_operations)selfargsclientmessagesattachment_refdocker_versionloc
file_namesfile	file_namecreate_requestop_objop_refresults                 r%   Run
Create.Run[   s    !F%%H]]--335N 66t{{CN''//>3L3LL00
C
J
J))+^-J-J-L 
 
*
*((11C (((00
D
K
K))+^-J-J-L 
 '',,0M0MM00
F
M
M))+^-J-J-L  J

""
"7"7"9i 	"	  cc&&!002%% $ 9 9	 ' 
 $$&335#11 d 	N ??FFF 11 P 2 F
 ^^..00	
 	F Mr(   c                 r   [         R                  " [        R                  R                  R
                  R                  5       5      nUR                  UR                  5       UR                  5       S9n[        R                  " U5      nS n [        R                  R                  XUS9n	UR                  R                  XiS9n
U
R                   n["        R$                  R'                  UR(                  SS9n[*        R,                  " [*        R.                  " UR                  UR0                  5      USR3                  U5      5      n["        R$                  R'                  UR(                  SS9nUR                  5       nU(       a0  U R?                  U[@        RB                  RE                  U5      X#5        U$ ! [*        R4                   aj  nS[7        U5      ;   aQ  [8        R:                  " S	U S
3R3                  U5      5        U R=                  U5      nUR                  5       nU SU 3n S nANS nAff = f)N)uploadFileRequestr-   )	mime_type	chunksize)uploadr/   r0   zUploading file: {}z6artifactregistry.projects.locations.repositories.fileszalready existszFile z already existsz/files/)#r   ParseIntegerr   VALUESstorageupload_chunk_sizeGet?ArtifactregistryProjectsLocationsRepositoriesFilesUploadRequestUploadFileRequestr>   r   GetMimetyper   UploadFromFile%projects_locations_repositories_files	operationr   rN   rO   rP   r   rQ   CloudOperationPollerrS   r=   OperationErrorstrr   infocomputeSha256OfFileupdate_file_name_annotationospathbasename)rT   	file_pathrV   rW   repo_refrg   requestrf   result_file_namerh   r_   opr`   ra   result_file_refedigestrepo_relative_names                     r%   rE   Create.upload_file   s   ++!!33779I VV"446$$& W G
   +IB''
I ( f ;;BB
 C f b!!33
''N 4 f
 ~~

%
%::22 

%
%i
0f "**<<
++M = o )557 
&&
BGG,,Y7     B	SV	#5?3::9EF)))4%22401ABs   C3F8 8H6A H11H6c                     UR                  UUR                  UR                  R                  UR                  R                  R                  SUS9/S9S9SS9nUR                  R                  U5        g )Nz)artifactregistry.googleapis.com/file_name)keyvalue)additionalProperties)annotationsr   )rP   $googleDevtoolsArtifactregistryV1File
updateMask)>ArtifactregistryProjectsLocationsRepositoriesFilesPatchRequest$GoogleDevtoolsArtifactregistryV1FileAnnotationsValueAdditionalPropertyrs   Patch)rT   file_resource_namer]   rV   rW   update_requests         r%   rz   "Create.update_file_name_annotation   s     \\-5-Z-Z EEVVAARReeG' f & W  .[ 	.
 ! ] N 0066~Fr(   c                     [         R                  " 5       n[        R                  " U5      nUR	                  U5        SUR                  5       -   $ )Nzsha256:)hashlibsha256r   ReadBinaryFileContentsupdate	hexdigest)rT   r~   r   datas       r%   ry   Create.computeSha256OfFile   s>    ^^F''	2D
MM$v'')))r(    N)__name__
__module____qualname____firstlineno____doc__api_versiondetailed_helpstaticmethodr&   rb   rE   rz   ry   __static_attributes__r   r(   r%   r   r   %   sK     A+ %- " "HIV2hG&*r(   r   )r   r   r{   apitools.base.pyr    googlecloudsdk.api_lib.artifactsr   r;   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   $googlecloudsdk.command_lib.artifactsr   r	   r
   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   r   DefaultUniverseOnlyReleaseTracksReleaseTrackGACommandr   r   r(   r%   <module>r      s    P  	 % H . / ( < 6 9 5 # * ) * 3 D%%(()J*T\\ J* * J*r(   