feat: Add new gcloud commands, API clients, and third-party libraries across various services.

This commit is contained in:
2026-01-01 20:26:35 +01:00
parent 5e23cbece0
commit a19e592eb7
25221 changed files with 8324611 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Commands for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
class HealthChecksUpdate(base.Group):
"""Update health checks for load balanced instances."""
HealthChecksUpdate.detailed_help = {
'brief': ('Update health checks for load balanced instances')
}

View File

@@ -0,0 +1,218 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief':
'Update a gRPC health check.',
'DESCRIPTION':
"""\
*{command}* is used to update an existing gRPC health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
}
def _Args(parser, include_log_config):
health_check_arg = flags.HealthCheckArgument('gRPC')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddGrpcRelatedUpdateArgs(parser)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'gRPC')
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(client, args, existing_check, include_log_config):
"""Returns a modified HealthCheck message."""
# We do not support using 'update grpc' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.GRPC):
raise core_exceptions.Error(
'update grpc subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description, PortName, and GrpcServiceName are the only attributes that can
# be cleared by passing in an empty string (but we don't want to set it to
# an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
if args.grpc_service_name:
grpc_service_name = args.grpc_service_name
elif args.grpc_service_name is None:
grpc_service_name = existing_check.grpcHealthCheck.grpcServiceName
else:
grpc_service_name = None
port, port_specification = health_checks_utils.\
HandlePortRelatedFlagsForGRPCUpdate(
args, existing_check.grpcHealthCheck)
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.GRPC,
grpcHealthCheck=client.messages.GRPCHealthCheck(
port=port,
portSpecification=port_specification,
grpcServiceName=grpc_service_name),
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
return new_health_check
def _ValidateArgs(args, include_log_config):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.check_interval or args.timeout or
args.healthy_threshold or args.unhealthy_threshold or
args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
if (args.description is None and args.grpc_service_name is None and
args_unset):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(args, holder, include_log_config):
"""Issues the requests necessary for updating the health check."""
client = holder.client
_ValidateArgs(args, include_log_config)
health_check_arg = flags.HealthCheckArgument('gRPC')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(client, args, objects[0], include_log_config)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Update(base.UpdateCommand):
"""Update a gRPC health check."""
_include_log_config = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
return _Run(args, holder, self._include_log_config)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(Update):
pass
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(UpdateBeta):
pass

View File

@@ -0,0 +1,256 @@
# -*- coding: utf-8 -*- #
# Copyright 2025 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief': 'Update a gRPC with TLS health check.',
'DESCRIPTION': """\
*{command}* is used to update an existing gRPC with TLS health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
}
def _Args(parser, include_log_config):
health_check_arg = flags.HealthCheckArgument('gRPC with TLS')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddGrpcRelatedUpdateArgs(parser)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'gRPC with TLS')
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(client, args, existing_check, include_log_config):
"""Returns a modified HealthCheck message."""
# We do not support using 'update grpc-with-tls' with a health check of a
# different protocol.
if (
existing_check.type
!= client.messages.HealthCheck.TypeValueValuesEnum.GRPC_WITH_TLS
):
raise core_exceptions.Error(
'update grpc-with-tls subcommand applied to health check with protocol '
+ existing_check.type.name
)
# Description, PortName, and GrpcServiceName are the only attributes that can
# be cleared by passing in an empty string (but we don't want to set it to
# an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
if args.grpc_service_name:
grpc_service_name = args.grpc_service_name
elif args.grpc_service_name is None:
grpc_service_name = existing_check.grpcTlsHealthCheck.grpcServiceName
else:
grpc_service_name = None
port, port_specification = (
health_checks_utils.HandlePortRelatedFlagsForGRPCUpdate(
args, existing_check.grpcTlsHealthCheck
)
)
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.GRPC_WITH_TLS,
grpcTlsHealthCheck=client.messages.GRPCTLSHealthCheck(
port=port,
portSpecification=port_specification,
grpcServiceName=grpc_service_name,
),
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(
args.healthy_threshold or existing_check.healthyThreshold
),
unhealthyThreshold=(
args.unhealthy_threshold or existing_check.unhealthyThreshold
),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
return new_health_check
def _ValidateArgs(args, include_log_config):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.check_interval or args.timeout or
args.healthy_threshold or args.unhealthy_threshold or
args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
if (args.description is None and args.grpc_service_name is None and
args_unset):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(args, holder, include_log_config):
"""Issues the requests necessary for updating the health check."""
client = holder.client
_ValidateArgs(args, include_log_config)
health_check_arg = flags.HealthCheckArgument('gRPC with TLS')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(client, args, objects[0], include_log_config)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(base.UpdateCommand):
"""Update a gRPC with TLS health check."""
_include_log_config = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config)
def Run(self, args):
return _Run(
args,
base_classes.ComputeApiHolder(self.ReleaseTrack()),
self._include_log_config,
)
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(base.UpdateCommand):
"""Update a gRPC with TLS health check."""
_include_log_config = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config)
def Run(self, args):
return _Run(
args,
base_classes.ComputeApiHolder(self.ReleaseTrack()),
self._include_log_config,
)
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class UpdateGa(base.UpdateCommand):
"""Update a gRPC with TLS health check."""
_include_log_config = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config)
def Run(self, args):
return _Run(
args,
base_classes.ComputeApiHolder(self.ReleaseTrack()),
self._include_log_config,
)

View File

@@ -0,0 +1,323 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief':
'Update a HTTP health check.',
'DESCRIPTION':
"""\
*{command}* is used to update an existing HTTP health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
}
def _Args(
parser,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Adds all the args in the parser."""
health_check_arg = flags.HealthCheckArgument('HTTP')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddHttpRelatedUpdateArgs(parser,
include_weighted_load_balancing)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'HTTP')
health_checks_utils.AddHttpRelatedResponseArg(parser)
if include_source_regions:
health_checks_utils.AddHealthCheckSourceRegionsRelatedArgs(parser)
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(
client,
args,
existing_check,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Returns a modified HealthCheck message."""
# We do not support using 'update http' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.HTTP):
raise core_exceptions.Error(
'update http subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description, PortName, and Host are the only attributes that can be
# cleared by passing in an empty string (but we don't want to set it to
# an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
if args.host:
host = args.host
elif args.host is None:
host = existing_check.httpHealthCheck.host
else:
host = None
port, port_name, port_specification = health_checks_utils.\
HandlePortRelatedFlagsForUpdate(
args, existing_check.httpHealthCheck)
if include_weighted_load_balancing:
weight_report_mode = existing_check.httpHealthCheck.weightReportMode
if args.IsSpecified('weight_report_mode'):
weight_report_mode = client.messages.HTTPHealthCheck.WeightReportModeValueValuesEnum(
args.weight_report_mode)
proxy_header = existing_check.httpHealthCheck.proxyHeader
if args.proxy_header is not None:
proxy_header = client.messages.HTTPHealthCheck.ProxyHeaderValueValuesEnum(
args.proxy_header)
if args.response:
response = args.response
elif args.response is None:
response = existing_check.httpHealthCheck.response
else:
response = None
http_health_check = client.messages.HTTPHealthCheck(
host=host,
port=port,
portName=port_name,
requestPath=(args.request_path or
existing_check.httpHealthCheck.requestPath),
portSpecification=port_specification,
proxyHeader=proxy_header,
response=response)
if include_weighted_load_balancing:
http_health_check.weightReportMode = weight_report_mode
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.HTTP,
httpHealthCheck=http_health_check,
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
if include_source_regions:
source_regions = existing_check.sourceRegions
if args.IsSpecified('source_regions'):
source_regions = args.source_regions
new_health_check.sourceRegions = source_regions
return new_health_check
def _ValidateArgs(
args,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.request_path or args.check_interval or
args.timeout or args.healthy_threshold or
args.unhealthy_threshold or args.proxy_header or
args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
source_regions_modified = False
if include_source_regions and args.IsSpecified('source_regions'):
source_regions_modified = True
weight_report_mode_modified = False
if include_weighted_load_balancing and args.IsSpecified('weight_report_mode'):
weight_report_mode_modified = True
if (
args.description is None
and args.host is None
and args.response is None
and args.port_name is None
and not weight_report_mode_modified
and not source_regions_modified
and args_unset
):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(
args,
holder,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Issues the requests necessary for updating the health check."""
client = holder.client
_ValidateArgs(
args,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
)
health_check_arg = flags.HealthCheckArgument('HTTP')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(
client,
args,
objects[0],
include_log_config,
include_weighted_load_balancing,
include_source_regions,
)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.ReleaseTracks(base.ReleaseTrack.GA)
@base.DefaultUniverseOnly
class Update(base.UpdateCommand):
"""Update a HTTP health check."""
_include_log_config = True
_include_weighted_load_balancing = False
_include_source_regions = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(
parser,
cls._include_log_config,
cls._include_weighted_load_balancing,
cls._include_source_regions,
)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
return _Run(
args,
holder,
self._include_log_config,
self._include_weighted_load_balancing,
self._include_source_regions,
)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(Update):
_include_weighted_load_balancing = False
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(UpdateBeta):
_include_weighted_load_balancing = True

View File

@@ -0,0 +1,266 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief':
'Update a HTTP2 health check.',
'DESCRIPTION':
"""\
*{command}* is used to update an existing HTTP2 health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
'EXAMPLES':
"""\
To update health check interval to 10s, run:
$ {command} my-health-check-name --check-interval=10s
""",
}
def _Args(parser, include_log_config, include_weighted_load_balancing):
"""Adds all the args in the parser."""
health_check_arg = flags.HealthCheckArgument('HTTP2')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddHttpRelatedUpdateArgs(parser,
include_weighted_load_balancing)
health_checks_utils.AddHttpRelatedResponseArg(parser)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'HTTP2')
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(client, args, existing_check, include_log_config,
include_weighted_load_balancing):
"""Returns a modified HealthCheck message."""
# We do not support using 'update http2' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.HTTP2):
raise core_exceptions.Error(
'update http2 subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description, PortName, Response and Host are the only attributes that can
# be cleared by passing in an empty string (but we don't want to set it to
# an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
if args.host:
host = args.host
elif args.host is None:
host = existing_check.http2HealthCheck.host
else:
host = None
if args.response:
response = args.response
elif args.response is None:
response = existing_check.http2HealthCheck.response
else:
response = None
port, port_name, port_specification = health_checks_utils.\
HandlePortRelatedFlagsForUpdate(args, existing_check.http2HealthCheck)
if include_weighted_load_balancing:
weight_report_mode = existing_check.http2HealthCheck.weightReportMode
if args.IsSpecified('weight_report_mode'):
weight_report_mode = client.messages.HTTP2HealthCheck.WeightReportModeValueValuesEnum(
args.weight_report_mode)
proxy_header = existing_check.http2HealthCheck.proxyHeader
if args.proxy_header is not None:
proxy_header = (
client.messages.HTTP2HealthCheck.ProxyHeaderValueValuesEnum(
args.proxy_header))
http2_health_check = client.messages.HTTP2HealthCheck(
host=host,
port=port,
portName=port_name,
portSpecification=port_specification,
requestPath=(args.request_path or
existing_check.http2HealthCheck.requestPath),
proxyHeader=proxy_header,
response=response)
if include_weighted_load_balancing:
http2_health_check.weightReportMode = weight_report_mode
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.HTTP2,
http2HealthCheck=http2_health_check,
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
return new_health_check
def _ValidateArgs(args,
include_log_config,
include_weighted_load_balancing=False):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.request_path or args.check_interval or
args.timeout or args.healthy_threshold or
args.unhealthy_threshold or args.proxy_header or
args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
weight_report_mode_modified = False
if include_weighted_load_balancing and args.IsSpecified('weight_report_mode'):
weight_report_mode_modified = True
if (args.description is None and args.host is None and
args.response is None and args.port_name is None and
not weight_report_mode_modified and args_unset):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(args, holder, include_log_config, include_weighted_load_balancing):
"""Issues requests necessary to update the HTTP2 Health Checks."""
client = holder.client
_ValidateArgs(args, include_log_config, include_weighted_load_balancing)
health_check_arg = flags.HealthCheckArgument('HTTP2')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(client, args, objects[0], include_log_config,
include_weighted_load_balancing)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Update(base.UpdateCommand):
"""Update a HTTP2 health check."""
_include_log_config = True
_include_weighted_load_balancing = False
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config, cls._include_weighted_load_balancing)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
return _Run(args, holder, self._include_log_config,
self._include_weighted_load_balancing)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(Update):
_include_weighted_load_balancing = False
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(UpdateBeta):
_include_weighted_load_balancing = True

View File

@@ -0,0 +1,321 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief':
'Update a HTTPS health check.',
'DESCRIPTION':
"""\
*{command}* is used to update an existing HTTPS health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
}
def _Args(
parser,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Adds all the args in the parser."""
health_check_arg = flags.HealthCheckArgument('HTTPS')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddHttpRelatedUpdateArgs(parser,
include_weighted_load_balancing)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'HTTPS')
health_checks_utils.AddHttpRelatedResponseArg(parser)
if include_source_regions:
health_checks_utils.AddHealthCheckSourceRegionsRelatedArgs(parser)
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(
client,
args,
existing_check,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Returns a modified HealthCheck message."""
# We do not support using 'update https' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.HTTPS):
raise core_exceptions.Error(
'update https subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description, PortName, and Host are the only attributes that can be
# cleared by passing in an empty string (but we don't want to set it to
# an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
if args.host:
host = args.host
elif args.host is None:
host = existing_check.httpsHealthCheck.host
else:
host = None
port, port_name, port_specification = health_checks_utils. \
HandlePortRelatedFlagsForUpdate(args, existing_check.httpsHealthCheck)
if include_weighted_load_balancing:
weight_report_mode = existing_check.httpsHealthCheck.weightReportMode
if args.IsSpecified('weight_report_mode'):
weight_report_mode = client.messages.HTTPSHealthCheck.WeightReportModeValueValuesEnum(
args.weight_report_mode)
proxy_header = existing_check.httpsHealthCheck.proxyHeader
if args.proxy_header is not None:
proxy_header = (
client.messages.HTTPSHealthCheck.ProxyHeaderValueValuesEnum(
args.proxy_header))
if args.response:
response = args.response
elif args.response is None:
response = existing_check.httpsHealthCheck.response
else:
response = None
https_health_check = client.messages.HTTPSHealthCheck(
host=host,
port=port,
portName=port_name,
requestPath=(args.request_path or
existing_check.httpsHealthCheck.requestPath),
portSpecification=port_specification,
proxyHeader=proxy_header,
response=response)
if include_weighted_load_balancing:
https_health_check.weightReportMode = weight_report_mode
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.HTTPS,
httpsHealthCheck=https_health_check,
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
if include_source_regions:
source_regions = existing_check.sourceRegions
if args.IsSpecified('source_regions'):
source_regions = args.source_regions
new_health_check.sourceRegions = source_regions
return new_health_check
def _ValidateArgs(
args,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.request_path or args.check_interval or
args.timeout or args.healthy_threshold or
args.unhealthy_threshold or args.proxy_header or
args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
source_regions_modified = False
if include_source_regions and args.IsSpecified('source_regions'):
source_regions_modified = True
weight_report_mode_modified = False
if include_weighted_load_balancing and args.IsSpecified('weight_report_mode'):
weight_report_mode_modified = True
if (
args.description is None
and args.host is None
and args.response is None
and args.port_name is None
and not weight_report_mode_modified
and not source_regions_modified
and args_unset
):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(
args,
holder,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
):
"""Issues the requests necessary for updating the health check."""
client = holder.client
_ValidateArgs(
args,
include_log_config,
include_weighted_load_balancing,
include_source_regions,
)
health_check_arg = flags.HealthCheckArgument('HTTPS')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(
client,
args,
objects[0],
include_log_config,
include_weighted_load_balancing,
include_source_regions,
)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.ReleaseTracks(base.ReleaseTrack.GA)
@base.DefaultUniverseOnly
class Update(base.UpdateCommand):
"""Update a HTTPS health check."""
_include_log_config = True
_include_weighted_load_balancing = False
_include_source_regions = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(
parser,
cls._include_log_config,
cls._include_weighted_load_balancing,
cls._include_source_regions,
)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
return _Run(
args,
holder,
self._include_log_config,
self._include_weighted_load_balancing,
self._include_source_regions,
)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(Update):
_include_weighted_load_balancing = False
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(UpdateBeta):
_include_weighted_load_balancing = True

View File

@@ -0,0 +1,230 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief':
'Update a SSL health check.',
'DESCRIPTION':
"""\
*{command}* is used to update an existing SSL health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
}
def _Args(parser, include_log_config):
health_check_arg = flags.HealthCheckArgument('SSL')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddTcpRelatedUpdateArgs(parser)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'SSL')
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(client, args, existing_check, include_log_config):
"""Returns a modified HealthCheck message."""
# We do not support using 'update ssl' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.SSL):
raise core_exceptions.Error(
'update ssl subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description, PortName, Request, and Response are the only attributes that
# can be cleared by passing in an empty string (but we don't want to set it
# to an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
port, port_name, port_specification = health_checks_utils. \
HandlePortRelatedFlagsForUpdate(args, existing_check.sslHealthCheck)
if args.request:
request = args.request
elif args.request is None:
request = existing_check.sslHealthCheck.request
else:
request = None
if args.response:
response = args.response
elif args.response is None:
response = existing_check.sslHealthCheck.response
else:
response = None
proxy_header = existing_check.sslHealthCheck.proxyHeader
if args.proxy_header is not None:
proxy_header = client.messages.SSLHealthCheck.ProxyHeaderValueValuesEnum(
args.proxy_header)
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.SSL,
sslHealthCheck=client.messages.SSLHealthCheck(
request=request,
response=response,
port=port,
portName=port_name,
portSpecification=port_specification,
proxyHeader=proxy_header),
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
return new_health_check
def _ValidateArgs(args, include_log_config):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.check_interval or args.timeout or
args.healthy_threshold or args.unhealthy_threshold or
args.proxy_header or args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
if (args.description is None and args.request is None and
args.response is None and args.port_name is None and args_unset):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(args, holder, include_log_config):
"""Issues the requests necessary for updating the health check."""
client = holder.client
_ValidateArgs(args, include_log_config)
health_check_arg = flags.HealthCheckArgument('SSL')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(client, args, objects[0], include_log_config)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Update(base.UpdateCommand):
"""Update a SSL health check."""
_include_log_config = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
return _Run(args, holder, self._include_log_config)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(Update):
pass
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(UpdateBeta):
pass

View File

@@ -0,0 +1,262 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
def _DetailedHelp():
return {
'brief':
'Update a TCP health check.',
'DESCRIPTION':
"""\
*{command}* is used to update an existing TCP health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
""",
}
def _Args(parser, include_log_config, include_source_regions):
health_check_arg = flags.HealthCheckArgument('TCP')
health_check_arg.AddArgument(parser, operation_type='update')
health_checks_utils.AddTcpRelatedUpdateArgs(parser)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'TCP')
if include_source_regions:
health_checks_utils.AddHealthCheckSourceRegionsRelatedArgs(parser)
if include_log_config:
health_checks_utils.AddHealthCheckLoggingRelatedArgs(parser)
def _GetGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def _Modify(
client, args, existing_check, include_log_config, include_source_regions
):
"""Returns a modified HealthCheck message."""
# We do not support using 'update tcp' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.TCP):
raise core_exceptions.Error(
'update tcp subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description, PortName, Request, and Response are the only attributes that
# can be cleared by passing in an empty string (but we don't want to set it
# to an empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
port, port_name, port_specification = health_checks_utils. \
HandlePortRelatedFlagsForUpdate(args, existing_check.tcpHealthCheck)
if args.request:
request = args.request
elif args.request is None:
request = existing_check.tcpHealthCheck.request
else:
request = None
if args.response:
response = args.response
elif args.response is None:
response = existing_check.tcpHealthCheck.response
else:
response = None
proxy_header = existing_check.tcpHealthCheck.proxyHeader
if args.proxy_header is not None:
proxy_header = client.messages.TCPHealthCheck.ProxyHeaderValueValuesEnum(
args.proxy_header)
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.TCP,
tcpHealthCheck=client.messages.TCPHealthCheck(
request=request,
response=response,
port=port,
portName=port_name,
portSpecification=port_specification,
proxyHeader=proxy_header),
checkIntervalSec=(args.check_interval or existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
if include_log_config:
new_health_check.logConfig = health_checks_utils.ModifyLogConfig(
client, args, existing_check.logConfig)
if include_source_regions:
source_regions = existing_check.sourceRegions
if args.IsSpecified('source_regions'):
source_regions = args.source_regions
new_health_check.sourceRegions = source_regions
return new_health_check
def _ValidateArgs(
args,
include_log_config,
include_source_regions,
):
"""Validates given args and raises exception if any args are invalid."""
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.check_interval or args.timeout or
args.healthy_threshold or args.unhealthy_threshold or
args.proxy_header or args.use_serving_port)
if include_log_config:
args_unset = (args.enable_logging is None and args_unset)
source_regions_modified = False
if include_source_regions and args.IsSpecified('source_regions'):
source_regions_modified = True
if (
args.description is None
and args.request is None
and args.response is None
and args.port_name is None
and not source_regions_modified
and args_unset
):
raise exceptions.ArgumentError('At least one property must be modified.')
def _Run(args, holder, include_log_config, include_source_regions):
"""Issues the requests necessary for updating the health check."""
client = holder.client
_ValidateArgs(args, include_log_config, include_source_regions)
health_check_arg = flags.HealthCheckArgument('TCP')
health_check_ref = health_check_arg.ResolveAsResource(
args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = _GetRegionalGetRequest(client, health_check_ref)
else:
get_request = _GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = _Modify(
client, args, objects[0], include_log_config, include_source_regions
)
# If existing object is equal to the proposed object or if
# _Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = _GetRegionalSetRequest(client, health_check_ref, new_object)
else:
set_request = _GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])
@base.ReleaseTracks(base.ReleaseTrack.GA)
@base.DefaultUniverseOnly
class Update(base.UpdateCommand):
"""Update a TCP health check."""
_include_log_config = True
_include_source_regions = True
detailed_help = _DetailedHelp()
@classmethod
def Args(cls, parser):
_Args(parser, cls._include_log_config, cls._include_source_regions)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
return _Run(
args, holder, self._include_log_config, self._include_source_regions
)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class UpdateBeta(Update):
pass
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(UpdateBeta):
pass

View File

@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*- #
# Copyright 2016 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for updating health checks."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import health_checks_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.health_checks import exceptions
from googlecloudsdk.command_lib.compute.health_checks import flags
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class Update(base.UpdateCommand):
"""Update a UDP health check.
*{command}* is used to update an existing UDP health check. Only
arguments passed in will be updated on the health check. Other
attributes will remain unaffected.
"""
HEALTH_CHECK_ARG = None
@classmethod
def Args(cls, parser):
cls.HEALTH_CHECK_ARG = flags.HealthCheckArgument('UDP')
cls.HEALTH_CHECK_ARG.AddArgument(parser, operation_type='update')
health_checks_utils.AddUdpRelatedArgs(
parser, request_and_response_required=False)
health_checks_utils.AddProtocolAgnosticUpdateArgs(parser, 'UDP')
def _GetGetRequest(self, client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.healthChecks, 'Get',
client.messages.ComputeHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project))
def _GetSetRequest(self, client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.healthChecks, 'Update',
client.messages.ComputeHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project))
def _GetRegionalGetRequest(self, client, health_check_ref):
"""Returns a request for fetching the existing health check."""
return (client.apitools_client.regionHealthChecks, 'Get',
client.messages.ComputeRegionHealthChecksGetRequest(
healthCheck=health_check_ref.Name(),
project=health_check_ref.project,
region=health_check_ref.region))
def _GetRegionalSetRequest(self, client, health_check_ref, replacement):
"""Returns a request for updating the health check."""
return (client.apitools_client.regionHealthChecks, 'Update',
client.messages.ComputeRegionHealthChecksUpdateRequest(
healthCheck=health_check_ref.Name(),
healthCheckResource=replacement,
project=health_check_ref.project,
region=health_check_ref.region))
def Modify(self, client, args, existing_check):
"""Returns a modified HealthCheck message."""
# We do not support using 'update udp' with a health check of a
# different protocol.
if (existing_check.type !=
client.messages.HealthCheck.TypeValueValuesEnum.UDP):
raise core_exceptions.Error(
'update udp subcommand applied to health check with protocol ' +
existing_check.type.name)
# Description and PortName are the only attributes that can be cleared by
# passing in an empty string (but we don't want to set it to empty string).
if args.description:
description = args.description
elif args.description is None:
description = existing_check.description
else:
description = None
if args.port_name:
port_name = args.port_name
elif args.port_name is None:
port_name = existing_check.udpHealthCheck.portName
else:
port_name = None
new_health_check = client.messages.HealthCheck(
name=existing_check.name,
description=description,
type=client.messages.HealthCheck.TypeValueValuesEnum.UDP,
udpHealthCheck=client.messages.UDPHealthCheck(
request=args.request or existing_check.udpHealthCheck.request,
response=args.response or existing_check.udpHealthCheck.response,
port=args.port or existing_check.udpHealthCheck.port,
portName=port_name),
checkIntervalSec=(args.check_interval or
existing_check.checkIntervalSec),
timeoutSec=args.timeout or existing_check.timeoutSec,
healthyThreshold=(args.healthy_threshold or
existing_check.healthyThreshold),
unhealthyThreshold=(args.unhealthy_threshold or
existing_check.unhealthyThreshold),
)
return new_health_check
def Run(self, args):
"""Issues requests necessary to update UDP Health Checks."""
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
health_checks_utils.CheckProtocolAgnosticArgs(args)
args_unset = not (args.port or args.check_interval or args.timeout or
args.healthy_threshold or args.unhealthy_threshold or
args.request or args.response)
if args.description is None and args.port_name is None and args_unset:
raise exceptions.ArgumentError('At least one property must be modified.')
# Check that request and response are not empty. It is acceptable for it to
# be None.
if args.request is not None and not args.request:
raise exceptions.ArgumentError(
'"request" field for UDP can not be empty.')
if args.response is not None and not args.response:
raise exceptions.ArgumentError(
'"response" field for UDP can not be empty.')
health_check_ref = self.HEALTH_CHECK_ARG.ResolveAsResource(
args, holder.resources)
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
get_request = self._GetRegionalGetRequest(client, health_check_ref)
else:
get_request = self._GetGetRequest(client, health_check_ref)
objects = client.MakeRequests([get_request])
new_object = self.Modify(client, args, objects[0])
# If existing object is equal to the proposed object or if
# Modify() returns None, then there is no work to be done, so we
# print the resource and return.
if objects[0] == new_object:
log.status.Print('No change requested; skipping update for [{0}].'.format(
objects[0].name))
return objects
if health_checks_utils.IsRegionalHealthCheckRef(health_check_ref):
set_request = self._GetRegionalSetRequest(client, health_check_ref,
new_object)
else:
set_request = self._GetSetRequest(client, health_check_ref, new_object)
return client.MakeRequests([set_request])