
    "                     ~   d Z ddlmZ ddlmZ ddlmZ ddlZddl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 ddlmZ ej:                   ej<                  ej>                  j@                         G d dejB                                      Z"y)zIImplements the command to create nand upload attachments to a repository.    )absolute_import)division)unicode_literalsN)transfer)
exceptions)waiter)arg_parsers)base)docker_util)flags)requests)util)log)
properties)	resources)files)scaled_integerc                   F    e Zd ZdZdZdddZed        Zd Zd Z	d	 Z
d
 Zy)Createz8Creates 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                    t        j                         j                  |        | j                  dddd       | j                  dddd       | j                  d	d
dd       | j                  dddt	        j
                         d       y)z[Set 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ArgszCreate.Args9   s     
##%11&9
O	   !%	    &*	     "M      c                    t        j                         }|j                  }|j                  j                  j                         }t        j                  |j                        }|j                  j                  j                  |j                  k7  rAt        j                  dj                  |j!                         |j#                                     t        j$                  |j                  j                  j&                        }||j(                  k7  rAt        j                  dj                  |j!                         |j#                                     |j                  j                  j*                  |j,                  k7  rAt        j                  dj                  |j!                         |j#                                     g }|j.                  D ]5  }| j1                  ||||j3                               }	|j5                  |	       7 |j7                  |j9                  |j#                         |j:                  |j<                  |      |j3                         j!                         |j>                        }
|j@                  jC                  |
      }tD        jF                  jI                  |jJ                  d      }tM        jN                  tM        jP                  |jR                        |d      }|S )	aG  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'   Runz
Create.Run^   s    !F%%H]]--335N 66t{{CN''//>3L3LL00
C
J
J))+^-J-J-L 
 
*
*((11C n(((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                 |   t        j                  t        j                  j                  j
                  j                               }|j                  |j                         |j                               }t        j                  |      }d }	 t        j                  j                  |||      }	|j                  j                  ||	      }
|
j                   }t"        j$                  j'                  |j(                  d      }t+        j,                  t+        j.                  |j                  |j0                        |dj3                  |            }t"        j$                  j'                  |j(                  d      }|j                         }|r1| j?                  |t@        jB                  jE                  |      ||       |S # t*        j4                  $ rg}dt7        |      v rPt9        j:                  d	| d
j3                  |             | j=                  |      }|j                         }| d| }Y d }~d }~ww xY w)N)uploadFileRequestr.   )	mime_type	chunksize)uploadr0   r1   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   rO   rP   rQ   r   rR   CloudOperationPollerrT   r>   OperationErrorstrr   infocomputeSha256OfFileupdate_file_name_annotationospathbasename)rU   	file_pathrW   rX   repo_refrg   requestrf   result_file_namerh   r`   opra   rb   result_file_refedigestrepo_relative_names                     r'   rF   zCreate.upload_file   s   ++!!33779I VV"446$$& W G
   +IB''
yI ( 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   C?G H;AH66H;c                    |j                  ||j                  |j                  j                  |j                  j                  j                  d|      g            d      }|j                  j                  |       y )Nz)artifactregistry.googleapis.com/file_name)keyvalue)additionalProperties)annotationsr   )rQ   $googleDevtoolsArtifactregistryV1File
updateMask)>ArtifactregistryProjectsLocationsRepositoriesFilesPatchRequest$GoogleDevtoolsArtifactregistryV1FileAnnotationsValueAdditionalPropertyrs   Patch)rU   file_resource_namer^   rW   rX   update_requests         r'   rz   z"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                     t        j                         }t        j                  |      }|j	                  |       d|j                         z   S )Nzsha256:)hashlibsha256r   ReadBinaryFileContentsupdate	hexdigest)rU   r~   r   datas       r'   ry   zCreate.computeSha256OfFile   s>    ^^F''	2D
MM$v'')))r)   N)__name__
__module____qualname____doc__api_versiondetailed_helpstaticmethodr(   rc   rF   rz   ry    r)   r'   r   r   (   sK     A+ %- " "HIV2hG&*r)   r   )#r   
__future__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)   