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,118 @@
# -*- coding: utf-8 -*- #
# Copyright 2019 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.
"""Client for interaction with Api Config CRUD on API Gateway API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.api_gateway import base
from googlecloudsdk.calliope import exceptions as calliope_exceptions
from googlecloudsdk.command_lib.api_gateway import common_flags
class ApiConfigClient(base.BaseClient):
"""Client for Api Config objects on Cloud API Gateway API."""
def __init__(self, client=None):
base.BaseClient.__init__(
self,
client=client,
message_base='ApigatewayProjectsLocationsApisConfigs',
service_name='projects_locations_apis_configs')
self.DefineDelete()
self.DefineList('apiConfigs')
self.DefineUpdate('apigatewayApiConfig')
self.supported_views = {
'FULL':
self.messages.ApigatewayProjectsLocationsApisConfigsGetRequest
.ViewValueValuesEnum.FULL,
'BASIC':
self.messages.ApigatewayProjectsLocationsApisConfigsGetRequest
.ViewValueValuesEnum.BASIC
}
def Create(self, api_config_ref, display_name=None, labels=None,
backend_auth=None, managed_service_configs=None,
grpc_service_defs=None, open_api_docs=None):
"""Creates an Api Config object.
Args:
api_config_ref: A parsed resource reference for the api
display_name: Optional string display name
labels: Optional cloud labels (as provided in the labels argument)
backend_auth: Optional string to set the service account for backend auth
managed_service_configs: Optional field to send in a list of managed
service configurations. Should be in the form of the
ApigatewayApiConfigFileMessage's generated from the discovery document
grpc_service_defs: Optional field to send in a list of GRPC service
definitions. Should be in the form of
ApigatewayApiConfigGrpcServiceDefinition's generated from the discovery
document
open_api_docs: Optional field to send in a list of Open API documents.
Should be in the form of ApigatewayApiConfigOpenApiDocument's generated
from the discovery document
Returns:
Long running operation
"""
labels = common_flags.ProcessLabelsFlag(
labels,
self.messages.ApigatewayApiConfig.LabelsValue)
api_config = self.messages.ApigatewayApiConfig(
name=api_config_ref.RelativeName(),
displayName=display_name,
labels=labels,
gatewayServiceAccount=backend_auth,
managedServiceConfigs=managed_service_configs,
grpcServices=grpc_service_defs,
openapiDocuments=open_api_docs)
req = self.create_request(
apiConfigId=api_config_ref.Name(),
apigatewayApiConfig=api_config,
parent=api_config_ref.Parent().RelativeName())
return self.service.Create(req)
def Get(self, api_config_ref, view=None):
"""Returns an API Config object.
Args:
api_config_ref: A parsed resource reference for the API.
view: Optional string. If specified as FULL, the source config files will
be returned.
Returns:
An API Config object.
Raises:
calliope.InvalidArgumentException: If an invalid view (i.e. not FULL,
BASIC, or none) was
provided.
"""
view_enum = None
if view is not None:
try:
view_enum = self.supported_views[view.upper()]
except KeyError:
raise calliope_exceptions.InvalidArgumentException(
'--view', 'View must be one of: "FULL" or "BASIC".')
req = self.get_request(name=api_config_ref.RelativeName(), view=view_enum)
return self.service.Get(req)

View File

@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*- #
# Copyright 2019 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.
"""Client for interaction with Api CRUD on API Gateway API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import exceptions as apitools_exceptions
from googlecloudsdk.api_lib.api_gateway import base
from googlecloudsdk.command_lib.api_gateway import common_flags
class ApiClient(base.BaseClient):
"""Client for Api objects on Cloud API Gateway API."""
def __init__(self, client=None):
base.BaseClient.__init__(self,
client=client,
message_base='ApigatewayProjectsLocationsApis',
service_name='projects_locations_apis')
self.DefineGet()
self.DefineList('apis')
self.DefineUpdate('apigatewayApi')
self.DefineDelete()
self.DefineIamPolicyFunctions()
def DoesExist(self, api_ref):
"""Checks if an Api object exists.
Args:
api_ref: Resource, a resource reference for the api
Returns:
Boolean, indicating whether or not exists
"""
try:
self.Get(api_ref)
except apitools_exceptions.HttpNotFoundError:
return False
return True
def Create(self, api_ref, managed_service=None, labels=None,
display_name=None):
"""Creates a new Api object.
Args:
api_ref: Resource, a resource reference for the api
managed_service: Optional string, reference name for OP service
labels: Optional cloud labels
display_name: Optional display name
Returns:
Long running operation response object.
"""
labels = common_flags.ProcessLabelsFlag(
labels,
self.messages.ApigatewayApi.LabelsValue)
api = self.messages.ApigatewayApi(
name=api_ref.RelativeName(),
managedService=managed_service,
labels=labels,
displayName=display_name)
req = self.create_request(
apiId=api_ref.Name(),
apigatewayApi=api,
parent=api_ref.Parent().RelativeName())
return self.service.Create(req)

View File

@@ -0,0 +1,250 @@
# -*- coding: utf-8 -*- #
# Copyright 2019 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.
"""Client for interaction with Gateway CRUD on API Gateway API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import types
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.util import apis
from googlecloudsdk.command_lib.iam import iam_util
def GetClientInstance(version='v1', no_http=False):
return apis.GetClientInstance('apigateway', version, no_http=no_http)
def GetMessagesModule(version='v1'):
return apis.GetMessagesModule('apigateway', version)
class BaseClient(object):
"""Base for building API Clients."""
def __init__(self, client=None, message_base=None, service_name=None):
self.client = client or GetClientInstance()
self.messages = self.client.MESSAGES_MODULE
self.service = getattr(self.client, service_name, None)
# Define standard request types if they exist for the base message
self.get_request = getattr(self.messages, message_base + 'GetRequest', None)
self.create_request = getattr(self.messages,
message_base + 'CreateRequest',
None)
self.list_request = getattr(self.messages,
message_base + 'ListRequest',
None)
self.patch_request = getattr(self.messages,
message_base + 'PatchRequest',
None)
self.delete_request = getattr(self.messages,
message_base + 'DeleteRequest',
None)
# Define IAM request types if they exist for the base message
self.get_iam_policy_request = getattr(self.messages,
message_base + 'GetIamPolicyRequest',
None)
self.set_iam_policy_request = getattr(self.messages,
message_base + 'SetIamPolicyRequest',
None)
def DefineGet(self):
"""Defines basic get function on an assigned class."""
def Get(self, object_ref):
"""Gets an object.
Args:
self: The self of the class this is set on.
object_ref: Resource, resource reference for object to get.
Returns:
The object requested.
"""
req = self.get_request(name=object_ref.RelativeName())
return self.service.Get(req)
# Bind the function to the method and set the attribute
setattr(self, 'Get', types.MethodType(Get, self))
def DefineDelete(self):
"""Defines basic delete function on an assigned class."""
def Delete(self, object_ref):
"""Deletes a given object given an object name.
Args:
self: The self of the class this is set on.
object_ref: Resource, resource reference for object to delete.
Returns:
Long running operation.
"""
req = self.delete_request(name=object_ref.RelativeName())
return self.service.Delete(req)
# Bind the function to the method and set the attribute
setattr(self, 'Delete', types.MethodType(Delete, self))
def DefineList(self, field_name, is_operations=False):
"""Defines the List functionality on the calling class.
Args:
field_name: The name of the field on the list response to list
is_operations: Operations have a slightly altered message structure, set
to true in operations client
"""
def List(self, parent_name, filters=None, limit=None, page_size=None,
sort_by=None):
"""Lists the objects under a given parent.
Args:
self: the self object function will be bound to.
parent_name: Resource name of the parent to list under.
filters: Filters to be applied to results (optional).
limit: Limit to the number of results per page (optional).
page_size: the number of results per page (optional).
sort_by: Instructions about how to sort the results (optional).
Returns:
List Pager.
"""
if is_operations:
req = self.list_request(filter=filters, name=parent_name)
else:
req = self.list_request(filter=filters, parent=parent_name,
orderBy=sort_by)
return list_pager.YieldFromList(
self.service,
req,
limit=limit,
batch_size_attribute='pageSize',
batch_size=page_size,
field=field_name)
# Bind the function to the method and set the attribute
setattr(self, 'List', types.MethodType(List, self))
def DefineUpdate(self, update_field_name):
"""Defines the Update functionality on the calling class.
Args:
update_field_name: the field on the patch_request to assign updated object
to
"""
def Update(self, updating_object, update_mask=None):
"""Updates an object.
Args:
self: The self of the class this is set on.
updating_object: Object which is being updated.
update_mask: A string saying which fields have been updated.
Returns:
Long running operation.
"""
req = self.patch_request(name=updating_object.name,
updateMask=update_mask)
setattr(req, update_field_name, updating_object)
return self.service.Patch(req)
# Bind the function to the method and set the attribute
setattr(self, 'Update', types.MethodType(Update, self))
def DefineIamPolicyFunctions(self):
"""Defines all of the IAM functionality on the calling class."""
def GetIamPolicy(self, object_ref):
"""Gets an IAM Policy on an object.
Args:
self: The self of the class this is set on.
object_ref: Resource, reference for object IAM policy belongs to.
Returns:
The IAM policy.
"""
req = self.get_iam_policy_request(resource=object_ref.RelativeName())
return self.service.GetIamPolicy(req)
def SetIamPolicy(self, object_ref, policy, update_mask=None):
"""Sets an IAM Policy on an object.
Args:
self: The self of the class this is set on.
object_ref: Resource, reference for object IAM policy belongs to.
policy: the policy to be set.
update_mask: fields being update on the IAM policy.
Returns:
The IAM policy.
"""
policy_request = self.messages.ApigatewaySetIamPolicyRequest(
policy=policy,
updateMask=update_mask)
req = self.set_iam_policy_request(
apigatewaySetIamPolicyRequest=policy_request,
resource=object_ref.RelativeName())
return self.service.SetIamPolicy(req)
def AddIamPolicyBinding(self, object_ref, member, role):
"""Adds an IAM role to a member on an object.
Args:
self: The self of the class this is set on.
object_ref: Resource, reference for object IAM policy belongs to.
member: the member the binding is being added to.
role: the role which to bind to the member.
Returns:
The IAM policy.
"""
policy = self.GetIamPolicy(object_ref)
iam_util.AddBindingToIamPolicy(self.messages.ApigatewayBinding, policy,
member, role)
return self.SetIamPolicy(object_ref, policy, 'bindings,etag')
def RemoveIamPolicyBinding(self, object_ref, member, role):
"""Adds an IAM role for a member on an object.
Args:
self: The self of the class this is set on
object_ref: Resource, reference for object IAM policy belongs to
member: the member the binding is removed for
role: the role which is being removed from the member
Returns:
The IAM policy
"""
policy = self.GetIamPolicy(object_ref)
iam_util.RemoveBindingFromIamPolicy(policy, member, role)
return self.SetIamPolicy(object_ref, policy, 'bindings,etag')
# Bind the function to the method and set the attribute
setattr(self, 'GetIamPolicy', types.MethodType(GetIamPolicy, self))
setattr(self, 'SetIamPolicy', types.MethodType(SetIamPolicy, self))
setattr(self, 'AddIamPolicyBinding', types.MethodType(AddIamPolicyBinding,
self))
setattr(self, 'RemoveIamPolicyBinding', types.MethodType(
RemoveIamPolicyBinding, self))

View File

@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*- #
# Copyright 2019 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.
"""Client for interaction with Gateway CRUD on API Gateway API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.api_gateway import base
from googlecloudsdk.command_lib.api_gateway import common_flags
class GatewayClient(base.BaseClient):
"""Client for gateway objects on Cloud API Gateway API."""
def __init__(self, client=None):
base.BaseClient.__init__(self,
client=client,
message_base='ApigatewayProjectsLocationsGateways',
service_name='projects_locations_gateways')
self.DefineGet()
self.DefineDelete()
self.DefineList('gateways')
self.DefineUpdate('apigatewayGateway')
self.DefineIamPolicyFunctions()
def Create(self, gateway_ref, api_config, display_name=None, labels=None):
"""Creates a new gateway object.
Args:
gateway_ref: Resource, a resource reference for the gateway
api_config: Resource, a resource reference for the gateway
display_name: Optional display name
labels: Optional cloud labels
Returns:
Long running operation.
"""
labels = common_flags.ProcessLabelsFlag(
labels,
self.messages.ApigatewayGateway.LabelsValue)
gateway = self.messages.ApigatewayGateway(
name=gateway_ref.RelativeName(),
labels=labels,
apiConfig=api_config.RelativeName(),
displayName=display_name,
)
req = self.create_request(
parent=gateway_ref.Parent().RelativeName(),
gatewayId=gateway_ref.Name(),
apigatewayGateway=gateway,
)
resp = self.service.Create(req)
return resp

View File

@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*- #
# Copyright 2019 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.
"""Client for interaction with Operations CRUD on API Gateway API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.api_gateway import base
from googlecloudsdk.api_lib.util import waiter
class OperationsClient(base.BaseClient):
"""Client for operation objects on Cloud API Gateway API."""
def __init__(self, client=None):
base.BaseClient.__init__(
self,
client=client,
message_base='ApigatewayProjectsLocationsOperations',
service_name='projects_locations_operations')
self.DefineGet()
self.DefineList('operations', is_operations=True)
def Cancel(self, operation_ref):
"""Cancel an operation.
Args:
operation_ref: The message to process (expected to be of type Operation)
Returns:
(Empty) The response message.
"""
req = self.messages.ApigatewayProjectsLocationsOperationsCancelRequest(
name=operation_ref.RelativeName())
return self.service.Cancel(req)
def WaitForOperation(self, operation_ref, message=None, service=None):
"""Waits for the given google.longrunning.Operation to complete.
Args:
operation_ref: The operation to poll.
message: String to display for default progress_tracker.
service: The service to get the resource after the long running operation
completes.
Raises:
apitools.base.py.HttpError: if the request returns an HTTP error
Returns:
The Operation or the Resource the Operation is associated with.
"""
# Consumers of OperationsClient can be resource-aware and if so, they can
# provide the service used for interacting with the Resource the Operation
# is associated with. In this case, OperationsClient#WaitForOperation will
# return the Resource the polled Operation is associated with. Otherwise,
# no service is provided and the Operation object itself is returned.
#
# Example: `gateways create` is resource-aware and returns an
# ApigatewayGateway while `operations wait` is not resource-aware and will
# return the Operation itself.
if service is None:
poller = waiter.CloudOperationPollerNoResources(
self.service)
else:
poller = waiter.CloudOperationPoller(
service,
self.service)
if message is None:
message = 'Waiting for Operation [{}] to complete'.format(
operation_ref.RelativeName())
return waiter.WaitFor(poller, operation_ref, message)