boltops-tools / terraspace

Terraspace: The Terraform Framework
https://terraspace.cloud
Apache License 2.0
678 stars 46 forks source link

Only generate output files with content #284

Closed johnlister closed 1 year ago

johnlister commented 1 year ago

Hi I'm playing with leveraging layering to support custom behaviour with Terraspace by using .rb files in the tfvars folder; not sure if there is a better way to do this. Essentially I'm running custom ruby to set configuration required by a custom-helper later on, I want this configuration to be handled per environment, etc through layering.

Currently Terraspace expects that these files contain at least one tfvar DSL method, the DSL code checks if any variables values are generated and returns null if not. Unfortunately the writer still creates a corresponding but empty JSON file. This causes terraform to fail as the file is empty.

Another use case is logic in terraform to check the environment and define different variable values using DSL, if this results in no values being defined for that environment, terraform will fail.

Summary

Don;t generate files for terraform if their contents are empty/missing

Motivation

Allow none DSL ruby code by the Terraspace DSL compiler

Guide-level explanation

Reference-level explanation

Modify compiler/writer.rb to check the value of the content before outputing to a file

line 38 : IO.write(dest_path, content, mode: "wb") unless content.nil?

Drawbacks

Can't think of any. I purposely didn't modify to check for empty content - but you would probably want to filter that out as well. Can't think why you'd want to generate empty files for terraform to process?

tongueroo commented 1 year ago

Thanks been following https://community.boltops.com/t/using-ruby-files-in-tfvars-folder/993/2

Don't believe there's a reason to generate empty files instead of not generating a file at all 🤔 Just haven't test it.

Think fastest path is to check. A quick way to test and check is modify the gem locally on your system and see if it breaks anything. In your terraspace project run:

bundle info terraspace

That should give you info about the path where the gem is actually installed. Monkeypatch the gem on the spot. That's a benefit of scripting languages, you can modified on the spot. It'll provide a good quick sanity check. Then send a PR! Thanks

johnlister commented 1 year ago

PR attached, already ended up doing what you said :)

Couldn't see any existing tests and my knowledge of Ruby is limited to know what if any tests should be added, especially for lightweight fix. Current fix only checks for explicit Nil content, but equally empty content should also be stripped - only risk I could see is another downstream script/tool relying on an empty file. I'd guess you could check if it was passthrough mode and only copy, or add an option