forked from laurmurclar/datadog-to-terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor_generator.js
94 lines (84 loc) · 2.33 KB
/
monitor_generator.js
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
const REQUIRED_KEYS = ["name", "type", "query", "message"];
const ALLOWED_OPTIONS_KEYS = [
"notify_no_data",
"new_host_delay",
"evaluation_delay",
"no_data_timeframe",
"renotify_interval",
"notify_audit",
"timeout_h",
"include_tags",
"require_full_window",
"locked",
"escalation_message",
"thresholds",
"silenced",
"threshold_windows"
];
function literalString(value) {
if (typeof value == "string") {
if (value.includes("\n")) {
return `<<EOF\n${value}\nEOF`;
}
return `"${value}"`;
} else if (Array.isArray(value)) {
let result = "[";
value.forEach((elem, index) => {
result += literalString(elem);
if (index != value.length - 1) result += ",";
});
return result + "]";
}
return value;
}
export function assignmentString(key, value) {
if (value === null) return "";
const displayValue = literalString(value);
return `${key} = ${displayValue}\n`;
}
export function convertMapping(mappingName, mapping) {
let result = "\n";
Object.entries(mapping).forEach(([key, value]) => {
result += assignmentString(key, value);
});
return `${mappingName} {${result}}`;
}
function convertOptions(options) {
let result = "";
Object.entries(options).forEach(([key, value]) => {
if (ALLOWED_OPTIONS_KEYS.includes(key)) {
if (key === "thresholds" || key === "threshold_windows" || key === "silenced") {
result += convertMapping(key, value);
} else {
result += assignmentString(key, value);
}
} else {
throw `Conversion for "${key}" not found`;
}
});
return result;
}
function convert(key, value) {
let result = "";
if (REQUIRED_KEYS.includes(key) || key === "tags") {
result += assignmentString(key, value);
} else if (key === "options") {
result += convertOptions(value);
} else {
throw `Conversion for "${key}" not found`;
}
return result;
}
function monitorBody(monitorJson) {
let result = "\n";
Object.entries(monitorJson).forEach(([key, value]) => {
result += convert(key, value);
});
return result;
}
export function generateTerraformCode(resourceName, monitorJson) {
if (!resourceName || !monitorJson || !REQUIRED_KEYS.every(key => key in monitorJson)) {
throw "You're missing a required key.";
}
return `resource "datadog_monitor" "${resourceName}" {${monitorBody(monitorJson)}}`;
}