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,121 @@
# -*- 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.
"""Experiment client for Faultinjectiontesting Cloud SDK."""
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.fault_injection import utils as api_lib_utils
class ExperimentsClient(object):
"""Client for faults in Faultinjection Testing API."""
def __init__(self, client=None, messages=None):
self.client = client or api_lib_utils.GetClientInstance()
self.messages = messages or api_lib_utils.GetMessagesModule()
self._experiments_client = self.client.projects_locations_experiments
def Describe(self, experiment):
"""Describe a Experiment in the Project/location.
Args:
experiment: str, the name for the experiment being described.
Returns:
Described Experiment Resource.
"""
describe_req = self.messages.FaultinjectiontestingProjectsLocationsExperimentsGetRequest(
name=experiment
)
return self._experiments_client.Get(describe_req)
def Delete(self, experiment):
"""Delete a Experiment in the Project/location.
Args:
experiment: str, the name for the Experiment being deleted
Returns:
Empty Response Message.
"""
delete_req = self.messages.FaultinjectiontestingProjectsLocationsExperimentsDeleteRequest(
name=experiment
)
return self._experiments_client.Delete(delete_req)
def Create(self, experiment, experiment_config, parent):
"""Create a fault in the Project/location.
Args:
experiment: str, the name for the experiment being created
experiment_config: file, the file which contains experiment config
parent: parent for fault resource
Returns:
Experiment.
"""
create_req = api_lib_utils.ParseCreateExperimentFromYaml(
experiment, experiment_config, parent
)
return self._experiments_client.Create(create_req)
def Update(self, experiment, experiment_config):
"""Update a experiment in the Project/location.
Args:
experiment: str, the name for the experiment being created
experiment_config: file, the file which contains experiment config
Returns:
Experiment.
"""
patch_req = api_lib_utils.ParseUpdateExperimentFromYaml(
experiment, experiment_config
)
return self._experiments_client.Patch(patch_req)
def List(
self,
parent,
limit=None,
page_size=100,
):
"""List Experiments in the Projects/Location.
Args:
parent: str, projects/{projectId}/locations/{location}
limit: int or None, the total number of results to return.
page_size: int, the number of entries in each batch (affects requests
made, but not the yielded results).
Returns:
Generator of matching Experiments.
"""
list_req = self.messages.FaultinjectiontestingProjectsLocationsExperimentsListRequest(
parent=parent
)
return list_pager.YieldFromList(
self._experiments_client,
list_req,
field='experiments',
batch_size=page_size,
limit=limit,
batch_size_attribute='pageSize',
)

View File

@@ -0,0 +1,125 @@
# -*- 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.
"""Fault client for Faultinjectiontesting Cloud SDK."""
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.fault_injection import utils as api_lib_utils
class FaultsClient(object):
"""Client for faults in Faultinjection Testing API."""
def __init__(self, client=None, messages=None):
self.client = client or api_lib_utils.GetClientInstance()
self.messages = messages or api_lib_utils.GetMessagesModule()
self._faults_client = self.client.projects_locations_faults
def Describe(self, fault):
"""Describe a Fault in the Project/location.
Args:
fault: str, the name for the fault being described.
Returns:
Described Fault Resource.
"""
describe_req = (
self.messages.FaultinjectiontestingProjectsLocationsFaultsGetRequest(
name=fault
)
)
return self._faults_client.Get(describe_req)
def Delete(self, fault):
"""Delete a fault in the Project/location.
Args:
fault: str, the name for the fault being deleted
Returns:
Empty Response Message.
"""
delete_req = (
self.messages.FaultinjectiontestingProjectsLocationsFaultsDeleteRequest(
name=fault
)
)
return self._faults_client.Delete(delete_req)
def Create(self, fault, faultconfig, parent):
"""Create a fault in the Project/location.
Args:
fault: str, the name for the fault being created
faultconfig: file, the file which contains fault config
parent: parent for fault resource
Returns:
Fault.
"""
create_req = (api_lib_utils.ParseCreateFaultFromYaml(
fault, faultconfig, parent
))
return self._faults_client.Create(create_req)
def Update(self, fault, faultconfig):
"""Update a fault in the Project/location.
Args:
fault: str, the name for the fault being created
faultconfig: file, the file which contains fault config
Returns:
Fault.
"""
patch_req = (api_lib_utils.ParseUpdateFaultFromYaml(fault, faultconfig))
return self._faults_client.Patch(patch_req)
def List(
self,
parent,
limit=None,
page_size=100,
):
"""List Faults in the Projects/Location.
Args:
parent: str, projects/{projectId}/locations/{location}
limit: int or None, the total number of results to return.
page_size: int, the number of entries in each batch (affects requests
made, but not the yielded results).
Returns:
Generator of matching Faults.
"""
list_req = (
self.messages.FaultinjectiontestingProjectsLocationsFaultsListRequest(
parent=parent
)
)
return list_pager.YieldFromList(
self._faults_client,
list_req,
field='faults',
batch_size=page_size,
limit=limit,
batch_size_attribute='pageSize',
)

View File

@@ -0,0 +1,126 @@
# -*- 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.
"""Jobs client for Faultinjectiontesting Cloud SDK."""
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.fault_injection import utils as api_lib_utils
class JobsClient(object):
"""Client for Jobs in Faultinjection Testing API."""
def __init__(self, client=None, messages=None):
self.client = client or api_lib_utils.GetClientInstance()
self.messages = messages or api_lib_utils.GetMessagesModule()
self._jobs_client = self.client.projects_locations_jobs
def Describe(self, job):
"""Describe a job in the Project/location.
Args:
job: str, the name for the job being described.
Returns:
Described Job Resource.
"""
describe_req = (
self.messages.FaultinjectiontestingProjectsLocationsJobsGetRequest(
name=job
)
)
return self._jobs_client.Get(describe_req)
def List(
self,
parent,
limit=None,
page_size=100,
):
"""List Jobs in the Projects/Location.
Args:
parent: str, projects/{projectId}/locations/{location}
limit: int or None, the total number of results to return.
page_size: int, the number of entries in each batch (affects requests
made, but not the yielded results).
Returns:
Generator of matching jobs.
"""
list_req = (
self.messages.FaultinjectiontestingProjectsLocationsJobsListRequest(
parent=parent
)
)
return list_pager.YieldFromList(
self._jobs_client,
list_req,
field='jobs',
batch_size=page_size,
limit=limit,
batch_size_attribute='pageSize',
)
def Create(self, job_id, experiment_id, fault_targets, dry_run, parent):
"""Create a job in the Project/location.
Args:
job_id: str, the name for the job being created
experiment_id: str, name of the experiment
fault_targets: targets for the faults
dry_run: Boolean value for dry-run
parent: parent for fault resource
Returns:
Job.
"""
targets = []
# For MVP, we are supporting only service names as targets
for fault_target in fault_targets:
targets.append(
self.messages.FaultInjectionTargetMatcher(service=fault_target)
)
job = self.messages.Job(experiment=experiment_id, faultTargets=targets)
create_req = (
self.messages.FaultinjectiontestingProjectsLocationsJobsCreateRequest(
jobId=job_id,
job=job,
validateOnly=dry_run,
parent=parent,
)
)
return self._jobs_client.Create(create_req)
def Delete(self, job):
"""Delete a Job in the Project/location.
Args:
job: str, the name for the job being deleted
Returns:
Empty Response Message.
"""
delete_req = (
self.messages.FaultinjectiontestingProjectsLocationsJobsDeleteRequest(
name=job
)
)
return self._jobs_client.Delete(delete_req)

View File

@@ -0,0 +1,213 @@
# -*- 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.
"""Util for Fault Injection Cloud SDK."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import encoding
from googlecloudsdk.api_lib.util import apis
from googlecloudsdk.calliope import base
from googlecloudsdk.core import exceptions
import six
VERSION_MAP = {
base.ReleaseTrack.ALPHA: 'v1alpha1'
}
_API_NAME = 'faultinjectiontesting'
# The messages module can also be accessed from client.MESSAGES_MODULE
def GetMessagesModule(release_track=base.ReleaseTrack.ALPHA):
api_version = VERSION_MAP.get(release_track)
return apis.GetMessagesModule(_API_NAME, api_version)
def GetClientInstance(release_track=base.ReleaseTrack.ALPHA):
api_version = VERSION_MAP.get(release_track)
return apis.GetClientInstance(_API_NAME, api_version)
class InvalidFaultConfigurationError(exceptions.Error):
"""Error if a fault configuration is improperly specified."""
class InvalidExperimentConfigurationError(exceptions.Error):
"""Error if a Experiment configuration is improperly specified."""
def ParseCreateFaultFromYaml(fault, fault_config, parent):
"""Converts the given fault dict to the corresponding import request.
Args:
fault: faultId, fault name
fault_config: dict, fault configuation of the create fault request.
parent: parent for fault resource
Returns:
FaultinjectiontestingProjectsLocationsFaultsCreateRequest
Raises:
InvalidFaultConfigurationError: If the fault config is invalid.
"""
messages = GetMessagesModule(release_track=base.ReleaseTrack.ALPHA)
request = messages.FaultinjectiontestingProjectsLocationsFaultsCreateRequest
try:
import_request_message = encoding.DictToMessage(
{'fault': fault_config, 'faultId': fault, 'parent': parent}, request
)
except AttributeError:
raise InvalidFaultConfigurationError(
'An error occurred while parsing the '
'serialized fault. Please check your '
'input file.'
)
unrecognized_field_paths = _GetUnrecognizedFieldPaths(import_request_message)
if unrecognized_field_paths:
error_msg_lines = [
'Invalid fault config, the following fields are ' + 'unrecognized:'
]
error_msg_lines += unrecognized_field_paths
raise InvalidFaultConfigurationError('\n'.join(error_msg_lines))
return import_request_message
def ParseUpdateFaultFromYaml(fault, fault_config):
"""Converts the given fault dict to the corresponding import request.
Args:
fault: faultId, fault name
fault_config: dict, fault configuation of the create fault request.
Returns:
FaultinjectiontestingProjectsLocationsFaultsPatchRequest
Raises:
InvalidFaultConfigurationError: If the fault config is invalid.
"""
messages = GetMessagesModule(release_track=base.ReleaseTrack.ALPHA)
request = messages.FaultinjectiontestingProjectsLocationsFaultsPatchRequest
try:
import_request_message = encoding.DictToMessage(
{'fault': fault_config, 'name': fault}, request
)
except AttributeError:
raise InvalidFaultConfigurationError(
'An error occurred while parsing the '
'serialized fault. Please check your '
'input file.'
)
unrecognized_field_paths = _GetUnrecognizedFieldPaths(import_request_message)
if unrecognized_field_paths:
error_msg_lines = [
'Invalid fault config, the following fields are ' + 'unrecognized:'
]
error_msg_lines += unrecognized_field_paths
raise InvalidFaultConfigurationError('\n'.join(error_msg_lines))
return import_request_message
def ParseCreateExperimentFromYaml(experiment, experiment_config, parent):
"""Converts the given fault dict to the corresponding import request.
Args:
experiment: ExperimentId, Experiment name
experiment_config: dict, experiment config of the create experiment request.
parent: parent for experiment resource
Returns:
FaultinjectiontestingProjectsLocationsExperimentsCreateRequest
Raises:
InvalidExperimentConfigurationError: If the experiment config is invalid.
"""
messages = GetMessagesModule(release_track=base.ReleaseTrack.ALPHA)
req = messages.FaultinjectiontestingProjectsLocationsExperimentsCreateRequest
try:
import_request_message = encoding.DictToMessage(
{
'experiment': experiment_config,
'experimentId': experiment,
'parent': parent,
},
req,
)
except AttributeError:
raise InvalidExperimentConfigurationError(
'An error occurred while parsing the '
'serialized experiment. Please check your '
'input file.'
)
unrecognized_field_paths = _GetUnrecognizedFieldPaths(import_request_message)
if unrecognized_field_paths:
error_msg_lines = [
'Invalid experiment config, the following fields are ' + 'unrecognized:'
]
error_msg_lines += unrecognized_field_paths
raise InvalidExperimentConfigurationError('\n'.join(error_msg_lines))
return import_request_message
def ParseUpdateExperimentFromYaml(experiment, experiment_config):
"""Converts the given fault dict to the corresponding import request.
Args:
experiment: experimentId, experiment name
experiment_config: dict, fault configuation of the create fault request.
Returns:
FaultinjectiontestingProjectsLocationsExperimentsPatchRequest
Raises:
InvalidExperimentConfigurationError: If the experiment config is invalid.
"""
messages = GetMessagesModule(release_track=base.ReleaseTrack.ALPHA)
request = (
messages.FaultinjectiontestingProjectsLocationsExperimentsPatchRequest
)
try:
import_request_message = encoding.DictToMessage(
{'experiment': experiment_config, 'name': experiment}, request
)
except AttributeError:
raise InvalidExperimentConfigurationError(
'An error occurred while parsing the '
'serialized experiment. Please check your '
'input file.'
)
unrecognized_field_paths = _GetUnrecognizedFieldPaths(import_request_message)
if unrecognized_field_paths:
error_msg_lines = [
'Invalid experiment config, the following fields are ' + 'unrecognized:'
]
error_msg_lines += unrecognized_field_paths
raise InvalidExperimentConfigurationError('\n'.join(error_msg_lines))
return import_request_message
def _GetUnrecognizedFieldPaths(message):
"""Returns the field paths for unrecognized fields in the message."""
errors = encoding.UnrecognizedFieldIter(message)
unrecognized_field_paths = []
for edges_to_message, field_names in errors:
message_field_path = '.'.join(six.text_type(e) for e in edges_to_message)
for field_name in field_names:
unrecognized_field_paths.append(
'{}.{}'.format(message_field_path, field_name)
)
return sorted(unrecognized_field_paths)