-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing complex objects to the tf.plan(tf_vars=...) doesn't work #68
Comments
@ludoo
|
return tf.plan(output = True, **vars) |
|
@ludoo I can provide the whole example's code if necessary |
Gah started reproing and was distracted but I'm pretty sure I know what's happening: each var value needs to be the string representation of the HCL value, not Python literal code. |
@pytest.fixture(scope='session')
def vars() -> dict:
return {
"vnt_name": "vnet",
"pep_spec": '{ pep1 = { name = "test1" }, pep2 = { name= "test2" } }'
}
@pytest.fixture(scope='session')
def plan(vars):
tf = tftest.TerraformTest('Storageacc', './fixtures/')
tf.setup()
return tf.plan(output = True, tf_vars=vars) Sorry for misleading you, was thinking of the fixtures we use in the fabric repo :) |
@ludoo yep, that is a workaround, but it won't solve the case. It will be hard to query and compare some internals from the string. |
I don't think we want to change the way we do this, it would also involve converting Python objects to TF format, and some of those are really complex. |
@ludoo Terraform accepts json format there. What I'm proposing is an extension, not a change. There's a condition to encode params as json only There will be no breaking changes. |
Can you send a PR? And thanks for being persistent despite my attempts at discouraging / sidetracking. :) |
Done.
You're welcome! My team depends on that feature. |
Fixed by #69 will close once the new release is cut and pushed on pypi |
Thanks again for the change! |
@ludoo Thanks for your support! |
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
@ludoo
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.
The text was updated successfully, but these errors were encountered: