
                         n   d 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 Zd Zd Zd Zd Z e
j0                  e
j2                  j4                  e
j2                  j6                  e
j2                  j8                         G d de
j:                               Zy)z0Command for removing a host rule from a URL map.    )absolute_import)division)unicode_literals)encoding)base_classes)base)
exceptions)scope)flags)url_maps_utilsc                      ddddS )N"Remove a host rule from a URL map.z
*{command}* is used to remove a host rule from a URL map. When
a host rule is removed, its path matcher is only removed if
it is not referenced by any other host rules and
`--delete-orphaned-path-matcher` is provided.
z
To remove a host rule that contains the host `example.com`
from the URL map named `MY-URL-MAP`, you can use this
command:

  $ {command} MY-URL-MAP --host=example.com
)briefDESCRIPTIONEXAMPLES r       0lib/surface/compute/url_maps/remove_host_rule.py_DetailedHelpr       s     /
 r   c                 r   t        j                  |      rL| j                  j                  d| j                  j                  |j                         |j                        fS | j                  j                  d| j                  j                  |j                         |j                  |j                        fS )z6Returns the request for the existing URL map resource.Get)urlMapproject)r   r   region)r   IsGlobalUrlMapRefapitools_clienturlMapsmessagesComputeUrlMapsGetRequestNamer   regionUrlMapsComputeRegionUrlMapsGetRequestr   )clienturl_map_refs     r   _GetGetRequestr%   4   s    %%k2""**EOO44"'');3F3F 5 HI I ""00%OO::"'')#++")) ; +, ,r   c                 v   t        j                  |      rM| j                  j                  d| j                  j                  |j                         ||j                        fS | j                  j                  d| j                  j                  |j                         ||j                  |j                        fS )NUpdate)r   urlMapResourcer   )r   r(   r   r   )r   r   r   r   r   ComputeUrlMapsUpdateRequestr    r   r!   !ComputeRegionUrlMapsUpdateRequestr   )r#   r$   replacements      r   _GetSetRequestr,   B   s    %%k2""**HOO77"'')*#++ 8 -. . ""00(OO=="'')*#++"))	 > +, ,r   c                 b   t        j                  |      }d}g }|j                  D ]8  }| j                  |j                  v r|j
                  }(|j                  |       : |s.t        j                  dj                  | j                              ||_        d}|j                  D ]  }|j
                  |k(  sd} n |sj| j                  r0|j                  D cg c]  }|j                  |k7  r| c}|_        |S t        j                  dj                  j
                              |S c c}w )z#Returns a modified URL map message.Nz%No host rule contains the host [{0}].FTzThis 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	hostRuleshosthostspathMatcherappendcompute_exceptionsArgumentErrorformatdelete_orphaned_path_matcherpathMatchersname)argsexistingr+   path_matcher_to_removenew_host_rules	host_rule#path_matcher_is_used_by_other_rulespath_matchers           r   _ModifyrA   R   sC   ))(3+.%%iyyIOO#(44I&	 & 
 

*
*/66tyyAC C )+(-%((i 66,0) )
 
-((+3+@+@"+@<"88 +@"k 
 ,,: ;A&##;%	& & 
"s   D,c                    |j                   }|j                  | |j                  t        j                  j
                        }t        ||      }|j                  |g      }t        | |d         }|j                  t        |||      g      S )z:Issues requests necessary to remove host rule on URL maps.)default_scoper   )
r#   ResolveAsResource	resourcescompute_scope	ScopeEnumGLOBALr%   MakeRequestsrA   r,   )r:   holderurl_map_argr#   r$   get_requestobjects
new_objects           r   _RunrO   {   s    ==&--
FM,C,C,J,J . L+v{3+.'tWQZ(*			nV[*MN	OOr   c                   8    e Zd ZdZ e       ZdZed        Zd Z	y)RemoveHostRuler   Nc                     t        j                         | _        | j                  j                  |       |j	                  ddd       |j	                  dddd	       y )
Nz--hostTz,One of the hosts in the host rule to remove.)requiredhelpz--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.)actiondefaultrT   )r   UrlMapArgumentURL_MAP_ARGAddArgumentadd_argument)clsparsers     r   ArgszRemoveHostRule.Args   sc    **,COOO'
;  = (	  r   c                 v    t        j                  | j                               }t        ||| j                        S )N)r   ComputeApiHolderReleaseTrackrO   rY   )selfr:   rJ   s      r   RunzRemoveHostRule.Run   s0    **4+<+<+>?Ffd..//r   )
__name__
__module____qualname____doc__r   detailed_helprY   classmethodr^   rc   r   r   r   rQ   rQ      s*     +/-+ 0r   rQ   N)rg   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   r4   r
   rF   +googlecloudsdk.command_lib.compute.url_mapsr   r   r   r%   r,   rA   rO   ReleaseTracksra   ALPHABETAGAUpdateCommandrQ   r   r   r   <module>ru      s     7 &  ' % 7 ( O E = F(,, &R	P D%%++T->->-C-C%%((*0T'' 0*0r   