Closed robinbowes closed 6 years ago
terrascript.dump()
is meant to be called once, i.e. at the end of your program. I accept that this may not be clearly explained in my documentation/examples. Try to change your code accordingly and let me know how you go.
That seems to work, however I was hoping to write each resource into a separate file, ie. I would want to dump the output once for each resource.
I guess I can dump it once and loop over the resulting json, but it seems less than ideal.
Ok, I see what you are trying to achieve. That would require some kind of terrascript.reset()
function which doesn't currently exists. Then one could do (in pseudo-code):
Loop over monitors:
datadog_monitor( monitor['name'], **kwargs)
dump = terrascript.dump()
write_dump_to_file(dump)
terrascript.reset()
Is that what you are trying to achieve?
Yes, exactly that.
Or maybe something like this:
Loop over monitors:
ts = Terrascript.create()
ts.add('datadog_monitor', monitor['name'], **kwargs)
write_dump_to_file(ts.dump())
Have a look at my comments to https://github.com/mjuenema/python-terrascript/pull/27. Once I have rewritten the code the following will be possible which would also make your original code possible (with some minor changes).
for x in range(1,4):
t = terrascript.Terrascript()
t += provider('aws', region='us-east-' + x) # us-east-1, us-east-2, us-east-3
config = t.dump()
# ... and write each config to separate files.
This is now possible. Have a look at tests/test_issues.py
for details.
Thanks for fixing this - it's working well for me.
@mjuenema, I too am running into this issue. After reading over the thread I am still not able to get this working as designed? Has there been an update to the code?
Hi @blackego30, there have been many changes to the code since 2018. Can you please provide some more details of the problem you are encountering. Some sample code would be very useful.
Hello @mjuenema, initialize(**options)
from datadog import api
datadog_monitors = api.Monitor.get_all()
timestamp = strftime("%Y-%m-%d_%H:%M:%S", gmtime())
print (timestamp) print ("Number of monitors: ", + len(str(datadog_monitors)))
with open('existing_monitors.json', 'w') as outfile: json.dump(datadog_monitors, outfile, indent=2, sort_keys=False)
def test_issue_26():
for name in ["first", "second", "third"]:
ts = terrascript.Terrascript()
ts += terrascript.provider.datadog(api_key='xxxxxxxxxx', app_key='xxxxxxx')
ts += terrascript.resource.datadog_monitor(
name,
name=name,
type="metric alert",
query="some query",
message="a message",
escalation_message="some message",
)
ts = config.dump()
assert len(ts["resource"]["datadog_monitor"].keys()) == 1
assert ts["resource"]["datadog_monitor"][name]["name"] == name
Hi,
I'm using terrascript to dump existing datadog monitors into terraform code.
Here's some sample code:
What I see is that the
datadog_monitor
resource doesn't clean up between invocations so I end up with this sort of output:Am I doing something wrong here?