-
Notifications
You must be signed in to change notification settings - Fork 13
/
recommendations-example-3.yaml
178 lines (171 loc) · 7.48 KB
/
recommendations-example-3.yaml
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
# Copyright 2021 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.
retryPeriod: 10 years ago
processors:
- recommendations
# If set to true, all outputs must succeed in order the function to be considered successful.
allOutputsMustSucceed: true
recommendations:
# Useful when querying multiple projects, so not every project needs recommender API activated.
# quota_project_id: "project-id"
fetch_recommendations: true
fetch_insights: true
recommender_types:
# Full list: https://cloud.google.com/recommender/docs/recommenders#recommenders
- google.compute.instance.MachineTypeRecommender
- google.compute.instanceGroupManager.MachineTypeRecommender
- google.compute.instance.IdleResourceRecommender
- google.compute.disk.IdleResourceRecommender
- google.compute.address.IdleResourceRecommender
- google.compute.image.IdleResourceRecommender
recommendation_filter: 'stateInfo.state=ACTIVE'
insight_types:
# Full list: https://cloud.google.com/recommender/docs/insights/insight-types
- google.compute.firewall.Insight
- google.iam.policy.Insight
- google.iam.serviceAccount.Insight
insight_filter: 'stateInfo.state=ACTIVE'
projects:
- my-project-1
- my-project-2
locations:
- global
- europe
- europe-west1*
- europe-west4*
vars:
types:
organizations: "Organization"
projects: "Project"
folders: "Folder"
billingAccounts: "Billing account"
commitment_types:
THIRTY_SIX_MONTH: "36 months"
TWELVE_MONTH: "12 months"
links:
google.compute.instance.IdleResourceRecommender: https://console.cloud.google.com/home/recommendations/list/COMPUTE_IDLE_RESOURCES?
google.compute.instance.MachineTypeRecommender: https://console.cloud.google.com/home/recommendations/list/VM_COST?
google.compute.instanceGroupManager.MachineTypeRecommender: https://console.cloud.google.com/home/recommendations/list/VM_COST?
google.compute.disk.IdleResourceRecommender: https://console.cloud.google.com/home/recommendations/list/COMPUTE_IDLE_RESOURCES?
google.compute.firewall.Insight: https://console.cloud.google.com/home/recommendations/list/SHADOWED_FIREWALL_RULES?
google.compute.commitment.UsageCommitmentRecommender: https://console.cloud.google.com/home/recommendations/list/PROJECT_COMMITMENTS?
google.iam.policy.Insight: https://console.cloud.google.com/home/recommendations/list/IAM_ROLES?
outputs:
- type: gcs
bucket: my-bucket
object: recommendations.json
# Be very careful with extra linefeeds, spaces and such.
contents: |
{%- for parent, parent_recommendations in recommendations.items() -%}
{%- for r in parent_recommendations -%}
{{ {"project": r.parent[0], "location": r.location, "type": r.recommender_type, "name": r.recommendation.name, "description": r.recommendation.description}|json_encode }}
{% endfor -%}
{%- endfor -%}
- type: bigquery
datasetWithTable: my-project.my-dataset.my-table
location: EU
source: gs://my-bucket/recommendations.json
project: my-project
job:
sourceFormat: NEWLINE_DELIMITED_JSON
writeDisposition: WRITE_APPEND
createDisposition: CREATE_IF_NEEDED
autodetect: true
maxBadRecords: 100
- type: gcs
bucket: my-bucket
objects: |
{%- set files = dict() %}
{%- for parent, parent_recommendations in recommendations.items() %}
{%- for project in parent_recommendations|selectattr('type', '==', 'projects') %}
{%- set x = files.__setitem__(project.parent[0] + ".html", project.parent[0]) %}
{%- endfor %}
{%- endfor %}
{{ files|json_encode }}
contents: |
<html><head><title>Recommendations and Insights, {{ ""|strftime('%b %Y') }}</title>
<style type="text/css">
body {
font-family: 'Google Sans', 'Segoe UI', sans-serif;
}
table {
border-collapse: collapse;
}
thead {
background-color: #0F9D58;
color: white;
}
th {
padding: 0.5em;
text-align: left;
}
td {
padding: 0.2em;
}
.even {
background-color: #ddd;
}
</style>
</head>
<body>
<h1>Google Cloud Platform - Recommendations and Insights report, {{ ""|strftime('%b %Y') }}</h1>
<h2>Recommendations for project: {{ key }}</h2>
{% for parent, parent_recommendations in recommendations.items() %}
{% set total_cost = namespace(cost=0) %}
{% set row_class = cycler('odd', 'even') %}
{% for r in parent_recommendations|selectattr('type', '==', 'projects')|selectattr('parent', 'contains', key) %}
{% if loop.first %}
<table>
<thead>
<th>Resource</th>
<th>Recommended action</th>
<th>Description</th>
<th>Impact</th>
</thead>
<tbody>
{% endif %}
{% for og in r.recommendation.content.operation_groups %}
{% for op in og.operations %}
{% if op.action != 'test' and not op.resource.endswith('$snapshot-name') %}
<tr class="{{ row_class.next() }}">
<td>{% if r.recommender_type in links %}<a href="{{ links[r.recommender_type] }}{{ r.link }}">{% endif %}{% if op.resource_type != 'compute.googleapis.com/Commitment' -%}
{{ op.resource|split("/")|index(-1) }}
{%- else -%}
{% for res in op.value.resources %}{{ res.type|title }}: {% if res.type == 'VCPU' %}{{ res.amount }} vCPU(s){% else %}{{ (res.amount/1024)|round }} GB{% endif %}{% endfor %}
{%- endif %}{% if r.recommender_type in links %}</a>{% endif %}
</td>
<td>{{ op.action|title }}</td>
<td>{{ r.recommendation.description }}</td>
<td>{% if 'cost_projection' in r.recommendation.primary_impact %}{{ r.recommendation.primary_impact.cost_projection.cost|format_cost() }}{% endif %}</td>
</tr>
{% endif %}
{% endfor %}
{% endfor %}
{%- if 'cost_projection' in r.recommendation.primary_impact -%}
{%- set total_cost.cost = total_cost.cost + r.recommendation.primary_impact.cost_projection.cost|get_cost() -%}
{%- endif -%}
{% if loop.last %}
<tfoot>
<tr>
<td colspan="3">Total cost impact:</td>
<td>{{ "{:.2f}".format(total_cost.cost) }} USD</td>
</tr>
</tfoot>
</tbody>
</table>
{% endif %}
{% endfor %}
{% endfor %}
</body>
</html>