
    ?                        d 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  eh d      Zdedeej,                     fdZ ej0                  d       G d d             Zdej,                  defdZd ej6                         j8                  dej,                  fdZdedefdZy)zUtilities for GCR.    N)Iterator)
list_pager)docker_name)docker_http)docker_image)client_util)util)log)
properties>   eu.us.asia. parentreturnc              #   b  K   t        j                         j                  | ddt        j                  j
                  j                  j                          dgd      }t        j                  t        j                         j                  |ddd	      }|D ]  }t        |      }||  y
w)zLists GCR repositories under the parent resource.

  Args:
    parent: A parent resource, e.g. projects/123, folders/123, orgnizations/123.

  Yields:
    Each docker repository that is a GCR repo under the parent resource.
  zname:artifacts appspot comzstorage.z/Bucketzname,parentFullResourceName)scopequery
assetTypesreadMaskSearchAllResourcesresultspageSize)methodfieldbatch_size_attributeN)asset_client_utilGetMessages#CloudassetSearchAllResourcesRequestr   VALUEScoreuniverse_domainGetr   YieldFromList	GetClientv1_BucketToRepo)r   gcr_buckets_search_requestgcr_bucketsbucketrepos        8lib/googlecloudsdk/api_lib/container/images/gcr_utils.pyListGCRReposr-       s      ##%II,**//??CCEFgN 1 J   ((!!#&& !%+ f Dj s   B%B/(B/T)frozenc                   &    e Zd ZU dZeed<   eed<   y)GCRUsagez{GCRUsage represents usage for a GCR repo.

  Attributes:
    repository: A GCR repo name.
    usage: Usage for the repo.
  
repositoryusageN)__name__
__module____qualname____doc__str__annotations__     r,   r0   r0   B   s     /	*r:   r0   r+   c                    	 t        j                  t        j                         | t        j                               5 }t        t        |       |j                               cddd       S # 1 sw Y   yxY w# t        j                  t        j                  f$ r(}t        t        |       t        |            cY d}~S d}~ww xY w)zjChecks usage for a GCR repo.

  Args:
    repo: A docker repository.

  Returns:
    A GCRUsage object.
  )basic_credsname	transportN)r   FromRegistryr	   CredentialProviderHttpr0   r7   check_usage_onlyr   V2DiagnosticExceptionTokenRefreshException)r+   res      r,   CheckGCRUsagerG   O   s    '		"	"++-))+
 
c$i!3!3!56
 
 
 ''''
 ' CIs1v&&	's:   <A7 #A+!	A7 +A40A7 4A7 7#CB=7C=Cr*   c                 Z   dt         j                  j                  j                  j	                          d}| j
                  j                  |      s#t        j                  | j
                   d       y| j
                  t        |      d }dt         j                  j                  j                  j	                          d}t        |      }| j                  j                  |      r| j                  j                  |      s#t        j                  | j                   d       y| j                  t        |      t        |        }|t        vr#t        j                  | j                   d       ydj                  ||j                  d	dd
            }t!        j"                  |      S )zConverts a GCS bucket to a GCR repo.

  Args:
    bucket: A CAIS ResourceSearchResult for a GCS bucket.

  Returns:
    A docker repository.
  z//cloudresourcemanager.z
/projects/z# is not a Project name. Skipping...Nz
//storage./z0 is not a Container Registry bucket. Skipping...z{region}gcr.io/{project}:   )regionproject)r   r    r!   r"   r#   parentFullResourceName
startswithr
   warninglen_BucketSuffixr=   endswith_VALID_GCR_REGION_PREFIXformatreplacer	   ValidateRepositoryPath)r*   project_prefix
project_idbucket_prefixbucket_suffixgcr_region_prefixgcr_repo_paths          r,   r'   r'   g   s}    -Z->->-C-C-S-S-W-W-Y,ZZde.		&	&	1	1.	AKK((
))LM ,,S-@-AB*z0055EEIIKLAN-
+-				.fkk6J6J7 KK;;-GH kk#m"4c-6H5HI66KK;;-GH ,33
(:(:3Q(G 4 - 
	$	$]	33r:   rY   c                 l    | j                  dd      }t        |      dk(  rd|d    d|d    dS d|  dS )	a  Converts a project ID to a GCR bucket suffix.

  Args:
    project_id: The project ID.

  Returns:
    A string representing the suffix of GCR buckets in the project. The suffix
    format is different for normal projects and domain-scoped projects. For
    example:

    my-proj           -> artifacts.my-proj.appspot.com
    my-domain:my-proj -> artifacts.my-proj.my-domain.a.appspot.com
  rJ   rK      z
artifacts..r   z.a.appspot.comz.appspot.com)splitrQ   )rY   chunkss     r,   rR   rR      sN     C#&[Aq	{!F1I;n==j\	..r:   )r6   dataclassestypingr   apitools.base.pyr   containerregistry.clientr   containerregistry.client.v2_2r   r   googlecloudsdk.api_lib.assetr   r   'googlecloudsdk.api_lib.container.imagesr	   googlecloudsdk.corer
   r   	frozensetrT   r7   
Repositoryr-   	dataclassr0   rG   r   ResourceSearchResultr'   rR   r9   r:   r,   <module>ro      s        ' 0 5 6 I 8 # *$%@A  +*@*@!A D d#	 	 $	'.. '8 '0'4)))+@@'4'4T/c /c /r:   