Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Add SetInputVars and RequireSetInputVars to WorkingDir #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jrhouston
Copy link

@jrhouston jrhouston commented May 12, 2020

👋

Description

This PR adds the ability to set input variables by creating a terraform.tfvars inputs.auto.tfvars.json file in the working directory.

Context

I'm working on the new Kubernetes provider, which is a crazy wild west provider that doesn't use the existing terraform plugin SDK so we are using this helper package directly in the acceptance tests.

One of things we'd like to do is have the terraform configurations for the acceptance tests be in their own files so that we can easily run them by hand, without having to hack out Go formatting directives. The way we're doing this is by using terraform's own input variables syntax in the test configurations, and supplying them when we set up the test. This way all we need to do is supply variables if we run them by hand. It also makes the test configurations more legible for people looking for runnable examples.

At the moment I have a hack in the test code that creates variable blocks with defaults at the top of whatever is created with RequireSetConfig but it would be nice if the helper could do this for us.

Discussion

Not sure if this is even the right abstraction - we could also do this as an additional parameter to SetConfig, or by exposing a way to supply -var flags to the baseArgs array instead.

@kmoe
Copy link
Member

kmoe commented Jul 7, 2020

Thanks for this, @jrhouston. I think this may well be the right abstraction, but I want to get #27 done first just to make sure. Thanks for your patience!

@kmoe kmoe added this to the v1.6.0 milestone Jul 7, 2020
@apparentlymart
Copy link
Contributor

One alternative I might suggest considering here is to have the function accept either a map[string]interface{} or a map[string]cty.Value where the keys are the variable names, and then have it generate an .auto.tfvars.json file instead using a normal JSON encoder. That'd potentially then avoid the need for callers to construct valid HCL native syntax themselves (which requires dealing with escaping in the general case) and let them just specify what values they want to set instead.

The .tfvars.json variant of variable definition files is relatively easy generate programmatically because it interprets all of the values as literals, so you don't even need to worry about escaping template sequences inside strings as you would in the native syntax.

@jrhouston
Copy link
Author

jrhouston commented Jul 7, 2020

Good shout, @apparentlymart. As this will just be used in acceptance tests and will generate JSON I think being able to use map[string]interface{} would probably be slightly more convenient for the developer than using cty. I'll update the PR.

Accept an arbitrary map of values and generate a .auto.tfvars.json
file rather than just accepting a plaintext string and expecting
it to contain HCL.
@paultyng
Copy link
Contributor

@jrhouston we should chat about this, your implementation should probably be wrapping terraform-exec and/or using the reattach mode. Feel free to hit me up in Slack.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants