Skip to content
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

backend/local: do not use backend operation variables #19175

Merged
merged 2 commits into from
Oct 23, 2018
Merged

Conversation

svanharmelen
Copy link
Contributor

@svanharmelen svanharmelen commented Oct 23, 2018

Previously the local backend didn’t merge variables but completely replaced them when any backend operation variables were set.

Before this change this was essentially a dead codepath (that didn't have any tests) as the backend operation would never have a value for these variables and so the complete set of variables created while getting the ContextOpts would be used instead: https://github.com/hashicorp/terraform/blob/v0.11/command/meta.go#L312

But the variables were added to the backend operation because the remote backend needs access to these command line variables so they can be set as run variables. Run variables are not yet supported in this release, but this value is still used in the current remote backend to detect if command line variables were set, in which case a proper error can be returned.

We could also just remove the whole code path, instead of changing the behavior to merge instead of replace the variables. Will discuss that option before merging this.

I added a few tests to guard against this issue in any future releases.

Fixes #19163

EDIT: See the first commit for this initial solution.

Previously the `local` backend didn’t merge variables but completely replaced them when any backend operation variables were set.

Before [this change](67db9da000#diff-a0d5ae04114a139f2356fb16fbac7581R174) this was essentially a dead codepath as the backend operation would never have a value for these variables and so the complete set of variables created while getting the `ContextOpts` would be used instead: https://github.com/hashicorp/terraform/blob/v0.11/command/meta.go#L312

But the variables were added to the backend operation because the `remote` backend needs access to these command line variables so they can be set as run variables. Run variables are not yet supported in this release, but this value is still used in the current `remote` backend to detect if command line variables were set, in which case a proper error can be returned.

We could also just remove the whole code path, instead of changing the behavior to merge instead of replace the variables. Will discuss that option before merging this.

I added a few tests to guard against this issue in any future releases.

Fixes #19163
@svanharmelen
Copy link
Contributor Author

svanharmelen commented Oct 23, 2018

In the earlier stages of the command initialization, variables are separated by autoVariabels and variables. But in the backend.Operation struct there is only the Variables field to pass any variables. So it looks like this field was intended to have either all applicable variables, or none (in which case the ones from the context would be used). In that context, the code that was changes in this PR is actually correct and should not change.

But looking very closely through the code base, I noticed that the Variables field was never set (until the change referenced above) and read only once in the local backend. So I would suggest that I update this PR and just remove the updated lines of code from the local backend code instead of changing its behavior.

We can then assume that the field name matches the variable type that can be read from the Meta struct. In order to make that distinction more clear when reading the code we could add a AutoVariables field to the backend.Operation to reflect the different types of variables, but that field would never be used. At least not in the current code base.

So I suggest we, instead, add a very explicit comment to the Variables field to make clear it only expects the command line provided variables and not the auto loaded variables.

EDIT: Created a second commit that reflects the suggestions in this comment.

Alternative solution based on the additional comments in the PR.
Copy link
Contributor

@mildwonkey mildwonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but we should probably change the title of this PR to match the current behavior!

@svanharmelen svanharmelen changed the title Merge instead of replace any backend operation variables backend/local: do not use backend operation variables Oct 23, 2018
@svanharmelen svanharmelen merged commit 4a493e6 into v0.11 Oct 23, 2018
@svanharmelen svanharmelen deleted the b-vars branch October 23, 2018 14:39
@ghost
Copy link

ghost commented Apr 1, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants