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,131 @@
# -*- 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.
"""Common arguments for `gcloud source repos` commands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import arg_parsers
from googlecloudsdk.command_lib.source import resource_args
from googlecloudsdk.command_lib.util.concepts import concept_parsers
REPO_NAME_VALIDATOR = arg_parsers.RegexpValidator(
'[A-Za-z0-9_][-._A-Za-z0-9/]{0,127}',
'repostory name may contain between 1 and 128 (inclusive) letters, digits, '
'hyphens, periods, underscores and slashes.')
def AddPushblockFlags(group):
"""Add pushblock enabled/disabled flags to the given group."""
group.add_argument(
'--enable-pushblock',
action='store_true',
help="""\
Enable PushBlock for all repositories under current project.
PushBlock allows repository owners to block git push transactions containing
private key data.""")
group.add_argument(
'--disable-pushblock',
action='store_true',
help="""\
Disable PushBlock for all repositories under current project.
PushBlock allows repository owners to block git push transactions containing
private key data.""")
def AddOptionalTopicFlags(group, resource_name='repo'):
"""Add message_format and service_account flags to the topic arg group."""
group.add_argument(
'--message-format',
choices=['json', 'protobuf'],
help="""\
The format of the message to publish to the topic.""")
group.add_argument(
'--service-account',
help="""\
Email address of the service account used for publishing Cloud Pub/Sub messages.
This service account needs to be in the same project as the {}. When added, the
caller needs to have iam.serviceAccounts.actAs permission on this service
account. If unspecified, it defaults to the Compute Engine default service
account.""".format(resource_name))
group.add_argument(
'--topic-project',
help="""\
Cloud project for the topic. If not set, the currently set project will be
used.""")
def AddRepoUpdateArgs(parser, verb='to update'):
"""Add the arg groups for `source repos update` command."""
topic_group = parser.add_group(
'Manages Cloud Pub/Sub topics associated with the repository.',
required=True)
topic_resource_group = topic_group.add_argument_group(
mutex=True, required=True)
concept_parsers.ConceptParser(
[
resource_args.CreateTopicResourcePresentationSpec(
'add', 'The Cloud Pub/Sub topic to add to the repository.',
topic_resource_group),
resource_args.CreateTopicResourcePresentationSpec(
'remove',
'The Cloud Pub/Sub topic to remove from the repository.',
topic_resource_group),
resource_args.CreateTopicResourcePresentationSpec(
'update', 'The Cloud Pub/Sub topic to update in the project.',
topic_resource_group),
resource_args.CreateRepoResourcePresentationSpec(verb)
],
command_level_fallthroughs={
'--add-topic.project': ['--topic-project'],
'--remove-topic.project': ['--topic-project'],
'--update-topic.project': ['--topic-project'],
}).AddToParser(parser)
AddOptionalTopicFlags(topic_group)
def AddProjectConfigUpdateArgs(parser):
"""Add the arg groups for `source project-configs update` command."""
update_group = parser.add_group(required=True, mutex=True)
AddPushblockFlags(update_group)
topic_group = update_group.add_group(
'Manage Cloud Pub/Sub topics associated with the Cloud project.')
topic_resource_group = topic_group.add_argument_group(mutex=True)
concept_parsers.ConceptParser(
[
resource_args.CreateTopicResourcePresentationSpec(
'add', 'The Cloud Pub/Sub topic to add to the project.',
topic_resource_group),
resource_args.CreateTopicResourcePresentationSpec(
'remove', 'The Cloud Pub/Sub topic to remove from the project.',
topic_resource_group),
resource_args.CreateTopicResourcePresentationSpec(
'update', 'The Cloud Pub/Sub topic to update in the project.',
topic_resource_group),
],
command_level_fallthroughs={
'--add-topic.project': ['--topic-project'],
'--remove-topic.project': ['--topic-project'],
'--update-topic.project': ['--topic-project'],
}).AddToParser(parser)
AddOptionalTopicFlags(topic_group, 'project')

View File

@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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.
"""Shared resource flags for Cloud Source Repo."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope.concepts import concepts
from googlecloudsdk.calliope.concepts import deps
from googlecloudsdk.command_lib.util.concepts import presentation_specs
from googlecloudsdk.core import properties
def ProjectAttributeConfig():
"""Get project resource attribute with default value."""
return concepts.ResourceParameterAttributeConfig(
name='project',
help_text='Cloud Project for the {resource}.',
fallthroughs=[deps.PropertyFallthrough(properties.VALUES.core.project)])
def TopicAttributeConfig():
"""Get Pub/Sub topic resource attribute."""
return concepts.ResourceParameterAttributeConfig(
name='topic', help_text='Name of the topic.')
def RepoAttributeConfig():
"""Get Cloud Source Repo resource attribute."""
return concepts.ResourceParameterAttributeConfig(
name='repo', help_text='Name of the repository.')
def GetTopicResourceSpec():
return concepts.ResourceSpec(
'pubsub.projects.topics',
resource_name='topic',
topicsId=TopicAttributeConfig(),
projectsId=ProjectAttributeConfig())
def GetRepoResourceSpec():
return concepts.ResourceSpec(
'sourcerepo.projects.repos',
resource_name='repo',
reposId=RepoAttributeConfig(),
projectsId=ProjectAttributeConfig())
def CreateRepoResourcePresentationSpec(verb, positional=True):
name = 'repo' if positional else '--repo'
return presentation_specs.ResourcePresentationSpec(
name,
GetRepoResourceSpec(),
'Name of the Cloud Source repository {}.'.format(verb),
required=True,
)
def CreateTopicResourcePresentationSpec(verb, help_text, group):
"""Create add_topic, remove_topic or update_topic specs."""
name = '--' + verb + '-topic'
return presentation_specs.ResourcePresentationSpec(
name, GetTopicResourceSpec(), help_text, prefixes=True, group=group)

View File

@@ -0,0 +1,17 @@
project:
name: project
collection: sourcerepo.projects
attributes:
- &project
parameter_name: projectsId
attribute_name: project
help: The project name.
repo:
name: repo
collection: sourcerepo.projects.repos
attributes:
- *project
- parameter_name: reposId
attribute_name: repository_name
help: Name of the repository.

View File

@@ -0,0 +1,213 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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.
"""General utilties for Cloud Source commands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.util import apis
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
_API_NAME = 'sourcerepo'
_API_VERSION = 'v1'
_MESSAGES = apis.GetMessagesModule(_API_NAME, _API_VERSION)
class InvalidTopicError(exceptions.Error):
"""Raised when a topic cannot be found within the repo."""
def CreateProjectResource(args):
return resources.REGISTRY.Create(
'sourcerepo.projects',
projectsId=args.project or properties.VALUES.core.project.GetOrFail())
def CreateTopicResource(topic_name, topic_project):
return resources.REGISTRY.Create(
'pubsub.projects.topics',
projectsId=topic_project,
topicsId=topic_name,
)
def ParseProjectConfigWithPushblock(args):
project_ref = CreateProjectResource(args)
project_name = project_ref.RelativeName()
enable_pushblock = args.enable_pushblock
return _MESSAGES.ProjectConfig(
enablePrivateKeyCheck=enable_pushblock,
name=project_name,
pubsubConfigs=None # No need to specify when only update key check.
)
def ParseSourceRepoWithModifiedTopic(args, repo):
"""Parse and create a new Repo message with modified topic."""
topic_name = GetTopicName(args)
if args.add_topic:
new_config = _ParsePubsubConfig(topic_name, args.message_format,
args.service_account)
return _AddTopicToResource(
topic_name, new_config, repo, resource_name='repo')
elif args.remove_topic:
return _RemoveTopicFromResource(topic_name, repo, resource_name='repo')
elif args.update_topic:
return _UpdateTopicInResource(topic_name, args, repo, resource_name='repo')
return repo
def ParseProjectConfigWithModifiedTopic(args, project_config):
"""Parse and create a new ProjectConfig message with modified topic."""
topic_name = GetTopicName(args)
if args.add_topic:
new_config = _ParsePubsubConfig(topic_name, args.message_format,
args.service_account)
return _AddTopicToResource(
topic_name, new_config, project_config, resource_name='project')
elif args.remove_topic:
return _RemoveTopicFromResource(
topic_name, project_config, resource_name='project')
elif args.update_topic:
return _UpdateTopicInResource(
topic_name, args, project_config, resource_name='project')
return project_config
def GetTopicName(args):
"""Get the topic name based on project and topic_project flags."""
if args.add_topic:
topic_ref = args.CONCEPTS.add_topic.Parse()
elif args.remove_topic:
topic_ref = args.CONCEPTS.remove_topic.Parse()
else:
topic_ref = args.CONCEPTS.update_topic.Parse()
return topic_ref.RelativeName()
def _AddTopicToResource(topic_name, new_config, resource, resource_name):
"""Add the PubsubConfig message to Repo/ProjectConfig message."""
if resource.pubsubConfigs is None:
config_additional_properties = []
else:
config_additional_properties = resource.pubsubConfigs.additionalProperties
resource_msg_module = _MESSAGES.ProjectConfig
if resource_name == 'repo':
resource_msg_module = _MESSAGES.Repo
config_additional_properties.append(
resource_msg_module.PubsubConfigsValue.AdditionalProperty(
key=topic_name, value=new_config))
return resource_msg_module(
name=resource.name,
pubsubConfigs=resource_msg_module.PubsubConfigsValue(
additionalProperties=config_additional_properties))
def _RemoveTopicFromResource(topic_name, resource, resource_name):
"""Remove the topic from the Repo/ProjectConfig message."""
if resource.pubsubConfigs is None:
raise InvalidTopicError('Invalid topic [{0}]: No topics are configured '
'in the {1}.'.format(topic_name, resource_name))
config_additional_properties = resource.pubsubConfigs.additionalProperties
for i, config in enumerate(config_additional_properties):
if config.key == topic_name:
del config_additional_properties[i]
break
else:
raise InvalidTopicError('Invalid topic [{0}]: You must specify a '
'topic that is already configured in the {1}.'
.format(topic_name, resource_name))
resource_msg_module = _MESSAGES.ProjectConfig
if resource_name == 'repo':
resource_msg_module = _MESSAGES.Repo
return resource_msg_module(
name=resource.name,
pubsubConfigs=resource_msg_module.PubsubConfigsValue(
additionalProperties=config_additional_properties))
def _UpdateTopicInResource(topic_name, args, resource, resource_name):
"""Update the topic in the configuration and return a new repo message."""
if resource.pubsubConfigs is None:
raise InvalidTopicError('Invalid topic [{0}]: No topics are configured '
'in the {1}.'.format(topic_name, resource_name))
config_additional_properties = resource.pubsubConfigs.additionalProperties
for i, config in enumerate(config_additional_properties):
if config.key == topic_name:
config_additional_properties[i].value = _UpdateConfigWithArgs(
config.value, args)
break
else:
raise InvalidTopicError('Invalid topic [{0}]: You must specify a '
'topic that is already configured in the {1}.'
.format(topic_name, resource_name))
resource_msg_module = _MESSAGES.ProjectConfig
if resource_name == 'repo':
resource_msg_module = _MESSAGES.Repo
return resource_msg_module(
name=resource.name,
pubsubConfigs=resource_msg_module.PubsubConfigsValue(
additionalProperties=config_additional_properties))
def _ParsePubsubConfig(topic_name, message_format=None, service_account=None):
"""Parse and create PubsubConfig message."""
message_format_enums = _MESSAGES.PubsubConfig.MessageFormatValueValuesEnum
if message_format == 'protobuf':
parsed_message_format = message_format_enums.PROTOBUF
else:
# By default, the message format is 'json'.
parsed_message_format = message_format_enums.JSON
return _MESSAGES.PubsubConfig(
messageFormat=parsed_message_format,
serviceAccountEmail=service_account,
topic=topic_name)
def _GetMessageFormatString(pubsub_config):
message_format_type = getattr(pubsub_config, 'messageFormat')
message_format_enums = _MESSAGES.PubsubConfig.MessageFormatValueValuesEnum
if message_format_type == message_format_enums.PROTOBUF:
return 'protobuf'
return 'json'
def _UpdateConfigWithArgs(pubsub_config, args):
message_format = args.message_format or _GetMessageFormatString(pubsub_config)
service_account = args.service_account or getattr(pubsub_config,
'serviceAccountEmail')
topic_name = pubsub_config.topic
return _ParsePubsubConfig(topic_name, message_format, service_account)