
    2                     8   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Jr  SSKJr  SSKJr  SSKr " S S\R<                  5      r " S S\R@                  5      r!g)z'Command for modifying backend services.    )absolute_import)division)unicode_literalsN)messages)encoding)base_classes)property_selector)base)
exceptions)flags)backend_services_utils)	resources)yaml)
console_io)editc                       \ rS rSrSrg)InvalidResourceError(    N)__name__
__module____qualname____firstlineno____static_attributes__r       ,lib/surface/compute/backend_services/edit.pyr   r   (   s     r   r   c                   t    \ rS rSrSrSr\R                  r\	S 5       r
S rS rS rS rS	 rS
 rS rS rSrg)Edit/   aI  Modify a backend service.

*{command}* modifies a backend service of a Google Cloud load balancer or
Traffic Director. The backend service resource is fetched from the server
and presented in a text editor that displays the configurable fields.

The specific editor is defined by the ``EDITOR'' environment variable.

The name of each backend corresponds to the name of an instance group,
zonal NEG, serverless NEG, or internet NEG.

To add, remove, or swap backends, use the `gcloud compute backend-services
remove-backend` and `gcloud compute backend-services add-backend` commands.
r   c                 :    U R                   R                  U5        g )N)_BACKEND_SERVICE_ARGAddArgument)clsparsers     r   Args	Edit.ArgsB   s    ((0r   c                    SR                  S UR                  5        5       5      n[        R                  " XR                  =(       d    [
        R                  5      n	[        R                  " U R                  UR                  5      S9n
U
R                  U	5      n	Xi:X  a  SnOYUS   U	S'   UR                  S5      nU(       a  XS'   [        R                  " U	UR                  R                   R"                  5      nU(       a  XK:X  a  U/$ UR                  R%                  U R'                  UR                  X+5      /5      $ )z8Returns an updated resource that was edited by the user.
c              3   n   #    U  H+  nUR                  S 5      (       a  S[        U5      -  OUv   M-     g7f)# N)
startswithlen).0lines     r   	<genexpr>.Edit._ProcessEditedResource.<locals>.<genexpr>S   s2      "0.D  ??3//c$iT9.s   35)transformationsNnamefingerprint)join
splitlinesr   DeserializeValueformatr   DEFAULT_FORMATr	   PropertySelectorGetReferenceNormalizersr   Applygetr   DictToMessageclientr   BackendServiceMakeRequestsGetSetRequest)selfholderbackend_service_reffile_contentsoriginal_objectoriginal_recordmodifiable_recordargsnon_comment_linesmodified_recordreference_normalizer
new_objectr4   s                r   _ProcessEditedResourceEdit._ProcessEditedResourceF   s&    		 "0!,,."0 0 #33;;=$*=*=?O
 -==44V5E5EFH*00AO+j !0 7of#''6k	)4&))/*0--*@*@*O*OQj 6==%%			FMM+>	KLN Nr   c           	   #   8  #    [         R                  " U R                  5       5      nUR                  nU R                  R                  UUR                  [        R                  " 5       [        R                  " U5      S9nU R                  X45      nUR                  U/5      nUS   n[        R                  " U5      n[        R                   " / SQS9n	U	R#                  U5      n
U R%                  XUU
5      nU R'                  XUX*Xx5      nU H  nUv   M	     g 7f)N)default_scopescope_listerr   )backendscustomRequestHeaderscustomResponseHeadersdescription	enableCDNhealthChecksziap.enabledziap.oauth2ClientIdziap.oauth2ClientSecretportportNameprotocol
timeoutSec)
properties)r   ComputeApiHolderReleaseTrackr?   r!   ResolveAsResourcer   r   GetDefaultScopecompute_flagsGetDefaultScopeListerGetGetRequestrA   r   MessageToDictr	   r:   r<   BuildFileContentsEditResource)rC   rJ   rD   r?   rE   get_requestobjectsrG   rH   field_selectorrI   rF   resource_listresources                 r   RunEdit.Runu   s    **4+<+<+>?F]]F33EE,<<>"88@	 F B
 $$VAK!!;-0GajO,,_=O '77 D N ',,_=**4+<>M%%d&,&5HM "n "s   DDc                    [         R                  " 5       n[        R                  R	                  5        HN  nUR                  S5        U(       a  UR                  S5        UR                  U5        UR                  S5        MP     UR                  S5        UR                  [        R                  " UUR                  =(       d    [        R                  5      5        UR                  S5        [        R                  " [        R                  " U R                  U5      5      UR                  =(       d    [        R                  5      n[        R                  " USU5        UR                  S5        [        R                  " UUR                  =(       d    [        R                  5      n[        R                  " USU5        UR                  5       $ )Nr*   r+   r(   zExample resource:z#
zOriginal resource:)ioStringIOr   HELPr6   writeSerializeDictr8   r   r9   r   rf   GetExampleResourceWriteResourceInCommentBlockgetvalue)	rC   rJ   r?   rH   rI   bufr/   exampleoriginals	            r   rg   Edit.BuildFileContents   s@   
++-C!!,,.	iin			#	iio	iio / IIdOIIl(():)-)K8K8KM NIIdO((t66v>?*t**,G ,,W6I3OIIe))/*.++*L9L9LNH,,X7K-02<<>r   c           	      \     [         R                  " U5      n U R                  XBUUUXQ5      n U$ ! [         R                   a    [        R                  " S5      ef = f! [        [        R                  [        R                  [        R                  4 a  n	[        U	S[        R                  " U	5      5      n
[!        U	[        R                  5      (       a  SnOSnSR#                  X5      n
[$        R&                  " U
SS9(       d  [        R                  " S5      e S n	A	OS n	A	ff = fGM,  )NzEdit aborted by user.messageapplyingparsingz)There was a problem {0} your changes: {1}z*Would you like to edit the resource again?)r~   prompt_string)r   
OnlineEditNoSaveExceptionr   AbortedErrorrO   
ValueErrorr   YAMLParseErrorr   ValidationErrorcalliope_exceptionsToolExceptiongetattrsix	text_type
isinstancer8   r   PromptContinue)rC   rJ   rE   rF   rD   rI   rG   rH   rl   er~   problem_types               r   rh   Edit.EditResource   s"   
?6A33F4A4C4C4E	M
 	" 3 !! ?%%&=>>? $--&&!//1 A !Ya(89a,::;;#,",>F<1 	((FH ''(?@
@HA s!   2 A  +A 8D(BD##D(c                    SnSnUR                   R                  UR                   R                  UR                   R                  R                  R                  US-   SS9UR                   R                  UR                   R                  R                  R                  US-   SS9/S/S	/S
US-   US-   /SSSUR                   R                  R
                  R                  US-   SS9$ )Nz>https://compute.googleapis.com/compute/v1/projects/my-project/zDhttps://compute.googleapis.com/compute/v1/projects/my-project/zones/z$us-central1-a/instanceGroups/group-1d   )balancingModegroupmaxRatez%europe-west1-a/instanceGroups/group-2   zX-Forwarded-Port:443z&X-Client-Geo-Location:US,Mountain ViewzMy backend servicez)global/httpHealthChecks/my-health-check-1z)global/httpHealthChecks/my-health-check-2zbackend-serviceP   httpz&global/backendServices/backend-service   )rT   rU   rV   rW   rY   r3   rZ   r[   r\   selfLinkr]   )r   r@   BackendBalancingModeValueValuesEnumRATEProtocolValueValuesEnumHTTP)rC   r?   
uri_prefixinstance_groups_uri_prefixs       r   rv   Edit.GetExampleResource   s    J	  ??))OO##OO++HHMM1=> $  OO##OO++HHMM1>? $ 
 55GH(DDDD
 //GGLLFF7 *  r   c                 <   ^ U4S jnSU" SS5      4SU" SS5      4/$ )Nc                    >^ ^ UU U4S jnU$ )z4Returns a function to normalize resource references.c                   >  TR                  U 5      nUR                  5       T;  a  [        SR	                  TU S95      eUR                  5       $ ! [        R                   a    [        SR	                  TS95      ef = f)z&Returns normalized URI for field_name.z-[{field_name}] must be referenced using URIs.)
field_namez,Invalid [{field_name}] reference: [{value}].)r   value)Parser   UnknownCollectionExceptionr   r8   
CollectionSelfLink)	reference	value_refallowed_collectionsr   resource_registrys     r   NormalizeReferenceYEdit.GetReferenceNormalizers.<locals>.MakeReferenceNormalizer.<locals>.NormalizeReference   s    	*'--i8) !)<<$<DD'y E :; ; !!## 33 	*$=DD' E )* *	*s   A -A>r   )r   r   r   r   s   `` r   MakeReferenceNormalizer=Edit.GetReferenceNormalizers.<locals>.MakeReferenceNormalizer   s    $  r   zhealthChecks[]rY   )zcompute.httpHealthCheckszcompute.httpsHealthCheckszcompute.healthCheckszcompute.regionHealthCheckszbackends[].groupr   )zcompute.instanceGroupszcompute.regionInstanceGroupsr   )rC   r   r   s    ` r   r;   Edit.GetReferenceNormalizers   sI     , 
	 D
E	F
 
	 G
I	J
 
r   c                 .   UR                  5       S:X  aA  UR                  R                  SUR                  R                  " S0 UR                  5       D64$ UR                  R                  SUR                  R                  " S0 UR                  5       D64$ )Ncompute.regionBackendServicesGetr   )r   apitools_clientregionBackendServicesr   &ComputeRegionBackendServicesGetRequestAsDictbackendServices ComputeBackendServicesGetRequest)rC   r?   rE   s      r   re   Edit.GetGetRequest  s    %%'+JJ$$::EooDD 2'..023 3 ""22EOO<< 0%,,.01 1r   c                 6   UR                  5       S:X  aC  UR                  R                  SUR                  R                  " SSU0UR                  5       D64$ UR                  R                  SUR                  R                  " SSU0UR                  5       D64$ )Nr   UpdatebackendServiceResourcer   )r   r   r   r   )ComputeRegionBackendServicesUpdateRequestr   r   #ComputeBackendServicesUpdateRequest)rC   r?   rE   replacements       r   rB   Edit.GetSetRequest(  s    %%'+JJ$$::HooGG 2)42'..023 3 ""22HOO?? 0'20%,,.01 1r   r   N)r   r   r   r   __doc__r9   r   #GLOBAL_REGIONAL_BACKEND_SERVICE_ARGr!   classmethodr%   rO   rn   rg   rh   rv   r;   re   rB   r   r   r   r   r   r   /   sZ     .BB1 1-N^)V.@#J!F1	1r   r   )"r   
__future__r   r   r   rq   apitools.base.protorpcliter   apitools.base.pyr   googlecloudsdk.api_lib.computer   r	   googlecloudsdk.callioper
   r   r   "googlecloudsdk.command_lib.computer   rc   3googlecloudsdk.command_lib.compute.backend_servicesr   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   r   r   Commandr   r   r   r   <module>r      sh    . &  ' 	 / % 7 < ( E 9 E V E ) $ 2 ) 
.<< B14<< B1r   