
    M                        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 e
j$                   e
j&                  e
j(                  j*                         G d de
j,                                      Z e
j&                  e
j(                  j0                         G d de             Z e
j&                  e
j(                  j4                         G d de             Zd Z	 ddZy)z4Command for updating a backend in a backend service.    )absolute_import)division)unicode_literals)encoding)base_classes)base)
exceptions)flags)backend_flags)backend_services_utilsc                   N    e Zd ZdZed        Zd Zd Zd Zd Z		 ddZ
d	 Zd
 Zy)UpdateBackenda  Update an existing backend of a load balancer or Traffic Director.

  *{command}* updates attributes of a backend that is already associated with a
  backend service. Configurable attributes depend on the load balancing scheme
  and the type of backend (instance group, zonal NEG, serverless NEG, or
  internet NEG). For more information, see [traffic
  distribution](https://cloud.google.com/load-balancing/docs/backend-service#traffic_distribution).
  and the [Failover for Internal TCP/UDP Load Balancing
  overview](https://cloud.google.com/load-balancing/docs/internal/failover-overview).

  To add, remove, or swap backends, use the `gcloud compute backend-services
  remove-backend` and `gcloud compute backend-services add-backend` commands.
  c                    t         j                  j                  |        t        j                  |        t        j
                  | d       t        j                  |        t        j                  |        t        j                  |        t        j                  | d        t        j                  |        t        j                  | d       y N	update indefaultTadd_clear_argument)r
   #GLOBAL_REGIONAL_BACKEND_SERVICE_ARGAddArgumentr   AddDescription+AddInstanceGroupAndNetworkEndpointGroupArgsAddBalancingModeAddCapacityLimitsAddCapacityScalarAddFailoverAddPreferenceAddCustomMetrics)parsers    6lib/surface/compute/backend_services/update_backend.pyArgszUpdateBackend.Args2   s    	--99&A  (	55fkJ""6*##F+##F+fd3'""6dC    c                 n   |j                         dk(  rW|j                  j                  d|j                  j	                  |j                         |j                  |j                        fS |j                  j                  d|j                  j                  |j                         |j                        fS )Ncompute.regionBackendServicesGet)backendServiceregionproject)r'   r)   )

Collectionapitools_clientregionBackendServicesmessages&ComputeRegionBackendServicesGetRequestNamer(   r)   backendServices ComputeBackendServicesGetRequest)selfclientbackend_service_refs      r!   _GetGetRequestzUpdateBackend._GetGetRequest?   s    %%'+JJ$$::ooDD!4!9!9!;,33-55 E 78 8 ""22OO<<2779+33 = 56 6r#   c                 r   |j                         dk(  rX|j                  j                  d|j                  j	                  |j                         ||j                  |j                        fS |j                  j                  d|j                  j                  |j                         ||j                        fS )Nr%   Update)r'   backendServiceResourcer(   r)   )r'   r8   r)   )
r*   r+   r,   r-   )ComputeRegionBackendServicesUpdateRequestr/   r(   r)   r0   #ComputeBackendServicesUpdateRequest)r2   r3   r4   replacements       r!   _GetSetRequestzUpdateBackend._GetSetRequestM   s    %%'+JJ$$::ooGG!4!9!9!;)4,33-55	 H 78 8 ""22OO??2779'2+33 @ 56 6r#   c                    |j                   r5t        j                  j                  ||t	        j
                  |            S |j                  r9t        j                         j                  ||t	        j
                  |            S y )Nscope_lister)instance_groupr
   MULTISCOPE_INSTANCE_GROUP_ARGResolveAsResourcecompute_flagsGetDefaultScopeListernetwork_endpoint_groupGetNetworkEndpointGroupArg)r2   args	resourcesr3   s       r!   _GetGroupRefzUpdateBackend._GetGroupRef]   s    00BB

$::6B C D D ""--/AA

$::6B B D D #r#   c           	         t        j                  |      }| j                  |||      }d }|j                  D ]@  }	|j	                         |j                  |	j                        j	                         k(  s>|	} n |s{d }
d }t        |d      rd}
|j                  }t        |d      rd}
|j                  }t        j                  dj                  |j                         |
||j                                     |j                  r|j                  |_        n|j                  d |_        | j                  |||| j!                                ||j"                  |j"                  |_        |@|j$                  4|j&                  j(                  j+                  |j$                        |_        | j!                         t,        j                   j.                  k(  rB|@|j0                  4|j&                  j(                  j3                  |j0                        |_        |j6                  r|j6                  |_        |j:                  r|j:                  |_        |j<                  rg |_        |S )Nzoner(   zMNo backend with name [{0}] in {1} [{2}] is part of the backend service [{3}].)r   CopyProtoMessagerI   backendsRelativeNameParseURLgrouphasattrrK   r(   r	   ArgumentErrorformatr/   description_ModifyBalancingModeArgsReleaseTrackfailover
preferencer-   BackendPreferenceValueValuesEnumr   ALPHAtraffic_durationTrafficDurationValueValuesEnumtrafficDurationcustom_metricscustomMetricscustom_metrics_fileclear_custom_metrics)r2   r3   rH   r4   rG   existingr;   	group_refbackend_to_updatebackend
scope_type
scope_names               r!   _ModifyzUpdateBackend._Modifyi   s6   ++H5K!!$	6:I'' 
			!Y%7%7
--&%
(# ( jj	F	#
^^
	H	%
%%
$$!6).."2J
"5":":"<>? ?
 &*&6&6#				%&*#!!'):):)< $)B#'== $)D
//
!
!
;
;DOO
L "
 	t00666)!!- //
!
!
@
@## ' (,(;(;%(,(@(@%  (*%r#   Nc                 4    t        |j                  |||       y)a$  Update balancing mode fields in backend_to_update according to args.

    Args:
      client: The compute client.
      args: The arguments given to the update-backend command.
      backend_to_update: The backend message to modify.
      release_track: The release track of the command.
    N)rU   r-   )r2   r3   rG   re   release_tracks        r!   rU   z&UpdateBackend._ModifyBalancingModeArgs   s     0-r#   c                 x   t        |j                  du|j                  |j                  du|j                  du|j
                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  dug      st        j                  d      y)zValidatest that at least one field to update is specified.

    Args:
      args: The arguments given to the update-backend command.
    N'At least one property must be modified.anyrT   balancing_modemax_utilizationmax_ratemax_rate_per_instancemax_rate_per_endpointmax_connectionsmax_connections_per_instancemax_connections_per_endpointcapacity_scalerrW   rX   r	   UpdatePropertyErrorr2   rG   s     r!   _ValidateArgszUpdateBackend._ValidateArgs   s     $D(T!""$.""$.D())5))5
 	D(T!t#! $ **
35 5%r#   c                    | j                  |       t        j                  | j                               }|j                  }t
        j                  j                  ||j                  t        j                  |            }| j                  ||      }|j                  |g      d   }| j                  ||j                  |||      }|j                  | j                  |||      g      S )zCIssues requests necessary to update backend of the Backend Service.r>   r   )r{   r   ComputeApiHolderrV   r3   r
   r   rB   rH   rC   rD   r5   MakeRequestsri   r<   )r2   rG   holderr3   r4   get_requestbackend_servicemodified_backend_services           r!   RunzUpdateBackend.Run   s    t**4+<+<+>?F]]F 	11CC&<<VD 	D 	F 
 %%f.ABK));-8;O#||  "5t_ N F$74	6   r#   N)__name__
__module____qualname____doc__staticmethodr"   r5   r<   rI   ri   rU   r{   r    r#   r!   r   r   !   sH     
D 
D66 
D?D <@58r#   r   c                   &    e Zd ZdZed        Zd Zy)UpdateBackendBeta  Update an existing backend in a backend service.

  *{command}* updates a backend that is part of a backend
  service. This is useful for changing the way a backend
  behaves. Example changes that can be made include changing the
  load balancing policy and draining a backend by setting
  its capacity scaler to zero.

  Backends are instance groups or network endpoint groups. One
  of the `--network-endpoint-group` or `--instance-group` flags is required to
  identify the backend that you are modifying. You cannot change
  the instance group or network endpoint group associated with a backend, but
  you can remove a backend and add a new one with `backend-services
  remove-backend` and `backend-services add-backend`.

  The `gcloud compute backend-services edit` command can also
  update a backend if the use of a text editor is desired.

  For more information about the available settings, see
  https://cloud.google.com/load-balancing/docs/backend-service.
  c                    t         j                  j                  |       t        j                  |d       t	        j
                  |       t	        j                  |       t	        j                  |       t	        j                  |       t	        j                  |d        t	        j                  |       t	        j                  |d       y r   )r
   r   r   r   r   r   r   r   r   r   r   r   clsr    s     r!   r"   zUpdateBackendBeta.Args  s    	--99&A	55fkJ  (""6*##F+##F+fd3'""6dCr#   c                 x   t        |j                  du|j                  |j                  du|j                  du|j
                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  dug      st        j                  d      yz
Overrides.Nrm   rn   rz   s     r!   r{   zUpdateBackendBeta._ValidateArgs  s     $D(T!""$.""$.D())5))5
 	D(T!t#! $ **
35 5%r#   Nr   r   r   r   classmethodr"   r{   r   r#   r!   r   r      s"    , 	D 	D5r#   r   c                   &    e Zd ZdZed        Zd Zy)UpdateBackendAlphar   c                    t         j                  j                  |       t        j                  |d       t	        j
                  |       t	        j                  || j                                t	        j                  || j                                t	        j                  |       t	        j                  |d        t	        j                  |       t	        j                  |       t	        j                  |d       y )Nr   )rk   r   Tr   )r
   r   r   r   r   r   r   rV   r   r   r   r   AddTrafficDurationr   r   s     r!   r"   zUpdateBackendAlpha.ArgsD  s    	--99&A	55fkJ  (""69I9I9KL##F#:J:J:LM##F+fd3'$$V,""6dCr#   c                 .   t        |j                  du|j                  |j                  du|j                  du|j
                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                  du|j                   du|j"                  du|j$                  du|j&                  dug      st)        j*                  d      yr   )ro   rT   rp   rq   rr   rs   rt   ru   rv   rw   max_in_flight_requests#max_in_flight_requests_per_instance#max_in_flight_requests_per_endpointr\   rx   rW   rX   r_   ra   rb   r	   ry   rz   s     r!   r{   z UpdateBackendAlpha._ValidateArgsQ  s-    $D(T!""$.""$.D())5))5##4/00<00<T)D(T!t#4'  ,!!-' * **
35 5+r#   Nr   r   r#   r!   r   r   ,  s"    , 
D 
D5r#   r   c                 X    d| _         d| _        d| _        d| _        d| _        d| _        y)z@Initialize the backend's mutually exclusive capacity thresholds.N)maxRatemaxRatePerInstancemaxConnectionsmaxConnectionsPerInstancemaxRatePerEndpointmaxConnectionsPerEndpoint)rf   s    r!   ._ClearMutualExclusiveBackendCapacityThresholdsr   m  s1    '/#''&*'##'&*'#r#   Nc                 &   t        j                  | ||j                  |       |j                  r| j                  j                  |j                        |_        |j                  | j                  j
                  j                  k(  rPd|_        d|_        d|_	        d|_
        |t        j                  j                  k(  rd|_        d|_        d|_        n|j                  | j                  j
                  j"                  k(  rOd|_        d|_        d|_        d|_        |t        j                  j                  k(  rd|_        d|_        d|_        nt|t        j                  j                  k(  rW|j                  | j                  j
                  j*                  k(  r*d|_        d|_        d|_        d|_        d|_	        d|_
        |j,                  |j,                  |_        |j.                  t1        |       |j.                  |_        nf|j2                  t1        |       |j2                  |_        n<|j4                  t1        |       |j4                  |_        n|j6                  t1        |       |j6                  |_	        n|j8                  t1        |       |j8                  |_        n|j:                  t1        |       |j:                  |_
        n|t        j                  j                  k(  rz|j<                  t1        |       |j<                  |_        nQ|j>                  t1        |       |j>                  |_        n(|j@                  t1        |       |j@                  |_        |jB                  |jB                  |_"        yy)a;  Update balancing mode fields in backend_to_update according to args.

  Args:
    messages: API messages class, determined by the release track.
    args: The arguments given to the update-backend command.
    backend_to_update: The backend message to modify.
    release_track: The release track of the command.
  N)#r   ValidateBalancingModeArgsbalancingModerp   rY   BalancingModeValueValuesEnumRATEmaxUtilizationr   r   r   r   rV   r[   maxInFlightRequestsmaxInFlightRequestsPerInstancemaxInFlightRequestsPerEndpoint
CONNECTIONr   r   r   	IN_FLIGHTrq   rr   r   rs   ru   rv   rt   rw   r   r   r   rx   capacityScaler)r-   rG   re   rk   s       r!   rU   rU   w  sQ    22'55}F 
55	! # 	''55::	;)-&)-&481481	$++11	104-;?8;?8

)
)



7
7
B
BC)-&"&-1*-1*	$++11	104-;?8;?8**000++88BBC #'-1*-1*)-&481481
 
%'+';';$
 
]]23DE $!!-23DE+/+E+E('23DE'+';';$((423DE)) /!!-23DE+/+E+E(((423DE)) /))///"".45FG.2.I.I+		1	1	=45FG

2
2 6 
	1	1	=45FG

2
2 6 
%'+';';$ &r#   r   )r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   r
   rC   3googlecloudsdk.command_lib.compute.backend_servicesr   r   UniverseCompatibleReleaseTracksrV   GAUpdateCommandr   BETAr   r[   r   r   rU   r   r#   r!   <module>r      s     ; &  ' % 7 ( 9 E M V E D%%(()HD&& H * HV D%%**+:5 :5 ,:5z D%%++,=5* =5 -=5@+ 6:f<r#   