This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
/
recommendation.py
353 lines (284 loc) · 12.8 KB
/
recommendation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# 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.
#
import proto # type: ignore
from google.protobuf import duration_pb2 as gp_duration # type: ignore
from google.protobuf import struct_pb2 as struct # type: ignore
from google.protobuf import timestamp_pb2 as timestamp # type: ignore
from google.type import money_pb2 as money # type: ignore
__protobuf__ = proto.module(
package="google.cloud.recommender.v1beta1",
manifest={
"Recommendation",
"RecommendationContent",
"OperationGroup",
"Operation",
"ValueMatcher",
"CostProjection",
"Impact",
"RecommendationStateInfo",
},
)
class Recommendation(proto.Message):
r"""A recommendation along with a suggested action. E.g., a
rightsizing recommendation for an underutilized VM, IAM role
recommendations, etc
Attributes:
name (str):
Name of recommendation.
description (str):
Free-form human readable summary in English.
The maximum length is 500 characters.
recommender_subtype (str):
Contains an identifier for a subtype of recommendations
produced for the same recommender. Subtype is a function of
content and impact, meaning a new subtype might be added
when significant changes to ``content`` or
``primary_impact.category`` are introduced. See the
Recommenders section to see a list of subtypes for a given
Recommender.
Examples: For recommender = "google.iam.policy.Recommender",
recommender_subtype can be one of
"REMOVE_ROLE"/"REPLACE_ROLE".
last_refresh_time (~.timestamp.Timestamp):
Last time this recommendation was refreshed
by the system that created it in the first
place.
primary_impact (~.recommendation.Impact):
The primary impact that this recommendation
can have while trying to optimize for one
category.
additional_impact (Sequence[~.recommendation.Impact]):
Optional set of additional impact that this
recommendation may have when trying to optimize
for the primary category. These may be positive
or negative.
content (~.recommendation.RecommendationContent):
Content of the recommendation describing
recommended changes to resources.
state_info (~.recommendation.RecommendationStateInfo):
Information for state. Contains state and
metadata.
etag (str):
Fingerprint of the Recommendation. Provides
optimistic locking when updating states.
associated_insights (Sequence[~.recommendation.Recommendation.InsightReference]):
Insights that led to this recommendation.
"""
class InsightReference(proto.Message):
r"""Reference to an associated insight.
Attributes:
insight (str):
Insight resource name, e.g.
projects/[PROJECT_NUMBER]/locations/[LOCATION]/insightTypes/[INSIGHT_TYPE_ID]/insights/[INSIGHT_ID]
"""
insight = proto.Field(proto.STRING, number=1)
name = proto.Field(proto.STRING, number=1)
description = proto.Field(proto.STRING, number=2)
recommender_subtype = proto.Field(proto.STRING, number=12)
last_refresh_time = proto.Field(
proto.MESSAGE, number=4, message=timestamp.Timestamp,
)
primary_impact = proto.Field(proto.MESSAGE, number=5, message="Impact",)
additional_impact = proto.RepeatedField(proto.MESSAGE, number=6, message="Impact",)
content = proto.Field(proto.MESSAGE, number=7, message="RecommendationContent",)
state_info = proto.Field(
proto.MESSAGE, number=10, message="RecommendationStateInfo",
)
etag = proto.Field(proto.STRING, number=11)
associated_insights = proto.RepeatedField(
proto.MESSAGE, number=14, message=InsightReference,
)
class RecommendationContent(proto.Message):
r"""Contains what resources are changing and how they are
changing.
Attributes:
operation_groups (Sequence[~.recommendation.OperationGroup]):
Operations to one or more Google Cloud
resources grouped in such a way that, all
operations within one group are expected to be
performed atomically and in an order.
"""
operation_groups = proto.RepeatedField(
proto.MESSAGE, number=2, message="OperationGroup",
)
class OperationGroup(proto.Message):
r"""Group of operations that need to be performed atomically.
Attributes:
operations (Sequence[~.recommendation.Operation]):
List of operations across one or more
resources that belong to this group. Loosely
based on RFC6902 and should be performed in the
order they appear.
"""
operations = proto.RepeatedField(proto.MESSAGE, number=1, message="Operation",)
class Operation(proto.Message):
r"""Contains an operation for a resource loosely based on the JSON-PATCH
format with support for:
- Custom filters for describing partial array patch.
- Extended path values for describing nested arrays.
- Custom fields for describing the resource for which the operation
is being described.
- Allows extension to custom operations not natively supported by
RFC6902. See https://tools.ietf.org/html/rfc6902 for details on
the original RFC.
Attributes:
action (str):
Type of this operation. Contains one of
'and', 'remove', 'replace', 'move', 'copy',
'test' and 'custom' operations. This field is
case-insensitive and always populated.
resource_type (str):
Type of GCP resource being modified/tested.
This field is always populated. Example:
cloudresourcemanager.googleapis.com/Project,
compute.googleapis.com/Instance
resource (str):
Contains the fully qualified resource name.
This field is always populated. ex:
//cloudresourcemanager.googleapis.com/projects/foo.
path (str):
Path to the target field being operated on.
If the operation is at the resource level, then
path should be "/". This field is always
populated.
source_resource (str):
Can be set with action 'copy' to copy resource configuration
across different resources of the same type. Example: A
resource clone can be done via action = 'copy', path = "/",
from = "/", source_resource = and resource_name = . This
field is empty for all other values of ``action``.
source_path (str):
Can be set with action 'copy' or 'move' to indicate the
source field within resource or source_resource, ignored if
provided for other operation types.
value (~.struct.Value):
Value for the ``path`` field. Will be set for
actions:'add'/'replace'. Maybe set for action: 'test'.
Either this or ``value_matcher`` will be set for 'test'
operation. An exact match must be performed.
value_matcher (~.recommendation.ValueMatcher):
Can be set for action 'test' for advanced matching for the
value of 'path' field. Either this or ``value`` will be set
for 'test' operation.
path_filters (Sequence[~.recommendation.Operation.PathFiltersEntry]):
Set of filters to apply if ``path`` refers to array elements
or nested array elements in order to narrow down to a single
unique element that is being tested/modified. This is
intended to be an exact match per filter. To perform
advanced matching, use path_value_matchers.
- Example: ``{ "/versions/*/name" : "it-123"
"/versions/*/targetSize/percent": 20 }``
- Example: ``{ "/bindings/*/role": "roles/owner"
"/bindings/*/condition" : null }``
- Example: ``{ "/bindings/*/role": "roles/owner"
"/bindings/*/members/*" : ["x@example.com",
"y@example.com"] }`` When both path_filters and
path_value_matchers are set, an implicit AND must be
performed.
path_value_matchers (Sequence[~.recommendation.Operation.PathValueMatchersEntry]):
Similar to path_filters, this contains set of filters to
apply if ``path`` field referes to array elements. This is
meant to support value matching beyond exact match. To
perform exact match, use path_filters. When both
path_filters and path_value_matchers are set, an implicit
AND must be performed.
"""
action = proto.Field(proto.STRING, number=1)
resource_type = proto.Field(proto.STRING, number=2)
resource = proto.Field(proto.STRING, number=3)
path = proto.Field(proto.STRING, number=4)
source_resource = proto.Field(proto.STRING, number=5)
source_path = proto.Field(proto.STRING, number=6)
value = proto.Field(
proto.MESSAGE, number=7, oneof="path_value", message=struct.Value,
)
value_matcher = proto.Field(
proto.MESSAGE, number=10, oneof="path_value", message="ValueMatcher",
)
path_filters = proto.MapField(
proto.STRING, proto.MESSAGE, number=8, message=struct.Value,
)
path_value_matchers = proto.MapField(
proto.STRING, proto.MESSAGE, number=11, message="ValueMatcher",
)
class ValueMatcher(proto.Message):
r"""Contains various matching options for values for a GCP
resource field.
Attributes:
matches_pattern (str):
To be used for full regex matching. The
regular expression is using the Google RE2
syntax
(https://github.com/google/re2/wiki/Syntax), so
to be used with RE2::FullMatch
"""
matches_pattern = proto.Field(proto.STRING, number=1, oneof="match_variant")
class CostProjection(proto.Message):
r"""Contains metadata about how much money a recommendation can
save or incur.
Attributes:
cost (~.money.Money):
An approximate projection on amount saved or
amount incurred. Negative cost units indicate
cost savings and positive cost units indicate
increase. See google.type.Money documentation
for positive/negative units.
duration (~.gp_duration.Duration):
Duration for which this cost applies.
"""
cost = proto.Field(proto.MESSAGE, number=1, message=money.Money,)
duration = proto.Field(proto.MESSAGE, number=2, message=gp_duration.Duration,)
class Impact(proto.Message):
r"""Contains the impact a recommendation can have for a given
category.
Attributes:
category (~.recommendation.Impact.Category):
Category that is being targeted.
cost_projection (~.recommendation.CostProjection):
Use with CategoryType.COST
"""
class Category(proto.Enum):
r"""The category of the impact."""
CATEGORY_UNSPECIFIED = 0
COST = 1
SECURITY = 2
PERFORMANCE = 3
MANAGEABILITY = 4
category = proto.Field(proto.ENUM, number=1, enum=Category,)
cost_projection = proto.Field(
proto.MESSAGE, number=100, oneof="projection", message="CostProjection",
)
class RecommendationStateInfo(proto.Message):
r"""Information for state. Contains state and metadata.
Attributes:
state (~.recommendation.RecommendationStateInfo.State):
The state of the recommendation, Eg ACTIVE,
SUCCEEDED, FAILED.
state_metadata (Sequence[~.recommendation.RecommendationStateInfo.StateMetadataEntry]):
A map of metadata for the state, provided by
user or automations systems.
"""
class State(proto.Enum):
r"""Represents Recommendation State."""
STATE_UNSPECIFIED = 0
ACTIVE = 1
CLAIMED = 6
SUCCEEDED = 3
FAILED = 4
DISMISSED = 5
state = proto.Field(proto.ENUM, number=1, enum=State,)
state_metadata = proto.MapField(proto.STRING, proto.STRING, number=2)
__all__ = tuple(sorted(__protobuf__.manifest))