forked from laurmurclar/datadog-to-terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard_generator.js
132 lines (116 loc) · 3.95 KB
/
dashboard_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
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
import {assignmentString} from "./monitor_generator.js";
import {convertMapping} from "./monitor_generator.js";
const REQUIRED_KEYS = ["title", "description", "template_variables", "layout_type", "is_read_only", "notify_list", "id"];
const VALID_SLO_KEYS = ["view_type", "slo_id", "show_error_budget", "view_mode", "time_windows", "title", "title_size", "title_align"];
export function generateDashboardTerraformCode(resourceName, dashboardJson) {
if (!resourceName || !dashboardJson) {
throw "You're missing a required key.";
}
return `resource "datadog_dashboard" "${resourceName}" {${dashboardBody(dashboardJson)}}`;
}
function dashboardBody(dashboardJson) {
let result = "\n";
Object.entries(dashboardJson).forEach(([key, value]) => {
result += convert(key, value);
});
return result;
}
function convert(key, value) {
let result = "";
if (REQUIRED_KEYS.includes(key)) {
if (key === "id") return result;
if (key === "template_variables") {
return result + convertTemplateVariables(value);
}
result += assignmentString(key, value);
} else if (key === "widgets") {
result += convertWidgets(value);
} else {
throw `Conversion for "${key}" not found`;
}
return result;
}
function convertTemplateVariables(templateVariables) {
let result = "\n";
for (let templateVariable of templateVariables) {
result += "template_variable {\n";
Object.entries(templateVariable).forEach(([key, value]) => {
result += assignmentString(key, value);
});
result += "\n}";
}
return result;
}
function convertWidgets(widgets) {
let result = "";
for (let widget of widgets) {
result += "widget {\n";
Object.entries(widget).forEach(([key, value]) => {
if (key === "definition") {
result += constructWidgetDefinition(value);
} else if (key === "layout") {
result += convertNestedMappings(key, value);
}
});
result += "\n}";
}
return result;
}
function constructWidgetDefinition(definition) {
let result = "\n";
if (definition["type"] === "slo") {
result += "service_level_objective_definition {\n";
} else {
result += definition["type"] + "_definition {\n";
}
Object.entries(definition).forEach(([key, value]) => {
if (key === "type" || (definition["type"] === "slo" && !VALID_SLO_KEYS.includes(key))) {
return;
}
if (key === "time") {
result += convertNestedMappings(key, value);
} else if (key === "widgets") {
result += convertWidgets(value);
} else if (key === "requests") {
result += convertRequests(value);
} else if (key === "markers"){
result += convertMarkers(value);
} else {
result += assignmentString(key, value);
}
});
return result + "\n}";
}
function convertRequests(requests) {
let result = "\n";
for (let request of requests) {
let requestBody = "\n";
Object.entries(request).forEach(([key, value]) => {
if (key === "style") {
requestBody += convertMapping(key, value);
} else {
requestBody += assignmentString(key, value);
}
});
result += `request {${requestBody}}`
}
return result;
}
function convertNestedMappings(mappingName, mapping) {
let result = "\n";
Object.entries(mapping).forEach(([key, value]) => {
result += assignmentString(key, value);
});
return `${mappingName} = {${result}}`;
}
function convertMarkers(markers) {
let result = "\n";
for (let marker of markers) {
let markerBody = "\n";
Object.entries(marker).forEach(([key, value]) => {
markerBody += assignmentString(key, value);
});
result += `marker {${markerBody}}`
}
return result;
}