forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommendations-example-2.yaml
284 lines (273 loc) · 11.5 KB
/
recommendations-example-2.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
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
retryPeriod: 10 years ago
processIf: |
{% if "every second month on 29th"|recurring_date("2021-01-01") == ""|strftime('%Y-%m-%d') %}1{% endif %}
processors:
- recommendations
# If set to true, all outputs must succeed in order the function to be considered successful.
allOutputsMustSucceed: true
recommendations:
fetch_recommendations: true
fetch_insights: true
recommender_types:
# Full list: https://cloud.google.com/recommender/docs/recommenders#recommenders
- google.compute.instance.MachineTypeRecommender # Location: zone
- google.compute.instanceGroupManager.MachineTypeRecommender # Location: zone
- google.compute.instance.IdleResourceRecommender # Location: zone
- google.compute.disk.IdleResourceRecommender # Location: region & zone
- google.compute.address.IdleResourceRecommender # Location: region & global
- google.compute.image.IdleResourceRecommender # Location: region & global
- google.compute.commitment.UsageCommitmentRecommender # Location: region
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: "{{ projects|json_encode() }}" # Projects from Pub/Sub message
# organizations:
# - 123456789
# folders:
# - 987654321
# billingAccounts:
# - 000000-ABCDEF-000000
locations:
- global
- europe
- us
- europe-west4*
- europe-north1*
- us-central1*
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?
subtypes:
CHANGE_MACHINE_TYPE: "Change instance types"
CHANGE_MACHINE_FAMILY: "Change instance families"
STOP_VM: "Stop idle instances"
SNAPSHOT_AND_DELETE_DISK: "Snapshot and remove disks"
DELETE_DISK: "Remove disks"
PERMISSIONS_USAGE: "Improve IAM permissions"
SERVICE_ACCOUNT_USAGE: "Remove unused service accounts"
REPLACE_ROLE: "Replace IAM roles"
REMOVE_ROLE: "Remove unused IAM roles"
SHADOWED_RULE: "Resolve shadowed firewall rules"
UNUSED_RULE: "Remove unused firewall rules"
UNUSED_ATTRIBUTE: "Unused firewall attributes"
DELETE_ADDRESS: "Delete unused public IP addresses"
PROJECT_SCOPED_COMMITMENTS: "Purchase project commitments"
DELETE_IMAGE: "Delete unused images"
BILLING_ACCOUNT_SCOPED_COMMITMENTS: "Purchase commitments"
outputs:
- type: gcs
bucket: my-output-bucket
object: Recommendations_{{ ""|strftime('%b-%Y') }}.xlsx
base64decode: true
contents: |
{% set workbook %}
{% for parent, parent_recommendations in recommendations.items() %}
{% set total_cost = namespace(cost=0) %}
{% set row_class = cycler('odd', 'even') %}
{% for r in parent_recommendations %}
{% if loop.first %}
<table>
<thead>
<tr>
<td><b>Parent</b></td>
<td><b>Resource</td>
<td><b>Recommended action</b></td>
<td><b>Description</b></td>
<td><b>Monthly impact</b></td>
</tr>
</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>{{ types[r.type] }}: {% if r.type == "projects" %}{{ r.parent[2] }} ({{ r.parent[0] }}){% else %}{{ r.parent|make_list|first }}{% endif %}</td>
<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 %}
{% endset %}{{ workbook|html_table_to_xlsx }}
- type: mail
# Transports will be tried in sequence, until one succeeds
transports:
- type: smtp
host: your.smtp.server
port: 25
starttls: false
# ssl: false
- type: sendgrid
apiKey: your-sendgrid-api-key
from: [email protected]
subject: |
[Google Cloud Platform] Recommendations and Insights report, {{ ""|strftime('%b %Y') }}
body:
attachments:
- gs://my-output-bucket/Recommendations_{{ ""|strftime('%b-%Y') }}.xlsx
images: # Images to inline in the email
- img/sample-logo-small.png
html: |
<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;
}
.count {
text-align: right;
font-weight: bolder;
font-size: 1.5em;
color: #0F9D58;
}
.subtype {
font-weight: bold;
font-size: 1.5em;
}
.recommendation {
padding: 1em;
margin: 1em;
}
.one-recommendation {
border: 2px solid #4285F4;
}
.one-recommendation td {
padding: 0.5em;
}
.insight {
padding: 1em;
margin: 1em;
}
.one-insight {
border: 2px solid #F4B400;
}
.one-insight td {
padding: 0.5em;
}
</style>
</head>
<body>
<h1>Google Cloud Platform - Recommendations and Insights report, {{ ""|strftime('%b %Y') }}</h1>
<img src="cid:sample-logo-small.png" alt="Sample logo" />
<h2>Recommendations</h2>
{% for parent, rollup in recommendations_rollup.items() %}
{% set total_cost = namespace(cost=0) %}
{% set row_class = cycler('1', '2', '3', 'break') %}
{% for sub_type, stats in rollup.items() %}
{% if loop.first %}
<h3>{{ types[stats.type] }}: {% if stats.type == "projects" %}{{ stats.parent[2] }} ({{ stats.parent[0] }}){% else %}{{ stats.parent|make_list|first }}{% endif %}</h3>
<table>
<tr>
{% endif %}
{% if row_class.next() == 'break' %}
</tr>
<tr>
{% endif %}
<td class="recommendation">
<table class="one-recommendation" width="100%">
<tr>
<td class="subtype">{{ subtypes[sub_type] }}</td>
<td class="count">{{ stats.count }}</td>
</tr>
<tr>
<td colspan="2" class="impact">Total cost impact: <b>{{ stats.cost|format_cost }}</b></td>
</tr>
</table>
</td>
{% if loop.last %}
</tr>
</table>
<a href="https://console.cloud.google.com/home/recommendations?project={% if stats.type == "projects" %}{{ stats.parent[0] }}{% else %}{% if stats.type == "billingAccounts" %}{% else %}{{ stats.parent|make_list|first }}{% endif %}{% endif %}">View recommendations in Cloud Console.</a>
{% endif %}
{% endfor %}
{% endfor %}
{% for parent, rollup in insights_rollup.items() %}
{% if loop.first %}
<h2>Insights</h2>
{% endif %}
{% set row_class = cycler('1', '2', '3', 'break') %}
{% for sub_type, stats in rollup.items() %}
{% if loop.first %}
<h3>{{ types[stats.type] }}: {% if stats.type == "projects" %}{{ stats.parent[2] }} ({{ stats.parent[0] }}){% else %}{{ stats.parent|make_list|first }}{% endif %}</h3>
<table>
<tr>
{% endif %}
{% if row_class.next() == 'break' %}
</tr>
<tr>
{% endif %}
<td class="insight">
<table class="one-insight" width="100%">
<tr>
<td class="subtype">{{ subtypes[sub_type] }}</td>
<td class="count">{{ stats.count }}</td>
</tr>
</table>
</td>
{% if loop.last %}
</tr>
</table>
<a href="https://console.cloud.google.com/home/recommendations?project={% if stats.type == "projects" %}{{ stats.parent[0] }}{% else %}{% if stats.type == "billingAccounts" %}{% else %}{{ stats.parent|make_list|first }}{% endif %}{% endif %}">View insights in Cloud Console.</a>
{% endif %}
{% endfor %}
{% endfor %}
</body>
</html>