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,32 @@
# -*- 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.
"""Provide commands for managing AlloyDB clusters."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA,
base.ReleaseTrack.GA)
class Clusters(base.Group):
"""Provide commands for managing AlloyDB clusters.
Provide commands for managing AlloyDB clusters including creating,
configuring, restarting, and deleting clusters.
"""

View File

@@ -0,0 +1,146 @@
# -*- 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.
"""Creates a new AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.command_lib.kms import resource_args as kms_resource_args
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
# TODO: b/312466999 - Change @base.DefaultUniverseOnly to
# @base.UniverseCompatible once b/312466999 is fixed.
# See go/gcloud-cli-running-tpc-tests.
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Create(base.CreateCommand):
"""Create a new AlloyDB cluster within a given project."""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To create a new cluster, run:
$ {command} my-cluster --region=us-central1 --password=postgres
""",
}
@classmethod
def Args(cls, parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
alloydb_messages = api_util.GetMessagesModule(cls.ReleaseTrack())
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddNetwork(parser)
flags.AddPassword(parser)
flags.AddAllocatedIPRangeName(parser)
kms_resource_args.AddKmsKeyResourceArg(
parser,
'cluster',
permission_info=(
"The 'AlloyDB Service Agent' service account must hold permission"
" 'Cloud KMS CryptoKey Encrypter/Decrypter'"
),
)
flags.AddAutomatedBackupFlags(
parser, alloydb_messages, cls.ReleaseTrack(), update=False
)
flags.AddContinuousBackupConfigFlags(parser, cls.ReleaseTrack())
flags.AddDatabaseVersion(parser, alloydb_messages, cls.ReleaseTrack())
flags.AddEnablePrivateServiceConnect(parser)
flags.AddMaintenanceWindow(parser, alloydb_messages)
flags.AddDenyMaintenancePeriod(parser, alloydb_messages)
flags.AddSubscriptionType(parser, alloydb_messages)
flags.AddTags(parser)
def ConstructCreateRequestFromArgs(self, alloydb_messages, location_ref,
args):
return cluster_helper.ConstructCreateRequestFromArgsGA(
alloydb_messages, location_ref, args)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
location_ref = client.resource_parser.Create(
'alloydb.projects.locations',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region)
req = self.ConstructCreateRequestFromArgs(alloydb_messages, location_ref,
args)
op = alloydb_client.projects_locations_clusters.Create(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations')
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(op_ref, 'Creating cluster', self.ReleaseTrack())
return op
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class CreateBeta(Create):
"""Create a new AlloyDB cluster within a given project."""
@classmethod
def Args(cls, parser):
super(CreateBeta, cls).Args(parser)
def ConstructCreateRequestFromArgs(
self, alloydb_messages, location_ref, args
):
return cluster_helper.ConstructCreateRequestFromArgsBeta(
alloydb_messages, location_ref, args
)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class CreateAlpha(CreateBeta):
"""Create a new AlloyDB cluster within a given project."""
@classmethod
def Args(cls, parser):
super(CreateAlpha, cls).Args(parser)
def ConstructCreateRequestFromArgs(
self, alloydb_messages, location_ref, args
):
return cluster_helper.ConstructCreateRequestFromArgsAlpha(
alloydb_messages, location_ref, args
)

View File

@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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.
"""Creates a new AlloyDB secondary cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.command_lib.kms import resource_args as kms_resource_args
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class CreateSecondary(base.CreateCommand):
"""Create a new AlloyDB SECONDARY cluster within a given project."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To create a new cluster, run:
$ {command} my-cluster --region=us-central1 --primary-cluster=projects/12345/locations/us-central1/clusters/cluster1
""",
}
@classmethod
def Args(cls, parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
alloydb_messages = api_util.GetMessagesModule(cls.ReleaseTrack())
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddPrimaryCluster(parser)
flags.AddAllocatedIPRangeName(parser)
flags.AddContinuousBackupConfigFlagsForCreateSecondary(parser)
flags.AddAutomatedBackupFlagsForCreateSecondary(parser, alloydb_messages)
flags.AddTags(parser)
kms_resource_args.AddKmsKeyResourceArg(
parser,
'cluster',
permission_info=(
"The 'AlloyDB Service Agent' service account must hold permission"
" 'Cloud KMS CryptoKey Encrypter/Decrypter'"
),
)
def ConstructCreateSecondaryRequestFromArgs(
self, alloydb_messages, location_ref, args):
return cluster_helper.ConstructCreatesecondaryRequestFromArgsGA(
alloydb_messages, location_ref, args)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
location_ref = client.resource_parser.Create(
'alloydb.projects.locations',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
)
req = self.ConstructCreateSecondaryRequestFromArgs(
alloydb_messages, location_ref, args
)
op = alloydb_client.projects_locations_clusters.Createsecondary(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations'
)
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(op_ref, 'Creating cluster', self.ReleaseTrack())
return op
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class CreateSecondaryBeta(CreateSecondary):
"""Create a new AlloyDB SECONDARY cluster within a given project."""
@classmethod
def Args(cls, parser):
super(CreateSecondaryBeta, cls).Args(parser)
def ConstructCreateSecondaryRequestFromArgs(
self, alloydb_messages, location_ref, args):
return cluster_helper.ConstructCreatesecondaryRequestFromArgsBeta(
alloydb_messages, location_ref, args)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class CreateSecondaryAlpha(CreateSecondaryBeta):
"""Create a new AlloyDB SECONDARY cluster within a given project."""
@classmethod
def Args(cls, parser):
super(CreateSecondaryAlpha, cls).Args(parser)
def ConstructCreateSecondaryRequestFromArgs(
self, alloydb_messages, location_ref, args
):
return cluster_helper.ConstructCreatesecondaryRequestFromArgsAlpha(
alloydb_messages, location_ref, args
)

View File

@@ -0,0 +1,92 @@
# -*- 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.
"""Deletes a AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
from googlecloudsdk.core.console import console_io
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA,
base.ReleaseTrack.GA)
class Delete(base.DeleteCommand):
"""Delete an AlloyDB cluster in a given region."""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To delete a cluster, run:
$ {command} my-cluster --region=us-central1
""",
}
@staticmethod
def Args(parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddForce(parser)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region, clustersId=args.cluster)
prompt_message = (
'All of the cluster data will be lost when the cluster is deleted.')
if not console_io.PromptContinue(message=prompt_message):
return None
req = alloydb_messages.AlloydbProjectsLocationsClustersDeleteRequest(
name=cluster_ref.RelativeName(), force=args.force)
op = alloydb_client.projects_locations_clusters.Delete(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations')
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(op_ref, 'Deleting cluster', self.ReleaseTrack(),
False)
return op

View File

@@ -0,0 +1,90 @@
# -*- 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.
"""Describes a AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA,
base.ReleaseTrack.GA)
class Describe(base.DescribeCommand):
"""Describe an AlloyDB cluster in a given project and region."""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To describe a cluster, run:
$ {command} my-cluster --region=us-central1
""",
}
@staticmethod
def Args(parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
flags.AddRegion(parser)
flags.AddCluster(parser)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region, clustersId=args.cluster)
req = alloydb_messages.AlloydbProjectsLocationsClustersGetRequest(
name=cluster_ref.RelativeName()
)
cluster = alloydb_client.projects_locations_clusters.Get(req)
normalize_automated_backup_policy(cluster.automatedBackupPolicy)
return cluster
def normalize_automated_backup_policy(policy):
"""Normalizes the policy so that it looks correct when printed."""
if policy is None:
return
if policy.weeklySchedule is None:
return
for start_time in policy.weeklySchedule.startTimes:
# If the customer selects 00:00 as a start time, this ultimately becomes
# a start time with all None fields. In the terminal this is then
# confusingly printed as `{}`. We manually set the hours to be 0 in this
# case so that it appears as `hours: 0`.
if start_time.hours is None:
start_time.hours = 0

View File

@@ -0,0 +1,111 @@
# -*- coding: utf-8 -*- #
# Copyright 2024 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.
"""Exports data from an AlloyDB cluster to Google Cloud Storage."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
# TODO: b/312466999 - Change @base.DefaultUniverseOnly to
# @base.UniverseCompatible once b/312466999 is fixed.
# See go/gcloud-cli-running-tpc-tests.
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Export(base.SilentCommand):
"""Export data from an AlloyDB cluster to Google Cloud Storage."""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To export a cluster, run:
$ {command} my-cluster --region=us-central1 --database=my-database --gcs-uri=gs://my-bucket/my-export-file-path --csv --select-query="SELECT * FROM my-table"
""",
}
@staticmethod
def Args(parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddDatabase(parser)
flags.AddDestinationURI(parser)
flags.AddExportOptions(parser)
def ConstructExportRequestFromArgs(
self, alloydb_messages, cluster_ref, args
):
return cluster_helper.ConstructExportRequestFromArgs(
alloydb_messages, cluster_ref, args
)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
clustersId=args.cluster)
req = self.ConstructExportRequestFromArgs(
alloydb_messages, cluster_ref, args
)
op = alloydb_client.projects_locations_clusters.Export(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations')
if not args.async_:
cluster_operations.Await(
op_ref, 'Exporting cluster', self.ReleaseTrack(), False
)
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
return op
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class ExportAlpha(Export):
"""Export data from an AlloyDB cluster to Google Cloud Storage."""
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class ExportBeta(Export):
"""Export data from an AlloyDB cluster to Google Cloud Storage."""

View File

@@ -0,0 +1,110 @@
# -*- 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.
"""Imports data into an AlloyDB cluster from Google Cloud Storage."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
# TODO: b/312466999 - Change @base.DefaultUniverseOnly to
# @base.UniverseCompatible once b/312466999 is fixed.
# See go/gcloud-cli-running-tpc-tests.
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Import(base.SilentCommand):
"""Import data into an AlloyDB cluster from Google Cloud Storage."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To import data into a cluster, run:
$ {command} my-cluster --region=us-central1 --database=my-database --gcs-uri=gs://my-bucket/source-file-path --sql --user=my-user"
""",
}
@staticmethod
def Args(parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddDatabase(parser, False)
flags.AddSourceURI(parser)
flags.AddImportUser(parser)
flags.AddImportOptions(parser)
def ConstructImportRequestFromArgs(self, alloydb_messages, cluster_ref, args):
return cluster_helper.ConstructImportRequestFromArgs(
alloydb_messages, cluster_ref, args
)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
clustersId=args.cluster,
)
req = self.ConstructImportRequestFromArgs(
alloydb_messages, cluster_ref, args
)
op = alloydb_client.projects_locations_clusters.Import(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations'
)
if not args.async_:
cluster_operations.Await(
op_ref, 'Importing data from cluster', self.ReleaseTrack(), False
)
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
return op
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class ImportAlpha(Import):
"""Import data to an AlloyDB cluster from Google Cloud Storage."""
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class ImportBeta(Import):
"""Import data to an AlloyDB cluster from Google Cloud Storage."""

View File

@@ -0,0 +1,99 @@
# -*- 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.
"""Lists AlloyDB clusters."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import properties
CLUSTER_FORMAT = """
table(
name,
network,
state:label=STATUS
)
"""
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA,
base.ReleaseTrack.GA)
class List(base.ListCommand):
"""List AlloyDB clusters in a given project and region.
List AlloyDB clusters in a given project in alphabetical
order based on the cluster name.
"""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To list clusters, run:
$ {command} --region=us-central1
""",
}
@staticmethod
def Args(parser):
"""Constructs and sends request.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
parser.add_argument(
'--region',
default='-',
help=('Regional location (e.g. asia-east1, us-east1). See the full '
'list of regions at '
'https://cloud.google.com/sql/docs/instance-locations. '
'Default: list clusters in all regions.'))
parser.display_info.AddFormat(CLUSTER_FORMAT)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
location_ref = client.resource_parser.Create(
'alloydb.projects.locations',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region)
result = api_util.YieldFromListHandlingUnreachable(
alloydb_client.projects_locations_clusters,
alloydb_messages.AlloydbProjectsLocationsClustersListRequest(
parent=location_ref.RelativeName()
),
field='clusters',
limit=args.limit,
batch_size=args.page_size,
batch_size_attribute='pageSize',
)
return result

View File

@@ -0,0 +1,208 @@
# -*- 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.
"""Migrates a Cloud SQL instance to an AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import argparse
import types
from apitools.base.protorpclite import messages
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.command_lib.kms import resource_args as kms_resource_args
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
# TODO: b/312466999 - Change @base.DefaultUniverseOnly to
# @base.UniverseCompatible once b/312466999 is fixed.
# See go/gcloud-cli-running-tpc-tests.
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class MigrateCloudSqlGA(base.RestoreCommand):
"""Migrate Cloud SQL instance to an AlloyDB cluster using an existing Cloud SQL backup."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To migrate a Cloud SQL instance to an AlloyDB cluster from a backup, run:
$ {command} my-alloydb-cluster --region=us-central1 --cloud-sql-project-id=my-cloud-sql-project-id --cloud-sql-instance-id=my-cloud-sql-cluster-id --cloud-sql-backup-id=my-cloud-sql-backup-id
""",
}
@classmethod
def Args(cls, parser: argparse.PARSER) -> None:
"""Specifies additional command flags.
Args:
parser: Parser object for command line inputs.
"""
alloydb_messages = api_util.GetMessagesModule(cls.ReleaseTrack())
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddNetwork(parser)
flags.AddPassword(parser)
flags.AddAllocatedIPRangeName(parser)
kms_resource_args.AddKmsKeyResourceArg(
parser,
'cluster',
permission_info=(
"The 'AlloyDB Service Agent' service account must hold permission"
" 'Cloud KMS CryptoKey Encrypter/Decrypter'"
),
)
flags.AddAutomatedBackupFlags(
parser, alloydb_messages, cls.ReleaseTrack(), update=False
)
flags.AddContinuousBackupConfigFlags(parser, cls.ReleaseTrack())
flags.AddDatabaseVersion(parser, alloydb_messages, cls.ReleaseTrack())
flags.AddEnablePrivateServiceConnect(parser)
flags.AddMaintenanceWindow(parser, alloydb_messages)
flags.AddSubscriptionType(parser, alloydb_messages)
flags.AddTags(parser)
flags.AddMigrateCloudSqlFlags(parser)
flags.AddDenyMaintenancePeriod(parser, alloydb_messages)
def ConstructMigrateCloudSqlRequestFromArgs(
self,
alloydb_messages: types.ModuleType,
location_ref: resources.Resource,
args: argparse.Namespace,
) -> messages.Message:
"""Constructs the Migrate Cloud Sql request.
Args:
alloydb_messages: The AlloyDB messages module.
location_ref: The location reference for the request.
args: An object that contains the values for the arguments specified in
the .Args() method.
Returns:
The Migrate Cloud Sql request based on args.
"""
return cluster_helper.ConstructMigrateCloudSqlRequestFromArgsGA(
alloydb_messages, location_ref, args
)
def Run(self, args: argparse.Namespace) -> messages.Message:
"""Constructs request from args, and sends it to the server.
Args:
args: An object that contains the values for the arguments specified in
the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
location_ref = client.resource_parser.Create(
'alloydb.projects.locations',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
)
req = self.ConstructMigrateCloudSqlRequestFromArgs(
alloydb_messages, location_ref, args
)
op = alloydb_client.projects_locations_clusters.RestoreFromCloudSQL(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations'
)
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(
op_ref, 'Migrating Cloud SQL', self.ReleaseTrack()
)
return op
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class MigrateCloudSqlBeta(MigrateCloudSqlGA):
"""Migrate Cloud SQL instance to an AlloyDB cluster.
This command migrates a Cloud SQL instance to an AlloyDB cluster using an
existing Cloud SQL backup.
"""
@classmethod
def Args(cls, parser: argparse.PARSER) -> None:
super(MigrateCloudSqlBeta, cls).Args(parser)
def ConstructMigrateCloudSqlRequestFromArgs(
self,
alloydb_messages: types.ModuleType,
location_ref: resources.Resource,
args: argparse.Namespace,
) -> messages.Message:
"""Constructs the Migrate Cloud Sql request.
Args:
alloydb_messages: The AlloyDB messages module.
location_ref: The location reference for the request.
args: An object that contains the values for the arguments specified in
the .Args() method.
Returns:
The Migrate Cloud Sql request based on args.
"""
return cluster_helper.ConstructMigrateCloudSqlRequestFromArgsBeta(
alloydb_messages, location_ref, args
)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class MigrateCloudSqlAlpha(MigrateCloudSqlGA):
"""Migrate Cloud SQL instance to an AlloyDB cluster.
This command migrates a Cloud SQL instance to an AlloyDB cluster using an
existing Cloud SQL backup.
"""
@classmethod
def Args(cls, parser: argparse.PARSER) -> None:
super(MigrateCloudSqlAlpha, cls).Args(parser)
def ConstructMigrateCloudSqlRequestFromArgs(
self,
alloydb_messages: types.ModuleType,
location_ref: resources.Resource,
args: argparse.Namespace,
) -> messages.Message:
"""Constructs the Migrate Cloud Sql request.
Args:
alloydb_messages: The AlloyDB messages module.
location_ref: The location reference for the request.
args: An object that contains the values for the arguments specified in
the .Args() method.
Returns:
The Migrate Cloud Sql request based on args.
"""
return cluster_helper.ConstructMigrateCloudSqlRequestFromArgsAlpha(
alloydb_messages, location_ref, args
)

View File

@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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.
"""Promote an AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Promote(base.UpdateCommand):
"""Promote an AlloyDB SECONDARY cluster in a given project and region."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To promote a cluster, run:
$ {command} my-cluster --region=us-central1
""",
}
@staticmethod
def Args(parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
clustersId=args.cluster)
req = alloydb_messages.AlloydbProjectsLocationsClustersPromoteRequest(
name=cluster_ref.RelativeName(),
promoteClusterRequest=alloydb_messages.PromoteClusterRequest())
op = alloydb_client.projects_locations_clusters.Promote(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations')
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(op_ref, 'Promoting cluster', self.ReleaseTrack())
return op

View File

@@ -0,0 +1,177 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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.
"""Restores an AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.command_lib.kms import resource_args as kms_resource_args
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Restore(base.RestoreCommand):
# TODO(b/407008589): update the description and examples for Backup DR.
"""Restore an AlloyDB cluster from a given backup or a source cluster and a timestamp."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To restore a cluster from a backup, run:
$ {command} my-cluster --region=us-central1 --backup=my-backup
To restore a cluster from a source cluster and a timestamp, run:
$ {command} my-cluster --region=us-central1 \
--source-cluster=old-cluster \
--point-in-time=2012-11-15T16:19:00.094Z
""",
}
@staticmethod
def CommonArgs(parser):
base.ASYNC_FLAG.AddToParser(parser)
flags.AddCluster(parser)
flags.AddRegion(parser)
flags.AddNetwork(parser)
flags.AddAllocatedIPRangeName(parser)
flags.AddEnablePrivateServiceConnect(parser)
flags.AddTags(parser)
kms_resource_args.AddKmsKeyResourceArg(
parser,
'cluster',
permission_info=(
"The 'AlloyDB Service Agent' service account must hold permission"
" 'Cloud KMS CryptoKey Encrypter/Decrypter'"
),
)
@classmethod
def Args(cls, parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
Restore.CommonArgs(parser)
flags.AddRestoreClusterSourceFlags(parser, cls.ReleaseTrack())
def ConstructRestoreRequestFromArgs(self, alloydb_messages, location_ref,
resource_parser, args):
return cluster_helper.ConstructRestoreRequestFromArgsGA(
alloydb_messages, location_ref, resource_parser, args)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
location_ref = client.resource_parser.Create(
'alloydb.projects.locations',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region)
req = self.ConstructRestoreRequestFromArgs(
alloydb_messages, location_ref, client.resource_parser, args)
op = alloydb_client.projects_locations_clusters.Restore(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations')
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(op_ref, 'Restoring cluster', self.ReleaseTrack())
return op
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class RestoreBeta(Restore):
"""Restore an AlloyDB cluster from a given backup or a source cluster and a timestamp."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To restore a cluster from a backup, run:
$ {command} my-cluster --region=us-central1 --backup=my-backup
To restore a cluster from a source cluster and a timestamp, run:
$ {command} my-cluster --region=us-central1 \
--source-cluster=old-cluster \
--point-in-time=2012-11-15T16:19:00.094Z
""",
}
@classmethod
def Args(cls, parser):
super(RestoreBeta, cls).Args(parser)
def ConstructRestoreRequestFromArgs(
self, alloydb_messages, location_ref, resource_parser, args
):
return cluster_helper.ConstructRestoreRequestFromArgsBeta(
alloydb_messages, location_ref, resource_parser, args
)
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class RestoreAlpha(RestoreBeta):
"""Restore an AlloyDB cluster from a given backup or a source cluster and a timestamp."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To restore a cluster from a backup, run:
$ {command} my-cluster --region=us-central1 --backup=my-backup
To restore a cluster from a source cluster and a timestamp, run:
$ {command} my-cluster --region=us-central1 \
--source-cluster=old-cluster \
--point-in-time=2012-11-15T16:19:00.094Z
""",
}
@classmethod
def Args(cls, parser):
super(RestoreAlpha, cls).Args(parser)
def ConstructRestoreRequestFromArgs(
self, alloydb_messages, location_ref, resource_parser, args
):
return cluster_helper.ConstructRestoreRequestFromArgsAlpha(
alloydb_messages, location_ref, resource_parser, args
)

View File

@@ -0,0 +1,89 @@
# -*- 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.
"""Switchover an AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
@base.DefaultUniverseOnly
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Switchover(base.UpdateCommand):
"""Switchover an AlloyDB SECONDARY cluster in a given project and region."""
detailed_help = {
'DESCRIPTION': '{description}',
'EXAMPLES': """\
To switchover a cluster, run:
$ {command} my-cluster --region=us-central1
""",
}
@staticmethod
def Args(parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
clustersId=args.cluster,
)
req = alloydb_messages.AlloydbProjectsLocationsClustersSwitchoverRequest(
name=cluster_ref.RelativeName(),
switchoverClusterRequest=alloydb_messages.SwitchoverClusterRequest(),
)
op = alloydb_client.projects_locations_clusters.Switchover(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations'
)
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(
op_ref, 'Switchover cluster', self.ReleaseTrack()
)
return op

View File

@@ -0,0 +1,134 @@
# -*- 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.
"""Updates a AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
# TODO: b/312466999 - Change @base.DefaultUniverseOnly to
# @base.UniverseCompatible once b/312466999 is fixed.
# See go/gcloud-cli-running-tpc-tests.
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Update(base.UpdateCommand):
"""Update an AlloyDB cluster within a given project and region."""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To update a cluster, run:
$ {command} my-cluster --region=us-central1 --automated-backup-start-times=12:00 --automated-backup-days-of-week=MONDAY --automated-backup-retention-count=10
""",
}
def __init__(self, *args, **kwargs):
super(Update, self).__init__(*args, **kwargs)
self.parameters = [
(
'--automated-backup-* | --disable-automated-backup | '
'--clear-automated-backup'
),
(
'--enable-continuous-backup | '
'--continuous-backup-* | --clear-continuous-backup-encryption-key'
),
]
@classmethod
def Args(cls, parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
alloydb_messages = api_util.GetMessagesModule(cls.ReleaseTrack())
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddAutomatedBackupFlags(
parser, alloydb_messages, cls.ReleaseTrack(), update=True
)
flags.AddContinuousBackupConfigFlags(
parser, cls.ReleaseTrack(), update=True
)
flags.AddMaintenanceWindow(parser, alloydb_messages, update=True)
flags.AddDenyMaintenancePeriod(parser, alloydb_messages, update=True)
flags.AddMaintenanceVersion(parser)
flags.AddSubscriptionType(parser, alloydb_messages)
def ConstructPatchRequestFromArgs(self, alloydb_messages, cluster_ref, args):
return cluster_helper.ConstructPatchRequestFromArgsGA(
alloydb_messages, cluster_ref, args)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
clustersId=args.cluster)
req = self.ConstructPatchRequestFromArgs(alloydb_messages, cluster_ref,
args)
if not req.updateMask:
raise exceptions.MinimumArgumentException(
self.parameters, 'Please specify at least one property to update')
op = alloydb_client.projects_locations_clusters.Patch(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations')
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(op_ref, 'Updating cluster', self.ReleaseTrack(),
False)
return op
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
class UpdateBeta(Update):
"""Update an AlloyDB cluster within a given project and region."""
@classmethod
def Args(cls, parser):
super(UpdateBeta, UpdateBeta).Args(parser)
def ConstructPatchRequestFromArgs(self, alloydb_messages, cluster_ref, args):
return cluster_helper.ConstructPatchRequestFromArgsBeta(
alloydb_messages, cluster_ref, args
)

View File

@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*- #
# Copyright 2024 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.
"""Upgrades a AlloyDB cluster."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.alloydb import api_util
from googlecloudsdk.api_lib.alloydb import cluster_operations
from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions
from googlecloudsdk.command_lib.alloydb import cluster_helper
from googlecloudsdk.command_lib.alloydb import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
# TODO: b/312466999 - Change @base.DefaultUniverseOnly to
# @base.UniverseCompatible once b/312466999 is fixed.
# See go/gcloud-cli-running-tpc-tests.
@base.DefaultUniverseOnly
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Upgrade(base.SilentCommand):
"""Upgrade an AlloyDB cluster within a given project and region."""
detailed_help = {
'DESCRIPTION':
'{description}',
'EXAMPLES':
"""\
To upgrade a cluster, run:
$ {command} my-cluster --region=us-central1 --version=POSTGRES_15
""",
}
@classmethod
def Args(cls, parser):
"""Specifies additional command flags.
Args:
parser: argparse.Parser: Parser object for command line inputs.
"""
alloydb_messages = api_util.GetMessagesModule(cls.ReleaseTrack())
base.ASYNC_FLAG.AddToParser(parser)
flags.AddRegion(parser)
flags.AddCluster(parser)
flags.AddVersion(parser, alloydb_messages)
def ConstructUpgradeRequestFromArgs(
self, alloydb_messages, cluster_ref, args
):
return cluster_helper.ConstructUpgradeRequestFromArgs(
alloydb_messages, cluster_ref, args
)
def Run(self, args):
"""Constructs and sends request.
Args:
args: argparse.Namespace, An object that contains the values for the
arguments specified in the .Args() method.
Returns:
ProcessHttpResponse of the request made.
"""
client = api_util.AlloyDBClient(self.ReleaseTrack())
alloydb_client = client.alloydb_client
alloydb_messages = client.alloydb_messages
cluster_ref = client.resource_parser.Create(
'alloydb.projects.locations.clusters',
projectsId=properties.VALUES.core.project.GetOrFail,
locationsId=args.region,
clustersId=args.cluster,
)
req = self.ConstructUpgradeRequestFromArgs(
alloydb_messages, cluster_ref, args
)
if not req.upgradeClusterRequest.version:
raise exceptions.MinimumArgumentException(
self.parameters,
'Please specify target version for upgrade in --version flag',
)
op = alloydb_client.projects_locations_clusters.Upgrade(req)
op_ref = resources.REGISTRY.ParseRelativeName(
op.name, collection='alloydb.projects.locations.operations'
)
log.status.Print('Operation ID: {}'.format(op_ref.Name()))
if not args.async_:
cluster_operations.Await(
op_ref, 'Upgrading cluster', self.ReleaseTrack(), False
)
return op