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,29 @@
# -*- 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.
"""The command group for Cloud Build's PipelineRuns and TaskRuns."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
@base.UniverseCompatible
class Runs(base.Group):
"""Manage PipelineRuns and TaskRuns for Cloud Build."""
category = base.CI_CD_CATEGORY

View File

@@ -0,0 +1,119 @@
# -*- 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.
"""Create a PipelineRun/TaskRun."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.cloudbuild import cloudbuild_exceptions
from googlecloudsdk.api_lib.cloudbuild.v2 import client_util
from googlecloudsdk.api_lib.cloudbuild.v2 import input_util
from googlecloudsdk.api_lib.cloudbuild.v2 import pipeline_input_util
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.cloudbuild import run_flags
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
@base.UniverseCompatible
class Create(base.CreateCommand):
"""Create a PipelineRun/TaskRun."""
@staticmethod
def Args(parser):
"""Register flags for this command.
Args:
parser: An argparse.ArgumentParser-like object. It is mocked out in order
to capture some information, but behaves like an ArgumentParser.
"""
parser = run_flags.AddsCreateFlags(parser)
def Run(self, args):
"""This is what gets called when the user runs this command."""
client = client_util.GetClientInstance()
messages = client_util.GetMessagesModule()
yaml_data = input_util.LoadYamlFromPath(args.file)
run_type = yaml_data.get('kind', '')
if not run_type:
raise cloudbuild_exceptions.InvalidYamlError('kind is required')
run_id = yaml_data.get('metadata', {}).get('name', '')
if not run_id:
raise cloudbuild_exceptions.InvalidYamlError('metadata.name is required')
parent = args.CONCEPTS.region.Parse().RelativeName()
if run_type == 'PipelineRun':
pipeline_run = pipeline_input_util.TektonYamlDataToPipelineRun(
yaml_data)
operation = client.projects_locations_pipelineRuns.Create(
messages.CloudbuildProjectsLocationsPipelineRunsCreateRequest(
parent=parent,
pipelineRun=pipeline_run,
pipelineRunId=run_id,
))
operation_ref = resources.REGISTRY.ParseRelativeName(
operation.name, collection='cloudbuild.projects.locations.operations')
# Note that this operation only indicates that the run was created, not
# necessarily that it also completed.
created_pipeline_run = waiter.WaitFor(
waiter.CloudOperationPoller(client.projects_locations_pipelineRuns,
client.projects_locations_operations),
operation_ref, 'Creating PipelineRun')
pipeline_run_ref = resources.REGISTRY.Parse(
created_pipeline_run.name,
collection='cloudbuild.projects.locations.pipelineRuns',
api_version=client_util.RELEASE_TRACK_TO_API_VERSION[
self.ReleaseTrack()],
)
log.CreatedResource(pipeline_run_ref)
return created_pipeline_run
elif run_type == 'TaskRun':
task_run = pipeline_input_util.TektonYamlDataToTaskRun(
yaml_data)
operation = client.projects_locations_taskRuns.Create(
messages.CloudbuildProjectsLocationsTaskRunsCreateRequest(
parent=parent,
taskRun=task_run,
taskRunId=run_id,
))
operation_ref = resources.REGISTRY.ParseRelativeName(
operation.name, collection='cloudbuild.projects.locations.operations')
# Note that we'll only wait for the run to be created, not necessarily
# for it to be completed.
created_task_run = waiter.WaitFor(
waiter.CloudOperationPoller(client.projects_locations_taskRuns,
client.projects_locations_operations),
operation_ref, 'Creating TaskRun')
task_run_ref = resources.REGISTRY.Parse(
created_task_run.name,
collection='cloudbuild.projects.locations.taskRuns',
api_version=client_util.RELEASE_TRACK_TO_API_VERSION[
self.ReleaseTrack()],
)
log.CreatedResource(task_run_ref)
return created_task_run
else:
raise cloudbuild_exceptions.InvalidYamlError(
'Requested resource type {r} not supported'.format(r=run_type))

View File

@@ -0,0 +1,111 @@
# -*- 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.
"""Cancel a PipelineRun/TaskRun."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.cloudbuild.v2 import client_util as v2_client_util
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.cloudbuild import run_flags
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
@base.UniverseCompatible
class Cancel(base.Command):
"""Cancel a PipelineRun/TaskRun."""
@staticmethod
def Args(parser):
"""Register flags for this command.
Args:
parser: An argparse.ArgumentParser-like object. It is mocked out in order
to capture some information, but behaves like an ArgumentParser.
"""
parser = run_flags.AddsRunFlags(parser)
def Run(self, args):
"""This is what gets called when the user runs this command."""
region_ref = args.CONCEPTS.region.Parse()
region = region_ref.AsDict()['locationsId']
project = region_ref.AsDict()['projectsId']
run_id = args.RUN_ID
if args.type == 'pipelinerun':
client = v2_client_util.GetClientInstance()
messages = v2_client_util.GetMessagesModule()
pipeline_run_resource = resources.REGISTRY.Parse(
run_id,
collection='cloudbuild.projects.locations.pipelineRuns',
api_version='v2',
params={
'projectsId': project,
'locationsId': region,
'pipelineRunsId': run_id,
})
pipeline_run = messages.PipelineRun(
pipelineRunStatus=messages.PipelineRun
.PipelineRunStatusValueValuesEnum.PIPELINE_RUN_CANCELLED,)
update_mask = 'pipelineRunStatus'
operation = client.projects_locations_pipelineRuns.Patch(
messages.CloudbuildProjectsLocationsPipelineRunsPatchRequest(
name=pipeline_run_resource.RelativeName(),
pipelineRun=pipeline_run,
updateMask=update_mask,
))
operation_ref = resources.REGISTRY.ParseRelativeName(
operation.name, collection='cloudbuild.projects.locations.operations')
updated_pipeline_run = waiter.WaitFor(
waiter.CloudOperationPoller(client.projects_locations_pipelineRuns,
client.projects_locations_operations),
operation_ref, 'Cancelling PipelineRun')
log.status.Print('Cancelled PipelineRun {0}'.format(run_id))
return updated_pipeline_run
elif args.type == 'taskrun':
client = v2_client_util.GetClientInstance()
messages = v2_client_util.GetMessagesModule()
task_run_resource = resources.REGISTRY.Parse(
run_id,
collection='cloudbuild.projects.locations.taskRuns',
api_version='v2',
params={
'projectsId': project,
'locationsId': region,
'taskRunsId': run_id,
})
task_run = messages.TaskRun(
taskRunStatus=messages.TaskRun.TaskRunStatusValueValuesEnum
.TASK_RUN_CANCELLED,)
update_mask = 'taskRunStatus'
operation = client.projects_locations_taskRuns.Patch(
messages.CloudbuildProjectsLocationsTaskRunsPatchRequest(
name=task_run_resource.RelativeName(),
taskRun=task_run,
updateMask=update_mask,
))
operation_ref = resources.REGISTRY.ParseRelativeName(
operation.name, collection='cloudbuild.projects.locations.operations')
updated_task_run = waiter.WaitFor(
waiter.CloudOperationPoller(client.projects_locations_taskRuns,
client.projects_locations_operations),
operation_ref, 'Cancelling TaskRun')
log.status.Print('Cancelled TaskRun {0}'.format(run_id))
return updated_task_run

View File

@@ -0,0 +1,54 @@
# -*- 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.
"""Get a PipelineRun/TaskRun."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.cloudbuild.v2 import client_util as v2_client_util
from googlecloudsdk.api_lib.cloudbuild.v2 import pipeline_output_util
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.cloudbuild import run_flags
from googlecloudsdk.core.resource import resource_printer
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
@base.UniverseCompatible
class Describe(base.DescribeCommand):
"""Get a PipelineRun/TaskRun."""
@staticmethod
def Args(parser):
"""Register flags for this command.
Args:
parser: An argparse.ArgumentParser-like object. It is mocked out in order
to capture some information, but behaves like an ArgumentParser.
"""
parser = run_flags.AddsRunFlags(parser)
resource_printer.RegisterFormatter(
pipeline_output_util.PRINTER_FORMAT,
pipeline_output_util.TektonPrinter,
)
def Run(self, args):
"""This is what gets called when the user runs this command."""
region_ref = args.CONCEPTS.region.Parse()
region = region_ref.AsDict()['locationsId']
project = region_ref.AsDict()['projectsId']
run_id = args.RUN_ID
if args.format == 'tekton':
args.GetDisplayInfo().AddFormat(pipeline_output_util.PRINTER_FORMAT)
return v2_client_util.GetRun(project, region, run_id, args.type)

View File

@@ -0,0 +1,101 @@
# -*- 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.
"""List PipelineRuns and TaskRuns."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import exceptions
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.cloudbuild.v2 import client_util
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.cloudbuild import run_flags
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
def _GetResultURI(resource):
result = resources.REGISTRY.ParseRelativeName(
resource.name,
collection='cloudbuild.projects.locations.results',
api_version=client_util.GA_API_VERSION)
return result.SelfLink()
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
@base.UniverseCompatible
class List(base.ListCommand):
"""List all Cloud Build runs in a Google Cloud project."""
@staticmethod
def Args(parser):
parser.display_info.AddUriFunc(_GetResultURI)
run_flags.AddsRegionResourceArg(parser, False) # Not required.
base.LIMIT_FLAG.SetDefault(parser, 50)
parser.display_info.AddFormat(
"""
table(
recordSummaries[0].recordData.name.segment(5).yesno(no="-"):label=ID,
name.segment(3):label=REGION,
recordSummaries[0].createTime.date('%Y-%m-%dT%H:%M:%S%Oz', undefined='-'),
result_duration(undefined='-').slice(2:).join("").yesno(no="-"):label=DURATION,
recordSummaries[0].recordData.workflow.segment(5).yesno(no="-"):label=WORKFLOW,
result_status():label=STATUS
)
"""
)
def Run(self, args):
"""This is what gets called when the user runs this command."""
client = client_util.GetClientInstance()
messages = client_util.GetMessagesModule()
region_ref = args.CONCEPTS.region.Parse()
if region_ref:
parents = [region_ref.RelativeName()]
else:
# If no region is specified, list runs from all regions.
project = args.project or properties.VALUES.core.project.GetOrFail()
response = client_util.ListLocations(project)
parents = sorted([location.name for location in response.locations])
# Manually manage the limit since we'll be making repeated list requests.
total_limit = args.limit
parent_errors = []
# Note: if this serial approach is too slow, we could consider making
# requests in parallel (similar to http://shortn/_NWVYZQrCtp) although it
# will be more complicated and harder to do per-parent error reporting.
for p in parents:
try:
results = list_pager.YieldFromList(
client.projects_locations_results,
messages.CloudbuildProjectsLocationsResultsListRequest(
parent=p, filter=args.filter),
field='results',
batch_size=args.page_size,
batch_size_attribute='pageSize',
limit=total_limit)
for r in results:
yield r
if total_limit is not None:
total_limit -= 1
except exceptions.HttpError:
parent_errors.append(p)
if parent_errors:
raise exceptions.Error(
'Unable to fetch data from: {}'.format(parent_errors))

View File

@@ -0,0 +1,106 @@
# -*- 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.
"""Shows the logs for an in-progress or completed PipelineRun/TaskRun/Build."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.cloudbuild import cloudbuild_util as v1_client_util
from googlecloudsdk.api_lib.cloudbuild import logs as v1_logs
from googlecloudsdk.api_lib.cloudbuild.v2 import client_util as v2_client_util
from googlecloudsdk.api_lib.cloudbuild.v2 import logs as v2_logs
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.cloudbuild import run_flags
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
@base.Hidden
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
@base.UniverseCompatible
class Log(base.Command):
"""Show the logs for a PipelineRun/TaskRun/Build."""
@staticmethod
def Args(parser):
"""Register flags for this command.
Args:
parser: An argparse.ArgumentParser-like object. It is mocked out in order
to capture some information, but behaves like an ArgumentParser.
"""
parser.add_argument(
'--stream',
help=('If a run is ongoing, stream the logs to stdout until '
'the run completes.'),
default=False,
action='store_true')
parser = run_flags.AddsRunFlags(parser)
def Run(self, args):
"""This is what gets called when the user runs this command."""
region_ref = args.CONCEPTS.region.Parse()
region = region_ref.AsDict()['locationsId']
project = properties.VALUES.core.project.Get(required=True)
run_id = args.RUN_ID
if args.type == 'build':
client = v1_client_util.GetClientInstance()
messages = v1_client_util.GetMessagesModule()
build_ref = resources.REGISTRY.Parse(
run_id,
params={
'projectsId': project,
'locationsId': region,
'buildsId': run_id,
},
collection='cloudbuild.projects.locations.builds')
logger = v1_logs.CloudBuildClient(client, messages, True)
if args.stream:
logger.Stream(build_ref)
return
logger.PrintLog(build_ref)
return
else:
client = v2_client_util.GetClientInstance()
messages = v2_client_util.GetMessagesModule()
if args.type == 'pipelinerun':
pipeline_run_resource = resources.REGISTRY.Parse(
run_id,
collection='cloudbuild.projects.locations.pipelineRuns',
api_version='v2',
params={
'projectsId': project,
'locationsId': region,
'pipelineRunsId': run_id,
})
run_id = pipeline_run_resource.Name()
else:
task_run_resource = resources.REGISTRY.Parse(
run_id,
collection='cloudbuild.projects.locations.taskRuns',
api_version='v2',
params={
'projectsId': project,
'locationsId': region,
'taskRunsId': run_id,
})
run_id = task_run_resource.Name()
logger = v2_logs.CloudBuildLogClient()
if args.stream:
logger.Stream(project, region, run_id, args.type)
return
logger.PrintLog(project, region, run_id, args.type)
return