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,26 @@
# -*- 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.
"""The command group for os-config guest-policies CLI."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class OsconfigGuestPolicies(base.Group):
"""Manage guest OS policies for Compute Engine VM instances."""

View File

@@ -0,0 +1,80 @@
# -*- 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.
"""Implements command to create a new guest policy."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute.os_config import utils as osconfig_api_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.os_config import utils as osconfig_command_utils
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class Create(base.Command):
r"""Create a guest policy for a project.
## EXAMPLES
To create a guest policy `policy1` in the current project, run:
$ {command} policy1 --file=path_to_config_file
"""
@staticmethod
def Args(parser):
"""See base class."""
parser.add_argument(
'POLICY_ID',
type=str,
help="""\
Name of the guest policy to create.
This name must contain only lowercase letters, numbers, and hyphens,
start with a letter, end with a number or a letter, be between 1-63
characters, and unique within the project.""",
)
parser.add_argument(
'--file',
required=True,
help="""\
The JSON or YAML file with the guest policy to create. For information
about the guest policy format, see https://cloud.google.com/compute/docs/osconfig/rest/v1beta/projects.guestPolicies.""",
)
def Run(self, args):
"""See base class."""
release_track = self.ReleaseTrack()
client = osconfig_api_utils.GetClientInstance(release_track)
messages = osconfig_api_utils.GetClientMessages(release_track)
(guest_policy,
_) = osconfig_command_utils.GetResourceAndUpdateFieldsFromFile(
args.file, messages.GuestPolicy)
project = properties.VALUES.core.project.GetOrFail()
parent_path = osconfig_command_utils.GetProjectUriPath(project)
request = messages.OsconfigProjectsGuestPoliciesCreateRequest(
guestPolicy=guest_policy,
guestPolicyId=args.POLICY_ID,
parent=parent_path,
)
service = client.projects_guestPolicies
return service.Create(request)

View File

@@ -0,0 +1,61 @@
# -*- 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.
"""Implements command to delete a specified guest policy."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute.os_config import utils as osconfig_api_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.os_config import utils as osconfig_command_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class Delete(base.DeleteCommand):
"""Delete the specified guest policy.
## EXAMPLES
To delete the guest policy named `policy1` in the current project, run:
$ {command} policy1
"""
@staticmethod
def Args(parser):
"""See base class."""
parser.add_argument(
'POLICY_ID', type=str, help='Name of the guest policy to delete.')
def Run(self, args):
"""See base class."""
release_track = self.ReleaseTrack()
client = osconfig_api_utils.GetClientInstance(release_track)
messages = osconfig_api_utils.GetClientMessages(release_track)
project = properties.VALUES.core.project.GetOrFail()
guest_policy_name = osconfig_command_utils.GetGuestPolicyUriPath(
'projects', project, args.POLICY_ID)
request = messages.OsconfigProjectsGuestPoliciesDeleteRequest(
name=guest_policy_name)
service = client.projects_guestPolicies
response = service.Delete(request)
log.DeletedResource(guest_policy_name)
return response

View File

@@ -0,0 +1,57 @@
# -*- 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.
"""Implements command to describe a specified guest policy."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute.os_config import utils as osconfig_api_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.os_config import utils as osconfig_command_utils
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class Describe(base.DescribeCommand):
"""Describe the specified guest policy.
## EXAMPLES
To describe the guest policy `policy1` in the current project, run:
$ {command} policy1
"""
@staticmethod
def Args(parser):
"""See base class."""
parser.add_argument(
'POLICY_ID', type=str, help='Name of the guest policy to describe.')
def Run(self, args):
"""See base class."""
release_track = self.ReleaseTrack()
client = osconfig_api_utils.GetClientInstance(release_track)
messages = osconfig_api_utils.GetClientMessages(release_track)
project = properties.VALUES.core.project.GetOrFail()
request = messages.OsconfigProjectsGuestPoliciesGetRequest(
name=osconfig_command_utils.GetGuestPolicyUriPath(
'projects', project, args.POLICY_ID))
service = client.projects_guestPolicies
return service.Get(request)

View File

@@ -0,0 +1,104 @@
# -*- 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.
"""Implements command to list guest policies."""
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.compute.os_config import utils as osconfig_api_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.os_config import utils as osconfig_command_utils
from googlecloudsdk.core import properties
def _TransformGuestPolicyDescription(resource):
"""Returns a length-limited guest policy description."""
max_len = 30 # Show only the first 30 characters if description is long.
description = resource.get('description', '')
return (description[:max_len] +
'..') if len(description) > max_len else description
def _MakeGetUriFunc(registry):
"""Returns a transformation function from a guest policy resource to URI."""
def UriFunc(resource):
parent_type = resource.name.split('/')[0]
ref = registry.Parse(
resource.name,
collection='osconfig.{}.guestPolicies'.format(parent_type))
return ref.SelfLink()
return UriFunc
def _Args(parser, release_track):
"""Parses input flags and sets up output formats."""
parser.display_info.AddFormat("""
table(
name.basename(),
description(),
create_time,
update_time
)
""")
parser.display_info.AddTransforms(
{'description': _TransformGuestPolicyDescription})
registry = osconfig_api_utils.GetRegistry(release_track)
parser.display_info.AddUriFunc(_MakeGetUriFunc(registry))
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class List(base.ListCommand):
"""List guest policies in a project.
## EXAMPLES
To list guest policies in the current project, run:
$ {command}
"""
@staticmethod
def Args(parser):
"""See base class."""
_Args(parser, base.ReleaseTrack.BETA)
def Run(self, args):
"""See base class."""
release_track = self.ReleaseTrack()
client = osconfig_api_utils.GetClientInstance(release_track)
messages = osconfig_api_utils.GetClientMessages(release_track)
project = properties.VALUES.core.project.GetOrFail()
request = messages.OsconfigProjectsGuestPoliciesListRequest(
pageSize=args.page_size,
parent=osconfig_command_utils.GetProjectUriPath(project),
)
service = client.projects_guestPolicies
return list_pager.YieldFromList(
service,
request,
limit=args.limit,
batch_size=osconfig_command_utils.GetListBatchSize(args),
field='guestPolicies',
batch_size_attribute='pageSize',
)

View File

@@ -0,0 +1,165 @@
# -*- 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.
"""Implements command to look up all effective guest policies of an instance."""
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.os_config import utils as osconfig_api_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.instances import flags
from googlecloudsdk.core import log
from googlecloudsdk.core.resource import resource_projector
import six
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class Lookup(base.Command):
"""Display the guest policies that are applied to an instance.
## EXAMPLES
To view all guest policies that are applied to an instance named
`my-instance`, run:
$ {command} my-instance
"""
_OS_ARCHITECTURE_KEY = 'Architecture'
_OS_SHORTNAME_KEY = 'ShortName'
_OS_VERSION_KEY = 'Version'
_OS_INFO_FIELD_KEYS = (_OS_ARCHITECTURE_KEY, _OS_SHORTNAME_KEY,
_OS_VERSION_KEY)
def _GetInstanceRef(self, holder, args):
return flags.INSTANCE_ARG.ResolveAsResource(
args,
holder.resources,
scope_lister=flags.GetInstanceZoneScopeLister(holder.client),
)
def _GetGuestInventoryGuestAttributes(self, instance_ref):
try:
holder = base_classes.ComputeApiHolder(base.ReleaseTrack.GA)
client = holder.client
messages = client.messages
request = messages.ComputeInstancesGetGuestAttributesRequest(
instance=instance_ref.Name(),
project=instance_ref.project,
queryPath='guestInventory/',
zone=instance_ref.zone)
response = client.apitools_client.instances.GetGuestAttributes(request)
return response.queryValue.items
except Exception as e:
if ('The resource \'guestInventory/\' of type \'Guest Attribute\' was not'
' found.') in six.text_type(e):
return []
raise e
def _GetOsInfo(self, guest_attributes):
guest_attributes_json = resource_projector.MakeSerializable(
guest_attributes)
os_info = {}
for guest_attribute in guest_attributes_json:
guest_attribute_key = guest_attribute['key']
if guest_attribute_key in self._OS_INFO_FIELD_KEYS:
os_info[guest_attribute_key] = guest_attribute['value']
return os_info
def _CreateRequest(self, messages, instance_name, os_architecture,
os_shortname, os_version):
return messages.OsconfigProjectsZonesInstancesLookupEffectiveGuestPolicyRequest(
instance=instance_name,
lookupEffectiveGuestPolicyRequest=messages
.LookupEffectiveGuestPolicyRequest(
osArchitecture=os_architecture,
osShortName=os_shortname,
osVersion=os_version,
),
)
def _GetResponse(self, service, request):
return service.LookupEffectiveGuestPolicy(request)
@staticmethod
def Args(parser):
"""See base class."""
flags.INSTANCE_ARG.AddArgument(
parser, operation_type='look up guest policies for')
parser.display_info.AddFormat("""
table(
packages:format="table[box,title="PACKAGES"](
source,
package.name,
package.desiredState,
package.manager,
package.version)",
packageRepositories:format="table[box,title='PACKAGE REPOSITORIES'](
source,
packageRepository.apt:format='table[box,title="APT"](
uri,
distribution,
components.list())',
packageRepository.goo:format='table[box,title="GOO"](
name,
url)',
packageRepository.yum:format='table[box,title="YUM"](
id,
baseUrl)',
packageRepository.zypper:format='table[box,title="ZYPPER"](
id,
baseUrl)')",
softwareRecipes:format="table[box,title='SOFTWARE RECIPES'](
source,
softwareRecipe.name,
softwareRecipe.version,
softwareRecipe.desiredState
)"
)
""")
def Run(self, args):
"""See base class."""
release_track = self.ReleaseTrack()
holder = base_classes.ComputeApiHolder(release_track)
instance_ref = self._GetInstanceRef(holder, args)
guest_attributes = self._GetGuestInventoryGuestAttributes(instance_ref)
os_info = self._GetOsInfo(guest_attributes)
os_architecture = os_info.get(self._OS_ARCHITECTURE_KEY)
os_shortname = os_info.get(self._OS_SHORTNAME_KEY)
os_version = os_info.get(self._OS_VERSION_KEY)
client = osconfig_api_utils.GetClientInstance(release_track)
messages = osconfig_api_utils.GetClientMessages(release_track)
request = self._CreateRequest(messages, instance_ref.RelativeName(),
os_architecture, os_shortname, os_version)
response = self._GetResponse(client.projects_zones_instances, request)
if not any([
response.packages, response.packageRepositories,
response.softwareRecipes
]):
log.status.Print('No effective guest policy found for [{}].'.format(
instance_ref.RelativeName()))
return response

View File

@@ -0,0 +1,78 @@
# -*- 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.
"""Implements command to update a specified guest policy."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute.os_config import utils as osconfig_api_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.os_config import utils as osconfig_command_utils
from googlecloudsdk.core import properties
@base.ReleaseTracks(base.ReleaseTrack.BETA)
class Update(base.Command):
r"""Update the specified guest policy for a project.
## EXAMPLES
To update the guest policy `policy1` in the project `project1`, run:
$ {command} policy1 \
--file=path_to_config_file --project=project1
"""
@staticmethod
def Args(parser):
"""See base class."""
parser.add_argument(
'POLICY_ID', type=str, help='Name of the guest policy to update.')
parser.add_argument(
'--file',
required=True,
help="""\
The JSON or YAML file with the updated guest policy.
If this file specifies an etag value, the update succeeds only if
the policy that is already in place has a matching etag value. If no
etag value is specified, the specifications in the updated policy file
replaces the existing policy.
For information about the guest policy format, see https://cloud.google.com/compute/docs/osconfig/rest/v1beta/projects.guestPolicies.
""")
def Run(self, args):
"""See base class."""
release_track = self.ReleaseTrack()
client = osconfig_api_utils.GetClientInstance(release_track)
messages = osconfig_api_utils.GetClientMessages(release_track)
(guest_policy,
_) = osconfig_command_utils.GetResourceAndUpdateFieldsFromFile(
args.file, messages.GuestPolicy)
project = properties.VALUES.core.project.GetOrFail()
request = messages.OsconfigProjectsGuestPoliciesPatchRequest(
guestPolicy=guest_policy,
name=osconfig_command_utils.GetGuestPolicyUriPath(
'projects', project, args.POLICY_ID),
updateMask=None,
)
service = client.projects_guestPolicies
return service.Patch(request)