
    G                         S r SSKrSSKJr  SSKJr   " S S\R                  5      r " S S\5      rS	 r	S
 r
S rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS\S\S\S \4S! jrg)"z*Utilities for `gcloud network-management`.    N)Any)
exceptionsc                       \ rS rSrSrSrg)NetworkManagementError   z6Top-level exception for all Network Management errors. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       9lib/googlecloudsdk/command_lib/network_management/util.pyr   r      s    >r   r   c                       \ rS rSrSrSrg)InvalidInputError   zException for invalid input.r   Nr	   r   r   r   r   r      s    $r   r   c                    S/n[        U 5      S:  a  UR                  S5        [        U 5       H  u  p4UR                  SR                  XS95        US:X  a"  [        U 5      S:X  a  UR                  S5        MK  U[        U 5      S-
  :X  a  UR                  S	5        Mp  U[        U 5      S-
  :  d  M  UR                  S
5        M     UR                  S5        SR	                  U5      $ )zOCreates a message to specify at least one endpoint, separated by commas and or.zInvalid Connectivity Test.    zAt least one of z--{endpoint_type}-{endpoint})endpoint_typeendpointr      z or z, or z, z must be specified. )lenappend	enumerateformatjoin)	endpointsr   	error_msgindexr   s        r   "GetClearSingleEndpointAttrErrorMsgr#      s    ,-)^a'("9-oe&--' 	. 	

 zc)n)v	#i.1$	$w	Y!#	#t . ()		r   c                 0    U=R                   S-  sl         U$ )zXAdd locations/global to parent path, since it isn't automatically populated by apitools./locations/global)parent
unused_refunused_argsrequests      r   AppendLocationsGlobalToParentr+   6   s    	..''.	.r   c                 0    U=R                   S-  sl         U$ )Nr%   )namer'   s      r   "UpdateOperationRequestNameVariabler.   <   s    	,,%%,	.r   c                 `    UR                   nU(       d  Xl         U$ X;  a  US-   U -   Ul         U$ )z"Adds name of field to update mask.,)
updateMask)fieldpatch_requestupdate_masks      r   AddFieldToUpdateMaskr5   A   s>    ((+	$ 
 *S058M	r   c                 H    1 SknX;   a  [        XS 5        g [        XS5        g )N>   cloudFunctionappEngineVersioncloudRunRevisionr   )setattr)r   endpoint_nameproto_endpoint_fieldss      r   ClearEndpointValuer=   K   s(    
 +HT* HR(r   c                    U R                   n[        X15      n1 SknSnU H  n[        XGS5      (       d  M  US-  nM     US:  d  [        XBS5      (       d/  [        XB5        [        X1U5        X0l         [	        US-   U-   U 5      $ / SQnUS:X  a  UR                  / SQ5        US	:X  a  UR                  / S
Q5        [        [        X5      5      ezNChecks if given endpoint can be removed from Connectivity Test and removes it.>   gkePodinstance	ipAddressredisClusterr7   redisInstanceforwardingRuler8   r9   cloudSqlInstancegkeMasterClusterr   Nr   .)rA   z
ip-addresszgke-master-clusterzgke-podzcloud-sql-instancesource)zcloud-functionzapp-engine-versionzcloud-run-revisiondestination)zforwarding-rulezredis-instancezredis-clusterconnectivityTestgetattrr=   r:   r5   extendr   r#   	r3   r   r;   testr   endpoint_fieldsnon_empty_endpoint_fieldsr2   r    s	            r   ClearSingleEndpointAttrrS   Y       		'	'$T)(/  ex%%1$  "'t+ + x/D*%)"m+] I    	
 %  	
 *9D r   c                 p    / SQnU H+  u  pEnUR                  U5      (       d  M  [        UUU5      nM-     U$ z5Handles clear_source_* and clear_destination_* flags.))clear_source_instancerI   rA   )clear_source_ip_addressrI   rB   )clear_source_gke_master_clusterrI   rG   )clear_source_gke_podrI   r@   )clear_source_cloud_sql_instancerI   rF   )clear_source_cloud_functionrI   r7   )clear_source_app_engine_versionrI   r8   )clear_source_cloud_run_revisionrI   r9   )clear_destination_instancerJ   rA   )clear_destination_ip_addressrJ   rB   )$clear_destination_gke_master_clusterrJ   rG   )clear_destination_gke_podrJ   r@   )$clear_destination_cloud_sql_instancerJ   rF   )!clear_destination_forwarding_rulerJ   rE   ) clear_destination_redis_instancerJ   rD   )clear_destination_redis_clusterrJ   rC   )IsSpecifiedrS   r(   argsr3   flags_and_endpointsflagr   r;   s          r   ClearEndpointAttrsrl      sI    6 -@(d=-


m -@ 
r   c                    U R                   n[        X15      n1 SknSnU H  n[        XGS5      (       d  M  US-  nM     US:  d  [        XBS5      (       d/  [        XB5        [        X1U5        X0l         [	        US-   U-   U 5      $ / SQnUS:X  a  UR                  / SQ5        US	:X  a  UR                  / S
Q5        [        [        X5      5      er?   rK   rO   s	            r   ClearSingleEndpointAttrBetarn      rT   r   c                 p    / SQnU H+  u  pEnUR                  U5      (       d  M  [        UUU5      nM-     U$ rV   )rg   rn   rh   s          r   ClearEndpointAttrsBetarp      sI    6 -@(d=1


m -@ 
r   c                     SS/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )z5Checks if all provided instances are in valid format.source_instancedestination_instancezEprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/zones/[-\w]+/instances/[-\w]+zInvalid value for flag {}: {}
Expected instance in the following format:
  projects/my-project/zones/zone/instances/my-instancerecompilerg   rM   matchr   r   r(   ri   r*   flagsinstance_patternrk   rA   s          r   ValidateInstanceNamesr{     s     % ZZN d$h##H--EEKVF
 	
	  
.r   c                     SS/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )z4Checks if all provided networks are in valid format.source_networkdestination_networkz>projects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/global/networks/[-\w]+zxInvalid value for flag {}: {}
Expected network in the following format:
  projects/my-project/global/networks/my-networkrt   )r(   ri   r*   ry   network_patternrk   networks          r   ValidateNetworkURIsr   /  s     % JJG/ d#g""7++??Ev@
 	
	  
.r   c                     SS/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )zFChecks if all provided GKE Master Clusters URIs are in correct format.source_gke_master_clusterdestination_gke_master_clusterzPprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/(zones|locations)/[-\w]+/clusters/[-\w]+zInvalid value for flag {}: {}
Expected Google Kubernetes Engine master cluster in the following format:
  projects/my-project/location/location/clusters/my-clusterrt   )r(   ri   r*   ry   rz   rk   clusters          r   ValidateGKEMasterClustersURIsr   G  s     "&% ZZY d#g##G,,IIOJ
 	
	  
.r   c                     SS/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )zFChecks if all provided Cloud SQL Instances URIs are in correct format.source_cloud_sql_instancedestination_cloud_sql_instancez8projects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/instances/[-\w]+z~Invalid value for flag {}: {}
Expected Cloud SQL instance in the following format:
  projects/my-project/instances/my-instancert   rx   s          r   ValidateCloudSQLInstancesURIsr   ^  s     "&% ZZA d$h##H--::@&:P
 	
	  
.r   c                     S/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )zBChecks if all provided Cloud Functions URIs are in correct format.source_cloud_functionzIprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/locations/[-\w]+/functions/[-\w]+zInvalid value for flag {}: {}
Expected Cloud Function in the following format:
  projects/my-project/locations/location/functions/my-functionrt   )r(   ri   r*   ry   function_patternrk   functions          r   ValidateCloudFunctionsURIsr   s  s     % ZZR dD!!t"H!!(++K 6$!	   
.r   c                     S/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )zEChecks if all provided App Engine version URIs are in correct format.source_app_engine_versionzCapps/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/services/[-\w]+/versions/[-\w]+zInvalid value for flag {}: {}
Expected App Engine version in the following format:
  apps/my-project/services/my-service/versions/my-versionrt   )r(   ri   r*   ry   version_patternrk   versions          r   ValidateAppEngineVersionsURIsr     s     "% JJL/ dD!!d!G  ))FFLfG   
.r   c                     S/n[         R                  " S5      nU HV  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        SR                  XV5      5      e   U$ )zFChecks if all provided Cloud Run revisions URIs are in correct format.source_cloud_run_revisionzIprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/locations/[-\w]+/revisions/[-\w]+zInvalid value for flag {}: {}
Expected Cloud Run revision in the following format:
  projects/my-project/locations/location/revisions/my-revisionrt   )r(   ri   r*   ry   revision_patternrk   revisions          r   ValidateCloudRunRevisionsURIsr     s     "% ZZR dD!!t"H!!(++K 6$!	   
.r   c                     S/n[         R                  " S5      nU HG  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        S5      e   U$ )zCChecks if all provided forwarding rules URIs are in correct format.destination_forwarding_rulezVprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/(global|regions/[-\w]+)/forwardingRules/[-\w]+zInvalid value for flag {flag}: {forwarding_rule}
Expected forwarding rule in one of the following formats:
  projects/my-project/global/forwardingRules/my-forwarding-rule
  projects/my-project/regions/us-central1/forwardingRules/my-forwarding-ruleru   rv   rg   rM   rw   r   )r(   ri   r*   ry   forwarding_rule_patternrk   forwarding_rules          r   ValidateForwardingRulesURIsr     sw     $% JJ_ dD!!d)O"((99[   
.r   c                     S/n[         R                  " S5      nU HG  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        S5      e   U$ )zAChecks if all provided Redis Instance URIs are in correct format.destination_redis_instancezIprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/locations/[-\w]+/instances/[-\w]+zInvalid value for flag {flag}: {redis_instance}
Expected Redis Instance in the following format:
  projects/my-project/locations/location/instances/my-redis-instance
r   )r(   ri   r*   ry   redis_instance_patternrk   redis_instances          r   ValidateRedisInstancesURIsr     sq    '
(%::R dD!!T(N!''77U   
.r   c                     S/n[         R                  " S5      nU HG  nUR                  U5      (       d  M  [        X5      nUR	                  U5      (       a  M>  [        S5      e   U$ )z@Checks if all provided Redis Cluster URIs are in correct format.destination_redis_clusterzHprojects/(?:[a-z][a-z0-9-\.:]*[a-z0-9])/locations/[-\w]+/clusters/[-\w]+zInvalid value for flag {flag}: {redis_cluster}
Expected Redis Cluster in the following format:
  projects/my-project/locations/location/clusters/my-redis-cluster
r   )r(   ri   r*   ry   redis_cluster_patternrk   redis_clusters          r   ValidateRedisClustersURIsr     sq    &
'%**Q dD!!D'M &&}55S   
.r   c                 t    UR                  S5      (       a!  UR                  S5      (       d  [        S5      eU$ )zZChecks if destination-gke-master-cluster was specified when destination-fqdn is specified.destination_fqdnr   zXdestination-fqdn can only be specified when destination-gke-master-cluster is specified
)rg   r   )r(   ri   r*   s      r   ValidateFqdnr     sD    	())$2B2B&3 3 	9  
.r   r(   ri   r*   returnc                     SS/n[         R                  " S[         R                  5      nU HG  nUR                  U5      (       d  M  [	        X5      nUR                  U5      (       a  M>  [        S5      e   U$ )a8  Checks if all provided GKE Pod URIs are in correct format.

Args:
  unused_ref: The resource reference.
  args: The parsed command line arguments.
  request: The request proto message.

Returns:
  The request proto message.

Raises:
  InvalidInputError: If any provided GKE Pod URI is not in the correct format.
source_gke_poddestination_gke_poda  
      projects/
      (?:[a-z][a-z0-9-\.:]*[a-z0-9])  # Project ID
      /(zones|locations)/[-\w]+      # Zone or Region
      /clusters/[-\w]+              # Cluster Name
      /k8s/namespaces/[-\w]+        # Namespace
      /pods/[-\w]+                  # Pod Name
      a  Invalid value for flag {flag}: {gke_pod}
Expected GKE Pod in one of the following formats:
  projects/my-project/zones/zone/clusters/my-cluster/k8s/namespaces/my-namespace/pods/my-pod
  projects/my-project/locations/location/clusters/my-cluster/k8s/namespaces/my-namespace/pods/my-pod)ru   rv   VERBOSErg   rM   rw   r   )r(   ri   r*   ry   gke_pod_patternrk   gke_pods          r   ValidateGkePodsURIsr     s     2
3%JJ
 	jj
/ dD!!d!G  ))s   
.r   )r   ru   typingr   googlecloudsdk.corer   Errorr   r   r#   r+   r.   r5   r=   rS   rl   rn   rp   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s    1 	  *?Z-- ?%. %.
)4n%P4n%P00.*.0..((	&C &s &S &S &r   