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,85 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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.
"""Support library to handle the automation subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
AUTOMATION_UPDATE_MASK = '*,labels'
class AutomationsClient(object):
"""Client for automation service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize an automation.AutomationsClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations_deliveryPipelines_automations
def Patch(self, obj):
"""Patches a target resource.
Args:
obj: apitools.base.protorpclite.messages.Message, automation message.
Returns:
The operation message.
"""
return self._service.Patch(
self.messages.ClouddeployProjectsLocationsDeliveryPipelinesAutomationsPatchRequest(
automation=obj,
allowMissing=True,
name=obj.name,
updateMask=AUTOMATION_UPDATE_MASK,
)
)
def Get(self, name):
"""Gets the automation object by calling the automation get API.
Args:
name: automation name.
Returns:
an automation object.
"""
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesAutomationsGetRequest(
name=name
)
return self._service.Get(request)
def Delete(self, name):
"""Deletes an automation resource.
Args:
name: str, automation name.
Returns:
The operation message. It could be none if the resource doesn't exist.
"""
return self._service.Delete(
self.messages.ClouddeployProjectsLocationsDeliveryPipelinesAutomationsDeleteRequest(
allowMissing=True, name=name
)
)

View File

@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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.
"""Support library to handle the automation run subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
class AutomationRunsClient(object):
"""Client for automation run service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize an automation_run.AutomationRunsClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = (
self.client.projects_locations_deliveryPipelines_automationRuns
)
def Cancel(self, name):
"""Cancels an automation run.
Args:
name: Name of the AutomationRun. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/automationRuns/{automationRun}.
Returns:
CancelAutomationRunResponse message.
"""
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesAutomationRunsCancelRequest(
name=name
)
return self._service.Cancel(request)

View File

@@ -0,0 +1,149 @@
# -*- 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.
"""Utilities for the clouddeploy API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.util import apis
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
_API_NAME = 'clouddeploy'
_API_VERSION = 'v1'
def GetMessagesModule(client=None):
"""Returns the messages module for Cloud Deploy.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
Returns:
Module containing the definitions of messages for Cloud Deploy.
"""
client = client or GetClientInstance()
return client.MESSAGES_MODULE
def GetClientInstance(use_http=True):
"""Returns an instance of the Cloud Deploy client.
Args:
use_http: bool, True to create an http object for this client.
Returns:
base_api.BaseApiClient, An instance of the Cloud Deploy client.
"""
return apis.GetClientInstance(_API_NAME, _API_VERSION, no_http=(not use_http))
class DeployOperationPoller(waiter.CloudOperationPoller):
"""Poller for Cloud Deploy operations API.
This is necessary because the core operations library doesn't directly support
simple_uri.
"""
def __init__(self, client):
"""Initiates a DeployOperationPoller.
Args:
client: base_api.BaseApiClient, An instance of the Cloud Deploy client.
"""
self.client = client
super(DeployOperationPoller,
self).__init__(self.client.client.projects_locations_operations,
self.client.client.projects_locations_operations)
def Poll(self, operation_ref):
return self.client.Get(operation_ref)
def GetResult(self, operation):
return operation
class OperationsClient(object):
"""High-level client for the cloud deploy operations surface."""
def __init__(self, client=None, messages=None):
"""Initiates an OperationsClient.
Args:
client: base_api.BaseApiClient, An instance of the Cloud Deploy client.
messages: messages module for Cloud Deploy.
"""
self.client = client or GetClientInstance()
self.messages = messages or self.client.MESSAGES_MODULE
def Get(self, operation_ref):
return self.client.projects_locations_operations.Get(
self.messages.ClouddeployProjectsLocationsOperationsGetRequest(
name=operation_ref.RelativeName()))
def WaitForOperation(self, operation, operation_ref, message=None):
"""Wait until the operation is complete or times out.
Args:
operation: The operation resource to wait on
operation_ref: The operation reference to the operation resource. It's the
result by calling resources.REGISTRY.Parse
message: str, the message to print while waiting.
Returns:
The operation resource when it has completed
Raises:
OperationTimeoutError: when the operation polling times out
OperationError: when the operation completed with an error
"""
poller = DeployOperationPoller(self)
if poller.IsDone(operation):
return operation
if message is None:
message = 'Waiting for operation [{}]'.format(operation_ref.Name())
return waiter.WaitFor(poller, operation_ref, message)
def CheckOperationStatus(self, operation_dict, msg_template):
"""Checks operations status.
Only logs the errors instead of re-throwing them.
Args:
operation_dict: dict[str, oOptional[clouddeploy_messages.Operation],
dictionary of resource name and clouddeploy_messages.Operation. The
operation can be None if the operation isn't executed.
msg_template: output string template.
"""
for resource_name, operation in operation_dict.items():
if not operation or not operation.name:
continue
try:
operation_ref = resources.REGISTRY.ParseRelativeName(
operation.name,
collection='clouddeploy.projects.locations.operations')
_ = self.WaitForOperation(
operation, operation_ref,
'Waiting for the operation on resource {}'.format(
resource_name)).response
log.status.Print(msg_template.format(resource_name))
except core_exceptions.Error as e:
log.status.Print('Operation failed: {}'.format(e))

View File

@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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.
"""Support library to handle the requests for config."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
class ConfigClient(object):
"""Client for config service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a config.ConfigClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations
def GetConfig(self, project_id, location_id):
"""Gets a config resource.
Args:
project_id: project id.
location_id: region id.
Returns:
Config message.
"""
return self._service.GetConfig(
self.messages.ClouddeployProjectsLocationsGetConfigRequest(
name='projects/{project}/locations/{location}/config'.format(
project=project_id,
location=location_id)
)
)

View File

@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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.
"""Support library to handle the custom-target-type subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
CUSTOM_TARGET_TYPE_UPDATE_MASK = '*,labels'
class CustomTargetTypesClient(object):
"""Client for custom target type service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a custom_target_type.CustomTargetTypesClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations_customTargetTypes
def Get(self, name):
"""Gets the custom target type object.
Args:
name: custom target type name.
Returns:
a custom target type object.
"""
request = (
self.messages.ClouddeployProjectsLocationsCustomTargetTypesGetRequest(
name=name
)
)
return self._service.Get(request)
def Patch(self, obj):
"""Patches a custom target type resource.
Args:
obj: apitools.base.protorpclite.messages.Message, custom target type
message.
Returns:
The operation message.
"""
return self._service.Patch(
self.messages.ClouddeployProjectsLocationsCustomTargetTypesPatchRequest(
customTargetType=obj,
allowMissing=True,
name=obj.name,
updateMask=CUSTOM_TARGET_TYPE_UPDATE_MASK,
)
)
def Delete(self, name):
"""Deletes a custom target type resource.
Args:
name: str, custom target type name.
Returns:
The operation message.
"""
return self._service.Delete(
self.messages.ClouddeployProjectsLocationsCustomTargetTypesDeleteRequest(
name=name, allowMissing=True))

View File

@@ -0,0 +1,102 @@
# -*- 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.
"""Support library to handle the delivery-pipeline subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.clouddeploy import client_util
class DeliveryPipelinesClient(object):
"""Client for delivery pipeline service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a delivery_pipeline.DeliveryPipelinesClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations_deliveryPipelines
def Get(self, name):
"""Gets the delivery pipeline object by calling the delivery pipeline get API.
Args:
name: delivery pipeline name.
Returns:
a delivery pipeline object.
"""
request = (
self.messages.ClouddeployProjectsLocationsDeliveryPipelinesGetRequest(
name=name
)
)
return self._service.Get(request)
def List(
self, location, filter_str=None, order_by=None, page_size=0, limit=None
):
"""Lists Delivery Pipeline resources that belong to a location.
Args:
location: str, the full name of the location which owns the Delivery
Pipelines.
filter_str: optional[str], list filter.
order_by: optional[str], field to sort by.
page_size: optional[int], the maximum number of `DeliveryPipeline` objects
to return.
limit: int, The maximum number of records to yield. None if all available
records should be yielded.
Returns:
Delivery Pipeline list response.
"""
list_req = (
self.messages.ClouddeployProjectsLocationsDeliveryPipelinesListRequest(
parent=location, filter=filter_str, orderBy=order_by
)
)
return list_pager.YieldFromList(
self._service,
list_req,
field='deliveryPipelines',
batch_size=page_size,
limit=limit,
batch_size_attribute='pageSize',
)
def RollbackTarget(self, name, request):
"""Creates a rollback for a given target.
Args:
name: pipeline name
request: RollbackTargetRequest
Returns:
RollbackTargetResponse
"""
msg = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesRollbackTargetRequest(
name=name,
rollbackTargetRequest=request,
)
return self._service.RollbackTarget(msg)

View File

@@ -0,0 +1,185 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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.
"""Support library to handle the deploy subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
from googlecloudsdk.command_lib.deploy import automation_util
from googlecloudsdk.command_lib.deploy import custom_target_type_util
from googlecloudsdk.command_lib.deploy import deploy_policy_util
from googlecloudsdk.command_lib.deploy import manifest_util
from googlecloudsdk.command_lib.deploy import target_util
from googlecloudsdk.core import log
class DeployClient(object):
"""Client for managing Cloud Deploy delivery pipeline and target resources."""
def __init__(self, client=None, messages=None):
"""Initialize a deploy.DeployClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.operation_client = client_util.OperationsClient()
self.messages = messages or client_util.GetMessagesModule(client)
self._pipeline_service = self.client.projects_locations_deliveryPipelines
def CreateResources(self, manifests, region):
"""Creates Cloud Deploy resources.
Asynchronously calls the API then iterate the operations
to check the status.
Args:
manifests: the list of parsed resource yaml definitions.
region: location ID.
"""
resource_dict = manifest_util.ParseDeployConfig(
self.messages, manifests, region
)
msg_template = 'Created Cloud Deploy resource: {}.'
operation_dict = {
resource.name: self.CreateDeliveryPipeline(resource)
for resource in resource_dict[
manifest_util.ResourceKind.DELIVERY_PIPELINE
]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
operation_dict = {
resource.name: target_util.PatchTarget(resource)
for resource in resource_dict[manifest_util.ResourceKind.TARGET]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
# Create automation resource.
operation_dict = {
resource.name: automation_util.PatchAutomation(resource)
for resource in resource_dict[manifest_util.ResourceKind.AUTOMATION]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
# Create custom target type resource.
operation_dict = {
resource.name: custom_target_type_util.PatchCustomTargetType(resource)
for resource in resource_dict[
manifest_util.ResourceKind.CUSTOM_TARGET_TYPE
]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
# Create deploy policy resource.
operation_dict = {
resource.name: deploy_policy_util.PatchDeployPolicy(resource)
for resource in resource_dict[manifest_util.ResourceKind.DEPLOY_POLICY]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
def DeleteResources(self, manifests, region, force):
"""Delete Cloud Deploy resources.
Asynchronously calls the API then iterate the operations
to check the status.
Args:
manifests: [str], the list of parsed resource yaml definitions.
region: str, location ID.
force: bool, if true, the delivery pipeline with sub-resources will be
deleted and its sub-resources will also be deleted.
"""
resource_dict = manifest_util.ParseDeployConfig(
self.messages, manifests, region
)
msg_template = 'Deleted Cloud Deploy resource: {}.'
operation_dict = {
resource.name: target_util.DeleteTarget(resource.name)
for resource in resource_dict[manifest_util.ResourceKind.TARGET]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
operation_dict = {
resource.name: custom_target_type_util.DeleteCustomTargetType(
resource.name
)
for resource in resource_dict[
manifest_util.ResourceKind.CUSTOM_TARGET_TYPE
]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
operation_dict = {
resource.name: automation_util.DeleteAutomation(resource.name)
for resource in resource_dict[manifest_util.ResourceKind.AUTOMATION]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
operation_dict = {
resource.name: self.DeleteDeliveryPipeline(resource, force)
for resource in resource_dict[
manifest_util.ResourceKind.DELIVERY_PIPELINE
]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
operation_dict = {
resource.name: deploy_policy_util.DeleteDeployPolicy(resource.name)
for resource in resource_dict[manifest_util.ResourceKind.DEPLOY_POLICY]
}
self.operation_client.CheckOperationStatus(operation_dict, msg_template)
def CreateDeliveryPipeline(self, pipeline_config):
"""Creates a delivery pipeline resource.
Args:
pipeline_config: apitools.base.protorpclite.messages.Message, delivery
pipeline message.
Returns:
The operation message.
"""
log.debug('Creating delivery pipeline: ' + repr(pipeline_config))
return self._pipeline_service.Patch(
self.messages.ClouddeployProjectsLocationsDeliveryPipelinesPatchRequest(
deliveryPipeline=pipeline_config,
allowMissing=True,
name=pipeline_config.name,
updateMask=manifest_util.PIPELINE_UPDATE_MASK,
)
)
def DeleteDeliveryPipeline(self, pipeline_config, force):
"""Deletes a delivery pipeline resource.
Args:
pipeline_config: apitools.base.protorpclite.messages.Message, delivery
pipeline message.
force: if true, the delivery pipeline with sub-resources will be deleted
and its sub-resources will also be deleted.
Returns:
The operation message. It could be none if the resource doesn't exist.
"""
log.debug('Deleting delivery pipeline: ' + repr(pipeline_config))
return self._pipeline_service.Delete(
self.messages.ClouddeployProjectsLocationsDeliveryPipelinesDeleteRequest(
allowMissing=True, name=pipeline_config.name, force=force
)
)

View File

@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*- #
# Copyright 2024 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.
"""Support library to handle the deploy-policy subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
DEPLOY_POLICY_UPDATE_MASK = '*'
class DeployPoliciesClient(object):
"""Client for deploy policy service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a deploy_policy.DeployPoliciesClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations_deployPolicies
def Get(self, name):
"""Gets the deploy policy object.
Args:
name: deploy policy name.
Returns:
a deploy policy object.
"""
request = (
self.messages.ClouddeployProjectsLocationsDeployPoliciesGetRequest(
name=name
)
)
return self._service.Get(request)
def Patch(self, obj):
"""Patches a deploy policy resource.
Args:
obj: apitools.base.protorpclite.messages.Message, deploy policy message.
Returns:
The operation message.
"""
return self._service.Patch(
self.messages.ClouddeployProjectsLocationsDeployPoliciesPatchRequest(
deployPolicy=obj,
allowMissing=True,
name=obj.name,
updateMask=DEPLOY_POLICY_UPDATE_MASK,
)
)
def Delete(self, name):
"""Deletes a deploy policy resource.
Args:
name: str, deploy policy name.
Returns:
The operation message.
"""
return self._service.Delete(
self.messages.ClouddeployProjectsLocationsDeployPoliciesDeleteRequest(
name=name, allowMissing=True
)
)

View File

@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 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.
"""Support library to handle the job run subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
class JobRunsClient(object):
"""Client for job run service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a job_run.JobRunsClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = (
self.client.projects_locations_deliveryPipelines_releases_rollouts_jobRuns
)
def Terminate(
self,
name,
override_deploy_policies=None,
):
"""Terminates a job run.
Args:
name: Name of the JobRun. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/releases/{release}/rollouts/{rollout}/jobRuns/{jobRun}.
override_deploy_policies: List of Deploy Policies to override.
Returns:
TerminateJobRunResponse message.
"""
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsJobRunsTerminateRequest(
name=name,
terminateJobRunRequest=self.messages.TerminateJobRunRequest(
overrideDeployPolicy=override_deploy_policies,
),
)
return self._service.Terminate(request)

View File

@@ -0,0 +1,108 @@
# -*- 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.
"""Support library to handle the release subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
from googlecloudsdk.core import log
TARGET_FILTER_TEMPLATE = ('targetSnapshots.name:"{}"'
' AND renderState="SUCCEEDED"')
RELEASE_PARENT_TEMPLATE = 'projects/{}/locations/{}/deliveryPipelines/{}'
class ReleaseClient(object):
"""Client for release service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a release.ReleaseClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations_deliveryPipelines_releases
def Create(self, release_ref, release_config):
"""Create the release resource.
Args:
release_ref: release resource object.
release_config: release message.
Returns:
The operation message.
"""
log.debug('creating release: %r', release_config)
return self._service.Create(
self.messages
.ClouddeployProjectsLocationsDeliveryPipelinesReleasesCreateRequest(
parent=release_ref.Parent().RelativeName(),
release=release_config,
releaseId=release_ref.Name()))
def Get(self, name):
"""Gets a release resource.
Args:
name: release resource name.
Returns:
release message.
"""
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesGetRequest(
name=name)
return self._service.Get(request)
def ListReleasesByTarget(self, target_ref_project_number, project_id,
pipeline_id):
"""Lists the releases in a target.
Args:
target_ref_project_number: target reference with project number in the
name.
project_id: str, project ID.
pipeline_id: str, delivery pipeline ID.
Returns:
a list of release messages.
"""
target_dict = target_ref_project_number.AsDict()
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesListRequest(
parent=RELEASE_PARENT_TEMPLATE.format(project_id,
target_dict['locationsId'],
pipeline_id),
filter=TARGET_FILTER_TEMPLATE.format(
target_ref_project_number.RelativeName()))
return self._service.List(request).releases
def Abandon(self, name):
"""Abandons a release.
Args:
name: release resource name.
Returns:
AbandonReleaseResponse message.
"""
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesAbandonRequest(
name=name)
return self._service.Abandon(request)

View File

@@ -0,0 +1,267 @@
# -*- 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.
"""Support library to handle the rollout subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.clouddeploy import client_util
from googlecloudsdk.command_lib.deploy import deploy_util
from googlecloudsdk.core import log
class RolloutClient(object):
"""Client for release service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a release.ReleaseClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = (
self.client.projects_locations_deliveryPipelines_releases_rollouts
)
def Approve(self, name, approved, override_deploy_policies=None):
"""Calls the approve API to approve or reject a rollout..
Args:
name: Name of the Rollout. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/releases/{release}/rollouts/{rollout}.
approved: True = approve; False = reject
override_deploy_policies: List of Deploy Policies to override.
Returns:
ApproveRolloutResponse message.
"""
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsApproveRequest(
name=name,
approveRolloutRequest=self.messages.ApproveRolloutRequest(
approved=approved,
overrideDeployPolicy=override_deploy_policies,
),
)
return self._service.Approve(request)
def Get(self, name):
"""Gets a rollout resource.
Args:
name: rollout resource name.
Returns:
rollout message.
"""
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsGetRequest(
name=name
)
return self._service.Get(request)
def List(
self,
release_name,
filter_str=None,
order_by=None,
limit=None,
page_size=None,
):
"""Lists rollout resources that belongs to a release.
Args:
release_name: str, name of the release.
filter_str: optional[str], list filter.
order_by: optional[str], field to sort by.
limit: optional[int], the maximum number of `Rollout` objects to return.
page_size: optional[int], the number of `Rollout` objects to return per
request.
Returns:
Rollout list response.
"""
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsListRequest(
parent=release_name, filter=filter_str, orderBy=order_by
)
return list_pager.YieldFromList(
self._service,
request,
field='rollouts',
limit=limit,
batch_size=page_size,
batch_size_attribute='pageSize',
)
def Create(
self,
rollout_ref,
rollout_obj,
annotations=None,
labels=None,
starting_phase_id=None,
override_deploy_policies=None,
):
"""Creates a rollout resource.
Args:
rollout_ref: protorpc.messages.Message, rollout resource object.
rollout_obj: apitools.base.protorpclite.messages.Message, rollout message.
annotations: dict[str,str], a dict of annotation (key,value) pairs that
allow clients to store small amounts of arbitrary data in cloud deploy
resources.
labels: dict[str,str], a dict of label (key,value) pairs that can be used
to select cloud deploy resources and to find collections of cloud deploy
resources that satisfy certain conditions.
starting_phase_id: a str that specifies the rollout starting phase.
override_deploy_policies: List of Deploy Policies to override.
Returns:
The operation message.
"""
log.debug('Creating rollout: %r', rollout_obj)
deploy_util.SetMetadata(
self.messages,
rollout_obj,
deploy_util.ResourceType.ROLLOUT,
annotations,
labels,
)
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsCreateRequest(
parent=rollout_ref.Parent().RelativeName(),
rollout=rollout_obj,
rolloutId=rollout_ref.Name(),
startingPhaseId=starting_phase_id,
overrideDeployPolicy=override_deploy_policies,
)
return self._service.Create(request)
def RetryJob(
self,
name,
job,
phase,
override_deploy_policies=None,
):
"""Calls the retryjob API to retry a job on a rollout.
Args:
name: Name of the Rollout. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/releases/{release}/rollouts/{rollout}.
job: The job id on the rollout resource.
phase: The phase id on the rollout resource.
override_deploy_policies: List of Deploy Policies to override.
Returns:
RetryJobResponse message.
"""
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsRetryJobRequest(
rollout=name,
retryJobRequest=self.messages.RetryJobRequest(
jobId=job,
phaseId=phase,
overrideDeployPolicy=override_deploy_policies,
),
)
return self._service.RetryJob(request)
def AdvanceRollout(
self,
name,
phase,
override_deploy_policies=None,
):
"""Calls the AdvanceRollout API to advance a rollout to the next phase.
Args:
name: Name of the Rollout. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/releases/{release}/rollouts/{rollout}.
phase: The phase id on the rollout resource.
override_deploy_policies: List of Deploy Policies to override.
Returns:
AdvanceRolloutResponse message.
"""
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsAdvanceRequest(
name=name,
advanceRolloutRequest=self.messages.AdvanceRolloutRequest(
phaseId=phase,
overrideDeployPolicy=override_deploy_policies,
),
)
return self._service.Advance(request)
def CancelRollout(self, name, override_deploy_policies=None):
"""Calls the CancelRollout API to cancel a rollout.
Args:
name: Name of the Rollout. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/releases/{release}/rollouts/{rollout}.
override_deploy_policies: List of Deploy Policies to override.
Returns:
CancelRolloutResponse message.
"""
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsCancelRequest(
name=name,
cancelRolloutRequest=self.messages.CancelRolloutRequest(
overrideDeployPolicy=override_deploy_policies,
),
)
return self._service.Cancel(request)
def IgnoreJob(self, name, job, phase, override_deploy_policies=None):
"""Calls the IgnoreJob API to ignore a job on a rollout within a specified phase.
Args:
name: Name of the Rollout. Format is
projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}/releases/{release}/rollouts/{rollout}.
job: The job id on the rollout resource.
phase: The phase id on the rollout resource.
override_deploy_policies: List of Deploy Policies to override.
Returns:
IgnoreJobResponse message.
"""
if override_deploy_policies is None:
override_deploy_policies = []
request = self.messages.ClouddeployProjectsLocationsDeliveryPipelinesReleasesRolloutsIgnoreJobRequest(
rollout=name,
ignoreJobRequest=self.messages.IgnoreJobRequest(
jobId=job,
phaseId=phase,
overrideDeployPolicy=override_deploy_policies,
),
)
return self._service.IgnoreJob(request)

View File

@@ -0,0 +1,94 @@
# -*- 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.
"""Support library to handle the target subcommands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.clouddeploy import client_util
TARGET_UPDATE_MASK = '*,labels'
class TargetsClient(object):
"""Client for target service in the Cloud Deploy API."""
def __init__(self, client=None, messages=None):
"""Initialize a target.TargetClient.
Args:
client: base_api.BaseApiClient, the client class for Cloud Deploy.
messages: module containing the definitions of messages for Cloud Deploy.
"""
self.client = client or client_util.GetClientInstance()
self.messages = messages or client_util.GetMessagesModule(client)
self._service = self.client.projects_locations_targets
def Get(self, name):
"""Gets the shared target object by calling the ProjectsLocationsTargetsService.Get API.
Args:
name: str, target name.
Returns:
a target object.
"""
request = self.messages.ClouddeployProjectsLocationsTargetsGetRequest(
name=name)
return self._service.Get(request)
def Patch(self, target_obj):
"""Patches a target resource.
Args:
target_obj: apitools.base.protorpclite.messages.Message, target message.
Returns:
The operation message.
"""
return self._service.Patch(
self.messages.ClouddeployProjectsLocationsTargetsPatchRequest(
target=target_obj,
allowMissing=True,
name=target_obj.name,
updateMask=TARGET_UPDATE_MASK))
def Delete(self, name):
"""Deletes a target resource.
Args:
name: str, target name.
Returns:
The operation message. It could be none if the resource doesn't exist.
"""
return self._service.Delete(
self.messages.ClouddeployProjectsLocationsTargetsDeleteRequest(
allowMissing=True, name=name))
def List(self, location):
"""Lists target resources in a location.
Args:
location: str, the full name of the location which owns the targets.
Returns:
Returns a list of targets in the given location.
"""
return self._service.List(
self.messages.ClouddeployProjectsLocationsTargetsListRequest(
parent=location))