
    *                        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 ddlm	Z	 ddl
mc mc mc mc mZ ddlmc mc mc mc mZ dZd	Zd
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edejF                  defdZ$ G d dejJ                        Z&d Z'd  Z(d! Z)d" Z*y)#z,Utilities for `gcloud network-connectivity`.    )absolute_import)division)unicode_literalsN)Any)
exceptionsz	projects/z#location:projects/{0}/locations/{1}z-type:DYNAMIC_ROUTEc                       e Zd ZdZy)NetworkConnectivityErrorz8Top-level exception for all Network Connectivity errors.N__name__
__module____qualname____doc__     ;lib/googlecloudsdk/command_lib/network_connectivity/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   a  
    table(
      name.basename(),
      name.segment(3):label=LOCATION,
      hub.basename(),
      group.basename(),
      format(
        "{0}{1}{2}{3}{4}",
        linkedVpnTunnels.yesno(yes="VPN tunnel", no=""),
        linkedInterconnectAttachments.yesno(yes="VLAN attachment", no=""),
        linkedRouterApplianceInstances.yesno(yes="Router appliance", no=""),
        linkedVpcNetwork.yesno(yes="VPC network", no=""),
        gateway.yesno(yes="Gateway", no="")
      ):label=TYPE,
      firstof(linkedVpnTunnels.uris, linkedInterconnectAttachments.uris, linkedRouterApplianceInstances.instances).len().yesno(no="1"):label="RESOURCE COUNT",
      format(
        "{0}{1}",
        linkedVpcNetwork.yesno(yes="N/A", no=""),
        firstof(linkedVpnTunnels.siteToSiteDataTransfer, linkedInterconnectAttachments.siteToSiteDataTransfer, linkedRouterApplianceInstances.siteToSiteDataTransfer, Gateway).yesno(yes="On", no="")
      ).yesno(no="Off"):label="DATA TRANSFER",
      description
    )
an  
    table(
      name.basename(),
      group.basename(),
      name.segment(1):label=PROJECT,
      name.segment(3):label=LOCATION,
      spokeType:label=TYPE,
      state,
      reasons.code.list():label="STATE REASON",
      etag,
      format(
        "{0}{1}",
        linkedVpcNetwork.yesno(yes="N/A", no=""),
        firstof(linkedVpnTunnels.siteToSiteDataTransfer, linkedInterconnectAttachments.siteToSiteDataTransfer, linkedRouterApplianceInstances.siteToSiteDataTransfer, gateway).yesno(yes="On", no="")
      ).yesno(no="Off").if(view=detailed):label="DATA TRANSFER",
      description.if(view=detailed)
    )
c                 0    |xj                   dz  c_         |S )z$Add locations/global to parent path.z/locations/global)parent
unused_refunused_argsrequests      r   AppendLocationsGlobalToParentr   X   s     
..''.	.r   c                 z    |j                  d      s)|j                  s|j                  rd|j                  _        |S )z3Set legacy export_psc field based on new PSC flags.
export_pscT)IsSpecified6export_psc_published_services_and_regional_google_apisexport_psc_global_google_apis'googleCloudNetworkconnectivityV1betaHub	exportPscr   argsr   s      r   SetExportPscBetar$   _   s7    			,	' 	CC--BFg55?	.r   c                     t         | vrt        dj                  |             | | j                  t               t	        t               z   d }|j                  d      d   }|S )z+Returns the project from a resource string.z7Resource must contain a project path, but received: {0}N/r   )PROJECTS_RESOURCE_PATHr   formatindexlensplit)resourceprojects     r   DeriveProjectFromResourcer.   k   si    8+
AHH	
 
 nn+,s3I/JJL' MM#q!'	.r   c                     |j                  d      rR|j                  }t        |j                        }t        j                  ||      }dj                  |t              |_        |S )zGAppend filter to limit listing dynamic routes at an effective location.effective_locationz
{0} OR {1})r   r0   r.   r   LOCATION_FILTER_FMTr(   ROUTE_TYPE_FILTERfilter)r   r#   r   locationr-   location_filters         r   AppendEffectiveLocationFilterr6   z   sX     
*+&&H'7G)00(CO!((:KLGN	.r   c                       y)zSet default location to global.globalr   r   r   r   SetGlobalLocationr9      s    	r   c                 |    |j                  d      r*|j                  r|xj                  dz  c_        |S d|_        |S )zHandles clear_overlaps flag.clear_overlapsz	,overlapsoverlapsr   
updateMaskr   r#   patch_requests      r   ClearOverlapsrA      sE     
&'+- 
 ",m	r   c                 |    |j                  d      r*|j                  r|xj                  dz  c_        |S d|_        |S )zHandles clear_labels flag.clear_labelsz,labelslabelsr=   r?   s      r   ClearLabelsrE      sD     
n%)+ 
 "*m	r   c                    |j                  d      r|j                  dk7  r/|j                  d      s|j                  d      rt        d      |S |j                  d      r|j                  d      st        d      |j                  d      r|j                  dk7  rt        d	      |j                  st        d
      |j
                  st        d      |j                  |j
                  k(  rt        d      |S )z.Validates internal range migration parameters.usagezfor-migrationmigration_sourcemigration_targetz_migration_source and migration_target can only be specified when usage is set to for-migration.z=Both migration_source and migration_target must be specified.peeringzfor-selfzCPeering must be set to for-self when usage is set to for-migration.z!migration_source cannot be empty.z!migration_target cannot be empty.z9migration_source and migration_target cannot be the same.)r   rG   r   rJ   rH   rI   r"   s      r   ValidateMigrationrK      s   			'	"djjO&C*+t/?/?0 , 
 n			,	-T5E5E6 G  
i T\\Z%?
M  
		
?
@@			
?
@@	d333
C  
.r   refr#   r   c                     ~ |j                  d      r<|j                  dk(  r-|j                  d      r|j                  dk  rt        d      |S |j                  d      rt        d      |S )z,Validates internal range allocation options.allocation_strategyRANDOM_FIRST_N_AVAILABLE"first_available_ranges_lookup_size   zwfirst_available_ranges_lookup_size must be set and greater than 0 when allocation_strategy is RANDOM_FIRST_N_AVAILABLE.zhfirst_available_ranges_lookup_size can only be set when allocation_strategy is RANDOM_FIRST_N_AVAILABLE.)r   rN   rP   r   )rL   r#   r   s      r   ValidateAllocationOptionsrR      s     

,-

"
"&@
@ AB22Q6C  
. <=
	<  
.r   c                   &     e Zd ZdZ	 d fd	Z xZS )StoreGlobalActionz1Return "global" if the --global argument is used.c                 8    t         t        |   ||d|||       y )Nr8   )option_stringsdestconstdefaultrequiredhelp)superrT   __init__)selfrV   rW   rY   rZ   r[   	__class__s         r   r]   zStoreGlobalAction.__init__   s,     

T+% , r   ) FN)r   r   r   r   r]   __classcell__)r_   s   @r   rT   rT      s     : DH
 
r   rT   c                     |j                   r3t        j                  j                  j                  |j
                  _        |S )zSet the route's `recipient` field based on boolean flags.

  Args:
    args: The command arguments.
    request: The request to set the `recipient` field on.

  Returns:
    The request with the `recipient` field set.
  )advertise_to_hubv1beta:GoogleCloudNetworkconnectivityV1betaGatewayAdvertisedRouteRecipientValueValuesEnumADVERTISE_TO_HUB:googleCloudNetworkconnectivityV1betaGatewayAdvertisedRoute	recipientr"   s      r   "SetGatewayAdvertisedRouteRecipientrj      s7     
IIbbss FFP 
.r   c                 v    d|j                   v }|j                   j                  d      }|r|st        d      |S )a  If a spoke name is specified, then its region must also be specified.

  This is because CCFE doesn't support a wildcard ("-") in this case but returns
  a confusing error message. So we give the user a friendlier error.

  Args:
    request: The request object. We will inspect the parent field.

  Returns:
    The unmodified request object.
  Raises:
    InvalidInputError: If the region is unspecified when a spoke is.
  z/locations/-/z	/spokes/-z7A region must be specified if a spoke name is specifiedr   endswithr   )r   r   r   region_wildcardspoke_wildcards        r   $CheckRegionSpecifiedIfSpokeSpecifiedrp      sB     $w~~5/>>**;7.^
A  
.r   c                     d|j                   v }|j                   j                  d      }|r|st        d      |r|st        d      |S )a  Check that hub and route table are both specified or both unspecified.

  This is because CCFE doesn't support wildcards ("-") in this case but returns
  a confusing error message. So we give he user a friendlier error.

  Args:
   request: The request object.

  Returns:
    The unmodified request object.
  Raises:
    InvalidInputError: If the user needs to specify a hub name or route table
    name.
  z/hubs/-/z/routeTables/-z5A hub must be specified if a route table is specifiedz5A route table must be specified if a hub is specifiedrl   )r   r   r   hub_wildcardroute_table_wildcards        r   (CheckForRouteTableAndHubWildcardMismatchrt     s\     w~~-, 001AB.
?  ,
?  
.r   c                     t        |j                  d      s|S |j                  j                  t        j                  j
                  j                  k7  r|S t        d      )a  Reject requests with HYBRID_INSPECTION preset topology.

  Args:
    request: A CreateHubRequest object.

  Returns:
    The unmodified request object.
  Raises:
    InvalidInputError: If the CreateHubRequest has the HYBRID_INSPECTION preset
    topology.
  presetTopologyzHYBRID_INSPECTION unsupported in the GA component; use the beta component instead. See https://cloud.google.com/sdk/gcloud#release_levels)hasattrhubrv   v1HubPresetTopologyValueValuesEnumHYBRID_INSPECTIONr   r   s      r   ProhibitHybridInspectionr}   0  sT     
.	/Nkk  ff**<<=N?	 r   )+r   
__future__r   r   r   argparsetypingr   googlecloudsdk.corer   \googlecloudsdk.generated_clients.apis.networkconnectivity.v1.networkconnectivity_v1_messagesgenerated_clientsapisnetworkconnectivityry   networkconnectivity_v1_messagesdgooglecloudsdk.generated_clients.apis.networkconnectivity.v1beta.networkconnectivity_v1beta_messagesrd   #networkconnectivity_v1beta_messagesr'   r1   r2   Errorr	   r   LIST_FORMATLIST_SPOKES_FORMATr   r$   r.   r6   r9   rA   rE   rK   	NamespacerR   _StoreConstActionrT   rj   rp   rt   r}   r   r   r   <module>r      s    3 &  '   * i i i u u u % ; ) Az// A%0 %
0 (	
D3 h.@.@ 3 022 $".8r   