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,44 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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 utilities for accessing the Private CA API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.util import apis
import six.moves.urllib.parse
DEFAULT_API_VERSION = 'v1'
V1_API_VERSION = 'v1'
def GetClientClass(api_version=DEFAULT_API_VERSION):
return apis.GetClientClass('privateca', api_version)
def GetClientInstance(api_version=DEFAULT_API_VERSION):
return apis.GetClientInstance('privateca', api_version)
def GetMessagesModule(api_version=DEFAULT_API_VERSION):
return apis.GetMessagesModule('privateca', api_version)
def GetServiceName(api_version=DEFAULT_API_VERSION):
"""Gets the service name based on the configured API endpoint."""
endpoint = apis.GetEffectiveApiEndpoint('privateca', api_version)
return six.moves.urllib.parse.urlparse(endpoint).hostname

View File

@@ -0,0 +1,84 @@
# -*- 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.
"""Certificate utilities for Privateca commands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import random
import string
from googlecloudsdk.api_lib.privateca import base
from googlecloudsdk.calliope import exceptions
from googlecloudsdk.core.util import times
def GetCertificateBySerialNum(ca_pool_ref, serial_num):
"""Obtains a certificate by serial num by filtering all certs in a CA pool.
Args:
ca_pool_ref: The resource reference to the CA pool.
serial_num: The serial number to lookup the certificate by.
Returns:
The certificate message of the corresponding serial number. Ignores
duplicate certificates.
Raises:
exceptions.InvalidArgumentError if there were no certificates with the
specified CA pool and serial number.
"""
cert_filter = (
'certificate_description.subject_description.hex_serial_number:{}'.format(
serial_num
)
)
client = base.GetClientInstance(api_version='v1')
messages = base.GetMessagesModule(api_version='v1')
response = client.projects_locations_caPools_certificates.List(
messages.PrivatecaProjectsLocationsCaPoolsCertificatesListRequest(
parent=ca_pool_ref.RelativeName(), filter=cert_filter
)
)
if not response.certificates:
raise exceptions.InvalidArgumentException(
'--serial-number',
'The serial number specified does not exist under the CA pool [{}]]'
.format(ca_pool_ref.RelativeName()),
)
return response.certificates[0]
def GenerateCertId():
"""Generate a certificate id with the date and two length 3 alphanum strings.
E.G. YYYYMMDD-ABC-DEF.
Returns:
The generated certificate id string.
"""
# Avoid name collisions in certificate id generation. Normal random.choice
# seeds on system time, which might not be sufficiently random of a seed.
# SystemRandom uses other sources from the OS to generate the random ID.
sys_rng = random.SystemRandom()
alphanum = string.ascii_uppercase + string.digits
alphanum_rand1 = ''.join(sys_rng.choice(alphanum) for i in range(3))
alphanum_rand2 = ''.join(sys_rng.choice(alphanum) for i in range(3))
date_str = times.FormatDateTime(times.Now(), '%Y%m%d')
return '{}-{}-{}'.format(date_str, alphanum_rand1, alphanum_rand2)

View File

@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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.
"""Helpers for dealing with Private CA locations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import exceptions
from googlecloudsdk.api_lib.privateca import base
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
# The list of locations where the v1 API is available. Used as a fallback in
# case the ListLocations API is down.
_V1Locations = [
'asia-east1',
'asia-east2',
'asia-northeast1',
'asia-northeast2',
'asia-northeast3',
'asia-south1',
'asia-southeast1',
'asia-southeast2',
'australia-southeast1',
'europe-north1',
'europe-west1',
'europe-west2',
'europe-west3',
'europe-west4',
'europe-west6',
'northamerica-northeast1',
'southamerica-east1',
'us-central1',
'us-east1',
'us-east4',
'us-west1',
'us-west2',
'us-west3',
'us-west4',
]
def GetSupportedLocations(version='v1'):
"""Gets a list of supported Private CA locations for the current project."""
if version != 'v1':
raise exceptions.NotYetImplementedError(
'Unknown API version: {}'.format(version))
client = base.GetClientInstance(api_version='v1')
messages = base.GetMessagesModule(api_version='v1')
project = properties.VALUES.core.project.GetOrFail()
try:
response = client.projects_locations.List(
messages.PrivatecaProjectsLocationsListRequest(
name='projects/{}'.format(project)))
return [location.locationId for location in response.locations]
except exceptions.HttpError as e:
log.debug('ListLocations failed: %r.', e)
log.debug('Falling back to hard-coded list.')
return _V1Locations

View File

@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*- #
# Copyright 2025 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.
"""Helpers for the operations API client."""
from apitools.base.py import exceptions
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.privateca import base
from googlecloudsdk.api_lib.util import exceptions as api_exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
def ListOperations(location, list_filter, limit, page_size):
"""Lists operations in a given project.
Args:
location: The location to list operations in, or '-' for all locations.
list_filter: A filter to apply to the list request.
limit: The number of operations to retrieve.
page_size: The number of operations to retrieve per page.
Returns:
A generator of matching operations.
"""
client = base.GetClientInstance(api_version='v1')
messages = base.GetMessagesModule(api_version='v1')
project = properties.VALUES.core.project.GetOrFail()
parent_resource = 'projects/{}/locations/{}'.format(project, location)
request = messages.PrivatecaProjectsLocationsOperationsListRequest(
name=parent_resource, filter=list_filter)
return list_pager.YieldFromList(
client.projects_locations_operations,
request,
field='operations',
limit=limit,
batch_size_attribute='pageSize',
batch_size=page_size)
def GetOperation(operation_ref):
"""Gets an operation resource."""
client = base.GetClientInstance(api_version='v1')
messages = base.GetMessagesModule(api_version='v1')
request = messages.PrivatecaProjectsLocationsOperationsGetRequest(
name=operation_ref.RelativeName())
try:
return client.projects_locations_operations.Get(request)
except exceptions.HttpError as e:
log.debug('GetOperation failed: %r.', e)
raise api_exceptions.HttpException(e)

View File

@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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.
"""Helpers for creating API requests."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import uuid
import six
def GenerateRequestId():
return six.text_type(uuid.uuid4())

View File

@@ -0,0 +1,34 @@
# -*- 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 dealing with Private CA Resources."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.privateca import base as privateca_base
from googlecloudsdk.core import resources
def MakeGetUriFunc(collection):
"""Returns a function which turns a resource into a uri."""
def _GetUri(resource):
registry = resources.REGISTRY.Clone()
registry.RegisterApiByName('privateca', privateca_base.V1_API_VERSION)
parsed = registry.Parse(resource.name, collection=collection)
return parsed.SelfLink()
return _GetUri