
    A                         d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ  G d dej                        Z	 G d	 d
e	      Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)z*Utilities for `gcloud network-management`.    )absolute_import)division)unicode_literalsN)
exceptionsc                       e Zd ZdZy)NetworkManagementErrorz6Top-level exception for all Network Management errors.N__name__
__module____qualname____doc__     9lib/googlecloudsdk/command_lib/network_management/util.pyr   r      s    >r   r   c                       e Zd ZdZy)InvalidInputErrorzException for invalid input.Nr	   r   r   r   r   r      s    $r   r   c                    dg}t        |       dkD  r|j                  d       t        |       D ]  \  }}|j                  dj                  ||             |dk(  r t        |       dk(  r|j                  d       M|t        |       dz
  k(  r|j                  d	       p|t        |       dz
  k  s|j                  d
        |j                  d       dj	                  |      S )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-oeX&--'( 	. 	

 zc)n)v	#i.1$	$w	Y!#	#t . ()		r   c                 0    |xj                   dz  c_         |S )zXAdd locations/global to parent path, since it isn't automatically populated by apitools./locations/global)parent
unused_refunused_argsrequests      r   AppendLocationsGlobalToParentr)   8   s    	..''.	.r   c                 0    |xj                   dz  c_         |S )Nr#   )namer%   s      r   "UpdateOperationRequestNameVariabler,   >   s    	,,%%,	.r   c                 V    |j                   }|s	| |_         |S | |vr|dz   | z   |_         |S )z"Adds name of field to update mask.,)
updateMask)fieldpatch_requestupdate_masks      r   AddFieldToUpdateMaskr3   C   sB    ((+	$M 
 K*S058M	r   c                 J    h d}||v rt        | |d        y t        | |d       y )N>   cloudFunctionappEngineVersioncloudRunRevisionr   )setattr)r   endpoint_nameproto_endpoint_fieldss      r   ClearEndpointValuer;   M   s/    
 ++HmT* HmR(r   c                    | j                   }t        ||      }h d}d}|D ]  }t        ||d      s|dz  } |dkD  st        ||d      s2t        ||       t        |||       || _         t	        |dz   |z   |       S g d}|dk(  r|j                  g d       |d	k(  r|j                  g d
       t        t        ||            zNChecks if given endpoint can be removed from Connectivity Test and removes it.>
   instance	ipAddressredisClusterr5   redisInstanceforwardingRuler6   r7   cloudSqlInstancegkeMasterClusterr   Nr   .)r>   z
ip-addresszgke-master-clusterzcloud-sql-instancesource)zcloud-functionzapp-engine-versionzcloud-run-revisiondestination)zforwarding-rulezredis-instancezredis-clusterconnectivityTestgetattrr;   r8   r3   extendr   r!   	r1   r   r9   testr   endpoint_fieldsnon_empty_endpoint_fieldsr0   r   s	            r   ClearSingleEndpointAttrrP   [       		'	'$T=)(/  ex%1$  "'t+ x/D-*%)M"m+] I    	
 %  	
 *9mD r   c                 b    g d}|D ]%  \  }}}|j                  |      st        |||      }' |S z5Handles clear_source_* and clear_destination_* flags.))clear_source_instancerF   r>   )clear_source_ip_addressrF   r?   )clear_source_gke_master_clusterrF   rD   )clear_source_cloud_sql_instancerF   rC   )clear_source_cloud_functionrF   r5   )clear_source_app_engine_versionrF   r6   )clear_source_cloud_run_revisionrF   r7   )clear_destination_instancerG   r>   )clear_destination_ip_addressrG   r?   )$clear_destination_gke_master_clusterrG   rD   )$clear_destination_cloud_sql_instancerG   rC   )!clear_destination_forwarding_rulerG   rB   ) clear_destination_redis_instancerG   rA   )clear_destination_redis_clusterrG   r@   )IsSpecifiedrP   r&   argsr1   flags_and_endpointsflagr   r9   s          r   ClearEndpointAttrsrg      sH    2 -@(dM=-


m -@ 
r   c                    | j                   }t        ||      }h d}d}|D ]  }t        ||d      s|dz  } |dkD  st        ||d      s2t        ||       t        |||       || _         t	        |dz   |z   |       S g d}|dk(  r|j                  g d       |d	k(  r|j                  g d
       t        t        ||            r=   rH   rL   s	            r   ClearSingleEndpointAttrBetari      rQ   r   c                 b    g d}|D ]%  \  }}}|j                  |      st        |||      }' |S rS   )rb   ri   rc   s          r   ClearEndpointAttrsBetark      sH    2 -@(dM=1


m -@ 
r   c                     ddg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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compilerb   rJ   matchr   r   r&   rd   r(   flagsinstance_patternrf   r>   s          r   ValidateInstanceNamesrv     s     % ZZN dt$h##H-EEKVhF
 	
	  
.r   c                     ddg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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-networkro   )r&   rd   r(   rt   network_patternrf   networks          r   ValidateNetworkURIsr|   )  s     % JJG/ dd#g""7+??Evg@
 	
	  
.r   c                     ddg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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-clusterro   )r&   rd   r(   rt   ru   rf   clusters          r   ValidateGKEMasterClustersURIsr   A  s     "&% ZZY dd#g##G,IIOgJ
 	
	  
.r   c                     ddg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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-instancero   rs   s          r   ValidateCloudSQLInstancesURIsr   X  s~     "&% ZZA dt$h##H-::@&x:P
 	
	  
.r   c                     dg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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-functionro   )r&   rd   r(   rt   function_patternrf   functions          r   ValidateCloudFunctionsURIsr   m  s     % ZZR dD!tT"H!!(+K 6$!	   
.r   c                     dg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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-versionro   )r&   rd   r(   rt   version_patternrf   versions          r   ValidateAppEngineVersionsURIsr     s     "% JJL/ dD!dD!G  )FFLfGG   
.r   c                     dg}t        j                  d      }|D ]L  }|j                  |      st        ||      }|j	                  |      r3t        dj                  ||             |S )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-revisionro   )r&   rd   r(   rt   revision_patternrf   revisions          r   ValidateCloudRunRevisionsURIsr     s     "% ZZR dD!tT"H!!(+K 6$!	   
.r   c                     dg}t        j                  d      }|D ]<  }|j                  |      st        ||      }|j	                  |      r3t        d       |S )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rp   rq   rb   rJ   rr   r   )r&   rd   r(   rt   forwarding_rule_patternrf   forwarding_rules          r   ValidateForwardingRulesURIsr     ss     $% JJ_ dD!dD)O"((9[   
.r   c                     dg}t        j                  d      }|D ]<  }|j                  |      st        ||      }|j	                  |      r3t        d       |S )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&   rd   r(   rt   redis_instance_patternrf   redis_instances          r   ValidateRedisInstancesURIsr     sm    '
(%::R dD!T4(N!''7U   
.r   c                     dg}t        j                  d      }|D ]<  }|j                  |      st        ||      }|j	                  |      r3t        d       |S )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&   rd   r(   rt   redis_cluster_patternrf   redis_clusters          r   ValidateRedisClustersURIsr     sm    &
'%**Q dD!D$'M &&}5S   
.r   c                 `    |j                  d      r|j                  d      st        d      |S )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
)rb   r   )r&   rd   r(   s      r   ValidateFqdnr     s<    	()$2B2B&3 	9  
.r   )r   
__future__r   r   r   rp   googlecloudsdk.corer   Errorr   r   r!   r)   r,   r3   r;   rP   rg   ri   rk   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s    1 &  ' 	 *?Z-- ?%. %.
)2j#L2j#L00.*.0..((	r   