
    >@                        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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$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- ej\                  ej^                  j`                  ej^                  jb                  ej^                  jd                        ejf                   G d! d"ejh                                      Z5y#)%zImport URL maps command.    )absolute_import)division)unicode_literals)
exceptions)base_classes)base)flags)scope)url_maps_utils)util)log)yaml_validator)
console_ioc                      ddddS )NImport a URL map.zC          Imports a URL map's configuration from a file.
          zp          A URL map can be imported by running:

            $ {command} NAME --source=<path-to-file>
          )briefDESCRIPTIONEXAMPLES r       &lib/surface/compute/url_maps/import.py_DetailedHelpr   #   s     
 r   c                 |    | t         j                  j                  k(  ry| t         j                  j                  k(  ryy)z3Returns the API version based on the release track.alphabetav1)r   ReleaseTrackALPHABETA)release_tracks    r   _GetApiVersionr!   4   s3    d''---))...	r   c                 F    t        j                  dt        |       d|      S )z!Returns the resource schema path.computeUrlMapfor_help)export_utilGetSchemaPathr!   )r    r&   s     r   _GetSchemaPathr)   =   s%    		"	".8
M Mr   c                 .   |j                         dk(  r{| j                  j                  }| j                  j                  j                  | j                  j                  |j                  |j                  |j                         |            }no| j                  j                  }| j                  j                  j                  | j                  j                  |j                  |j                         |            }t        j                  ||||d      S )a
  Sends a URL map patch request and waits for the operation to finish.

  Args:
    client: The API client.
    resources: The resource parser.
    url_map_ref: The URL map reference.
    replacement: The URL map to patch with.

  Returns:
    The operation result.
  compute.regionUrlMaps)projectregionurlMapurlMapResource)r,   r.   r/   zUpdating URL map)
Collectionapitools_clientregionUrlMapsPatchmessages ComputeRegionUrlMapsPatchRequestr,   r-   NameurlMapsComputeUrlMapsPatchRequestr   WaitForOperation)client	resourcesurl_map_refreplacementservice	operations         r   _SendPatchRequestr@   C   s     !88$$22G&&44::88''%%##%&	 	9 	()I $$,,G&&..4422''##%& 	3 	()I 
	(	(GY)46H
J Jr   c                    |j                         dk(  rl| j                  j                  }| j                  j                  j                  | j                  j                  |j                  |j                  |            }n`| j                  j                  }| j                  j                  j                  | j                  j                  |j                  |            }t        j                  ||||d      S )a  Sends a URL map insert request and waits for the operation to finish.

  Args:
    client: The API client.
    resources: The resource parser.
    url_map_ref: The URL map reference.
    url_map: The URL map to insert.

  Returns:
    The operation result.
  r+   )r,   r-   r.   )r,   r.   zCreating URL map)r0   r1   r2   Insertr4   !ComputeRegionUrlMapsInsertRequestr,   r-   r7   ComputeUrlMapsInsertRequestr   r9   )r:   r;   r<   url_mapr>   r?   s         r   _SendInsertRequestrF   c   s     !88$$22G&&44;;99''%% 	: 	I $$,,G&&..5533'' 	4 	9:I 
	(	(GY)46H
J Jr   c                     g }t        | d      r|j                  |dz          t        | d      r|j                  |dz          |S )z7Gets a list of fields cleared by the user for Duration.secondsnanos)hasattrappend)durationfield_prefixcleared_fieldss      r   _GetClearedFieldsForDurationrO      sG    .Xy!,23Xw,01	r   c                     g }| j                   s|j                  |dz          | j                  s|j                  |dz          |S )z9Gets a list of fields cleared by the user for UrlRewrite.pathPrefixRewritehostRewrite)rQ   rK   rR   )url_rewriterM   rN   s      r   _GetClearedFieldsForUrlRewriterT      sF    .		&	&,)<<=		 	 ,67	r   c                    g }| j                   s|j                  |dz          t        | d      r|j                  |dz          | j                  s|j                  |dz          |S |t	        | j                  |dz         z   }|S )z:Gets a list of fields cleared by the user for RetryPolicy.retryConditions
numRetriesperTryTimeoutzperTryTimeout.)rV   rK   rJ   rX   rO   )retry_policyrM   rN   s      r   _GetClearedFieldsForRetryPolicyrZ      s    .		%	%,)::;\<(,56		#	#,89 
 $&B""L3C$C'E EN	r   c                 J    g }| j                   s|j                  |dz          |S )zBGets a list of fields cleared by the user for RequestMirrorPolicy.backendService)r\   rK   )mirror_policyrM   rN   s      r   '_GetClearedFieldsForRequestMirrorPolicyr^      s*    .		%	%,)99:	r   c                 
   g }| j                   s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          | j
                  s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          |S )	z9Gets a list of fields cleared by the user for CorsPolicy.allowOriginsallowOriginRegexesallowMethodsallowHeadersexposeHeadersmaxAgeallowCredentialsdisabled)	r`   rK   ra   rb   rc   rd   re   rf   rg   )cors_policyrM   rN   s      r   _GetClearedFieldsForCorsPolicyri      s    .		!	!,78		'	',)==>		!	!,78		!	!,78		"	",89			,12		%	%,);;<			,34	r   c                     g }| j                   s|j                  |dz          n|t        | j                   |dz         z   }| j                  s|j                  |dz          |S )z=Gets a list of fields cleared by the user for HttpFaultDelay.
fixedDelayzfixedDelay.
percentage)rk   rK   rO   rl   )fault_delayrM   rN   s      r   _GetClearedFieldsForFaultDelayrn      sg    .			,56#&B} <'> >N			,56	r   c                     g }| j                   s|j                  |dz          | j                  s|j                  |dz          |S )z=Gets a list of fields cleared by the user for HttpFaultAbort.
httpStatusrl   )rp   rK   rl   )fault_abortrM   rN   s      r   _GetClearedFieldsForFaultAbortrr      sE    .			,56			,56	r   c                     g }| j                   s|j                  |dz          n|t        | j                   |dz         z   }| j                  s|j                  |dz          |S |t	        | j                  |dz         z   }|S )zCGets a list of fields cleared by the user for FaultInjectionPolicy.delayzdelay.abortzabort.)rt   rK   rn   ru   rr   )fault_injection_policyrM   rN   s      r   (_GetClearedFieldsForFaultInjectionPolicyrw      s     .		%	%,01#&D$$lX&='? ?N		%	%,01 
 $&D$$lX&='? ?N	r   c                 (   g }| j                   s|j                  |dz          | j                  s|j                  |dz          n|t        | j                  |dz         z   }| j                  s|j                  |dz          n|t        | j                  |dz         z   }| j                  s|j                  |dz          n|t        | j                  |dz         z   }| j                  s|j                  |dz          n|t        | j                  |d	z         z   }| j                  s|j                  |d
z          n|t        | j                  |dz         z   }| j                  s|j                  |dz          |S |t        | j                  |dz         z   }|S )z>Gets a list of fields cleared by the user for HttpRouteAction.weightedBackendServices
urlRewritezurlRewrite.timeoutztimeout.retryPolicyzretryPolicy.requestMirrorPolicyzrequestMirrorPolicy.
corsPolicyzcorsPolicy.faultInjectionPolicyzfaultInjectionPolicy.)ry   rK   rz   rT   r{   rO   r|   rZ   r}   r^   r~   ri   r   rw   )route_actionrM   rN   s      r   _GetClearedFieldsForRoutActionr      s   .		-	-,)BBC		 	 ,56#&D!='? ?N			,23#&BlZ7'9 9N		!	!,67#&E  ,"?'A AN		)	),)>>?#&M((,9O*O'Q QN		 	 ,56#&D!='? ?N		*	*,)??@
 
 $&N))..'0 0N 
r   c                     g }| j                   s|j                  |dz          | j                  s|j                  |dz          |S )zHGets a list of fields cleared by the user for CustomErrorResponsePolicy.errorResponseRuleserrorService)r   rK   r   )custom_error_response_policyrM   rN   s      r   -_GetClearedFieldsForCustomErrorResponsePolicyr     sH     .	%	8	8,)==>	%	2	2,78	r   c                    g }| j                   s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          | j
                  s|j                  |dz          | j                  s|j                  |dz          |S )z:Gets a list of fields cleared by the user for UrlRedirect.hostRedirectpathRedirectprefixRedirectredirectResponseCodehttpsRedirect
stripQuery)r   rK   r   r   r   r   r   )url_redirectrM   rN   s      r   _GetClearedFieldsForUrlRedirectr     s    .		"	",78		"	",78		$	$,)99:		*	*,)??@		#	#,89		 	 ,56	r   c                 
   g }| j                   s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          | j                  s|j                  |dz          |S )z;Gets a list of fields cleared by the user for HeaderAction.requestHeadersToRemoverequestHeadersToAddresponseHeadersToRemoveresponseHeadersToAdd)r   rK   r   r   r   )header_actionrM   rN   s      r    _GetClearedFieldsForHeaderActionr   .  s    .		-	-,)AAB		*	*,)>>?		.	.,)BBC		+	+,)??@	r   c                 X   |j                   }|j                  }|j                  | |t        j                  j
                  t        j                  |            }t        j                  | j                  xs dd      }	 t        j                  |j                  j                  |t        |            }|j(                  |j+                         k7  rt-        j.                  d       	 t1        j2                  ||      }
|
|k(  ryt        j<                  d	j?                  |j+                               d
       |j@                  s|
j@                  |_         |jB                  r&t-        j.                  d       |
jB                  |_!        g }|jD                  s|jG                  d       |jH                  s|jG                  d       |jJ                  s|jG                  d       |jL                  s|jG                  d       |jN                  s|jG                  d       |jP                  s|jG                  d       n|tS        |jP                  d      z   }|jT                  s|jG                  d       n|tW        |jT                  d      z   }|jX                  s|jG                  d       n|t[        |jX                  d      z   }|j\                  s|jG                  d       n|t_        |j\                  d      z   }|j`                  jc                  |      5  te        ||||      cddd       S # t         j"                  $ r#}	t%        j"                  t'        |	            d}	~	ww xY w# t4        j6                  $ r)}|j8                  dk7  r|t;        ||||      cY d}~S d}~ww xY w# 1 sw Y   yxY w)z-Issues requests necessary to import URL maps.)default_scopescope_lister-F)binary)message_typestreamschema_pathNzThe name of the Url Map must match the value of the 'name' attribute in the YAML file. Future versions of gcloud will fail with an error.i  z"Url Map [{0}] will be overwritten.T)messagecancel_on_nozAn up-to-date fingerprint must be provided to update the Url Map. Future versions of gcloud will fail with an error '412 conditionNotMet'description	hostRulespathMatcherstestsdefaultService defaultCustomErrorResponsePolicyz!defaultCustomErrorResponsePolicy.defaultRouteActionzdefaultRouteAction.defaultUrlRedirectzdefaultUrlRedirect.headerActionzheaderAction.)3r:   r;   ResolveAsResourcecompute_scope	ScopeEnumGLOBALcompute_flagsGetDefaultScopeListerr   ReadFromFileOrStdinsourcer'   Importr4   r$   r)   r   ValidationErrorcompute_exceptionsstrnamer6   r   warningr   SendGetRequestapitools_exceptions	HttpErrorstatus_coderF   PromptContinueformatidfingerprintr   rK   r   r   r   r   r   r   r   r   r   r   r   r   r1   IncludeFieldsr@   )argsholderurl_map_argr    r:   r;   r<   datarE   eurl_map_olderrorrN   s                r   _Runr   <  sC   ==&)--
!++22 66v>	 . @+ 
	'	'(:s5	I$5  __++"=13G \\[%%''KK 2 3G //DK G
3;;K<L<L<NO
 
GJKK 8 9 &11G .			-(			+&			.)	'"			*+		1	1<= 	
744/
	
  
	#	#./#&D""$9'; ;N		#	#./#&E""$9'; ;N			.)#&Fo'/ /N ++N;VYWE <;S 
	'	' 5

,
,SV
445 
	&	& GCkfigFF	G| <;sB   ?5L( 'M! N (M;MM!N4NNN N)c                   8    e Zd ZdZ e       ZdZed        Zd Z	y)r   r   Nc                     t        j                         | _        | j                  j                  |d       t	        j
                  |t        | j                         d             y )Nimport)operation_typeTr%   )r	   UrlMapArgumentURL_MAP_ARGAddArgumentr'   AddImportFlagsr)   r   )clsparsers     r   ArgszImport.Args  sM    **,COOOx@s//1DACr   c                     t        j                  | j                               }t        ||| j                  | j                               S )N)r   ComputeApiHolderr   r   r   )selfr   r   s      r   Runz
Import.Run  s;    **4+<+<+>?Ffd..0A0A0CDDr   )
__name__
__module____qualname____doc__r   detailed_helpr   classmethodr   r   r   r   r   r   r     s-    
 /-+C CEr   r   N)F)6r   
__future__r   r   r   apitools.base.pyr   r   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer   r	   r   r
   r   +googlecloudsdk.command_lib.compute.url_mapsr   !googlecloudsdk.command_lib.exportr   r'   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   r   r!   r)   r@   rF   rO   rT   rZ   r^   ri   rn   rr   rw   r   r   r   r   r   ReleaseTracksr   GAr   r   UniverseCompatibleUpdateCommandr   r   r   r   <module>r      s    &  ' > 7 ( O E E = F A # . 2"MJ@J:,
"$N	$\F~ $++00$2C2C2I2I ET E Er   