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

New Terraform Workspace select flag: -or-create #31633

Merged
merged 6 commits into from Dec 16, 2022

Conversation

brittandeyoung
Copy link
Contributor

@brittandeyoung brittandeyoung commented Aug 12, 2022

This adds the new terraform workspace select flag -or-create. This flag is intended to make managing workspaces easier in CI environments where auto creating or selecting a workspace is useful (especially when using remote backend). This flag will first attempt to select the provided workspace name. If this workspace is not found, it will then create and select it.

Traditionally this functionality has been achieved through some sort of scripting. We currently get this behavior using the following script: terraform workspace select test || terraform workspace new test; however, this does not work well when attempting to use the official terraform docker image as it does not handle shell scripts by design. Having this functionality in the terraform cli itself would be very useful. We want to be able to provide the same functionally locally to our developers as exists in our CI environment. Using the terraform official docker image helps provide this, but missing this functionality is making it difficult to auto provision workspaces for new projects. Calling the workspace new command will error if it already exists, and calling the workspace select command will error if it does not exist.

This will especially help when using terraform with https://dagger.io or any other docker based CI as it relies on docker for running pipelines where bash scripting my not be an option.

Closes: #16191

@hashicorp-cla
Copy link

hashicorp-cla commented Aug 12, 2022

CLA assistant check
All committers have signed the CLA.

@crw
Copy link
Collaborator

crw commented Aug 12, 2022

Although I cannot commit to having this PR reviewed at this time, we acknowledge your contribution and appreciate it!

@brittandeyoung
Copy link
Contributor Author

@crw Does this comment mean that this PR will not be reviewed, or that there will be a delay in the review? This feature will be a huge help to those running terraform using docker.

@crw
Copy link
Collaborator

crw commented Aug 31, 2022

Hi @brittandeyoung, thanks for the bump. First off thanks for the great initial write-up for the PR - that is always a huge help.

The initial feedback is that per #16191 (comment), a flag would be more appropriate than a new subcommand. Implementing this feature as a flag would make it more likely to get reviewed. Thanks for this submission, and apologies for the late response on this.

@brittandeyoung
Copy link
Contributor Author

@crw Thank you for the direction. I will modify this PR to match what is requested in #16191 and update.

@brittandeyoung brittandeyoung changed the title New Terraform Workspace subcommand: selectornew @brittandeyoung New Terraform Workspace select flag: -or-create Aug 31, 2022
@brittandeyoung brittandeyoung changed the title @brittandeyoung New Terraform Workspace select flag: -or-create New Terraform Workspace select flag: -or-create Aug 31, 2022
@brittandeyoung
Copy link
Contributor Author

brittandeyoung commented Aug 31, 2022

@crw Pull request has been update to follow the strategy suggested. It is ready for review when you have time.

Here is a screen shot running the binary local with both the select a valid state, select a missing state, and select a missing state with new -or-create flag.

image

@brittandeyoung
Copy link
Contributor Author

brittandeyoung commented Sep 9, 2022

Friendly bump @crw. Do we have an idea as to when or if this pull request will be able to be reviewed? The pull request is much smaller now using the flag.

@crw
Copy link
Collaborator

crw commented Sep 9, 2022

Hi @brittandeyoung, thanks for the bump! I admit I lost track of this due to the long Labor Day weekend. We are working on finishing up the 1.3 release so apologize if this process is going a bit slowly. There is a bit of functionality coming that may overlap with this feature -- I'm looking into whether it makes sense to review this or wait for the new functionality. I'll keep you updated. Thanks for your patience.

@brittandeyoung
Copy link
Contributor Author

@crw Thank you for the update. I will wait to hear back on what you find in terms of functionality overlap.

@jbardin jbardin self-assigned this Sep 16, 2022
c.Ui.Error(fmt.Sprintf(envDoesNotExist, name))
return 1
if orCreate {
_, err = b.StateMgr(name)
Copy link
Member

Choose a reason for hiding this comment

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

This is a "Time of Check Time of Use" race condition, but the workspace select command doesn't have the ability to take a lock. I think this may need to incorporate the new command. I'll have to look into the other code path to see what that entails.

Copy link
Member

Choose a reason for hiding this comment

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

OK, double checked and the StateMgr implementations are expected to handle the check race again internally, so that's not an issue here. The missing options are then only to exclude the lock, but I think for the sake of keeping this simple, we can leave that to using the new subcommand directly,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jbardin are there any changes needed? From reading your comments we are good to leave state locking for the new command?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jbardin Are changes actually needed here? or am I able to resolve this conversation?

Copy link
Member

Choose a reason for hiding this comment

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

sorry, no changes needed here

@brittandeyoung
Copy link
Contributor Author

@crw Looks like 1.3.0 release is out. I did not see any potential overlap in the release notes. Any update on that end?

@crw
Copy link
Collaborator

crw commented Sep 22, 2022

@brittandeyoung Yes, apologies - @jbardin moving forward with the review is the acknowledgement that we are reviewing this for inclusion. I'll check with @jbardin on what we need to do to press "merge." Thanks again for the contribution!

@brittandeyoung
Copy link
Contributor Author

@crw and @jbardin Any update on this PR?

@brittandeyoung
Copy link
Contributor Author

@crw ans @jbardin any update on this pr? Am I missing a required change?

@crw
Copy link
Collaborator

crw commented Oct 17, 2022

Hi @brittandeyoung, this is still on @jbardin's radar. He's just been busy closing issues in the 1.3 release of Terraform. This and one other community PR are in the pipe to be reviewed once 1.3 is stable. Thanks for checking in, apologies for the stop-and-start here.

@brittandeyoung
Copy link
Contributor Author

@crw thank you for the response. I will patiently wait for y'all to get caught up. I just wanted to make sure I wasn't missing something.

@crw
Copy link
Collaborator

crw commented Nov 11, 2022

Quick update that I have not forgotten this, we are still working on bugs from 1.3. This is still in the queue to be reviewed.

@brittandeyoung
Copy link
Contributor Author

@crw thank you for the update 👍

@brittandeyoung
Copy link
Contributor Author

@crw and @jbardin . Friendly bump to make sure this didn't fall of the radar.

@anderssonw
Copy link

This would be a great feature for us, as our action feels needlessly complicated just to deal with the potential creation of a workspace :)

@crw
Copy link
Collaborator

crw commented Dec 13, 2022

1.3 seems to have settled out - I'll get this back in the queue. Thanks!

Copy link
Member

@jbardin jbardin left a comment

Choose a reason for hiding this comment

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

just a quick typo

internal/command/workspace_select.go Outdated Show resolved Hide resolved
Co-authored-by: James Bardin <j.bardin@gmail.com>
@vercel
Copy link

vercel bot commented Dec 16, 2022

@brittandeyoung is attempting to deploy a commit to the HashiCorp Team on Vercel.

A member of the Team first needs to authorize it.

@jbardin jbardin merged commit 25ac4d3 into hashicorp:main Dec 16, 2022
@github-actions
Copy link

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@anderssonw
Copy link

@jbardin @crw Any plans for which release this flag will be available?

@crw
Copy link
Collaborator

crw commented Dec 20, 2022

1.4, which is the next non-bugfix release. I believe it should start showing up in the alpha builds when we do the next one of those, which will likely be in January.

@anderssonw
Copy link

Cheers :)

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terraform workspace select - Option to create if it doesnt exist
5 participants