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,43 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 reading and manipulating target pools."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.UniverseCompatible
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA)
class TargetPools(base.Group):
"""Control Compute Engine target pools for network load balancing."""
TargetPools.category = base.LOAD_BALANCING_CATEGORY
TargetPools.detailed_help = {
'DESCRIPTION': """
Control Compute Engine target pools for external passthrough
Network Load Balancers.
For more information about target pools, see the
[target pools documentation](https://cloud.google.com/load-balancing/docs/target-pools).
See also: [Target pools API](https://cloud.google.com/compute/docs/reference/rest/v1/targetPools).
""",
}

View File

@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 adding health checks to target pools."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.http_health_checks import (
flags as http_health_check_flags)
from googlecloudsdk.command_lib.compute.target_pools import flags
class AddHealthChecks(base.SilentCommand):
"""Add a legacy HTTP health check to a target pool.
*{command}* is used to add a legacy HTTP health check
to a target pool. Legacy health checks are used to determine
the health status of instances in the target pool. Only one
health check can be attached to a target pool, so this command
will fail if there as already a health check attached to the target
pool. For more information on health checks and load balancing, see
[](https://cloud.google.com/compute/docs/load-balancing-and-autoscaling/)
"""
HEALTH_CHECK_ARG = None
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
cls.HEALTH_CHECK_ARG = (
http_health_check_flags.HttpHealthCheckArgumentForTargetPool('add to'))
cls.HEALTH_CHECK_ARG.AddArgument(parser)
cls.TARGET_POOL_ARG = flags.TargetPoolArgument(
help_suffix=' to which to add the health check.')
cls.TARGET_POOL_ARG.AddArgument(
parser, operation_type='add health checks to')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
health_check_ref = self.HEALTH_CHECK_ARG.ResolveAsResource(
args, holder.resources)
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
request = client.messages.ComputeTargetPoolsAddHealthCheckRequest(
region=target_pool_ref.region,
project=target_pool_ref.project,
targetPool=target_pool_ref.Name(),
targetPoolsAddHealthCheckRequest=(
client.messages.TargetPoolsAddHealthCheckRequest(
healthChecks=[client.messages.HealthCheckReference(
healthCheck=health_check_ref.SelfLink())])))
return client.MakeRequests([(client.apitools_client.targetPools,
'AddHealthCheck', request)])

View File

@@ -0,0 +1,107 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 adding instances to target pools."""
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 utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import exceptions
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.instances import flags as instance_flags
from googlecloudsdk.command_lib.compute.target_pools import flags
from googlecloudsdk.core import log
class AddInstances(base.SilentCommand):
"""Add instances to a target pool.
*{command}* is used to add one or more instances to a target pool.
For more information on health checks and load balancing, see
[](https://cloud.google.com/compute/docs/load-balancing-and-autoscaling/)
"""
INSTANCE_ARG = None
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
cls.INSTANCE_ARG = instance_flags.InstanceArgumentForTargetPool('add to')
cls.INSTANCE_ARG.AddArgument(
parser,
operation_type='add to the target pool',
cust_metavar='INSTANCE')
cls.TARGET_POOL_ARG = flags.TargetPoolArgumentForAddRemoveInstances(
help_suffix=' to which to add the instances.')
cls.TARGET_POOL_ARG.AddArgument(parser)
compute_flags.AddZoneFlag(
parser,
resource_type='instances',
operation_type='add to the target pool',
explanation=(
'DEPRECATED, use --instances-zone. '
'If not specified, you will be prompted to select a zone.'))
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
if args.zone and not args.instances_zone:
args.instances_zone = args.zone
log.warning('The --zone flag is deprecated. Use equivalent '
'--instances-zone=%s flag.', args.instances_zone)
instance_refs = self.INSTANCE_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
instances = [
client.messages.InstanceReference(instance=instance_ref.SelfLink())
for instance_ref in instance_refs]
unique_regions = set(utils.ZoneNameToRegionName(instance_ref.zone)
for instance_ref in instance_refs)
# Check that all regions are the same.
if len(unique_regions) > 1:
raise exceptions.ArgumentError(
'Instances must all be in the same region as the target pool.')
region = unique_regions.pop()
# Check that the region of the instances is the same as target pool region.
if args.region and region != args.region:
raise exceptions.ArgumentError(
'Instances must all be in the same region as the target pool.')
args.region = region
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(args,
holder.resources)
request = client.messages.ComputeTargetPoolsAddInstanceRequest(
region=target_pool_ref.region,
project=target_pool_ref.project,
targetPool=target_pool_ref.Name(),
targetPoolsAddInstanceRequest=(
client.messages.TargetPoolsAddInstanceRequest(instances=instances)))
return client.MakeRequests([(client.apitools_client.targetPools,
'AddInstance', request)])

View File

@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*- #
# Copyright 2021 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 group for managing Compute Engine target pool configurations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class Config(base.Group):
"""Manage Compute Engine target pool configurations."""

View File

@@ -0,0 +1,38 @@
release_tracks: [ALPHA]
command_type: CONFIG_EXPORT
help_text:
brief: Export the configuration for a Compute Engine target pool.
description: |
*{command}* exports the configuration for a Compute Engine target pool.
Target pool configurations can be exported in
Kubernetes Resource Model (krm) or Terraform HCL formats. The
default format is `krm`.
Specifying `--all` allows you to export the configurations for all
target pools within the project.
Specifying `--path` allows you to export the configuration(s) to
a local directory.
examples: |
To export the configuration for a target pool, run:
$ {command} my-target-pool
To export the configuration for a target pool to a file, run:
$ {command} my-target-pool --path=/path/to/dir/
To export the configuration for a target pool in Terraform
HCL format, run:
$ {command} my-target-pool --resource-format=terraform
To export the configurations for all target pools within a
project, run:
$ {command} --all
arguments:
resource:
help_text: Target pool to export the configuration for.
spec: !REF googlecloudsdk.command_lib.compute.resources:target_pool

View File

@@ -0,0 +1,170 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 creating target pools."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions as calliope_exceptions
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.backend_services import (
flags as backend_services_flags)
from googlecloudsdk.command_lib.compute.http_health_checks import (
flags as http_health_check_flags)
from googlecloudsdk.command_lib.compute.target_pools import flags
from googlecloudsdk.core import log
class Create(base.CreateCommand):
"""Define a load-balanced pool of virtual machine instances.
*{command}* is used to create a target pool. A target pool resource
defines a group of instances that can receive incoming traffic
from forwarding rules. When a forwarding rule directs traffic to a
target pool, Compute Engine picks an instance from the
target pool based on a hash of the source and
destination IP addresses and ports. For more
information on load balancing, see
[](https://cloud.google.com/compute/docs/load-balancing-and-autoscaling/)
To add instances to a target pool, use 'gcloud compute
target-pools add-instances'.
"""
BACKUP_POOL_ARG = None
HTTP_HEALTH_CHECK_ARG = None
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
parser.display_info.AddFormat(flags.DEFAULT_LIST_FORMAT)
cls.BACKUP_POOL_ARG = flags.BackupPoolArgument(required=False)
cls.HTTP_HEALTH_CHECK_ARG = (
http_health_check_flags.HttpHealthCheckArgumentForTargetPoolCreate(
required=False))
cls.HTTP_HEALTH_CHECK_ARG.AddArgument(parser)
cls.TARGET_POOL_ARG = flags.TargetPoolArgument()
cls.TARGET_POOL_ARG.AddArgument(parser, operation_type='create')
parser.display_info.AddCacheUpdater(flags.TargetPoolsCompleter)
parser.add_argument(
'--backup-pool',
help="""\
Together with ``--failover-ratio'', this flag defines the fallback
behavior of the target pool (primary pool) to be created by this
command. If the ratio of the healthy instances in the primary pool
is at or below the specified ``--failover-ratio value'', then traffic
arriving at the load-balanced IP address will be directed to the
backup pool. If this flag is provided, then ``--failover-ratio'' is
required.
""")
parser.add_argument(
'--description',
help='An optional description of this target pool.')
parser.add_argument(
'--failover-ratio',
type=float,
help="""\
Together with ``--backup-pool'', defines the fallback behavior of the
target pool (primary pool) to be created by this command. If the
ratio of the healthy instances in the primary pool is at or below this
number, traffic arriving at the load-balanced IP address will be
directed to the backup pool. For example, if 0.4 is chosen as the
failover ratio, then traffic will fail over to the backup pool if
more than 40% of the instances become unhealthy.
If not set, the traffic will be directed the
instances in this pool in the ``force'' mode, where traffic will be
spread to the healthy instances with the best effort, or to all
instances when no instance is healthy.
If this flag is provided, then ``--backup-pool'' is required.
""")
parser.add_argument(
'--health-check',
metavar='HEALTH_CHECK',
help="""\
DEPRECATED, use --http-health-check.
Specifies an HTTP health check resource to use to determine the health
of instances in this pool. If no health check is specified, traffic will
be sent to all instances in this target pool as if the instances
were healthy, but the health status of this pool will appear as
unhealthy as a warning that this target pool does not have a health
check.
""")
backend_services_flags.AddSessionAffinity(parser, target_pools=True)
def Run(self, args):
"""Issues requests necessary for adding a target pool."""
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
if ((args.backup_pool and not args.IsSpecified('failover_ratio')) or
(args.failover_ratio and not args.IsSpecified('backup_pool'))):
raise calliope_exceptions.BadArgumentException(
'--failover-ratio',
'Either both or neither of [--failover-ratio] and [--backup-pool] '
'must be provided.')
if args.failover_ratio is not None:
if args.failover_ratio < 0 or args.failover_ratio > 1:
raise calliope_exceptions.InvalidArgumentException(
'--failover-ratio',
'[--failover-ratio] must be a number between 0 and 1, inclusive.')
if args.health_check:
args.http_health_check = args.health_check
log.warning('The --health-check flag is deprecated. Use equivalent '
'--http-health-check=%s flag.', args.health_check)
if args.http_health_check:
http_health_check = [self.HTTP_HEALTH_CHECK_ARG.ResolveAsResource(
args, holder.resources).SelfLink()]
else:
http_health_check = []
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
if args.backup_pool:
args.backup_pool_region = target_pool_ref.region
backup_pool_uri = self.BACKUP_POOL_ARG.ResolveAsResource(
args, holder.resources).SelfLink()
else:
backup_pool_uri = None
request = client.messages.ComputeTargetPoolsInsertRequest(
targetPool=client.messages.TargetPool(
backupPool=backup_pool_uri,
description=args.description,
failoverRatio=args.failover_ratio,
healthChecks=http_health_check,
name=target_pool_ref.Name(),
sessionAffinity=(
client.messages.TargetPool.SessionAffinityValueValuesEnum(
args.session_affinity))),
region=target_pool_ref.region,
project=target_pool_ref.project)
return client.MakeRequests([(client.apitools_client.targetPools, 'Insert',
request)])

View File

@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 deleting target pools."""
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 utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.target_pools import flags
class Delete(base.DeleteCommand):
"""Delete target pools.
*{command}* deletes one or more Compute Engine target pools.
"""
TARGET_POOL_ARG = None
@staticmethod
def Args(parser):
Delete.TARGET_POOL_ARG = flags.TargetPoolArgument(
help_suffix=None, plural=True)
Delete.TARGET_POOL_ARG.AddArgument(parser, operation_type='delete')
parser.display_info.AddCacheUpdater(flags.TargetPoolsCompleter)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
target_pool_refs = Delete.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
utils.PromptForDeletion(target_pool_refs, 'region')
requests = []
for target_pool_ref in target_pool_refs:
requests.append((client.apitools_client.targetPools, 'Delete',
client.messages.ComputeTargetPoolsDeleteRequest(
**target_pool_ref.AsDict())))
return client.MakeRequests(requests)

View File

@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 describing target pools."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.target_pools import flags
class Describe(base.DescribeCommand):
"""Describe a Compute Engine target pool.
*{command}* displays all data associated with a Compute Engine
target pool in a project.
"""
TARGET_POOL_ARG = None
@staticmethod
def Args(parser):
Describe.TARGET_POOL_ARG = flags.TargetPoolArgument()
Describe.TARGET_POOL_ARG.AddArgument(parser, operation_type='describe')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
request = client.messages.ComputeTargetPoolsGetRequest(
**target_pool_ref.AsDict())
return client.MakeRequests([(client.apitools_client.targetPools,
'Get', request)])[0]

View File

@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 getting a target pool's health."""
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 utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.target_pools import flags
class GetHealth(base.DescribeCommand):
"""Get the health of instances in a target pool.
*{command}* displays the health of instances in a target pool.
"""
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
cls.TARGET_POOL_ARG = flags.TargetPoolArgument()
cls.TARGET_POOL_ARG.AddArgument(
parser, operation_type='get health information for')
def GetTargetPool(self, client, target_pool_ref):
"""Fetches the target pool resource."""
objects = client.MakeRequests(
[(client.apitools_client.targetPools, 'Get',
client.messages.ComputeTargetPoolsGetRequest(
project=target_pool_ref.project,
region=target_pool_ref.region,
targetPool=target_pool_ref.Name()))])
return objects[0]
def Run(self, args):
"""Returns a list of TargetPoolInstanceHealth objects."""
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
target_pool = self.GetTargetPool(client, target_pool_ref)
instances = target_pool.instances
# If the target pool has no instances, we should return an empty
# list.
if not instances:
return
requests = []
for instance in instances:
request_message = client.messages.ComputeTargetPoolsGetHealthRequest(
instanceReference=client.messages.InstanceReference(
instance=instance),
project=target_pool_ref.project,
region=target_pool_ref.region,
targetPool=target_pool_ref.Name())
requests.append((client.apitools_client.targetPools, 'GetHealth',
request_message))
errors = []
resources = client.MakeRequests(
requests=requests,
errors_to_collect=errors)
for resource in resources:
yield resource
if errors:
utils.RaiseToolException(
errors,
error_message='Could not get health for some targets:')

View File

@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 listing target pools."""
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 lister
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.target_pools import flags
class List(base.ListCommand):
"""List target pools."""
@staticmethod
def Args(parser):
parser.display_info.AddFormat(flags.DEFAULT_LIST_FORMAT)
parser.display_info.AddCacheUpdater(flags.TargetPoolsCompleter)
lister.AddRegionsArg(parser)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
request_data = lister.ParseRegionalFlags(args, holder.resources)
list_implementation = lister.RegionalLister(
client, client.apitools_client.targetPools)
return lister.Invoke(request_data, list_implementation)
List.detailed_help = base_classes.GetRegionalListerHelp('target pools')

View File

@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 removing health checks from target pools."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.http_health_checks import (
flags as http_health_check_flags)
from googlecloudsdk.command_lib.compute.target_pools import flags
class RemoveHealthChecks(base.SilentCommand):
"""Remove an HTTP health check from a target pool.
*{command}* is used to remove an HTTP health check
from a target pool. Health checks are used to determine
the health status of instances in the target pool. For more
information on health checks and load balancing, see
[](https://cloud.google.com/compute/docs/load-balancing-and-autoscaling/)
"""
HEALTH_CHECK_ARG = None
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
cls.HEALTH_CHECK_ARG = (
http_health_check_flags.HttpHealthCheckArgumentForTargetPool(
'remove from'))
cls.HEALTH_CHECK_ARG.AddArgument(parser)
cls.TARGET_POOL_ARG = flags.TargetPoolArgument(
help_suffix=' from which to remove the health check.')
cls.TARGET_POOL_ARG.AddArgument(
parser, operation_type='remove health checks from')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
http_health_check_ref = self.HEALTH_CHECK_ARG.ResolveAsResource(
args, holder.resources)
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
request = client.messages.ComputeTargetPoolsRemoveHealthCheckRequest(
region=target_pool_ref.region,
project=target_pool_ref.project,
targetPool=target_pool_ref.Name(),
targetPoolsRemoveHealthCheckRequest=(
client.messages.TargetPoolsRemoveHealthCheckRequest(
healthChecks=[client.messages.HealthCheckReference(
healthCheck=http_health_check_ref.SelfLink())])))
return client.MakeRequests([(client.apitools_client.targetPools,
'RemoveHealthCheck', request)])

View File

@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 removing instances from target pools."""
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 utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import exceptions as compute_exceptions
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.instances import flags as instance_flags
from googlecloudsdk.command_lib.compute.target_pools import flags
from googlecloudsdk.core import log
class RemoveInstances(base.SilentCommand):
"""Remove instances from a target pool.
*{command}* is used to remove one or more instances from a
target pool.
For more information on health checks and load balancing, see
[](https://cloud.google.com/compute/docs/load-balancing-and-autoscaling/)
"""
INSTANCE_ARG = None
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
cls.INSTANCE_ARG = instance_flags.InstanceArgumentForTargetPool(
'remove from')
cls.INSTANCE_ARG.AddArgument(
parser,
operation_type='remove from the target pool',
cust_metavar='INSTANCE')
cls.TARGET_POOL_ARG = flags.TargetPoolArgumentForAddRemoveInstances(
help_suffix=' from which to remove the instances.')
cls.TARGET_POOL_ARG.AddArgument(parser)
compute_flags.AddZoneFlag(
parser,
resource_type='instances',
operation_type='remove from the target pool',
explanation=(
'DEPRECATED, use --instances-zone. '
'If not specified, you will be prompted to select a zone.'))
def Run(self, args):
"""Issues a TargetPools.RemoveInstance request."""
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
if args.zone and not args.instances_zone:
args.instances_zone = args.zone
log.warning('The --zone flag is deprecated. Use equivalent '
'--instances-zone=%s flag.', args.instances_zone)
instance_refs = self.INSTANCE_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
instances = [
client.messages.InstanceReference(instance=instance_ref.SelfLink())
for instance_ref in instance_refs]
# This check to make sure the regions for the instances are the same is not
# really necessary, but it does allow for a fast fail if the user passes in
# instances from different regions.
unique_regions = set(utils.ZoneNameToRegionName(instance_ref.zone)
for instance_ref in instance_refs)
if len(unique_regions) > 1:
raise compute_exceptions.ArgumentError(
'Instances must all be in the same region as the target pool.')
region = unique_regions.pop()
# Check that the region of the instances is the same as target pool region.
if args.region and region != args.region:
raise compute_exceptions.ArgumentError(
'Instances must all be in the same region as the target pool.')
args.region = region
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(args,
holder.resources)
request = client.messages.ComputeTargetPoolsRemoveInstanceRequest(
region=target_pool_ref.region,
project=target_pool_ref.project,
targetPool=target_pool_ref.Name(),
targetPoolsRemoveInstanceRequest=(
client.messages.TargetPoolsRemoveInstanceRequest(
instances=instances)))
return client.MakeRequests([(client.apitools_client.targetPools,
'RemoveInstance', request)])

View File

@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*- #
# Copyright 2014 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 setting a backup target pool."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import exceptions as compute_exceptions
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.target_pools import flags
class SetBackup(base.SilentCommand):
# pylint: disable=line-too-long
"""Set a backup pool for a target pool.
*{command}* is used to set a backup target pool for a primary
target pool, which defines the fallback behavior of the primary
pool. If the ratio of the healthy instances in the primary pool
is at or below the specified ``--failover-ratio value'', then traffic
arriving at the load-balanced IP address will be directed to the
backup pool.
## EXAMPLES
To cause `TARGET-POOL` (in region `us-central1`) to fail over
to `BACKUP-POOL` when more than half of the `TARGET-POOL`
instances are unhealthy, run:
$ {command} TARGET-POOL --backup-pool=BACKUP-POOL --failover-ratio=0.5 --region=us-central1
To remove `BACKUP-POOL` as a backup to `TARGET-POOL`, run:
$ {command} TARGET-POOL --backup-pool='' --region=us-central1
"""
# pylint: enable=line-too-long
BACKUP_POOL_ARG = None
TARGET_POOL_ARG = None
@classmethod
def Args(cls, parser):
cls.BACKUP_POOL_ARG = flags.BackupPoolArgument()
cls.TARGET_POOL_ARG = flags.TargetPoolArgument(
help_suffix=' for which to set the backup pool.')
cls.TARGET_POOL_ARG.AddArgument(
parser, operation_type='set a backup pool for')
backup_pool_group = parser.add_mutually_exclusive_group(required=True)
backup_pool_group.add_argument(
'--no-backup-pool', action='store_true',
help='Unsets the backup pool. This disables failover.')
backup_pool_group.add_argument(
'--backup-pool',
completer=flags.TargetPoolsCompleter,
help='Name of the target pool that will serve as backup.')
parser.add_argument(
'--failover-ratio',
type=float,
help=('The new failover ratio value for the target pool. '
'This must be a float in the range of [0, 1].'))
def Run(self, args):
"""Issues a request necessary for setting a backup target pool."""
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
if args.backup_pool:
args.backup_pool_region = target_pool_ref.region
backup_pool_ref = self.BACKUP_POOL_ARG.ResolveAsResource(args,
holder.resources)
target_reference = client.messages.TargetReference(
target=backup_pool_ref.SelfLink())
else:
target_reference = client.messages.TargetReference()
if args.backup_pool and args.failover_ratio is None:
raise compute_exceptions.ArgumentError(
'[--failover-ratio] must be provided when setting a backup pool.')
if args.failover_ratio is not None and (
args.failover_ratio < 0 or args.failover_ratio > 1):
raise compute_exceptions.ArgumentError(
'[--failover-ratio] must be a number between 0 and 1, inclusive.')
request = client.messages.ComputeTargetPoolsSetBackupRequest(
targetPool=target_pool_ref.Name(),
targetReference=target_reference,
failoverRatio=args.failover_ratio,
region=target_pool_ref.region,
project=target_pool_ref.project)
return client.MakeRequests([(client.apitools_client.targetPools,
'SetBackup', request)])

View File

@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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 target pools."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.security_policies import (
flags as security_policy_flags)
from googlecloudsdk.command_lib.compute.target_pools import flags
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Update(base.UpdateCommand):
r"""Update a Compute Engine target pool.
*{command}* updates a Compute Engine target pool.
## EXAMPLES
To update the security policy run this:
$ {command} TARGET_POOL --security-policy='my-policy'
"""
TARGET_POOL_ARG = None
SECURITY_POLICY_ARG = None
@classmethod
def Args(cls, parser):
cls.TARGET_POOL_ARG = flags.TargetPoolArgument()
cls.TARGET_POOL_ARG.AddArgument(parser, operation_type='update')
cls.SECURITY_POLICY_ARG = (
security_policy_flags
.SecurityPolicyRegionalArgumentForTargetResource(
resource='target pool'))
cls.SECURITY_POLICY_ARG.AddArgument(parser)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(client))
# Empty string is a valid value.
if getattr(args, 'security_policy', None) is not None:
if getattr(args, 'security_policy', None):
security_policy_ref = self.SECURITY_POLICY_ARG.ResolveAsResource(
args, holder.resources).SelfLink()
# If security policy is an empty string we should clear the current policy
else:
security_policy_ref = None
request = client.messages.ComputeTargetPoolsSetSecurityPolicyRequest(
project=target_pool_ref.project,
targetPool=target_pool_ref.Name(),
region=target_pool_ref.region,
securityPolicyReference=client.messages.SecurityPolicyReference(
securityPolicy=security_policy_ref
)
)
return client.MakeRequests([(client.apitools_client.targetPools,
'SetSecurityPolicy', request)])
parameter_names = ['--security-policy']
raise exceptions.MinimumArgumentException(
parameter_names, 'Please specify at least one property to update')