Description
Reopening the issue #67
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.
The example is simplified. Passing a proper dictionary is required if I need to compare the value from the plan with the value from a dictionary to check if the value passes correctly.
Possible Solution is:
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
So, usage in your case should be:
tf.plan(output = True, pep_spec='{ "pep1": "test" }')
That is just a workaround like I sent above, not a solution. That won’t be possible to compare the equality of some field inside the dictionary with the value from the plan. Since there’s no proper handling I consider that behaviour as a bug anyway.
Please, consider to fix that.