
    CX                     <   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rS	rS
rS	rS
rS	rS rS rS'S jrS rS'S jrS rS rS rS rS(S jr    S)S jrS(S jrS rS*S jrS r S r!S r"S r#S r$S r%S r&S(S  jr'S! r(S" r)S# r*S$ r+S% r,S& r-g)+z>Code that's shared between multiple health-checks subcommands.    )absolute_import)division)unicode_literalsN)arg_parsers)
exceptions
      i,  c                    U R                  S[        R                  " 5       SSS9  U R                  S[        R                  " 5       SSS9  U R                  S[        SS	S9  U R                  S
[        SSS9  U R                  SSU-   S-   S9  g)z;Adds parser arguments common to creation for all protocols.--check-interval5sz      How often to perform a health check for an instance. For example,
      specifying ``10s'' will run the check every 10 seconds. The default
      value is ``5s''.
      See $ gcloud topic datetimes for information on duration formats.
       typedefaulthelp	--timeouta        If Google Compute Engine doesn't receive a healthy response from the
      instance by the time specified by the value of this flag, the health
      check request is considered a failure. For example, specifying ``10s''
      will cause the check to wait for 10 seconds before considering the
      request a failure. The default value is ``5s''.
      See $ gcloud topic datetimes for information on duration formats.
      --unhealthy-threshold   z      The number of consecutive health check failures before a healthy
      instance is marked as unhealthy. The default is 2.
      --healthy-thresholdz      The number of consecutive successful health checks before an
      unhealthy instance is marked as healthy. The default is 2.
      --descriptionz-      An optional string description for the z health
      check.
      r   Nadd_argumentr   Durationintparserprotocol_strings     9lib/googlecloudsdk/api_lib/compute/health_checks_utils.pyAddProtocolAgnosticCreationArgsr   #   s     	!	  	 	!
	   	
	   	
	   	13BCF

      c                    U R                  S[        R                  " 5       SS9  U R                  S[        R                  " 5       SS9  U R                  S[        SS9  U R                  S[        S	S9  U R                  S
SU-   S-   S9  g)zDAdds parser arguments common to update subcommand for all protocols.r   z      How often to perform a health check for an instance. For example,
      specifying ``10s'' will run the check every 10 seconds.
      See $ gcloud topic datetimes for information on duration formats.
      r   r   r   a        If Google Compute Engine doesn't receive a healthy response from the
      instance by the time specified by the value of this flag, the health
      check request is considered a failure. For example, specifying ``10s''
      will cause the check to wait for 10 seconds before considering the
      request a failure.
      See $ gcloud topic datetimes for information on duration formats.
      r   zt      The number of consecutive health check failures before a healthy
      instance is marked as unhealthy.
      r   zx      The number of consecutive successful health checks before an
      unhealthy instance is marked as healthy.
      r   zA textual description for the z0 health check. Pass in an empty string to unset.r   Nr   r   s     r   AddProtocolAgnosticUpdateArgsr#   X   s     	!
   	!
  
 	
   	
   	,>>?  Ar    c                     [        U 5        [        U 5        U R                  SSS9  U R                  SSSS9  U(       a  U R                  S/ S	QS
S9  gg)z3Adds parser arguments for creation related to HTTP.--hostz      The value of the host header used for the health check. If unspecified,
      Google Cloud sets the host header to the IP address of the load balancer's
      forwarding rule.
      r   --request-path/z      The request path that this health check monitors. For example,
      ``/healthcheck''. The default value is ``/''.
      )r   r   --weight-report-modeENABLEDISABLEDRY_RUND        Defines whether Weighted Load Balancing is enabled.
        choicesr   N)_AddPortRelatedCreationArgsAddProxyHeaderRelatedCreateArgsr   r   include_weighted_load_balancings     r   AddHttpRelatedCreationArgsr4      sw     f%!&)
   	
   %
0   %r    c                 $    U R                  SSS9  g)z-Adds parser argument for HTTP response field.
--responsez      When empty, status code of the response determines health. When not empty,
      presence of specified string in first 1024 characters of response body
      determines health. Only ASCII characters allowed.
      r   Nr   r   s    r   AddHttpRelatedResponseArgr9      s     	
  r    c                     [        U 5        [        U 5        U R                  SSS9  U R                  SSS9  U(       a  U R                  S/ SQSS	9  g
g
)z=Adds parser arguments for update subcommands related to HTTP.r%   aA        The value of the host header used in this HTTP health check request.
      The host header is empty by default. When empty, the health check will set
      the host header to the IP address of the backend VM or endpoint. You can
      set the host header to an empty value to return to this default behavior.
      r   r&   zc      The request path that this health check monitors. For example,
      ``/healthcheck''.
      r(   r)   r-   r.   N)_AddPortRelatedUpdateArgsAddProxyHeaderRelatedUpdateArgsr   r2   s     r   AddHttpRelatedUpdateArgsr=      st     F#!&)
   	
   %
0   %r    c                 D    [        U 5        [        U 5        [        SU S9  g)z2Adds parser arguments for creation related to TCP.Fadd_info_about_clearingr   N)r0   r1   _AddTcpRelatedArgsImplr8   s    r   AddTcpRelatedCreationArgsrB      s     f%!&)vFr    c                 D    [        U 5        [        U 5        [        SU S9  g)z<Adds parser arguments for update subcommands related to TCP.Tr?   N)r;   r<   rA   r8   s    r   AddTcpRelatedUpdateArgsrD      s     F#!&)fEr    c                 >    [        U SSSSS9  U R                  SSS9  g)	z3Adds parser arguments for creation related to gRPC.FTTCPN)use_port_nameuse_serving_port	port_typedefault_port--grpc-service-namez      An optional gRPC service name string of up to 1024 characters to include
      in the gRPC health check request. Only ASCII characters are allowed.r   r0   r   r8   s    r   AddGrpcRelatedCreationArgsrM      s:      	N  Or    c                 8    [        U SS9  U R                  SSS9  g)z=Adds parser arguments for update subcommands related to gRPC.F)rG   rK   z      An optional gRPC service name string of up to 1024 characters to include
      in the gRPC health check request. Pass in an empty string to unset.
      Only ASCII characters are allowed.r   N)r;   r   r8   s    r   AddGrpcRelatedUpdateArgsrO      s)     F%8,  -r    c                 `    [        U SSSS9  U R                  SUSS9  U R                  SUS	S9  g)
z%Adds parser arguments related to UDP.FUDPN)rH   rI   rJ   	--requestzi      Application data to send in payload of an UDP packet. It is an error if
      this is empty.
      )requiredr   r6   zr      The bytes to match against the beginning of the response data.
      It is an error if this is empty.
      rL   )r   request_and_response_requireds     r   AddUdpRelatedArgsrU      sV     uDJ 	,
   	,
  r    c                 D   S/nU(       a  UR                  S5        U(       a  UR                  S5        U R                  SR                  U5      S9nUR                  S[        USR                  U5      S9  U(       a  UR                  S	S
S9  U(       a  [        Xa5        gg)z?Adds parser create subcommand arguments --port and --port-name.z>These flags configure the port that the health check monitors.z5If none is specified, the default port of 80 is used.zLIf both `--port` and `--port-name` are specified, `--port` takes precedence. r   --portz@      The {} port number that this health check monitors.
      r   --port-namezb        The port name that this health check monitors. By default, this is
        empty.
        N)append	add_groupjoinr   r   format_AddUseServingPortFlag)r   rG   rH   rI   rJ   port_group_help
port_groups          r   r0   r0     s     G/ ? 	 SXXo%>?*

&
      :5 r    c                     U R                  SU(       a  SOS-  S9nUR                  S[        SS9  U(       a  UR                  SS	S9  [        X!5        g
)z?Adds parser update subcommand arguments --port and --port-name.z@These flags configure the port that the health check monitors.%szM If both `--port` and `--port-name` are specified, `--port` takes precedence. r   rX   z4The TCP port number that this health check monitors.r"   rY   z        The port name that this health check monitors. By default, this is
        empty. Setting this to an empty string will clear any existing
        port-name value.
        N)r[   r   r   r^   )r   rG   r`   s      r   r;   r;   =  su     N,246   8*
 A  C
    3r    c                 n    SnSnU (       a
  US-  nUS-  nUR                  SUS9  UR                  SUS9  g)	z-Adds TCP-related subcommand parser arguments.a        An optional string of up to 1024 characters to send once the health check
      TCP connection has been established. The health checker then looks for a
      reply of the string provided in the `--response` field.

      If `--response` is not configured, the health checker does not wait for a
      response and regards the probe as successful if the TCP or SSL handshake
      was successful.
      a        An optional string of up to 1024 characters that the health checker
      expects to receive from the instance. If the response is not received
      exactly, the health check probe fails. If `--response` is configured, but
      not `--request`, the health checker will wait for a response anyway.
      Unless your system automatically sends out a message in response to a
      successful handshake, only configure `--response` to match an explicit
      `--request`.
      zT
      Setting this to an empty string will clear any existing request value.
      zZ      Setting this to an empty string will clear any existing
      response value.
      rR   r   r6   Nr7   )r@   r   request_helpresponse_helps       r   rA   rA   V  sk    
,
-  
 
L  
 
M
 	   	  r    c                 .    U R                  SSSS.USS9  g)z:Adds parser arguments for creation related to ProxyHeader.z--proxy-headerzNo proxy header is added.z$Adds the header "PROXY UNKNOWN\r\n".)NONEPROXY_V1z<The type of proxy protocol header to be sent to the backend.)r/   r   r   Nr7   )r   r   s     r   r1   r1   ~  s.     	-= I  Kr    c                     [        U SS9  g)z8Adds parser arguments for update related to ProxyHeader.N)r   )r1   r8   s    r   r<   r<     s     "&$7r    c                 v   U R                   ba  U R                   [        :  d  U R                   [        :  a9  [        R                  " SR                  [        [        U R                   5      5      eU R                  ba  U R                  [        :  d  U R                  [        :  a9  [        R                  " SR                  [        [        U R                  5      5      eU R                  ba  U R                  [        :  d  U R                  [        :  a9  [        R                  " SR                  [        [        U R                  5      5      eU R                  bb  U R                  [        :  d  U R                  [        :  a9  [        R                  " SR                  [        [        U R                  5      5      egg)z;Raises exception if any protocol-agnostic args are invalid.Nzh[--check-interval] must not be less than {0} second or greater than {1} seconds; received [{2}] seconds.zb[--timeout] must not be less than {0} second or greater than {1} seconds; received: [{2}] seconds.zY[--healthy-threshold] must be an integer between {0} and {1}, inclusive; received: [{2}].zZ[--unhealthy-threshold] must be an integer between {0} and {1}, inclusive; received [{2}].)check_intervalCHECK_INTERVAL_LOWER_BOUND_SECCHECK_INTERVAL_UPPER_BOUND_SEChc_exceptionsArgumentErrorr]   timeoutTIMEOUT_LOWER_BOUND_SECTIMEOUT_UPPER_BOUND_SEChealthy_thresholdTHRESHOLD_LOWER_BOUNDTHRESHOLD_UPPER_BOUNDunhealthy_threshold)argss    r   CheckProtocolAgnosticArgsrx     s    %!??!!$BB

%
%	44:F*,J5!" " ll<<11ll44

%
%	,,2F#%<dll-LM M
 (!!$99$$'<<

%
%	&&,f-B-B-1-C-C'EF F *##&;;&&)>>

%
%	%%+V,A,A,0,D,D&FG G ? +r    c                 N    [         R                  " USR                  X5      5      e)Nz({0} cannot be specified when using: {1}.)calliope_exceptionsInvalidArgumentExceptionr]   )invalid_flagport_spec_flaginvalid_values      r   _RaiseBadPortSpecificationErrorr     s)    44@GG
'	( (r    c                 p   U R                  S5      (       a  U R                  S5      (       d  SUl        [        U5      R                  nU R                  (       a_  U R                  S5      (       a  [        SSS5        U R                  S5      (       a  [        SSS5        UR                  Ul        SUl        gU R                  S5      (       a/  U R                  S5      (       a  UR                  Ul        SUl	        gU R                  S5      (       a  UR                  Ul        gUR                  Ul        g)zDModifies the health check as needed and adds port spec to the check.	port_nameportNrY   --use-serving-portrX   )IsSpecifiedr   r    PortSpecificationValueValuesEnumrH   r   USE_SERVING_PORTportSpecificationUSE_FIXED_PORTportNameUSE_NAMED_PORTrw   x_health_check
enum_classs      r   ,ValidateAndAddPortSpecificationToHealthCheckr     s   	k""4+;+;F+C+C NN#DD*	$$%m5I&:<%h0D&:<'1'B'BN$ND$4$4[$A$A)3)B)Bn& $n			+	&	&)3)B)Bn&)3)B)Bn&r    c                     [        U5      R                  nU R                  (       a<  U R                  S5      (       a  [	        SSS5        UR
                  Ul        SUl        gUR                  Ul        g)zEModifies the gRPC health check as needed and adds port specification.r   rX   r   N)	r   r   rH   r   r   r   r   r   r   r   s      r   0ValidateAndAddPortSpecificationToGRPCHealthCheckr     sb     N#DD*	%h0D&:<'1'B'BN$N'1'@'@N$r    c                 P   UR                   nUR                  nUR                  n[        U5      R                  nU R
                  (       aV  U R                  S5      (       a  [        SSS5        U R                  S5      (       a  [        SSS5        SnSnUR                  nU R                  S5      (       a  U R                   nSnUR                  nORU R                  S5      (       a;  U R                  (       a  SnU R                  nUR                  nOSnUR                  nO X#U4$ )z?Calculate port, port_name and port_specification for HC update.r   rY   r   r   rX   N)r   r   r   r   r   rH   r   r   r   r   r   r   )rw   r   r   r   port_specificationr   s         r   HandlePortRelatedFlagsForUpdater     s   			$%%)%77N#DD*	$$%m5I&:<%h0D&:<DI#44	f99DI#22$$~~d..i%44 i%44 		,	,,r    c                 J   UR                   nUR                  n[        U5      R                  nU R                  (       a1  U R                  S5      (       a  [        SSS5        SnUR                  nU R                  S5      (       a  U R                   nUR                  nX#4$  X#4$ )z9Calculate port and port_specification for gRPC HC update.r   rX   r   N)	r   r   r   r   rH   r   r   r   r   )rw   r   r   r   r   s        r   #HandlePortRelatedFlagsForGRPCUpdater     s    			$%77N#DD*	%h0D&:<D#44	f99D#22
 
	!! 			!!r    c                 >    U R                  SSSU(       a  SOS-  S9  g)z3Adds parser argument for using serving port option.r   
store_truea        If given, use the "serving port" for health checks:

        - When health checking network endpoints in a Network Endpoint
          Group, use the port specified with each endpoint.
          `--use-serving-port` must be used when using a Network Endpoint Group
          as a backend as this flag specifies the `portSpecification` option for
          a Health Check object.
        - When health checking other backends, use the port%s of
          the backend service.z or named portrb   )actionr   Nr7   )r   rG   s     r   r^   r^   &  s2    	" :G%5B	P  Qr    c                 (    U R                  5       S:H  $ )z7Returns True if the health check reference is regional.zcompute.regionHealthChecks
Collectionhealth_check_refs    r   IsRegionalHealthCheckRefr   7  s     
	$	$	&*F	FFr    c                 (    U R                  5       S:H  $ )z5Returns True if the health check reference is global.zcompute.healthChecksr   r   s    r   IsGlobalHealthCheckRefr   =  s     
	$	$	&*@	@@r    c                 (    U R                  SSSSS9  g)z2Adds parser arguments for health check log config.z--enable-loggingr   Nz      Enable logging of health check probe results to Stackdriver. Logging is
      disabled by default.

      Use --no-enable-logging to disable logging.)r   r   r   r7   r8   s    r    AddHealthCheckLoggingRelatedArgsr   C  s%    
 	5	  6r    c           	      N    U R                  SSS[        R                  " SS9/ S9  g)z6Adds parser arguments for health check source regions.z--source-regionsREGIONaW          Define the list of Google Cloud regions from which health checks are
        performed. This option is supported only for global health checks that
        will be referenced by DNS routing policies. If specified, the
        --check-interval field should be at least 30 seconds. The
        --proxy-header and --request fields (for TCP health checks) are not
        supported with this option.

        If --source-regions is specified for a health check, then that health
        check cannot be used by a backend service or by a managed instance
        group (for autohealing).
           )
min_length)metavarr   r   r   N)r   r   ArgListr8   s    r   &AddHealthCheckSourceRegionsRelatedArgsr   S  s5     	 !,!  r    c                 n    U R                   nSnUR                  b  UR                  UR                  S9nU$ )z9Returns a HealthCheckLogconfig message if args are valid.N)enable)messagesenable_loggingHealthCheckLogConfig)clientrw   r   
log_configs       r   CreateLogConfigr   j  s=     __(*	$..d6I6I.JJ	r    c                     U R                   nSnU(       d   UR                  c  U$ UR                  5       nO[        R                  " U5      nUR                  b  UR                  Ul        U$ )z0Returns a modified HealthCheckLogconfig message.N)r   r   r   copydeepcopyr   )r   rw   existing_log_configr   r   s        r   ModifyLogConfigr   t  sd     __(*	"..0J23J	$++J	r    )F)T)TTrF   P   )rg   ).__doc__
__future__r   r   r   r   googlecloudsdk.callioper   r   rz   0googlecloudsdk.command_lib.compute.health_checksrn   ru   rt   rr   rq   rm   rl   r   r#   r4   r9   r=   rB   rD   rM   rO   rU   r0   r;   rA   r1   r<   rx   r   r   r   r   r   r^   r   r   r   r   r   r    r    r   <module>r      s    E &  '  / E X     !$ !" 2j+A\>	>GFO"
-0 /315*/-/	%6P42%P
K8$GN(C6A$-N".Q"GA6 .r    