
    t                      j   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ZdZdZd	Zd
ZdZ ej"                  de de d      Z ej"                  de de de de d	      Z G d d      Zd Z G d d      Zd Zd Zd Z G d d      Z G d d      Z G d d      Zd  Z G d! d"      Zd# Zd$ Z y)%z@Common utility functions for Developer Connect Insights Configs.    N)
exceptions)projects_api)utilz
google-mfpzF^([^\.]+)-docker.pkg.dev/([^/]+)/([^/]+)/([^@:]+)(@sha256:[a-f0-9]+)?$zprojects/([^/]+)z//apphub.googleapis.comz//container.googleapis.comz$([a-zA-Z0-9-._~%!$&'()*+,;=@]{1,64})z^(?:zL/)?projects/((?:[^:]+:.)?[a-z0-9\\-]+)/locations/([\w-]{2,40})/applications/$zP/)?projects/((?:[^:]+:.)?[a-z0-9\\-]+)/(locations|zones)/([\w-]{2,40})/clusters//k8s/namespaces//apps/deployments/c                       e Zd ZdZd Zd Zy)ProjectzRepresents a project.c                     t        j                  t        j                  |            }|j                  | _        |j                  | _        y N)r   Getprojects_utilParseProject	projectId
project_idprojectNumberproject_number)selfproject_identifierproject_detailss      8lib/googlecloudsdk/command_lib/developer_connect/name.py__init__zProject.__init__+   s>    "&&""#56O &//DO)77D    c                      d| j                    S )N	projects/)r   r   s    r   resource_namezProject.resource_name2   s    t'((r   N__name__
__module____qualname____doc__r   r    r   r   r
   r
   (   s    8)r   r
   c                 ^    t        j                  t        |       }|r|j                  d      S y)z)Extracts the project from a resource URI.   N)research_PROJECT_PATTERNgroupurimatchs     r   extract_projectr-   6   s'    
))$c
*%
;;q>	r   c                   (    e Zd ZdZd Zd Zd Zd Zy)ArtifactRegistryUriz/Parses and represents an Artifact Registry URI.c                 J    || _         || _        || _        || _        || _        y r   )	_location_project_repository_image_name_sha_suffix)r   locationproject
repository
image_name
sha_suffixs         r   r   zArtifactRegistryUri.__init__A   s(    DNDM!D!D!Dr   c                     | j                   S )zThe project ID.)r2   r   s    r   r   zArtifactRegistryUri.project_idH   s    ==r   c                     | j                   S )z@The SHA suffix (e.g., '@sha256:...') if present, otherwise None.)r5   r   s    r   r:   zArtifactRegistryUri.sha_suffixL   s    r   c                 l    | j                    d| j                   d| j                   d| j                   S )z(The artifact URI without the SHA suffix.z-docker.pkg.dev//)r1   r2   r3   r4   r   s    r   base_urizArtifactRegistryUri.base_uriP   s8    nn-dmm_Ad>N>N=OqQUQaQaPbccr   N)r   r    r!   r"   r   r   r:   r?   r#   r   r   r/   r/   >   s    7"dr   r/   c                 <   t        j                  t        |       }|rt        |j	                               dk7  ry|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        |||||      S )zValidates the artifact URI.   Fr%            )r&   r,   _ARTIFACT_URI_PATTERNlengroupsr)   r/   )r+   r,   r6   r7   r8   r9   r:   s          r   validate_artifact_urirH   U   s     (((#
.%	#elln%*[[^(KKN'{{1~*{{1~*{{1~*	Z
 r   c                 ,    | j                  t              S )z:Checks if the app hub application is a management project.)
startswith!_APPHUB_MANAGEMENT_PROJECT_PREFIX)app_hub_applications    r   is_management_projectrM   g   s    		'	'(I	JJr   c                 R    t        j                  t        j                  |             S )zValidates the build project.)r   r   r   r   )build_projects    r   validate_build_projectrP   l   s    			-44]C	DDr   c                   "    e Zd ZdZd Zd Zd Zy)
GKEClusterzRepresents a GKE cluster.c                 .    || _         || _        || _        y r   )r7   location_id
cluster_id)r   r7   rT   rU   s       r   r   zGKECluster.__init__t   s    DL"D DOr   c                     | j                   S r   )rU   r   s    r   idzGKECluster.idy   s    ??r   c                 `    t          d| j                   d| j                   d| j                   S )N
/projects//locations/
/clusters/)gke_service_prefixr7   rT   rU   r   s    r   r   zGKECluster.resource_name|   s4     !DLL>TEUEUDVV`aeapap`qrrr   N)r   r    r!   r"   r   rW   r   r#   r   r   rR   rR   q   s    !!
sr   rR   c                       e Zd ZdZd Zd Zy)GKENamespacezRepresents a GKE namespace.c                      || _         || _        y r   )gke_clusternamespace_id)r   r`   ra   s      r   r   zGKENamespace.__init__   s    "D$Dr   c           	          t          d| j                  j                   d| j                  j                   d| j                  j                   d| j
                   	S )NrY   rZ   r[   r   )r\   r`   r7   rT   rU   ra   r   s    r   r   zGKENamespace.resource_name   s     !D,<,<,D,D+E[QUQaQaQmQmPnnxy}  zJ  zJ  zU  zU  yV  Vf  gk  gx  gx  fy  z  zr   Nr   r#   r   r   r^   r^      s    #%zr   r^   c                       e Zd ZdZd Zd Zy)GKEWorkloadzRepresents a GKE workload.c                      || _         || _        y r   )gke_namespacedeployment_id)r   rf   rg   s      r   r   zGKEWorkload.__init__   s    
 'D&Dr   c                     t          d| j                  j                  j                   d| j                  j                  j                   d| j                  j                  j
                   d| j                  j                   d| j                   S )NrY   rZ   r[   r   r   )r\   rf   r`   r7   rT   rU   ra   rg   r   s    r   r   zGKEWorkload.resource_name   s     !D,>,>,J,J,R,R+SS^_c_q_q_}_}  `J  `J  _K  KU  VZ  Vh  Vh  Vt  Vt  V  V  U@  @P  QU  Qc  Qc  Qp  Qp  Pq  qC  DH  DV  DV  CW  X  Xr   Nr   r#   r   r   rd   rd      s    "'Xr   rd   c                 D   t         j                  |       }|rt        |j                               dk7  ryt	        t        t        |j                  d      |j                  d      |j                  d            |j                  d            |j                  d            S )z/Parses a GKE deployment URI into a GKEWorkload.   Fr%   rC   rD   rA   )rg   )gke_deployment_path_regex	fullmatchrF   rG   rd   r^   rR   r)   r*   s     r   parse_gke_deployment_urirm      s    
#
-
-c
2%	#elln%*	
kk!nkk!nkk!n
 ++a. KKN

 
r   c                   (    e Zd ZdZd Zd Zd Zd Zy)AppHubApplicationz"Represents an App Hub Application.c                 .    || _         || _        || _        y r   )r7   rT   application_id)r   r7   rT   rq   s       r   r   zAppHubApplication.__init__   s    DL"D(Dr   c                 h    d| j                   j                   d| j                   d| j                   S )Nr   rZ   z/applications/)r7   r   rT   rq   r   s    r   r   zAppHubApplication.resource_name   s5    t||../{4;K;K:LN[_[n[nZoppr   c                 .    | j                   j                  S )z2Returns the project ID of the app hub application.)r7   r   r   s    r   r   zAppHubApplication.project_id   s    <<"""r   c                 .    | j                   j                  S )z6Returns the project number of the app hub application.)r7   r   r   s    r   r   z AppHubApplication.project_number   s    <<&&&r   N)r   r    r!   r"   r   r   r   r   r#   r   r   ro   ro      s    *)
q#'r   ro   c                 6   t         j                  |       }|rt        |j                               dk7  rt	        d|        t        |j                  d            }|st	        d|        |j                  d      }|j                  d      }t        |||      S )z<Parses an App Hub Application URI into an AppHubApplication.rC   zapp_hub_application must be in the format //apphub.googleapis.com/projects/{project}/locations/{location}/applications/{application}: r%   rB   )app_hub_application_path_regexrl   rF   rG   
ValueErrorr
   r)   ro   )r+   r,   r7   r6   rq   s        r   parse_app_hub_application_urirx      s    
(
2
23
7%	#elln%*
	5	 
 EKKN#'	
	5	 
 [[^(;;q>.	x
 r   c           	         i }| s|S | D ]T  }|j                         D ]?  \  }}t        |      }	 t        |       |r|||j                         <   3t        d|        V |S # t        j                  $ r t        dj                  |            t        j                  $ r t        dj                  |            t        j                  $ r}t        d| d|       d}~ww xY w)z4Parses a list of artifact configs into a dictionary.zPermission denied when checking build project [{}]. Please ensure your account has necessary permissions or that the project exists.zSInvalid user provided build project ID [{}]. Please ensure it is a valid project IDz Error validating build project [z]: Nz<Inalid user provided artifact uri, please check the format: )itemsrH   rP   apitools_exceptionsHttpForbiddenErrorrw   formatHttpBadRequestErrorr   Errorr?   )user_artifact_configsartifact_configs_dictuser_config_datar+   rO   	valid_uries          r   parse_artifact_configsr      s   	  /.446]',i
}-" 
6Ci0023 !#
 	
/ 7 08 
/ !33 
**0&*?
 	

 !44 
  &} 5
 	
  
.}oSD
 	

s   A""A+C#CC#)!r"   r&   apitools.base.pyr   r{   +googlecloudsdk.api_lib.cloudresourcemanagerr   #googlecloudsdk.command_lib.projectsr   r   googlecloudsdk.corerK   rE   r(   apphub_service_prefixr\   name_segment_recompilerv   rk   r
   r-   r/   rH   rM   rP   rR   r^   rd   rm   ro   rx   r   r#   r   r   <module>r      sF    G 	 > D E *$0 !M  ' 1 1 9!+!""p  rA  qB  BC  D"  'BJJq  sB  rC  CS  Tc  Sd  dv  wF  vG  GH  I 
) )d d.$K
E
s sz zX X&' '(.!r   