
    4                        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y)z2Command for adding a backend to a backend service.    )absolute_import)division)unicode_literals)encoding)base_classes)base)
exceptions)flags)backend_flags)backend_services_utilsc                   T    e Zd ZdZdZdZdZdZed        Z	d Z
d Zd Zd Zd	 Zd
 Zy)
AddBackend@  Add a backend to a backend service.

  *{command}* adds a backend to a Google Cloud load balancer or Traffic
  Director. Depending on the load balancing scheme of the backend service,
  backends can be instance groups (managed or unmanaged), zonal network endpoint
  groups (zonal NEGs), serverless NEGs, or an internet NEG. For more
  information, see the [backend services
  overview](https://cloud.google.com/load-balancing/docs/backend-service).

  For most load balancers, you can define how Google Cloud measures capacity by
  selecting a balancing mode. For more information, see [traffic
  distribution](https://cloud.google.com/load-balancing/docs/backend-service#traffic_distribution).

  To modify a backend, use the `gcloud compute backend-services update-backend`
  or `gcloud compute backend-services edit` command.
  TFc                    t         j                  j                  |       t        j                  |       t        j
                  |d| j                  | j                         t        j                  || j                  | j                  | j                                t        j                  || j                  | j                  | j                                t        j                  || j                  | j                         t        j                  |       | j                  rt        j                  |d        | j                  rt        j                   |       t        j"                  |       y )Nzadd tosupport_global_negsupport_region_neg)r   r   release_track)default)r
   #GLOBAL_REGIONAL_BACKEND_SERVICE_ARGAddArgumentr   AddDescription+AddInstanceGroupAndNetworkEndpointGroupArgsr   r   AddBalancingModeReleaseTrackAddCapacityLimitsAddCapacityScalarAddPreferencesupport_failoverAddFailoversupport_in_flight_balancingAddTrafficDurationAddCustomMetrics)clsparsers     3lib/surface/compute/backend_services/add_backend.pyArgszAddBackend.Args:   s   	--99&A  (	551111	3
 ""1111&&(	 ##1111&&(	 ##1111
 '
5
&&&&v.""6*    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AddBackend._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,   r=   r.   )
r/   r0   r1   r2   )ComputeRegionBackendServicesUpdateRequestr4   r-   r.   r5   #ComputeBackendServicesUpdateRequest)r7   r8   r9   replacements       r&   _GetSetRequestzAddBackend._GetSetRequesti   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                  rPt        j                  | j                  | j                        j                  ||t	        j
                  |            S y )Nscope_listerr   )
instance_groupr
   MULTISCOPE_INSTANCE_GROUP_ARGResolveAsResourcecompute_flagsGetDefaultScopeListernetwork_endpoint_groupGetNetworkEndpointGroupArgr   r   )r7   args	resourcesr8   s       r&   _GetGroupRefzAddBackend._GetGroupRefy   s    00BB

$::6B C D D ""--!44!4466G6G(>>vF 7H 7HH #r(   c                    t        j                  ||| j                                ||j                  |||j                  |j
                  ||j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                        }| j                         t        j                  j                  k(  r:|j                   |_        |j$                  |_        |j(                  |_        ||_        |S |j                  ||j                  |j
                  ||j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                        }| j                         t        j                  j                  k(  r:|j                   |_        |j$                  |_        |j(                  |_        ||_        |S )ai  Create a backend message.

    Args:
      messages: The available API proto messages.
      group_uri: String. The backend instance group uri.
      balancing_mode: Backend.BalancingModeValueValuesEnum. The backend load
        balancing mode.
      preference: Backend.PreferenceValueValuesEnum. The backend preference
      traffic_duration: Backend.TrafficDurationValueValuesEnum. The traffic
        duration for the backend.
      args: argparse Namespace. The arguments given to the add-backend command.

    Returns:
      A new Backend message with its fields set according to the given
      arguments.
    )balancingMode
preferencecapacityScalerdescriptiongroupmaxRatemaxRatePerInstancemaxRatePerEndpointmaxUtilizationmaxConnectionsmaxConnectionsPerInstancemaxConnectionsPerEndpointfailover)rP   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   )r   ValidateBalancingModeArgsr   Backendcapacity_scalerrS   max_ratemax_rate_per_instancemax_rate_per_endpointmax_utilizationmax_connectionsmax_connections_per_instancemax_connections_per_endpointr\   r   ALPHAmax_in_flight_requestsmaxInFlightRequests#max_in_flight_requests_per_instancemaxInFlightRequestsPerInstance#max_in_flight_requests_per_endpointmaxInFlightRequestsPerEndpointtrafficDuration)r7   r2   	group_uribalancing_moderQ   traffic_durationrL   backends           r&   _CreateBackendMessagez AddBackend._CreateBackendMessage   s   2 44$))+   &--&&--!77!77----$($E$E$($E$E== ! g 
			 1 1 7 7	7&*&A&A#44 	. 44 	. #3n  &--&&--!77!77----$($E$E$($E$E== ! g 
			 1 1 7 7	7&*&A&A#44 	. 44 	. #3nr(   c           	         t        j                  |      }| j                  |||      }|j                         }d}	|j                  D ]  }
||
j
                  k(  s|j                         dk(  s|j                         dk(  rdt        |d      z   dz   }	nN|j                         dk(  s|j                         dk(  rd	t        |d
      z   dz   }	n|j                         dk(  rd}	t        j                  dj                  |j                         |	|j                                      |j                  r0|j                  j                  j                  |j                        }nd }d }|j                   r/|j                  j                  j#                  |j                         }d }| j%                         t&        j$                  j(                  k(  r;|j*                  r/|j                  j                  j-                  |j*                        }| j/                  |j                  |||||      }
|j0                  r|j0                  |
_        |j4                  r|j4                  |
_        |j                  j7                  |
       |S )N zcompute.instanceGroupszcompute.networkEndpointGroupszzone [zone]zcompute.regionInstanceGroupsz#compute.regionNetworkEndpointGroupszregion [r-   z#compute.globalNetworkEndpointGroupsglobalz:Backend [{}] in {} already exists in backend service [{}].)r   CopyProtoMessagerN   SelfLinkbackendsrT   r/   getattrr	   ArgumentErrorformatr4   rp   r2   r^   BalancingModeValueValuesEnumrQ   PreferenceValueValuesEnumr   r   rg   rq   TrafficDurationValueValuesEnumrs   custom_metricscustomMetricscustom_metrics_fileappend)r7   r8   rM   r9   rL   existingr@   	group_refro   scoperr   rp   rQ   rq   s                 r&   _ModifyzAddBackend._Modify   s?   ++H5K!!$	6:I""$IE$$	gmm	#  "&>>##%)HHWY77#=%  "&DD##%)NNwy(;;cA%!!#'LL%&&HOO %)<)A)A)C
 	
 %* ..KK


n nJ??**DD
//j t00666!!00OO


 ((G "11g"66g(r(   c                    t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t        j                  |            }| j                  ||      }|j                  |g      }| j                  ||j                  |||d         }|j                  | j                  |||      g      S )z@Issues requests necessary to add backend to the Backend Service.rC   r   )r   ComputeApiHolderr   r8   r
   r   rG   rM   rH   rI   r:   MakeRequestsr   rA   )r7   rL   holderr8   r9   get_requestobjects
new_objects           r&   RunzAddBackend.Run  s    **4+<+<+>?F]]F 	11CC&<<VD 	D 	F 
 %%f.ABK!!;-0Gff&6&68K"GAJ0J 			V%8*	EFH Hr(   N)__name__
__module____qualname____doc__r   r   r   r!   classmethodr'   r:   rA   rN   rs   r   r    r(   r&   r   r   !   sW    "  %+ +@66 HPd>@Hr(   r   c                       e Zd ZdZy)AddBackendBetar   N)r   r   r   r   r   r(   r&   r   r   .  s    r(   r   c                       e Zd ZdZdZy)AddBackendAlphar   TN)r   r   r   r   r!   r   r(   r&   r   r   D  s    " !%r(   r   N)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   r
   rH   3googlecloudsdk.command_lib.compute.backend_servicesr   r   UniverseCompatibleReleaseTracksr   GAUpdateCommandr   BETAr   rg   r   r   r(   r&   <module>r      s     9 &  ' % 7 ( 9 E M V E D%%(()HH## HH * HHV D%%**+Z  ,* D%%++,%j % -%r(   