-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathaws_reserved_instance_expiration.pt
339 lines (299 loc) · 11.1 KB
/
aws_reserved_instance_expiration.pt
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
name "AWS Expiring Reserved Instances"
rs_pt_ver 20180301
type "policy"
short_description "Reports on AWS Reserved Instances that have or will soon expire. See the [README](https://github.com/flexera-public/policy_templates/tree/master/cost/aws/reserved_instances/expiration) and [docs.flexera.com/flexera/EN/Automation](https://docs.flexera.com/flexera/EN/Automation/AutomationGS.htm) to learn more."
long_description ""
category "Cost"
severity "medium"
default_frequency "daily"
info(
version: "3.0.1",
provider: "AWS",
service: "Compute",
policy_set: "Reserved Instances",
hide_skip_approvals: "true"
)
###############################################################################
# Parameters
###############################################################################
parameter "param_email" do
type "list"
category "Policy Settings"
label "Email Addresses"
description "Email addresses of the recipients you wish to notify when new incidents are created."
default []
end
parameter "param_days_expiration" do
type "number"
category "Policy Settings"
label "Days Until Expiration"
description "The number of days until expiration to include a Reservation in the report. Set to '0' to only report expired Reservations."
min_value 0
default 15
end
parameter "param_bc_allow_or_deny" do
type "string"
category "Filters"
label "Allow/Deny Billing Centers"
description "Allow or Deny entered Billing Centers."
allowed_values "Allow", "Deny"
default "Allow"
end
parameter "param_billing_centers" do
type "list"
category "Filters"
label "Allow/Deny Billing Center List"
description "A list of allowed or denied Billing Center names/IDs. Leave blank to report on Reservations in all Billing Centers."
default []
end
###############################################################################
# Authentication
###############################################################################
credentials "auth_flexera" do
schemes "oauth2"
label "Flexera"
description "Select Flexera One OAuth2 credentials"
tags "provider=flexera"
end
###############################################################################
# Datasources & Scripts
###############################################################################
# Get applied policy metadata for use later
datasource "ds_applied_policy" do
request do
auth $auth_flexera
host rs_governance_host
path join(["/api/governance/projects/", rs_project_id, "/applied_policies/", policy_id])
header "Api-Version", "1.0"
end
end
datasource "ds_billing_centers" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/analytics/orgs/", rs_org_id, "/billing_centers"])
header "Api-Version", "1.0"
header "User-Agent", "RS Policies"
end
result do
encoding "json"
collect jmes_path(response, "[*]") do
field "href", jmes_path(col_item, "href")
field "id", jmes_path(col_item, "id")
field "name", jmes_path(col_item, "name")
field "parent_id", jmes_path(col_item, "parent_id")
end
end
end
datasource "ds_billing_centers_filtered" do
run_script $js_billing_centers_filtered, $ds_billing_centers, $param_bc_allow_or_deny, $param_billing_centers
end
script "js_billing_centers_filtered", type: "javascript" do
parameters "ds_billing_centers", "param_bc_allow_or_deny", "param_billing_centers"
result "result"
code <<-EOS
allow_deny_test = { "Allow": true, "Deny": false }
if (param_billing_centers.length > 0) {
billing_centers = _.filter(ds_billing_centers, function(item) {
id_found = _.contains(param_billing_centers, item['id']) == allow_deny_test[param_bc_allow_or_deny]
name_found = _.contains(param_billing_centers, item['name']) == allow_deny_test[param_bc_allow_or_deny]
return id_found || name_found
})
// Check for conflicting parents/children and remove children if present
bc_ids = _.compact(_.pluck(billing_centers, 'id'))
bad_children = _.filter(ds_billing_centers, function(bc) { return _.contains(bc_ids, bc['parent_id']) })
bad_children_ids = _.pluck(bad_children, 'id')
// Create final result with the bad children removed
final_list = _.reject(billing_centers, function(bc) { return _.contains(bad_children_ids, bc['id']) })
} else {
// If we're not filtering at all, just grab all of the top level billing centers
final_list = _.filter(ds_billing_centers, function(bc) {
return bc['parent_id'] == null || bc['parent_id'] == undefined
})
}
result = _.compact(_.pluck(final_list, 'id'))
EOS
end
datasource "ds_accounts_by_billing_center" do
request do
run_script $js_accounts_by_billing_center, $ds_billing_centers_filtered, rs_org_id, rs_optima_host
end
result do
encoding "json"
collect jmes_path(response, "rows[*]") do
field "cost", jmes_path(col_item, "metrics.cost_amortized_unblended_adj")
field "billing_center_id", jmes_path(col_item, "dimensions.billing_center_id")
field "vendor_account_id", jmes_path(col_item, "dimensions.vendor_account")
field "vendor_account_name", jmes_path(col_item, "dimensions.vendor_account_name")
field "timestamp", jmes_path(col_item, "timestamp")
end
end
end
script "js_accounts_by_billing_center", type: "javascript" do
parameters "ds_billing_centers_filtered", "rs_org_id", "rs_optima_host"
result "request"
code <<-EOS
start_date = new Date().toISOString()
start_date = start_date.split('-')[0] + '-' + start_date.split('-')[1]
end_year = Number(start_date.split('-')[0])
end_month = Number(start_date.split('-')[1]) + 1
if (end_month == 13) { end_month = 1; end_year++ }
if (end_month < 10) { end_month = '0' + end_month.toString() }
end_date = end_year.toString() + '-' + end_month.toString()
var request = {
auth: "auth_flexera",
verb: "POST",
host: rs_optima_host,
path: "/bill-analysis/orgs/" + rs_org_id + "/costs/aggregated",
body_fields: {
"dimensions": [ "billing_center_id", "vendor_account", "vendor_account_name" ],
"granularity": "month",
"metrics": [ "cost_amortized_unblended_adj" ],
"billing_center_ids": ds_billing_centers_filtered,
"start_at": start_date,
"end_at": end_date
}
}
EOS
end
datasource "ds_aws_reservations" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/reserved_instances/orgs/", rs_org_id, "/clouds/aws"])
end
result do
encoding "json"
collect jmes_path(response, "[*]") do
field "end_date", jmes_path(col_item, "end_datetime")
field "start_date", jmes_path(col_item, "start_datetime")
field "account_name", jmes_path(col_item, "account_name")
field "account_id", jmes_path(col_item, "account_id")
field "lease_id", jmes_path(col_item, "lease_id")
field "region", jmes_path(col_item, "region")
field "instance_type", jmes_path(col_item, "instance_type")
field "number_of_instances", jmes_path(col_item, "number_of_instances")
field "scope", jmes_path(col_item, "scope")
field "tenancy", jmes_path(col_item, "tenancy")
field "offering_type", jmes_path(col_item, "offering_type")
end
end
end
datasource "ds_filtered_aws_reservations" do
run_script $js_filtered_aws_reservations, $ds_accounts_by_billing_center, $ds_aws_reservations
end
script "js_filtered_aws_reservations", type: "javascript" do
parameters "ds_accounts_by_billing_center", "ds_aws_reservations"
result "result"
code <<-EOS
bc_account_ids = _.uniq(_.pluck(ds_accounts_by_billing_center, "vendor_account_id"))
result = _.filter(ds_aws_reservations, function(ri) {
return _.contains(bc_account_ids, ri['account_id'])
})
EOS
end
datasource "ds_account_list" do
run_script $js_account_list, $ds_accounts_by_billing_center, $ds_billing_centers
end
script "js_account_list", type: "javascript" do
parameters "ds_accounts_by_billing_center", "ds_billing_centers"
result "result"
code <<-EOS
bc_object = {}
_.each(ds_billing_centers, function(bc) { bc_object[bc['id']] = bc['name'] })
result = {}
_.each(ds_accounts_by_billing_center, function(account) {
if (result[account['vendor_account_id']] == undefined) {
result[account['vendor_account_id']] = []
}
result[account['vendor_account_id']].push(bc_object[account['billing_center_id']])
})
EOS
end
datasource "ds_expiring_aws_reservations" do
run_script $js_expiring_aws_reservations, $ds_filtered_aws_reservations, $ds_account_list, $ds_applied_policy, $param_days_expiration
end
script "js_expiring_aws_reservations", type: "javascript" do
parameters "ds_filtered_aws_reservations", "ds_account_list", "ds_applied_policy", "param_days_expiration"
result "result"
code <<-'EOS'
result = []
today = new Date()
_.each(ds_filtered_aws_reservations, function(ri) {
expiration_date = new Date(ri['end_date'])
days_until_expiry = (expiration_date - today) / 1000 / 60 / 60 / 24
if (days_until_expiry < 0) { days_until_expiry = 0 }
if (days_until_expiry < param_days_expiration || days_until_expiry == 0) {
billing_centers = ""
if (ds_account_list[ri['account_id']] != undefined) {
billing_centers = ds_account_list[ri['account_id']].join(', ')
}
result.push({
accountID: ri["account_id"],
accountName: ri["account_name"],
instanceType: ri["instance_type"],
instanceCount: ri["number_of_instances"],
resourceID: ri["lease_id"],
region: ri["region"],
expiration_date: expiration_date.toISOString().substring(0, 10),
days_until_expiry: Math.round(days_until_expiry),
billing_centers: billing_centers,
policy_name: ds_applied_policy["name"]
})
}
})
EOS
end
###############################################################################
# Policy
###############################################################################
policy "pol_ri_expiration" do
validate_each $ds_expiring_aws_reservations do
summary_template "{{ with index data 0 }}{{ .policy_name }}{{ end }}: AWS Expiring Reservations Found"
check eq(val(item, "resourceID"), "")
escalate $esc_email
export do
resource_level true
field "accountID" do
label "Account ID"
end
field "accountName" do
label "Account Name"
end
field "billing_centers" do
label "Associated Billing Center(s)"
end
field "region" do
label "Region"
end
field "resourceID" do
label "Lease ID"
end
field "instanceType" do
label "Instance Type"
end
field "instanceCount" do
label "Instance Count"
end
field "expiration_date" do
label "Expiration Date"
end
field "days_until_expiry" do
label "Days Until Expiration"
end
field "id" do
label "ID"
path "resourceID"
end
end
end
end
###############################################################################
# Escalations
###############################################################################
escalation "esc_email" do
automatic true
label "Send Email"
description "Send incident email"
email $param_email
end