
    >                     Z   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Z dd	l
mZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZd Zd Zd Zd Zd Z 	 ddZ!d Z"d Z#d Z$d Z%d Z&	 ddZ' ejP                  ejR                  jT                  ejR                  jV                        ejX                   G d dejZ                                      Z. ejP                  ejR                  j^                        ejX                   G d de.                    Z0y) 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                      dddd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                    | j                  dd       | j                  ddd       | j                  dt        j                  d	
      i dd       | j                         }|j                  dt        j                  d	
      dd       |j                  dd       | j                  dddd       | j                  d      }|j                  dd       |j                  dd       | j                  dt        j                  d	
      i dd       | j                  dt        j                  d	
      i d d!       y")#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                     | j                   j                  d| j                  j                  |j	                         |j
                        fS )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                     | j                   j                  d| j                  j                  |j	                         ||j
                        fS )NUpdate)r.   urlMapResourcer/   )r0   r1   r2   ComputeUrlMapsUpdateRequestr4   r/   r6   r7   replacements      r   _GetSetRequestr?      sM    $$oo11!!#$%% 2 
 r   c                 4   t        j                  |      }|j                  s|j                  sdg}n|j                  }|rt	        |      }|j
                  D ]F  }|j                  D ]5  }||v st        j                  dj                  ||j                               H |j
                  j                  | j                  j                  t        |      |j                               |S d}|j
                  D ]*  }|j                  D ]  }||j                  k(  s|} n |s* n |s.t        j                  dj                  |j                              d}|j
                  D ]6  }||k(  r|j                  |_
        |j                  |j                  k(  s4d} n |rt|j                   r:|j"                  D 	cg c]  }	|	j$                  |j                  k7  r|	 c}	|_        |S t        j                  dj                  j                              |S c c}	w )	a  Modifications 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''	//$9"00%vdI,A,AB  " (     #1G1G 	! 	
t 
] ''	//$4%%%&

 " 
 ( ,,99?  :  ! **		&	& $ 6 6				"2">">	> % +  
	*	* !) 5 5$
 5  $4$@$@@  5$
  
 !..< =CF%%=	
 	
 
$
s   9"Hc                    t        | ||      }t        j                  t              }	t        j                  t              }
t	        j
                  |j                        D ]  \  }}|	|   j                  |        t	        j
                  |j                        D ]  \  }}|	|   j                  |        t	        j
                  |j                        D ]  \  }}|
|   j                  |        g }t        t	        j
                  |	            D ]m  \  }}|j                  | j                  j                  t        |      |j                  |t        ||      t!        |            j#                                      o t        t	        j
                  |
            D ]  \  }}|ri|j                  | j                  j                  t        |      |j                  |t        ||      t%        |            j#                                      q|j                  | j                  j                  t        |      |j                  |dt&        j(                  j*                  j,                  j.                  id      j#                                       |j0                  r't3        j4                  ||||      j#                         }nI|r't3        j6                  ||||      j#                         }n |j9                  ||      j#                         }| j                  j;                  ||j<                  |j>                  |      }|j@                  j                  |       |S )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    FD'2+ '',+&&s+*}}T__5mdGT" 6}}T%D%DEmdGT" FmmD$B$BCldFv4  D*s}}[9:ngu  -OO1';G@I $  hj 	! 	
	 ; cmmJ78mfe'
//
"
"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                     | j                   j                  d| j                  j                  |j	                         |j
                  |j                        fS )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                     | j                   j                  d| j                  j                  |j	                         ||j
                  |j                        fS )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                     dt         j                  j                  j                  j                  i}t        | d      r| j                  r|j                  |d<   |S )Nr/   r   )r   rq   rr   r/   rs   hasattrr   )rz   r7   r[   s      r   rm   rm     sI    z((--55??@&WhGNN"))F8	-r   c                 6    t        | d      r| j                  ryy)Nr   zcompute.regionBackendServiceszcompute.backendServicesr   r   rz   s    r   rn   rn     s    WhGNN*$r   c                 6    t        | d      r| j                  ryy)Nr   zcompute.regionBackendBucketsr_   r   r   s    r   rp   rp     s    WhGNN)#r   c           
         |j                   }|j                  | |j                  t        j                  j
                        }t        j                  |      rt        ||      }nt        ||      }|j                  |g      d   }	t        ||j                  | |	||||      }
t        j                  |      rt        |||
      }nt        |||
      }|j                  |g      S )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     ==&--
FM,C,C,J,J . + ''4(=K 5K.q1'
&	 ''4(>NOK 6FGK			k]	++r   c                   D    e Zd ZdZ e       ZdZdZdZdZ	e
d        Zd Zy)AddPathMatcherr   NFc                 :   | j                   rt        j                  d      | _        nt        j                  d      | _        t        j                         | _        t        j                         | _
        | j                  j                  |       t        |       y )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   ArgszAddPathMatcher.Args  s}    
,,

M
M 
 
=
=u
M 
 	==?  **,COOO'	&Mr   c                     t        j                  | j                               }t        ||| j                  | j
                  | j                  | j                        S )N)r   ComputeApiHolderReleaseTrackr   r   r   r   r   )selfrS   r   s      r   RunzAddPathMatcher.Run  sR    **4+<+<+>?F  .. r   )__name__
__module____qualname____doc__r   detailed_helpr   r   r   r   classmethodr   r   r   r   r   r   r     s<     )/-+&+# &	r   r   c                       e Zd ZdZdZy)AddPathMatcherAlphar   TN)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   