Skip to content

Commit

Permalink
Merge pull request #13 from intercom/lmc/add-threshold-windows
Browse files Browse the repository at this point in the history
Add support for 'threshold_windows' option
  • Loading branch information
laurmurclar authored Jun 17, 2019
2 parents 745d11a + 88aa9f0 commit 3fa9a71
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
44 changes: 11 additions & 33 deletions generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ const REQUIRED_KEYS = [
'message',
];

const ALLOWED_THRESHOLD_KEYS = [
'ok',
'critical',
'critical_recovery',
'warning',
'warning_recovery',
'unknown',
];

const ALLOWED_OPTIONS_KEYS = [
'notify_no_data',
'new_host_delay',
Expand All @@ -28,28 +19,9 @@ const ALLOWED_OPTIONS_KEYS = [
'escalation_message',
'thresholds',
'silenced',
'threshold_windows',
];

function convertSilenced(value) {
let result = "\n";
Object.entries(value).forEach(([key, value]) => {
result += assignmentString(key, value);
});
return `silenced {${result}}`;
}

function convertThresholds(thresholds) {
let result = "\n";
Object.entries(thresholds).forEach(([key, value]) => {
if (ALLOWED_THRESHOLD_KEYS.includes(key)) {
result += assignmentString(key, value);
} else {
throw `Conversion for "${key}" not found`;
}
});
return `thresholds {${result}}`;
}

function literalString(value) {
if (typeof value == 'string') {
if (value.includes('\n')) {
Expand All @@ -73,14 +45,20 @@ function assignmentString(key, value) {
return `${key} = ${displayValue}\n`;
}

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') {
result += convertThresholds(value);
} else if (key === 'silenced') {
result += convertSilenced(value);
if (key === 'thresholds' || key === 'threshold_windows' || key === 'silenced') {
result += convertMapping(key, value);
} else {
result += assignmentString(key, value);
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Datadog-to-Terraform Converter",
"version": "1.1",
"version": "1.2",
"description": "Converts Datadog monitor JSON into Terraform alarm code.",
"manifest_version": 2,
"browser_action": {
Expand Down

0 comments on commit 3fa9a71

Please # to comment.