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 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.
"""Commands for reading and manipulating instant snapshots."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
DETAILED_HELP = {
'brief': 'Create, list and delete Compute Engine instant snapshots',
}
@base.UniverseCompatible
@base.ReleaseTracks(base.ReleaseTrack.GA,
base.ReleaseTrack.BETA, base.ReleaseTrack.ALPHA)
class InstantSnapshots(base.Group):
"""List, create, and delete Compute Engine instant snapshots."""
InstantSnapshots.category = base.INSTANCES_CATEGORY
InstantSnapshots.detailed_help = {
'brief': 'Create, list and delete Compute Engine instant snapshots',
'DESCRIPTION': """\
List, create, describe, and delete Compute Engine instant snapshots.
See also: [Snapshots API](https://cloud.google.com/compute/docs/reference/rest/beta/instantSnapshots).
""",
}

View File

@@ -0,0 +1,81 @@
# -*- 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.
"""Command for adding labels to instant snapshots."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import instant_snapshots_utils as api_util
from googlecloudsdk.api_lib.compute.operations import poller
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags
from googlecloudsdk.command_lib.compute import labels_doc_helper
from googlecloudsdk.command_lib.compute import labels_flags
from googlecloudsdk.command_lib.compute.instant_snapshots import flags as ips_flags
from googlecloudsdk.command_lib.util.args import labels_util
@base.ReleaseTracks(base.ReleaseTrack.GA,
base.ReleaseTrack.BETA, base.ReleaseTrack.ALPHA)
class AddLabels(base.UpdateCommand):
"""add-labels command for instant snapshots."""
IPS_ARG = None
@classmethod
def Args(cls, parser):
cls.IPS_ARG = ips_flags.MakeInstantSnapshotArg()
cls.IPS_ARG.AddArgument(parser)
labels_flags.AddArgsForAddLabels(parser)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client.apitools_client
messages = holder.client.messages
ips_ref = self.IPS_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=flags.GetDefaultScopeLister(holder.client))
add_labels = labels_util.GetUpdateLabelsDictFromArgs(args)
ips_info = api_util.GetInstantSnapshotInfo(ips_ref, client, messages)
ips = ips_info.GetInstantSnapshotResource()
set_label_req = ips_info.GetSetLabelsRequestMessage()
labels_update = labels_util.Diff(additions=add_labels).Apply(
set_label_req.LabelsValue, ips.labels)
request = ips_info.GetSetInstantSnapshotLabelsRequestMessage(
ips, labels_update.GetOrNone())
if not labels_update.needs_update:
return ips
service = ips_info.GetService()
operation = service.SetLabels(request)
operation_ref = holder.resources.Parse(
operation.selfLink, collection=ips_info.GetOperationCollection())
operation_poller = poller.Poller(service)
return waiter.WaitFor(
operation_poller, operation_ref,
'Updating labels of instant snapshot [{0}]'.format(ips_ref.Name()))
AddLabels.detailed_help = (
labels_doc_helper.GenerateDetailedHelpForAddLabels('instant-snapshots'))

View File

@@ -0,0 +1,149 @@
# -*- 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.
"""Create instant snapshot command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import scope as compute_scope
from googlecloudsdk.command_lib.compute.instant_snapshots import flags as ips_flags
from googlecloudsdk.command_lib.util.args import labels_util
import six
DETAILED_HELP = {
'brief': 'Create a Compute Engine instant snapshot',
'DESCRIPTION': """\
*{command}* creates an instant snapshot of a disk. Instant snapshots are useful for
backing up the disk data.
""",
'EXAMPLES': """\
To create an instant snapshot 'my-instant-snap' from a disk 'my-disk' in zone 'us-east1-a', run:
$ {command} my-instant-snap --source-disk=my-disk --zone=us-east1-a
""",
}
def _SourceArgs(parser):
source_disk = parser.add_group('Source disk options', required=True)
ips_flags.SOURCE_DISK_ARG.AddArgument(source_disk)
def _CommonArgs(parser):
"""A helper function to build args based on different API version."""
Create.IPS_ARG = ips_flags.MakeInstantSnapshotArg()
Create.IPS_ARG.AddArgument(parser, operation_type='create')
labels_util.AddCreateLabelsFlags(parser)
parser.display_info.AddFormat(
'table(name, location(), location_scope(), status)')
_SourceArgs(parser)
@base.ReleaseTracks(base.ReleaseTrack.GA)
@base.DefaultUniverseOnly
class Create(base.Command):
"""Create a Compute Engine instant snapshot."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
@classmethod
def _GetApiHolder(cls, no_http=False):
return base_classes.ComputeApiHolder(cls.ReleaseTrack())
def _GetSourceDiskUri(self, args, compute_holder, default_scope):
source_disk_ref = ips_flags.SOURCE_DISK_ARG.ResolveAsResource(
args, compute_holder.resources)
if source_disk_ref:
return source_disk_ref.SelfLink()
return None
def _Run(self, args):
compute_holder = self._GetApiHolder()
client = compute_holder.client
messages = client.messages
ips_ref = Create.IPS_ARG.ResolveAsResource(
args, compute_holder.resources
)
requests = []
if ips_ref.Collection() == 'compute.instantSnapshots':
instant_snapshot = messages.InstantSnapshot(
name=ips_ref.Name(),
sourceDisk=self._GetSourceDiskUri(args, compute_holder,
compute_scope.ScopeEnum.ZONE))
request = messages.ComputeInstantSnapshotsInsertRequest(
instantSnapshot=instant_snapshot,
project=ips_ref.project,
zone=ips_ref.zone)
request = (client.apitools_client.instantSnapshots, 'Insert', request)
elif ips_ref.Collection() == 'compute.regionInstantSnapshots':
instant_snapshot = messages.InstantSnapshot(
name=ips_ref.Name(),
sourceDisk=self._GetSourceDiskUri(args, compute_holder,
compute_scope.ScopeEnum.REGION))
request = messages.ComputeRegionInstantSnapshotsInsertRequest(
instantSnapshot=instant_snapshot,
project=ips_ref.project,
region=ips_ref.region)
request = (client.apitools_client.regionInstantSnapshots, 'Insert',
request)
args_labels = getattr(args, 'labels', None)
if args_labels:
labels = messages.InstantSnapshot.LabelsValue(additionalProperties=[
messages.InstantSnapshot.LabelsValue.AdditionalProperty(
key=key, value=value)
for key, value in sorted(six.iteritems(args_labels))
])
instant_snapshot.labels = labels
requests.append(request)
return client.MakeRequests(requests)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class CreateBeta(Create):
"""Create a Compute Engine instant snapshot in beta."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class CreateAlpha(Create):
"""Create a Compute Engine instant snapshot in alpha."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
Create.detailed_help = DETAILED_HELP

View File

@@ -0,0 +1,145 @@
# -*- 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.
"""Delete instant snapshot command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.instant_snapshots import flags as ips_flags
def _CommonArgs(parser):
"""A helper function to build args based on different API version."""
Delete.ips_arg = ips_flags.MakeInstantSnapshotArg(plural=True)
Delete.ips_arg.AddArgument(parser, operation_type='delete')
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Delete(base.DeleteCommand):
"""Delete Compute Engine instant snapshots."""
def _GetCommonScopeNameForRefs(self, refs):
"""Gets common scope for references."""
has_zone = any(hasattr(ref, 'zone') for ref in refs)
has_region = any(hasattr(ref, 'region') for ref in refs)
if has_zone and not has_region:
return 'zone'
elif has_region and not has_zone:
return 'region'
else:
return None
def _CreateDeleteRequests(self, client, ips_refs):
"""Returns a list of delete messages for instant snapshots."""
messages = client.MESSAGES_MODULE
requests = []
for ips_ref in ips_refs:
if ips_ref.Collection() == 'compute.instantSnapshots':
service = client.instantSnapshots
request = messages.ComputeInstantSnapshotsDeleteRequest(
instantSnapshot=ips_ref.Name(),
project=ips_ref.project,
zone=ips_ref.zone)
elif ips_ref.Collection() == 'compute.regionInstantSnapshots':
service = client.regionInstantSnapshots
request = messages.ComputeRegionInstantSnapshotsDeleteRequest(
instantSnapshot=ips_ref.Name(),
project=ips_ref.project,
region=ips_ref.region)
else:
raise ValueError('Unknown reference type {0}'.format(
ips_ref.Collection()))
requests.append((service, 'Delete', request))
return requests
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def _Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
ips_refs = Delete.ips_arg.ResolveAsResource(
args,
holder.resources,
scope_lister=compute_flags.GetDefaultScopeLister(holder.client),
)
scope_name = self._GetCommonScopeNameForRefs(ips_refs)
utils.PromptForDeletion(ips_refs, scope_name=scope_name, prompt_title=None)
requests = list(
self._CreateDeleteRequests(holder.client.apitools_client, ips_refs))
return holder.client.MakeRequests(requests)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class DeleteBeta(Delete):
"""Delete Compute Engine instant snapshots in beta."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class DeleteAlpha(Delete):
"""Delete Compute Engine instant snapshots in alpha."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
Delete.detailed_help = {
'brief': 'Delete a Compute Engine instant snapshot',
'DESCRIPTION': """\
*{command}* deletes a Compute Engine instant snapshot. A disk can be
deleted only if it is not attached to any virtual machine instances.
""",
'EXAMPLES': """\
To delete Compute Engine instant snapshots with the names 'instant-snapshot-1'
and 'instant-snapshot-2', run:
$ {command} instant-snapshot-1 instant-snapshot-2
To list all instant snapshots that were created before a specific date, use
the --filter flag with the `{parent_command} list` command.
$ {parent_command} list --filter="creationTimestamp<'2017-01-01'"
For more information on how to use --filter with the list command,
run $ gcloud topic filters.
""",
}

View File

@@ -0,0 +1,95 @@
# -*- 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.
"""Command for describing instant snapshot."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.instant_snapshots import flags as ips_flags
def _CommonArgs(parser):
Describe.ips_arg = ips_flags.MakeInstantSnapshotArg(plural=False)
Describe.ips_arg.AddArgument(parser, operation_type='describe')
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Describe(base.DescribeCommand):
"""Describe a Compute Engine instant snapshot."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def _Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
messages = client.messages
ips_ref = Describe.ips_arg.ResolveAsResource(args, holder.resources)
if ips_ref.Collection() == 'compute.instantSnapshots':
service = client.apitools_client.instantSnapshots
request_type = messages.ComputeInstantSnapshotsGetRequest
elif ips_ref.Collection() == 'compute.regionInstantSnapshots':
service = client.apitools_client.regionInstantSnapshots
request_type = messages.ComputeRegionInstantSnapshotsGetRequest
return client.MakeRequests([(service, 'Get',
request_type(**ips_ref.AsDict()))])
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class DescribeBeta(Describe):
"""Describe a Compute Engine instant snapshot in beta."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class DescribeAlpha(Describe):
"""Describe a Compute Engine instant snapshot in alpha."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
Describe.detailed_help = {
'brief': 'Describe a Compute Engine instant snapshot',
'DESCRIPTION': """\
*{command}* displays all data associated with a Compute
Engine instant snapshot in a project.
""",
'EXAMPLES': """\
To describe the instant snapshot 'instant-snapshot-1' in zone 'us-east1-a', run:
$ {command} instant-snapshot-1 --zone=us-east1-a
""",
}

View File

@@ -0,0 +1,92 @@
# -*- 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.
"""List instant snapshot command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import lister
from googlecloudsdk.api_lib.compute import utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import completers
from googlecloudsdk.command_lib.compute.instant_snapshots import flags as ips_flags
def _CommonArgs(parser):
parser.display_info.AddFormat(ips_flags.MULTISCOPE_LIST_FORMAT)
parser.display_info.AddUriFunc(utils.MakeGetUriFunc())
lister.AddMultiScopeListerFlags(parser, zonal=True, regional=True)
parser.display_info.AddCacheUpdater(completers.InstantSnapshotsCompleter)
@base.ReleaseTracks(base.ReleaseTrack.GA)
class List(base.ListCommand):
"""List Compute Engine persistent instant snapshots."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def _Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
request_data = lister.ParseMultiScopeFlags(args, holder.resources)
list_implementation = lister.MultiScopeLister(
client,
zonal_service=client.apitools_client.instantSnapshots,
regional_service=client.apitools_client.regionInstantSnapshots,
aggregation_service=client.apitools_client.instantSnapshots)
return lister.Invoke(request_data, list_implementation)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class ListBeta(List):
"""List Compute Engine persistent instant snapshots in beta."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class ListAlpha(List):
"""List Compute Engine persistent instant snapshots in alpha."""
@classmethod
def Args(cls, parser):
_CommonArgs(parser)
def Run(self, args):
return self._Run(args)
List.detailed_help = base_classes.GetMultiScopeListerHelp(
'instant snapshots',
scopes=[
base_classes.ScopeType.zonal_scope,
base_classes.ScopeType.regional_scope,
],
)