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,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
}