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 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.
"""The main command group for Declarative Resource Configuration Management."""
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)
class ResourceConfig(base.Group):
"""Commands for declarative management of Google Cloud Platform resources."""
category = base.DECLARATIVE_CONFIGURATION_CATEGORY
def Filter(self, context, args):
# TODO(b/190542262): Determine if command group works with project number
base.RequireProjectID(args)
del context, args

View File

@@ -0,0 +1,55 @@
# -*- 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.
"""Apply a KRM configuration to a Google Cloud Platform resource filename or stdin."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.util.declarative import flags as declarative_flags
from googlecloudsdk.command_lib.util.declarative.clients import kcc_client
from googlecloudsdk.core.console import progress_tracker
_DETAILED_HELP = {
'EXAMPLES':
"""
To create or update the resource in file 'my-resource.yaml' in the current project run:
$ {command} my-resource.yaml
"""
}
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class Apply(base.DeclarativeCommand):
"""Apply a KRM configuration to a Google Cloud Platform resource filename or stdin."""
detailed_help = _DETAILED_HELP
@classmethod
def Args(cls, parser):
declarative_flags.AddResolveResourcesArg(parser)
declarative_flags.AddApplyPathArg(parser)
parser.display_info.AddFormat('yaml')
def Run(self, args):
resource_path = args.PATH
resolve_refs = args.resolve_references
client = kcc_client.KccClient()
output = None
with progress_tracker.ProgressTracker(
message='Applying {}'.format(resource_path),
aborted_message='Apply Cancelled'):
output = client.ApplyConfig(resource_path, resolve_refs)
return output

View File

@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for retrieving declarative configurations for Google Cloud Platform resources."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.util.declarative import flags as declarative_flags
from googlecloudsdk.command_lib.util.declarative.clients import kcc_client
from googlecloudsdk.core import log
_DETAILED_HELP = {
'EXAMPLES':
"""
To export all resources in a project to a local directory, run:
$ {command} --path=/path/to/dir/
To export all resources in a organization to stdout, run:
$ {command} --organization=12345 --path=-
To export all resources in a folder to stdout in Terraform format,
run:
$ {command} --folder=12345 --resource-format=terraform
To export all resources in a project to stdout, using a custom Google Storage bucket for interim results,
run:
$ {command} --project=my-project --storage-path='gs://your-bucket-name/your/prefix/path'
To export all Storage Bucket and Compute Instances resources in project my-project to stdout,
run:
$ {command} --project=my-project --resource-types=storage.cnrm.cloud.google.com/StorageBucket,ComputeInstance
To export all resource types in file 'types-file.txt' in project my-project to stdout,
run:
$ {command} --project=my-project --resource-types-file=types-file.txt
"""
}
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
class Export(base.DeclarativeCommand):
"""Export configurations for all assets within the specified project, organization, or folder."""
detailed_help = _DETAILED_HELP
@classmethod
def Args(cls, parser):
declarative_flags.AddBulkExportArgs(parser)
def Run(self, args):
client = kcc_client.KccClient()
if args.IsSpecified('format'):
log.warning('`--format` flag not supported for bulk-export. '
'To change the format of exported resources use the '
'`--resource-format` flag.')
args.format = None
else:
client.BulkExport(args)
return

View File

@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command for listing all resources supported by bulk-export."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import collections
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.util.declarative import flags as declarative_flags
from googlecloudsdk.command_lib.util.declarative.clients import declarative_client_base
from googlecloudsdk.command_lib.util.declarative.clients import kcc_client
try:
# Python 3.3 and above.
collections_abc = collections.abc
except AttributeError:
collections_abc = collections
_DETAILED_HELP = {
'EXAMPLES':
"""
To list all exportable resource types, run:
$ {command}
To list all exportable resource types in yaml format, run:
$ {command} --format=yaml
To list all exportable resource types in project `my-project` in json format, run:
$ {command} --format=json --project=my-project
"""
}
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
class ListResources(base.DeclarativeCommand):
"""List all resources supported by bulk-export."""
detailed_help = _DETAILED_HELP
@classmethod
def Args(cls, parser):
declarative_flags.AddListResourcesFlags(parser)
parser.display_info.AddFormat(declarative_client_base.RESOURCE_LIST_FORMAT)
def Run(self, args):
client = kcc_client.KccClient()
output = client.ListResources(project=args.project,
organization=args.organization,
folder=args.folder)
return output

View File

@@ -0,0 +1,33 @@
# -*- 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.
"""The command group for Terraform provider configuration."""
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)
class Terraform(base.Group):
"""The command group for Terraform provider configuration."""
category = base.DECLARATIVE_CONFIGURATION_CATEGORY
def Filter(self, context, args):
base.RequireProjectID(args)
del context, args

View File

@@ -0,0 +1,88 @@
# -*- 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 for generating Terraform Import script for exported resources."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.util.declarative import flags
from googlecloudsdk.command_lib.util.declarative import terraform_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core.console import progress_tracker
from googlecloudsdk.core.util import files
_DETAILED_HELP = {
'EXAMPLES':
"""
To generate an import script named `import.sh` and a module file named `modules.tf` based on exported files in `my-dir/`, run:
$ {command} my-dir/ --output-script-file=import.sh --output-module-file=modules.tf
To generate an import script with the default `terraform_import_YYYYMMDD-HH-MM-SS.cmd`
and `gcloud-export-modules.tf` names on Windows, based on exported files in `my-dir/`, run:
$ {command} my-dir
"""
}
class GenerateImport(base.DeclarativeCommand):
"""Generate Terraform import script for exported resources."""
detailed_help = _DETAILED_HELP
@classmethod
def Args(cls, parser):
flags.AddTerraformGenerateImportArgs(parser)
def Run(self, args):
input_path = args.INPUT_PATH
import_data = terraform_utils.ParseExportFiles(input_path)
# Generate script file.
dest_script_file, dest_script_dir = terraform_utils.ProcessOutputParameters(
args.output_script_file, args.output_dir)
dest_script_file = dest_script_file or terraform_utils.GenerateDefaultScriptFileName(
)
dest_script_dir = dest_script_dir or files.GetCWD()
with progress_tracker.ProgressTracker(
message='Generating import script.',
aborted_message='Aborted script generation.'):
output_script_filename, script_successes = terraform_utils.GenerateImportScript(
import_data, dest_script_file, dest_script_dir)
log.status.Print(
'Successfully generated {} with imports for {} resources.'.format(
output_script_filename, script_successes))
# Generate module file..
dest_module_file, dest_module_dir = terraform_utils.ProcessOutputParameters(
args.output_module_file, args.output_dir)
dest_module_file = dest_module_file or terraform_utils.TF_MODULES_FILENAME
dest_module_dir = dest_module_dir or files.GetCWD()
with progress_tracker.ProgressTracker(
message='Generating terraform modules.',
aborted_message='Aborted module generation.'):
output_module_filename, module_successes = terraform_utils.GenerateModuleFile(
import_data, properties.VALUES.core.project.Get(required=True),
dest_module_file, dest_module_dir)
log.status.Print('Successfully generated {} with {} modules.'.format(
output_module_filename, module_successes))
return None

View File

@@ -0,0 +1,116 @@
# -*- 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 for generating main.tf file to configure Terraform Provider."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import os
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.util.declarative import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
from googlecloudsdk.core.console import console_io
from googlecloudsdk.core.console import progress_tracker
from googlecloudsdk.core.util import files
from mako import runtime
from mako import template
_SCHEMA_VERSION = '0.2'
_MIN_PROVIDER_VERSION = 'v3.90.0'
_SUPPORTED_MSG = ('This command supports Google Terraform Provider version'
' {}+ and Terraform Provider Schema {}'.format(
_MIN_PROVIDER_VERSION, _SCHEMA_VERSION))
_DETAILED_HELP = {
'DESCRIPTION':
"""{description}
"""+ _SUPPORTED_MSG,
'EXAMPLES':
"""
To generate a `main.tf` file in the current directory using the gcloud default values for `zone`, `region` and `project` run:
$ {command}
To generate a `main.tf` file in the current directory using the user suppplied values for `zone`, `region` and `project` run:
$ {command} --project="my-project-id" --region="us-central1" --zone="us-central1-c
To generate a `main.tf` file in the current directory using the gcloud default `billing_project` run:
$ {command} --use-gcloud-billing-project
To generate a `main.tf` file in the current directory using user specified `billing_project` value run:
$ {command} --tf-user-project-override --tf-billing-project="my-other-project-id"
"""
}
_INIT_TEMPLATE_NAME = os.path.join(
os.path.dirname(__file__), 'templates', 'main_tf.tpl')
INIT_FILE_TEMPLATE = template.Template(filename=_INIT_TEMPLATE_NAME)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class InitProvider(base.DeclarativeCommand):
"""Generate main.tf file to configure Google Cloud Terraform Provider."""
detailed_help = _DETAILED_HELP
def _GetBillingParams(self, args_namspace):
"""Process billing project flags in args and return Terraform settings."""
use_gcloud_billing = args_namspace.use_gcloud_billing_project
user_project_override = billing_project = None
if use_gcloud_billing:
billing_project = properties.VALUES.billing.quota_project.Get()
user_project_override = 'true'
elif args_namspace.tf_user_project_override:
billing_project = args_namspace.tf_billing_project
user_project_override = 'true'
return user_project_override, billing_project
@classmethod
def Args(cls, parser):
flags.AddInitProviderArgs(parser)
def Run(self, args):
do_override, billing_project = self._GetBillingParams(args)
project = args.project or properties.VALUES.core.project.Get()
region = args.region or properties.VALUES.compute.region.Get()
zone = args.zone or properties.VALUES.compute.zone.Get()
template_context = {
'project': project,
'region': region,
'zone': zone,
'user_override': do_override,
'billing_project': billing_project
}
path = os.path.join(files.GetCWD(), 'main.tf')
if os.path.isfile(path):
console_io.PromptContinue('{} Exists.'.format(path),
prompt_string='Overwrite?',
cancel_on_no=True,
cancel_string='Init Provider cancelled.')
with progress_tracker.ProgressTracker('Creating Terraform init module'):
with files.FileWriter(path, create_path=True) as f:
ctx = runtime.Context(f, **template_context)
INIT_FILE_TEMPLATE.render_context(ctx)
log.status.Print('Created Terraform module file {path}.'.format(path=path))

View File

@@ -0,0 +1,11 @@
provider "google" {
project = "${project}"
region = "${region}"
zone = "${zone}"
% if user_override:
user_project_override = "${user_override}"
% endif
% if billing_project:
billing_project = "${billing_project}"
% endif
}