GoogleCloudPlatform / terraform-python-testing-helper

Simple Python test helper for Terraform.
https://pypi.org/project/tftest/
Apache License 2.0
214 stars 31 forks source link

Passing complex objects to the tf.plan(tf_vars=...) doesn't work #67

Closed MrImpossibru closed 1 year ago

MrImpossibru commented 1 year ago

Hello,

I've met an issue with passing complex variables to the tf.plan.

That doesn't work: tf.plan(output = True, tf_vars = { "pep_spec": { "pep1": "test" } }) because of: cmd_args = ('-no-color', '-input=false', '-var', "pep_spec={'pep1': 'test'}", ...)

Error is: "Single quotes are not valid. Use double quotes (") to enclose strings."

That works: tf.plan(output = True, tf_vars = { "pep_spec": "{\"pep1\"=\"test\"}" }) and that: tf.plan(output = True, tf_vars = { "pep_spec": "{\"pep1\":\"test\"}" }) but that is unusable.

It looks like a super small problem. Could it be fixed ASAP?

MrImpossibru commented 1 year ago

Since I can't just easily create a Pull request I'll write the change down: Lines 147-149 of the tftest.py: cmd_args += list(itertools.chain.from_iterable( ("-var", "{}={}".format(k, json.dumps(v) if isinstance(v, (dict, list)) else v)) for k, v in tf_vars.items() ))

Terraform documentation about that - https://developer.hashicorp.com/terraform/language/values/variables#variables-on-the-command-line

@ludoo @marshall7m could you please take a look? Thanks

ludoo commented 1 year ago

Thanks for this, I did not have time to check this issue yet. Let me have a proper look today. :)

ludoo commented 1 year ago

So, usage in your case should be:

tf.plan(output = True, pep_spec='{ "pep1": "test" }')

Just pass in one variable at a time as a string. You can check our Fabric repo's tests for examples on how to do that.

I am going to close this, feel free to reopen if the above is unclear.