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,48 @@
# -*- 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.
"""The resource allowances command group for the Batch API CLI."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
DETAILED_HELP = {
'DESCRIPTION': """
The gcloud batch resource allowances command group lets you create, describe, list and delete
Batch resource allowances.
With Batch, you can utilize the fully managed service to schedule, queue, and
execute batch resource allowances on Google's infrastructure.
For more information about Batch, see the
[Batch overview](https://cloud.google.com/batch)
and the
[Batch documentation](https://cloud.google.com/batch/docs/).
""",
}
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
@base.DefaultUniverseOnly
class ResourceAllowances(base.Group):
"""Manage Batch resource allowance resources."""
detailed_help = DETAILED_HELP
category = base.BATCH_CATEGORY

View File

@@ -0,0 +1,109 @@
# -*- 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.
"""Command to create a specified Batch resource allowance."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.protorpclite.messages import DecodeError
from apitools.base.py import encoding
from googlecloudsdk.api_lib.batch import resource_allowances
from googlecloudsdk.calliope import arg_parsers
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.batch import resource_args
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core import yaml
@base.DefaultUniverseOnly
class Submit(base.Command):
"""Create a Batch resource allowance.
This command creates a Batch resource allowance.
## EXAMPLES
The following command submit a resource allowance with config.json sample
config file
`projects/foo/locations/us-central1/resousrceAllowances/bar`:
$ {command} projects/foo/locations/us-central1/resousrceAllowances/bar
--config config.json
"""
@staticmethod
def Args(parser):
resource_args.AddCreateResourceAllowanceResourceArgs(parser)
parser.add_argument(
'--config',
type=arg_parsers.FileContents(),
required=True,
help="""The config file of a resource allowance.""",
)
@classmethod
def _CreateResourceAllowanceMessage(cls, batch_msgs, config):
"""Parse into ResourceAllowance message using the config input.
Args:
batch_msgs: Batch defined proto message.
config: The input content being either YAML or JSON or the HEREDOC
input.
Returns:
The Parsed resource allowance message.
"""
try:
result = encoding.PyValueToMessage(
batch_msgs.ResourceAllowance, yaml.load(config)
)
except (ValueError, AttributeError, yaml.YAMLParseError):
try:
result = encoding.JsonToMessage(batch_msgs.ResourceAllowance, config)
except (ValueError, DecodeError) as e:
raise exceptions.Error('Unable to parse config file: {}'.format(e))
return result
def Run(self, args):
resource_allowance_ref = args.CONCEPTS.resource_allowance.Parse()
location_ref = resource_allowance_ref.Parent()
resource_allowance_id = resource_allowance_ref.RelativeName().split('/')[-1]
# Remove the invalid resource_allowance_id if no resource_allowance_id
# being specified, batch_client would create a valid job_id.
if resource_allowance_id == resource_args.INVALIDID:
resource_allowance_id = None
release_track = self.ReleaseTrack()
batch_client = resource_allowances.ResourceAllowancesClient(release_track)
batch_msgs = batch_client.messages
resource_allowance_msg = batch_msgs.ResourceAllowance()
if args.config:
resource_allowance_msg = self._CreateResourceAllowanceMessage(
batch_msgs, args.config
)
resp = batch_client.Create(
resource_allowance_id, location_ref, resource_allowance_msg
)
log.status.Print(
'ResourceAllowance {resourceAllowanceName} was successfully created.'
.format(resourceAllowanceName=resp.uid)
)
return resp

View File

@@ -0,0 +1,64 @@
# -*- 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.
"""Command to delete a specified Batch resource allowance."""
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.batch import resource_allowances
from googlecloudsdk.api_lib.batch import util
from googlecloudsdk.api_lib.util import exceptions
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.batch import resource_args
from googlecloudsdk.core import log
class Delete(base.DeleteCommand):
"""Delete a Batch resource allowance.
This command can fail for the following reasons:
* The resource allowance specified does not exist.
* The active account does not have permission to delete the given resource
allowance.
## EXAMPLES
To delete the resource allowance with name
`projects/foo/locations/us-central1/resourceAllowances/bar`, run:
$ {command} projects/foo/locations/us-central1/resourceAllowances/bar
"""
@staticmethod
def Args(parser):
resource_args.AddResourceAllowanceResourceArgs(parser)
def Run(self, args):
release_track = self.ReleaseTrack()
client = resource_allowances.ResourceAllowancesClient(release_track)
resource_allowance_ref = args.CONCEPTS.resource_allowance.Parse()
try:
operation = client.Delete(resource_allowance_ref)
except apitools_exceptions.HttpError as e:
raise exceptions.HttpException(e, util.HTTP_ERROR_FORMAT)
log.status.Print(
'ResourceAllowance {resourceAllowanceName} deletion is in progress'
.format(resourceAllowanceName=resource_allowance_ref.RelativeName())
)
return operation

View File

@@ -0,0 +1,58 @@
# -*- 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.
"""Command to show details for a specified Batch resource allowance."""
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.batch import resource_allowances
from googlecloudsdk.api_lib.util import exceptions
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.batch import resource_args
class Describe(base.DescribeCommand):
"""Show details of a resource allowance.
This command can fail for the following reasons:
* The resource allowance specified does not exist.
* The active account does not have permission to access the given resource
allowance.
## EXAMPLES
To print details of the resource allowance with name
`projects/foo/locations/us-central1/resourceAllowances/bar`, run:
$ {command} projects/foo/locations/us-central1/resourceAllowances/bar
"""
@staticmethod
def Args(parser):
resource_args.AddResourceAllowanceResourceArgs(parser)
def Run(self, args):
release_track = self.ReleaseTrack()
client = resource_allowances.ResourceAllowancesClient(release_track)
resource_allowance_ref = args.CONCEPTS.resource_allowance.Parse()
try:
return client.Get(resource_allowance_ref)
except apitools_exceptions.HttpNotFoundError as e:
raise exceptions.HttpException(
e, error_format='Could not fetch resource: {status_message}.')

View File

@@ -0,0 +1,79 @@
# -*- 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.
"""Command to list resource allowances for a specified Batch project/location."""
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.batch import resource_allowances
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.batch import resource_args
from googlecloudsdk.core import properties
class List(base.ListCommand):
"""List resource allowances for a specified Batch project/location.
This command can fail for the following reasons:
* The project/location specified do not exist.
* The active account does not have permission to access the given
project/location.
## EXAMPLES
To print all the resource allowances under all available locations for the
default project,
run:
$ {command}
To print all the resource allowances under projects/location
`projects/foo/locations/us-central1`, run:
$ {command} --project=foo --location=us-central1
"""
@staticmethod
def Args(parser):
resource_args.AddLocationResourceArgs(parser)
base.URI_FLAG.RemoveFromParser(parser)
parser.display_info.AddFormat(
'table(name, name.segment(3):label=LOCATION,'
' usageResourceAllowance.status.state)'
)
def Run(self, args):
release_track = self.ReleaseTrack()
client = resource_allowances.ResourceAllowancesClient(release_track)
location = args.location or properties.VALUES.batch.location.Get()
project = args.project or properties.VALUES.core.project.GetOrFail()
if location:
parent = 'projects/{}/locations/{}'.format(project, location)
else:
parent = 'projects/{}/locations/{}'.format(project, '-')
return list_pager.YieldFromList(
client.service,
client.messages.BatchProjectsLocationsResourceAllowancesListRequest(
parent=parent, pageSize=args.page_size
),
batch_size=args.page_size,
field='resourceAllowances',
limit=args.limit,
batch_size_attribute='pageSize',
)

View File

@@ -0,0 +1,93 @@
# -*- 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.
"""Command to update a specified Batch resource allowance."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.batch import resource_allowances
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.batch import resource_args
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
@base.DefaultUniverseOnly
class Update(base.Command):
"""Update a Batch resource allowance.
This command updates a Batch resource allowance.
## EXAMPLES
The following command updates a resource allowance limit to 0
`projects/foo/locations/us-central1/resousrceAllowances/bar`:
$ {command} projects/foo/locations/us-central1/resousrceAllowances/bar
--usage-limit 0
"""
@staticmethod
def Args(parser):
resource_args.AddResourceAllowanceResourceArgs(parser)
parser.add_argument(
'--usage-limit',
help="""Limit value of a UsageResourceAllowance within its one
duration. Limit cannot be a negative value. Default is 0.""",
)
def Run(self, args):
update_mask = self.GenerateUpdateMask(args)
if len(update_mask) < 1:
raise exceptions.Error(
'Update commands must specify at least one additional parameter to'
' change.'
)
release_track = self.ReleaseTrack()
batch_client = resource_allowances.ResourceAllowancesClient(release_track)
resource_allowance_ref = args.CONCEPTS.resource_allowance.Parse()
batch_msgs = batch_client.messages
resource_allowance_msg = batch_msgs.ResourceAllowance()
if args.IsSpecified('usage_limit'):
setattr(
resource_allowance_msg,
'usageResourceAllowance',
batch_msgs.UsageResourceAllowance(
spec=(
batch_msgs.UsageResourceAllowanceSpec(
limit=batch_msgs.Limit(
limit=float(args.usage_limit)
)
)
)
),
)
resp = batch_client.Update(
resource_allowance_ref, resource_allowance_msg, update_mask
)
log.status.Print(
'ResourceAllowance {resourceAllowanceName} was successfully updated.'
.format(resourceAllowanceName=resp.uid)
)
return resp
def GenerateUpdateMask(self, args):
"""Create Update Mask for ResourceAllowances."""
update_mask = []
if args.IsSpecified('usage_limit'):
update_mask.append('usageResourceAllowance.spec.limit.limit')
return update_mask