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,14 @@
# -*- 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.

View File

@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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.
"""Base classes for abstracting away common logic."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import enum
from googlecloudsdk.api_lib.util import apis
API_NAME = 'recommender'
RECOMMENDER_MESSAGE_PREFIX = {
'v1': 'GoogleCloudRecommenderV1',
'v1beta1': 'GoogleCloudRecommenderV1beta1',
'v1alpha2': 'GoogleCloudRecommenderV1alpha2'
}
class EntityType(enum.Enum):
"""Cloud Entity Types."""
ORGANIZATION = 1
FOLDER = 2
PROJECT = 3
BILLING_ACCOUNT = 4
class ClientBase(object):
"""Base client class for all versions."""
def __init__(self, api_version):
self._client = apis.GetClientInstance(API_NAME, api_version)
self._api_version = api_version
self._messages = self._client.MESSAGES_MODULE
self._message_prefix = RECOMMENDER_MESSAGE_PREFIX[api_version]
def _GetMessage(self, message_name):
"""Returns the API messages class by name."""
return getattr(self._messages, message_name, None)
def _GetVersionedMessage(self, message_name):
"""Returns the versioned API messages class by name."""
return self._GetMessage('{prefix}{name}'.format(
prefix=self._message_prefix, name=message_name))

View File

@@ -0,0 +1,29 @@
# -*- 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.
"""Wrapper for user-visible error exceptions to raise in the CLI."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.core import exceptions
class RecommenderError(exceptions.Error):
"""Exceptions for Recommender errors."""
class ArgumentError(RecommenderError):
"""Command argument error."""

View File

@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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.
"""recommender API utlities."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.recommender import service as recommender_service
from googlecloudsdk.api_lib.util import messages as messages_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import yaml
RECOMMENDER_API_ALPHA_VERSION = 'v1alpha2'
RECOMMENDER_API_BETA_VERSION = 'v1beta1'
RECOMMENDER_API_GA_VERSION = 'v1'
def ToCamelCase(s):
"""Converts CamelCase to camelCase."""
return s[0].lower() + s[1:]
def ReadConfig(config_file, message_type):
"""Parses json config file.
Args:
config_file: file path of the config file.
message_type: The protorpc Message type.
Returns:
A message of type "message_type".
"""
config = None
# Yaml is a superset of json, so parse json file as yaml.
data = yaml.load_path(config_file)
if data:
config = messages_util.DictToMessageWithErrorCheck(data, message_type)
return config
def GetConfigServiceFromArgs(api_version, is_insight_api):
"""Returns the config api service from the user-specified arguments.
Args:
api_version: API version string.
is_insight_api: boolean value sepcify whether this is a insight api,
otherwise will return a recommendation service api.
"""
if is_insight_api:
return recommender_service.ProjectsInsightTypeConfigsService(api_version)
return recommender_service.ProjectsRecommenderConfigsService(api_version)
def GetDescribeConfigRequestFromArgs(parent_resource, is_insight_api,
api_version):
"""Returns the describe request from the user-specified arguments.
Args:
parent_resource: resource url string, the flags are already defined in
argparse namespace.
is_insight_api: boolean value specifying whether this is a insight api,
otherwise treat as a recommender service api and return related describe
request message.
api_version: API version string.
"""
messages = recommender_service.RecommenderMessages(api_version)
if is_insight_api:
request = messages.RecommenderProjectsLocationsInsightTypesGetConfigRequest(
name=parent_resource)
else:
request = messages.RecommenderProjectsLocationsRecommendersGetConfigRequest(
name=parent_resource)
return request
def GetApiVersion(release_track):
"""Get API version string.
Converts API version string from release track value.
Args:
release_track: release_track value, can be ALPHA, BETA, GA
Returns:
API version string.
"""
switcher = {
base.ReleaseTrack.ALPHA: RECOMMENDER_API_ALPHA_VERSION,
base.ReleaseTrack.BETA: RECOMMENDER_API_BETA_VERSION,
base.ReleaseTrack.GA: RECOMMENDER_API_GA_VERSION,
}
return switcher.get(release_track, RECOMMENDER_API_ALPHA_VERSION)

View File

@@ -0,0 +1,146 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 Insight."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import encoding
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
def CreateClient(release_track):
"""Creates Client."""
api_version = flag_utils.GetApiVersion(release_track)
return Insight(api_version)
class Insight(base.ClientBase):
"""Base Insight client for all versions."""
def __init__(self, api_version):
super(Insight, self).__init__(api_version)
self._service = self._client.projects_locations_insightTypes_insights
def _CreateMarkRequest(self, name, state, state_metadata, etag):
"""Creates MarkRequest with the specified state."""
# Need to do it this way to dynamically set the versioned MarkRequest
request_name = 'MarkInsight{}Request'.format(state)
mark_request = self._GetVersionedMessage(request_name)(etag=etag)
if state_metadata:
metadata = encoding.DictToAdditionalPropertyMessage(
state_metadata,
self._GetVersionedMessage(request_name).StateMetadataValue,
sort_items=True)
mark_request.stateMetadata = metadata
# Need to do it this way to dynamically set the versioned MarkRequest
kwargs = {
'name':
name,
flag_utils.ToCamelCase(self._message_prefix + request_name):
mark_request
}
# Using Project message is ok for all entities if the name is correct.
return self._GetMessage(
'RecommenderProjectsLocationsInsightTypesInsightsMark{}Request'.format(
state))(**kwargs)
def Get(self, name):
"""Gets an Insight.
Args:
name: str, the name of the insight being retrieved.
Returns:
The Insight message.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsInsightTypesInsightsGetRequest(
name=name)
return self._service.Get(request)
def List(self, parent_name, page_size, limit=None, request_filter=None):
"""List Insights.
Args:
parent_name: str, the name of the parent.
page_size: int, The number of items to retrieve per request.
limit: int, The maximum number of records to yield.
request_filter: str, Optional request filter
Returns:
The Insight messages.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsInsightTypesInsightsListRequest(
parent=parent_name, filter=request_filter
)
return list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='insights')
def MarkAccepted(self, name, state_metadata, etag):
"""Mark an insight's state as ACCEPTED.
Args:
name: str, the name of the insight being updated.
state_metadata: A map of metadata for the state, provided by user or
automations systems.
etag: Fingerprint of the Insight. Provides optimistic locking when
updating states.
Returns:
The result insights after being marked as accepted
"""
request = self._CreateMarkRequest(name, 'Accepted', state_metadata, etag)
return self._service.MarkAccepted(request)
def MarkActive(self, name, etag):
"""Mark an insight's state as ACTIVE.
Args:
name: str, the name of the insight being updated.
etag: Fingerprint of the Insight. Provides optimistic locking when
updating states.
Returns:
The result insights after being marked as active
"""
request = self._CreateMarkRequest(name, 'Active', None, etag)
return self._service.MarkActive(request)
def MarkDismissed(self, name, etag):
"""Mark an insight's state as DISMISSED.
Args:
name: str, the name of the insight being updated.
etag: Fingerprint of the Insight. Provides optimistic locking when
updating states.
Returns:
The result insights after being marked as dismissed
"""
request = self._CreateMarkRequest(name, 'Dismissed', None, etag)
return self._service.MarkDismissed(request)

View File

@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 InsightType Config."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import encoding
from googlecloudsdk.api_lib.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
def CreateClient(release_track):
"""Creates Client."""
api_version = flag_utils.GetApiVersion(release_track)
return InsightTypeConfig(api_version)
class InsightTypeConfig(base.ClientBase):
"""Base InsightTypeConfig client for all versions."""
def __init__(self, api_version):
super(InsightTypeConfig, self).__init__(api_version)
self._service = self._client.projects_locations_insightTypes
def Get(self, config_name):
"""Gets a InsightTypeConfig.
Args:
config_name: str, the name of the config being retrieved.
Returns:
The InsightTypeConfig message.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsInsightTypesGetConfigRequest(
name=config_name)
return self._service.GetConfig(request)
def Update(self, config_name, args):
"""Updates a InsightTypeConfig.
Args:
config_name: str, the name of the config being retrieved.
args: argparse.Namespace, The arguments that the command was invoked with.
Returns:
The updated InsightTypeConfig message.
Raises:
Exception: If nothing is updated.
"""
update_mask = []
config = self._GetVersionedMessage('InsightTypeConfig')()
config.name = config_name
config.etag = args.etag
if args.config_file:
gen_config = flag_utils.ReadConfig(
args.config_file,
self._GetVersionedMessage('InsightTypeGenerationConfig'))
config.insightTypeGenerationConfig = gen_config
update_mask.append('insight_type_generation_config')
if args.display_name:
config.displayName = args.display_name
update_mask.append('display_name')
if args.annotations:
config.annotations = encoding.DictToAdditionalPropertyMessage(
args.annotations,
self._GetVersionedMessage('InsightTypeConfig').AnnotationsValue,
sort_items=True)
update_mask.append('annotations')
if not update_mask:
raise Exception(
'Nothing is being updated. Please specify one of config-file or display-name.'
)
# Need to do it this way to dynamically set the versioned InsightTypeConfig
kwargs = {
'name':
config_name,
flag_utils.ToCamelCase(self._message_prefix + 'InsightTypeConfig'):
config,
'updateMask':
','.join(update_mask),
'validateOnly':
args.validate_only
}
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsInsightTypesUpdateConfigRequest(
**kwargs)
return self._service.UpdateConfig(request)

View File

@@ -0,0 +1,64 @@
# -*- 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.
"""Utilities for Recommendation."""
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.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
def CreateClient(release_track):
"""Creates Client.
Args:
release_track: release_track value, can be ALPHA, BETA, GA
Returns:
The versioned client.
"""
api_version = flag_utils.GetApiVersion(release_track)
return InsightTypes(api_version)
class InsightTypes(base.ClientBase):
"""Base client to list Insight Types for all versions."""
def __init__(self, api_version):
super(InsightTypes, self).__init__(api_version)
self._service = self._client.insightTypes
def List(self, page_size, limit=None):
"""List Insight Types.
Args:
page_size: int, The number of items to retrieve per request.
limit: int, The maximum number of records to yield.
Returns:
The list of insight types.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderInsightTypesListRequest()
return list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='insightTypes')

View File

@@ -0,0 +1,122 @@
# -*- 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.
"""Utilities for Locations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import itertools
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
from googlecloudsdk.command_lib.projects import util
def CreateClient(release_track):
"""Creates Client.
Args:
release_track: release_track value, can be ALPHA, BETA, GA
Returns:
The versioned client.
"""
api_version = flag_utils.GetApiVersion(release_track)
return Location(api_version)
class Location(base.ClientBase):
"""Base Location client for all versions."""
def List(self, page_size, project=None, folder=None,
organization=None, billing_account=None, limit=None):
"""List Locations.
Args:
page_size: int, The number of items to retrieve per request.
project: string, The project name to retrieve locations for.
folder: string, The folder name to retrieve locations for.
organization: string, The organization name to retrieve locations for.
billing_account: string, The billing project to retrieve locations for.
limit: int, The maximum number of records to yield.
Returns:
The list of Locations.
"""
# Using Project message is ok for all entities if the name is correct.
folder_locations, organization_locations, project_locations = [], [], []
billing_account_locations = []
if folder:
self._service = self._client.folders_locations
request = self._messages.RecommenderFoldersLocationsListRequest(
name='folders/' + folder
)
folder_locations = list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='locations',
)
if organization:
self._service = self._client.organizations_locations
request = self._messages.RecommenderOrganizationsLocationsListRequest(
name='organizations/' + organization
)
organization_locations = list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='locations',
)
if project:
self._service = self._client.projects_locations
request = self._messages.RecommenderProjectsLocationsListRequest(
name='projects/' + str(util.GetProjectNumber(project))
)
project_locations = list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='locations',
)
if billing_account:
self._service = self._client.billingAccounts_locations
request = self._messages.RecommenderBillingAccountsLocationsListRequest(
name='billing-accounts/' + billing_account
)
billing_account_locations = list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='locations',
)
return itertools.chain(
folder_locations,
organization_locations,
project_locations,
billing_account_locations,
)

View File

@@ -0,0 +1,183 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 Recommendation."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import encoding
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
def CreateClient(release_track):
"""Creates Client.
Args:
release_track: release_track value, can be ALPHA, BETA, GA
Returns:
The versioned client.
"""
api_version = flag_utils.GetApiVersion(release_track)
return Recommendation(api_version)
class Recommendation(base.ClientBase):
"""Base Recommendation client for all versions."""
def __init__(self, api_version):
super(Recommendation, self).__init__(api_version)
self._service = self._client.projects_locations_recommenders_recommendations
def _CreateMarkRequest(self, name, state, state_metadata, etag):
"""Creates MarkRequest with the specified state."""
# Need to do it this way to dynamically set the versioned MarkRequest
request_name = 'MarkRecommendation{}Request'.format(state)
mark_request = self._GetVersionedMessage(request_name)(etag=etag)
if state_metadata:
metadata = encoding.DictToAdditionalPropertyMessage(
state_metadata,
self._GetVersionedMessage(request_name).StateMetadataValue,
sort_items=True)
mark_request.stateMetadata = metadata
# Need to do it this way to dynamically set the versioned MarkRequest
kwargs = {
'name':
name,
flag_utils.ToCamelCase(self._message_prefix + request_name):
mark_request
}
# Using Project message is ok for all entities if the name is correct.
return self._GetMessage(
'RecommenderProjectsLocationsRecommendersRecommendationsMark{}Request'
.format(state))(**kwargs)
def Get(self, name):
"""Gets a Recommendation.
Args:
name: str, the name of the recommendation being retrieved.
Returns:
The Recommendation message.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsRecommendersRecommendationsGetRequest(
name=name)
return self._service.Get(request)
def List(self, parent_name, page_size, limit=None):
"""List Recommendations.
Args:
parent_name: str, the name of the parent.
page_size: int, The number of items to retrieve per request.
limit: int, The maximum number of records to yield.
Returns:
The Recommendation messages.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsRecommendersRecommendationsListRequest(
parent=parent_name)
return list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='recommendations')
def MarkActive(self, name, etag):
"""Mark a recommendation's state as ACTIVE.
Args:
name: str, the name of the recommendation being updated.
etag: Fingerprint of the Recommendation. Provides optimistic locking when
updating states.
Returns:
The result recommendations after being marked as active
"""
request = self._CreateMarkRequest(name, 'Active', None, etag)
return self._service.MarkActive(request)
def MarkDismissed(self, name, etag):
"""Mark a recommendation's state as DISMISSED.
Args:
name: str, the name of the recommendation being updated.
etag: Fingerprint of the Recommendation. Provides optimistic locking when
updating states.
Returns:
The result recommendations after being marked as dismissed
"""
request = self._CreateMarkRequest(name, 'Dismissed', None, etag)
return self._service.MarkDismissed(request)
def MarkClaimed(self, name, state_metadata, etag):
"""Mark a recommendation's state as CLAIMED.
Args:
name: str, the name of the recommendation being updated.
state_metadata: A map of metadata for the state, provided by user or
automations systems.
etag: Fingerprint of the Recommendation. Provides optimistic locking when
updating states.
Returns:
The result recommendations after being marked as accepted
"""
request = self._CreateMarkRequest(name, 'Claimed', state_metadata, etag)
return self._service.MarkClaimed(request)
def MarkSucceeded(self, name, state_metadata, etag):
"""Mark a recommendation's state as SUCCEEDED.
Args:
name: str, the name of the recommendation being updated.
state_metadata: A map of metadata for the state, provided by user or
automations systems.
etag: Fingerprint of the Recommendation. Provides optimistic locking when
updating states.
Returns:
The result recommendations after being marked as accepted
"""
request = self._CreateMarkRequest(name, 'Succeeded', state_metadata, etag)
return self._service.MarkSucceeded(request)
def MarkFailed(self, name, state_metadata, etag):
"""Mark a recommendation's state as FAILED.
Args:
name: str, the name of the recommendation being updated.
state_metadata: A map of metadata for the state, provided by user or
automations systems.
etag: Fingerprint of the Recommendation. Provides optimistic locking when
updating states.
Returns:
The result recommendations after being marked as accepted
"""
request = self._CreateMarkRequest(name, 'Failed', state_metadata, etag)
return self._service.MarkFailed(request)

View File

@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 Recommender Config."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import encoding
from googlecloudsdk.api_lib.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
def CreateClient(release_track):
"""Creates Client."""
api_version = flag_utils.GetApiVersion(release_track)
return RecommenderConfig(api_version)
class RecommenderConfig(base.ClientBase):
"""Base RecommenderConfig client for all versions."""
def __init__(self, api_version):
super(RecommenderConfig, self).__init__(api_version)
self._project_service = self._client.projects_locations_recommenders
def Get(self, config_name):
"""Gets a RecommenderConfig.
Args:
config_name: str, the name of the config being retrieved.
Returns:
The RecommenderConfig message.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsRecommendersGetConfigRequest(
name=config_name)
return self._project_service.GetConfig(request)
def Update(self, config_name, args):
"""Updates a RecommenderConfig.
Args:
config_name: str, the name of the config being retrieved.
args: argparse.Namespace, The arguments that the command was invoked with.
Returns:
The updated RecommenderConfig message.
Raises:
Exception: If nothing is updated.
"""
update_mask = []
config = self._GetVersionedMessage('RecommenderConfig')()
config.name = config_name
config.etag = args.etag
if args.config_file:
gen_config = flag_utils.ReadConfig(
args.config_file,
self._GetVersionedMessage('RecommenderGenerationConfig'))
config.recommenderGenerationConfig = gen_config
update_mask.append('recommender_generation_config')
if args.display_name:
config.displayName = args.display_name
update_mask.append('display_name')
if args.annotations:
config.annotations = encoding.DictToAdditionalPropertyMessage(
args.annotations,
self._GetVersionedMessage('RecommenderConfig').AnnotationsValue,
sort_items=True)
update_mask.append('annotations')
if not update_mask:
raise Exception(
'Nothing is being updated. Please specify one of config-file or display-name.'
)
# Need to do it this way to dynamically set the versioned RecommenderConfig
kwargs = {
'name':
config_name,
flag_utils.ToCamelCase(self._message_prefix + 'RecommenderConfig'):
config,
'updateMask':
','.join(update_mask),
'validateOnly':
args.validate_only
}
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderProjectsLocationsRecommendersUpdateConfigRequest(
**kwargs)
return self._project_service.UpdateConfig(request)

View File

@@ -0,0 +1,65 @@
# -*- 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.
"""Utilities for Recommender."""
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.recommender import base
from googlecloudsdk.api_lib.recommender import flag_utils
def CreateClient(release_track):
"""Creates Client.
Args:
release_track: release_track value, can be ALPHA, BETA, GA
Returns:
The versioned client.
"""
api_version = flag_utils.GetApiVersion(release_track)
return Recommender(api_version)
class Recommender(base.ClientBase):
"""Base Recommendation client for all versions."""
def __init__(self, api_version):
super(Recommender, self).__init__(api_version)
self._service = self._client.recommenders
def List(self, page_size, limit=None):
"""List Recommenders.
Args:
page_size: int, The number of items to retrieve per request.
limit: int, The maximum number of records to yield.
Returns:
The list of Recommenders.
"""
# Using Project message is ok for all entities if the name is correct.
request = self._messages.RecommenderRecommendersListRequest()
return list_pager.YieldFromList(
self._service,
request,
batch_size_attribute='pageSize',
batch_size=page_size,
limit=limit,
field='recommenders',
)

View File

@@ -0,0 +1,92 @@
# -*- 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.
"""recommender API recommendations service."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.util import apis
RECOMMENDER_API_NAME = 'recommender'
def RecommenderClient(api_version):
return apis.GetClientInstance(RECOMMENDER_API_NAME, api_version)
def RecommenderMessages(api_version):
"""Returns the messages module for the Resource Settings service."""
return apis.GetMessagesModule(RECOMMENDER_API_NAME, api_version)
def BillingAccountsRecommenderRecommendationsService(api_version):
"""Returns the service class for the Billing Account recommendations."""
client = RecommenderClient(api_version)
return client.billingAccounts_locations_recommenders_recommendations
def ProjectsRecommenderRecommendationsService(api_version):
"""Returns the service class for the Project recommendations."""
client = RecommenderClient(api_version)
return client.projects_locations_recommenders_recommendations
def ProjectsRecommenderConfigsService(api_version):
"""Returns the service class for the Project recommender configs."""
client = RecommenderClient(api_version)
return client.projects_locations_recommenders
def ProjectsInsightTypeConfigsService(api_version):
"""Returns the service class for the Project insight type configs."""
client = RecommenderClient(api_version)
return client.projects_locations_insightTypes
def FoldersRecommenderRecommendationsService(api_version):
"""Returns the service class for the Folders recommendations."""
client = RecommenderClient(api_version)
return client.folders_locations_recommenders_recommendations
def OrganizationsRecommenderRecommendationsService(api_version):
"""Returns the service class for the Organization recommendations."""
client = RecommenderClient(api_version)
return client.organizations_locations_recommenders_recommendations
def BillingAccountsInsightTypeInsightsService(api_version):
"""Returns the service class for the Billing Account insights."""
client = RecommenderClient(api_version)
return client.billingAccounts_locations_insightTypes_insights
def ProjectsInsightTypeInsightsService(api_version):
"""Returns the service class for the Project insights."""
client = RecommenderClient(api_version)
return client.projects_locations_insightTypes_insights
def FoldersInsightTypeInsightsService(api_version):
"""Returns the service class for the Folders insights."""
client = RecommenderClient(api_version)
return client.folders_locations_insightTypes_insights
def OrganizationsInsightTypeInsightsService(api_version):
"""Returns the service class for the Organization insights."""
client = RecommenderClient(api_version)
return client.organizations_locations_insightTypes_insights