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,34 @@
# -*- coding: utf-8 -*- #
# Copyright 2017 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 databases of Cloud SQL instances."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
base.ReleaseTrack.ALPHA)
class Databases(base.Group):
"""Provide commands for managing databases of Cloud SQL instances.
Provide commands for managing databases of Cloud SQL instances,
including creating, deleting, listing, patching, updating, and getting
information about databases.
"""
category = base.DATABASES_CATEGORY

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 Sqladmin database 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 Sqladmin database configurations."""

View File

@@ -0,0 +1,38 @@
release_tracks: [ALPHA]
command_type: CONFIG_EXPORT
help_text:
brief: Export the configuration for a Sqladmin database.
description: |
*{command}* exports the configuration for a Sqladmin database.
Database 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
databases within the project.
Specifying `--path` allows you to export the configuration(s) to
a local directory.
examples: |
To export the configuration for a database, run:
$ {command} my-database
To export the configuration for a database to a file, run:
$ {command} my-database --path=/path/to/dir/
To export the configuration for a database in Terraform
HCL format, run:
$ {command} my-database --resource-format=terraform
To export the configurations for all databases within a
project, run:
$ {command} --all
arguments:
resource:
help_text: Database to export the configuration for.
spec: !REF googlecloudsdk.command_lib.sql.resources:database

View File

@@ -0,0 +1,108 @@
# -*- coding: utf-8 -*- #
# Copyright 2017 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 database for a Cloud SQL instance."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.sql import api_util
from googlecloudsdk.api_lib.sql import exceptions
from googlecloudsdk.api_lib.sql import operations
from googlecloudsdk.api_lib.sql import validate
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.sql import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
base.ReleaseTrack.ALPHA)
class AddDatabase(base.Command):
"""Creates a database for a Cloud SQL instance."""
@staticmethod
def Args(parser):
"""Args is called by calliope to gather arguments for this command.
Args:
parser: An argparse parser that you can use to add arguments that go
on the command line after this command. Positional arguments are
allowed.
"""
flags.AddDatabaseName(parser)
flags.AddCharset(parser)
flags.AddCollation(parser)
flags.AddInstance(parser)
base.ASYNC_FLAG.AddToParser(parser)
def Run(self, args):
"""Creates a database for a Cloud SQL instance.
Args:
args: argparse.Namespace, The arguments that this command was invoked
with.
Returns:
A dict object representing the operations resource describing the create
operation if the create was successful.
"""
client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
sql_client = client.sql_client
sql_messages = client.sql_messages
validate.ValidateInstanceName(args.instance)
instance_ref = client.resource_parser.Parse(
args.instance,
params={'project': properties.VALUES.core.project.GetOrFail},
collection='sql.instances')
new_database = sql_messages.Database(
kind='sql#database',
project=instance_ref.project,
instance=instance_ref.instance,
name=args.database,
charset=args.charset,
collation=args.collation)
# TODO(b/35386183): Move this API call logic.
result_operation = sql_client.databases.Insert(new_database)
operation_ref = client.resource_parser.Create(
'sql.operations',
operation=result_operation.name,
project=instance_ref.project)
if args.async_:
result = sql_client.operations.Get(
sql_messages.SqlOperationsGetRequest(
project=operation_ref.project, operation=operation_ref.operation))
else:
try:
operations.OperationsV1Beta4.WaitForOperation(
sql_client, operation_ref, 'Creating Cloud SQL database')
except exceptions.OperationError:
log.Print('Database creation failed. Check if a database named {0} '
'already exists.'.format(args.database))
# Must fail with non-zero exit code on API request failure.
# TODO(b/35156765): Refactor.
raise
result = new_database
result.kind = None
log.CreatedResource(args.database, kind='database', is_async=args.async_)
return result

View File

@@ -0,0 +1,91 @@
# -*- 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.
"""Deletes a database in a given instance."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.sql import api_util
from googlecloudsdk.api_lib.sql import operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.sql import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core.console import console_io
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
base.ReleaseTrack.ALPHA)
class Delete(base.DeleteCommand):
"""Deletes a Cloud SQL database.
For MySQL, also deletes all files in the database directory.
"""
@staticmethod
def Args(parser):
"""Args is called by calliope to gather arguments for this command.
Args:
parser: An argparse parser that you can use it to add arguments that go
on the command line after this command. Positional arguments are
allowed.
"""
flags.AddInstance(parser)
flags.AddDatabaseName(parser)
parser.display_info.AddCacheUpdater(flags.DatabaseCompleter)
def Run(self, args):
"""Deletes a Cloud SQL database.
For MySQL, also deletes all files in the database directory.
Args:
args: argparse.Namespace, The arguments that this command was invoked
with.
Returns:
SQL database resource iterator.
"""
client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
sql_client = client.sql_client
sql_messages = client.sql_messages
project_id = properties.VALUES.core.project.Get(required=True)
instance_ref = client.resource_parser.Parse(
args.instance,
params={'project': properties.VALUES.core.project.GetOrFail},
collection='sql.instances')
console_io.PromptContinue(
message='The database will be deleted. Any data stored in the database '
'will be destroyed. You cannot undo this action.',
default=True,
cancel_on_no=True)
result_operation = sql_client.databases.Delete(
sql_messages.SqlDatabasesDeleteRequest(
project=project_id, instance=args.instance, database=args.database))
operation_ref = client.resource_parser.Create(
'sql.operations',
operation=result_operation.name,
project=instance_ref.project)
operations.OperationsV1Beta4.WaitForOperation(sql_client, operation_ref,
'Deleting Cloud SQL database')
log.DeletedResource(args.database, 'database')

View File

@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*- #
# Copyright 2017 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.
"""Retrieves information about a Cloud SQL database."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.sql import api_util
from googlecloudsdk.api_lib.sql import validate
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.sql import flags
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
base.ReleaseTrack.ALPHA)
class Get(base.DescribeCommand):
"""Displays configuration and metadata about a Cloud SQL database.
Information such as database name, charset, and collation will be displayed.
"""
@staticmethod
def Args(parser):
"""Args is called by calliope to gather arguments for this command.
Args:
parser: An argparse parser that you can use it to add arguments that go
on the command line after this command. Positional arguments are
allowed.
"""
flags.AddDatabaseName(parser)
flags.AddInstance(parser)
def Run(self, args):
"""Displays configuration and metadata about a Cloud SQL database.
Information such as database name, charset, and collation will be displayed.
Args:
args: argparse.Namespace, The arguments that this command was invoked
with.
Returns:
A dict object representing the database resource if fetching the database
was successful.
"""
client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
sql_client = client.sql_client
sql_messages = client.sql_messages
validate.ValidateInstanceName(args.instance)
instance_ref = client.resource_parser.Parse(
args.instance,
params={'project': properties.VALUES.core.project.GetOrFail},
collection='sql.instances')
database_request = sql_messages.SqlDatabasesGetRequest(
project=instance_ref.project,
instance=instance_ref.instance,
database=args.database)
return sql_client.databases.Get(database_request)

View File

@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*- #
# Copyright 2013 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 databases for a Cloud SQL instance."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.sql import api_util
from googlecloudsdk.api_lib.sql import validate
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.sql import flags
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
base.ReleaseTrack.ALPHA)
class List(base.ListCommand):
"""Lists databases for a Cloud SQL instance."""
@staticmethod
def Args(parser):
"""Args is called by calliope to gather arguments for this command.
Args:
parser: An argparse parser that you can use to add arguments that go
on the command line after this command. Positional arguments are
allowed.
"""
flags.AddInstance(parser)
parser.display_info.AddFormat("""
table(
name,
charset,
collation
)
""")
parser.display_info.AddCacheUpdater(flags.DatabaseCompleter)
def Run(self, args):
"""Lists databases for a Cloud SQL instance.
Args:
args: argparse.Namespace, The arguments that this command was invoked
with.
Returns:
A dict object that has the list of database resources if the api request
was successful.
"""
client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
sql_client = client.sql_client
sql_messages = client.sql_messages
validate.ValidateInstanceName(args.instance)
instance_ref = client.resource_parser.Parse(
args.instance,
params={'project': properties.VALUES.core.project.GetOrFail},
collection='sql.instances')
result = sql_client.databases.List(
sql_messages.SqlDatabasesListRequest(
project=instance_ref.project, instance=instance_ref.instance))
return iter(result.items)

View File

@@ -0,0 +1,133 @@
# -*- coding: utf-8 -*- #
# Copyright 2017 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.
"""Patches the settings of a Cloud SQL database."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.sql import api_util
from googlecloudsdk.api_lib.sql import operations
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.sql import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
class _Result(object):
"""Run() method result object."""
def __init__(self, new, old):
self.new = new
self.old = old
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
base.ReleaseTrack.ALPHA)
class Patch(base.Command):
"""Patches the settings of a Cloud SQL database."""
@staticmethod
def Args(parser):
"""Args is called by calliope to gather arguments for this command.
Please add arguments in alphabetical order except for no- or a clear-
pair for that argument which can follow the argument itself.
Args:
parser: An argparse parser that you can use to add arguments that go
on the command line after this command. Positional arguments are
allowed.
"""
flags.AddCharset(parser)
custom_help = (
'Cloud SQL database collation setting, which specifies '
'the set of rules for comparing characters in a character set. Each'
' database version may support a different set of collations. This flag'
' can\'t be used with PostgreSQL instances.')
flags.AddCollation(parser, custom_help)
flags.AddDatabaseName(parser)
flags.AddInstance(parser)
parser.add_argument(
'--diff',
action='store_true',
help='Show what changed as a result of the patch.')
parser.display_info.AddFormat('table(new:format="default")')
def Run(self, args):
"""Patches settings of a Cloud SQL database using the patch api method.
Args:
args: argparse.Namespace, The arguments that this command was invoked
with.
Returns:
A dict object representing the operations resource describing the patch
operation if the patch was successful.
"""
if args.diff:
args.GetDisplayInfo().AddFormat('diff(old, new)')
client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
sql_client = client.sql_client
sql_messages = client.sql_messages
instance_ref = client.resource_parser.Parse(
args.instance,
params={'project': properties.VALUES.core.project.GetOrFail},
collection='sql.instances')
original_database_resource = sql_client.databases.Get(
sql_messages.SqlDatabasesGetRequest(
database=args.database,
project=instance_ref.project,
instance=instance_ref.instance))
patch_database = sql_messages.Database(
kind='sql#database',
project=instance_ref.project,
instance=instance_ref.instance,
name=args.database)
if hasattr(args, 'collation'):
patch_database.collation = args.collation
if hasattr(args, 'charset'):
patch_database.charset = args.charset
operation_ref = None
result_operation = sql_client.databases.Patch(
sql_messages.SqlDatabasesPatchRequest(
database=args.database,
databaseResource=patch_database,
project=instance_ref.project,
instance=instance_ref.instance))
operation_ref = client.resource_parser.Create(
'sql.operations',
operation=result_operation.name,
project=instance_ref.project)
operations.OperationsV1Beta4.WaitForOperation(sql_client, operation_ref,
'Patching Cloud SQL database')
log.UpdatedResource(args.database, 'database')
changed_database_resource = sql_client.databases.Get(
sql_messages.SqlDatabasesGetRequest(
database=args.database,
project=instance_ref.project,
instance=instance_ref.instance))
return _Result(changed_database_resource, original_database_resource)