-
-
Save ZimbiX/65025e1d49603e148734ebbfcb19cf77 to your computer and use it in GitHub Desktop.
Convert tfstate to tf for datadog_monitor on Terraform Datadog Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
import sys | |
import codecs | |
sys.stdout = codecs.getwriter('utf8')(sys.stdout) | |
f = open('terraform.tfstate', 'r') | |
data = json.load(f) | |
#print json.dumps(data) | |
f_downtime = open('downtime.tf', 'w'); | |
f_monitor = open('monitor.tf', 'w'); | |
f_user = open('user.tf', 'w'); | |
for module in data["modules"]: | |
for resource in module["resources"]: | |
resource_values = resource.split(".") | |
if resource_values[0] == "datadog_downtime": | |
fo = f_downtime | |
elif resource_values[0] == "datadog_monitor": | |
fo = f_monitor | |
elif resource_values[0] == "datadog_user": | |
fo = f_user | |
else: | |
continue | |
fo.write ("resource \"" + resource_values[0] + "\" \"" + resource_values[1] + "\" {\n") | |
values = module["resources"][resource]["primary"] | |
if resource_values[0] == "datadog_downtime": | |
kv_keys = ["start", "end", "message", "active"] | |
list_keys = ["scope"] | |
object_keys = [] | |
other_keys = ["recurrence"] | |
elif resource_values[0] == "datadog_monitor": | |
kv_keys = ["type", "name", "query", "message", "escalation_message", "notify_no_data", "new_host_delay", "evaluation_delay", "no_data_timeframe", "renotify_interval", "notify_audit", "timeout_h", "include_tags", "require_full_window", "locked"] | |
list_keys = ["tags"] | |
object_keys = ["thresholds", "silenced"] | |
other_keys = [] | |
elif resource_values[0] == "datadog_user": | |
kv_keys = ["disabled", "email", "handle", "is_admin", "name", "role", "disabled", "verified"] | |
list_keys = [] | |
object_keys = [] | |
other_keys = [] | |
else: | |
fo.write ("#!!! " + resource_values[0] + " not defined.\n") | |
# key value | |
for key in kv_keys: | |
if key in values["attributes"]: | |
value = values["attributes"][key] | |
value = value.replace('\n', '\\n') | |
value = value.replace('"', '\\"') | |
fo.write (" " + key + " = \"" + value + "\"\n") | |
# list | |
for key in list_keys: | |
keysuffix = "#" | |
if (key + "." + keysuffix in values["attributes"]) and (int(values["attributes"][key + "." + keysuffix]) > 0): | |
value = key + " = [" | |
for i in range(0, int(values["attributes"][key + "." + keysuffix])): | |
if i > 0: | |
value = value + "," | |
value = value + "\"" + values["attributes"][key + "." + str(i)] + "\"" | |
fo.write (" " + value + "]\n") | |
# object | |
for key in object_keys: | |
keysuffix = "%" | |
if (key + "." + keysuffix in values["attributes"]) and (int(values["attributes"][key + "." + keysuffix]) > 0): | |
fo.write (" " + key + " {\n") | |
if key == "thresholds": | |
for subkey in ["critical", "critical_recovery", "warning", "warning_recovery", "ok"]: | |
if key + "." + subkey in values["attributes"]: | |
fo.write (" " + subkey + " = " + values["attributes"][key + "." + subkey] + "\n") | |
elif key == "silenced": | |
for subkey in values["attributes"]: | |
if subkey.startswith(key + ".") and subkey != key + "." + keysuffix: | |
subkeys = subkey.split(".") | |
fo.write (" \"" + subkeys[1] + "\" = " + "\"" + values["attributes"][subkey] + "\"\n") | |
else: | |
fo.write ("#!!! " + key + " on " + resource_values[0] + " not defined.\n") | |
fo.write (" }\n") | |
fo.write ("}\n") | |
f_downtime.close() | |
f_monitor.close() | |
f_user.close() | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment