
    >                     ^   S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SS	K
Jr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKrS rS rS rS rS r  SS jr!S r"S r#S r$S r%S r& SS jr'\RP                  " \RR                  RT                  \RR                  RV                  5      \RX                   " S S\RZ                  5      5       5       r.\RP                  " \RR                  R^                  5      \RX                   " S S\.5      5       5       r0g) z/Command for adding a path matcher to a URL map.    )absolute_import)division)unicode_literalsN)encoding)base_classes)arg_parsers)base)
exceptions)scope)flags)url_maps_utils)
propertiesc                      SSSS.$ )N Add a path matcher to a URL map.a  
*{command}* is used to add a path matcher to a URL map. A path
matcher maps HTTP request paths to backend services or backend
buckets. Each path matcher must be referenced by at least one
host rule. This command can create a new host rule through the
`--new-hosts` flag or it can reconfigure an existing host rule
to point to the newly added path matcher using `--existing-host`.
In the latter case, if a path matcher is orphaned as a result
of the operation, this command will fail unless
`--delete-orphaned-path-matcher` is provided. Path matcher
constraints can be found
[here](https://cloud.google.com/load-balancing/docs/url-map-concepts#pm-constraints).
a  
To create a rule for mapping the path ```/search/*``` to the
hypothetical ```search-service```, ```/static/*``` to the
```static-bucket``` backend bucket and ```/images/*``` to the
```images-service``` under the hosts ```example.com``` and
```*.example.com```, run:

  $ {command} MY-URL-MAP --path-matcher-name=MY-MATCHER --default-service=MY-DEFAULT-SERVICE --backend-service-path-rules='/search/*=search_service,/images/*=images-service' --backend-bucket-path-rules='/static/*=static-bucket' --new-hosts=example.com '*.example.com'

Note that a default service or default backend bucket must be
provided to handle paths for which there is no mapping.
)briefDESCRIPTIONEXAMPLES r       0lib/surface/compute/url_maps/add_path_matcher.py_DetailedHelpr   *   s     2
 r   c                 .   U R                  SSS9  U R                  SSSS9  U R                  S[        R                  " S	S
90 SSS9  U R                  5       nUR                  S[        R                  " S	S
9SSS9  UR                  SSS9  U R                  SSSSS9  U R                  SS9nUR                  SSS9  UR                  SSS9  U R                  S[        R                  " S	S
90 SSS9  U R                  S[        R                  " S	S
90 S S!S9  g")#zECommon arguments to add-path-matcher commands for each release track.z--descriptionz6An optional, textual description for the path matcher.)helpz--path-matcher-nameTz'The name to assign to the path matcher.)requiredr   z--path-rules   )
min_lengthzPATH=SERVICEz,Rules for mapping request paths to services.)typedefaultmetavarr   z--new-hostsNEW_HOSTzpIf specified, a new host rule with the given hosts is created and the path matcher is tied to the new host rule.)r   r   r   z--existing-hostz      An existing host rule to tie the new path matcher to. Although
      host rules can contain more than one host, only a single host
      is needed to uniquely identify the host rule.
      z--delete-orphaned-path-matcher
store_trueFzIf provided and a path matcher is orphaned as a result of this command, the command removes the orphaned path matcher instead of failing.)actionr   r   r   z--default-servicezA backend service that will be used for requests that the path matcher cannot match. Exactly one of --default-service or --default-backend-bucket is required.z--default-backend-bucketzA backend bucket that will be used for requests that the path matcher cannot match. Exactly one of --default-service or --default-backend-bucket is required.z--backend-service-path-rulesz--backend-bucket-path-ruleszPATH=BUCKETz3Rules for mapping request paths to backend buckets.N)add_argumentr   ArgDictadd_mutually_exclusive_groupArgList)parser	host_rulegroups      r   _Argsr+   K   s   C  
 	4   	!,9   113)!,?   
   	&  	 
-
-t
-
<%2    2   	$!,9   	#!,@  r   c                     U R                   R                  SU R                  R                  UR	                  5       UR
                  S94$ )z6Returns the request for the existing URL map resource.Get)urlMapproject)apitools_clienturlMapsmessagesComputeUrlMapsGetRequestNamer/   clienturl_map_refs     r   _GetGetRequestr8      sK     $$oo..!!#[-@-@ / 
 r   c                     U R                   R                  SU R                  R                  UR	                  5       UUR
                  S94$ )NUpdate)r.   urlMapResourcer/   )r0   r1   r2   ComputeUrlMapsUpdateRequestr4   r/   r6   r7   replacements      r   _GetSetRequestr?      sM    $$oo11!!#$%% 2 
 r   c                    [         R                  " U5      nUR                  (       d  UR                  (       d  S/nOUR                  nU(       a  [	        U5      nUR
                   HJ  nUR                   H7  nXd;   d  M
  [        R                  " SR                  XeR                  5      5      e   ML     UR
                  R                  U R                  R                  [        U5      UR                  S95        U$ SnUR
                   H2  nUR                   H  nXaR                  :X  d  M  Un  O   U(       d  M2    O   U(       d/  [        R                  " SR                  UR                  5      5      eSnUR
                   H9  nXW:X  a  UR                  Ul
        M  UR                  UR                  :X  d  M7  Sn  O   U(       a  UR                   (       a@  UR"                   V	s/ s H!  n	U	R$                  UR                  :w  d  M  U	PM#     sn	Ul        U$ [        R                  " SR                  WR                  5      5      eU$ s  sn	f )	zModifications to the URL map that are shared between release tracks.

Args:
  client: The compute client.
  args: the argparse arguments that this command was invoked with.
  existing: the existing URL map message.

Returns:
  A modified URL map message.
*zCannot create a new host rule with host [{0}] because the host is already part of a host rule that references the path matcher [{1}].)hostspathMatcherNzhNo host rule with host [{0}] exists. Check your spelling or use [--new-hosts] to create a new host rule.TFzThis operation will orphan the path matcher [{0}]. To delete the orphan path matcher, rerun this command with [--delete-orphaned-path-matcher] or use [gcloud compute url-maps edit] to modify the URL map by hand.)r   CopyProtoMessage	new_hostsexisting_hostset	hostRulesrB   compute_exceptionsArgumentErrorformatrC   appendr2   HostRulesortedpath_matcher_namedelete_orphaned_path_matcherpathMatchersname)
r6   argsexistingr>   rE   r)   hosttarget_host_rulepath_matcher_orphanedpath_matchers
             r   _ModifyBaserY      s)    ))(3+	 2 2II
 II''	//$"00%vd,A,AB  " (     #1G1G 	! 	
t 
] ''	//$%%%&

 " 
	 ( ,,99?  :  ! **			& $ 6 6				"2">">	> % +  
	*	* !) 5 5$
 5  $4$@$@@  5$
  
 !..< =CF%%=	
 	
 
$
s   (I
Ic                    [        XU5      n[        R                  " [        5      n	[        R                  " [        5      n
[        R
                  " UR                  5       H  u  pX   R                  U5        M     [        R
                  " UR                  5       H  u  pX   R                  U5        M     [        R
                  " UR                  5       H  u  pX   R                  U5        M     / n[        [        R
                  " U	5      5       Hf  u  pUR                  U R                  R                  [        U5      UR                  U[        X45      [!        U5      S9R#                  5       S95        Mh     [        [        R
                  " U
5      5       H  u  pU(       ac  UR                  U R                  R                  [        U5      UR                  U[        X45      [%        U5      S9R#                  5       S95        Mo  UR                  U R                  R                  [        U5      UR                  US[&        R(                  R*                  R,                  R.                  0SS9R#                  5       S95        M     UR0                  (       a&  [2        R4                  " X%XA5      R#                  5       nOLU(       a&  [2        R6                  " X&XA5      R#                  5       nOUR9                  X!5      R#                  5       nU R                  R;                  UUR<                  UR>                  US9nUR@                  R                  U5        U$ )z#Returns a modified URL map message.)params
collection)pathsservicer/   compute.backendBuckets)defaultServicedescriptionrR   	pathRules)!rY   collectionsdefaultdictrG   six	iteritems
path_rulesaddbackend_service_path_rulesbackend_bucket_path_rulesrN   rL   r2   PathRuleParse_GetBackendParamsForUrlMap%_GetBackendServiceCollectionForUrlMapSelfLink$_GetBackendBucketCollectionForUrlMapr   VALUEScorer/   	GetOrFaildefault_servicer   ResolveUrlMapDefaultService!ResolveUrlMapDefaultBackendBucketResolveAsResourcePathMatcherra   rO   rQ   )r6   	resourcesrS   url_mapr7   backend_service_argbackend_bucket_arg supports_regional_backend_bucketr>   service_map
bucket_mappathr^   bucketrg   r]   default_backend_urinew_path_matchers                     r   _Modifyr     s    F'2+ '',+&&s+*}}T__5mdT" 6}}T%D%DEmdT" FmmD$B$BCld4  D*s}}[9:ng  -OO1'G@I $  hj 	! 	
	 ; cmmJ78mf'
//
"
"5Moo3GIA'J &  
 # 	 
//
"
"5Moo#Z%6%6%;%;%C%C%M%MN5 &  
 # 	 90 
(DD;hj  (*LL
K
  /@@

  __00(""!!	 1  !!"23	r   c                     U R                   R                  SU R                  R                  UR	                  5       UR
                  UR                  S94$ )zAReturns the request to get an existing regional URL map resource.r-   )r.   r/   region)r0   regionUrlMapsr2   ComputeRegionUrlMapsGetRequestr4   r/   r   r5   s     r   _GetRegionalGetRequestr   i  sU     **oo44!!#%%## 5 
 r   c           	          U R                   R                  SU R                  R                  UR	                  5       UUR
                  UR                  S94$ )zDReturns the request to update an existing regional URL map resource.r:   )r.   r;   r/   r   )r0   r   r2   !ComputeRegionUrlMapsUpdateRequestr4   r/   r   r=   s      r   _GetRegionalSetRequestr   v  sX     **oo77!!#$%%##	 8 	
 	r   c                     S[         R                  R                  R                  R                  0n[        U S5      (       a   U R                  (       a  UR                  US'   U$ )Nr/   r   )r   rq   rr   r/   rs   hasattrr   )rz   r7   r[   s      r   rm   rm     sL    z((--55??@&WhGNN"))F8	-r   c                 J    [        U S5      (       a  U R                  (       a  gg)Nr   zcompute.regionBackendServiceszcompute.backendServicesr   r   rz   s    r   rn   rn     s    WhGNN*$r   c                 J    [        U S5      (       a  U R                  (       a  gg)Nr   zcompute.regionBackendBucketsr_   r   r   s    r   rp   rp     s    WhGNN)#r   c           
         UR                   nUR                  XR                  [        R                  R
                  S9n[        R                  " U5      (       a  [        Xg5      nO[        Xg5      nUR                  U/5      S   n	[        UUR                  U U	UUUU5      n
[        R                  " U5      (       a  [        XgU
5      nO[        XgU
5      nUR                  U/5      $ )z=Issues requests necessary to add path matcher to the Url Map.)default_scoper   )r6   rw   ry   compute_scope	ScopeEnumGLOBALr   IsRegionalUrlMapRefr   r8   MakeRequestsr   r   r?   )rS   holderurl_map_argbackend_servie_argr|   r}   r6   r7   get_requestrz   modified_url_mapset_requests               r   _Runr     s     ==&--
M,C,C,J,J . + ''44(=K 5K.q1'
&	 ''44(>NOK 6FGK			k]	++r   c                   L    \ rS rSrSr\" 5       rSrSrSr	Sr
\S 5       rS rSrg)AddPathMatcheri  r   NFc                 @   U R                   (       a  [        R                  " SS9U l        O[        R                  " SS9U l        [
        R                  " 5       U l        [        R                  " 5       U l
        U R                  R                  U5        [        U5        g )NFr#   )!_supports_regional_backend_bucketbackend_bucket_flags.RegionSupportingBackendBucketArgumentForUrlMapBACKEND_BUCKET_ARGBackendBucketArgumentForUrlMapbackend_service_flagsBackendServiceArgumentForUrlMapBACKEND_SERVICE_ARGr   UrlMapArgumentURL_MAP_ARGAddArgumentr+   )clsr(   s     r   ArgsAddPathMatcher.Args  s}    
,,

M
M 
 
=
=u
M 
 	==?  **,COOO'	&Mr   c                     [         R                  " U R                  5       5      n[        UUU R                  U R
                  U R                  U R                  5      $ )N)r   ComputeApiHolderReleaseTrackr   r   r   r   r   )selfrS   r   s      r   RunAddPathMatcher.Run  sR    **4+<+<+>?F  .. r   r   )__name__
__module____qualname____firstlineno____doc__r   detailed_helpr   r   r   r   classmethodr   r   __static_attributes__r   r   r   r   r     s<     )/-+&+# &	r   r   c                       \ rS rSrSrSrSrg)AddPathMatcherAlphai  r   Tr   N)r   r   r   r   r   r   r   r   r   r   r   r     s     )&*#r   r   )F)1r   
__future__r   r   r   rc   apitools.base.pyr   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   r	   "googlecloudsdk.command_lib.computer
   rI   r   r   2googlecloudsdk.command_lib.compute.backend_bucketsr   r   3googlecloudsdk.command_lib.compute.backend_servicesr   +googlecloudsdk.command_lib.compute.url_mapsr   googlecloudsdk.corer   re   r   r+   r8   r?   rY   r   r   r   rm   rn   rp   r   ReleaseTracksr   BETAGAUniverseCompatibleUpdateCommandr   ALPHAr   r   r   r   <module>r      s$    6 &  '  % 7 / ( O E > F * 
BSl	ZJ &+Qh
%$ &+%,P D%%**D,=,=,@,@A&T'' &  B&R D%%++,+. +  -+r   