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

Support workspaces for multi-state migrations #31

Merged
merged 14 commits into from
Aug 3, 2021

Conversation

dhaven
Copy link
Contributor

@dhaven dhaven commented Jul 5, 2021

Implements the workspace feature request from #3

Main logic is in setupWorkDir() where I create the local workspace folder required before calling OverrideBackendToLocal()

Would love some feedback on this PR @minamijoyo

@minamijoyo
Copy link
Owner

@dhaven Thank you for working on this! I'll review this after studying how workspace works in Terraform core. Please wait.

)

// tfWorkspaceRe is a pattern to parse outputs from terraform workspace show.
var tfWorkspaceRe = regexp.MustCompile(`^[a-zA-Z\d_-]{1,90}`)
Copy link
Owner

Choose a reason for hiding this comment

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

I couldn't find any docs for a valid workspace name.
Could you tell me where this regular expression came from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found it here : https://www.terraform.io/docs/cloud/workspaces/naming.html

Workspace names need to be 90 characters or less and can only include letters, numbers, -, and _.

Looking online that doesn't actually seem to be correct and the actual expectation is that the workspace name should be url-safe. (see: hashicorp/terraform#24564).

All things considered I don't think it is useful to implement this check as it is already handled by terraform so I have simply removed it.

Copy link
Owner

@minamijoyo minamijoyo Aug 2, 2021

Choose a reason for hiding this comment

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

It's a common pitfall for workspace. The word workspace has two meanings in Terraform. That is, one is a workspace for Terraform Cloud and the other is a workspace for Terraform CLI. The doc you liked is for Terraform Cloud. It's actually not related to terraform workspace commands.

From the error message, I found the validation for terraform workspace new. It only requires a URL safe string.
https://github.com/hashicorp/terraform/blob/v1.0.3/internal/command/workspace_command.go#L42-L47

However, I agree that we don't need duplicated validation here by ourselves 👍

tfmigrate/migrator.go Outdated Show resolved Hide resolved
@@ -90,18 +113,19 @@ func TestAccMultiStateMigratorApply(t *testing.T) {
ctx := context.Background()

fromBackend := tfexec.GetTestAccBackendS3Config(t.Name() + "/fromDir")
fromWorkspace := "work1"
Copy link
Owner

Choose a reason for hiding this comment

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

I want to confirm this change doesn't break default workspace behavior.
Can we parameterize the fromWorkspace and toWorkspace variables with the table-driven test?

Copy link
Owner

Choose a reason for hiding this comment

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

Or just copy and paste to a new test case such as TestAccMultiStateMigratorApplyWithWorkspace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have followed your suggestion to use table-driven test. Please let me know what you think about the new test and if any changes are required.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks. Great work 👍

@minamijoyo
Copy link
Owner

Hi, @dhaven. Sorry for the late review.

Although creating a local workspace directory terraform.tfstate.d without the workspace new command depends on undocumented behaviors, I agree it's a reasonable compromise under the current technical limitations.

I think your implementation is almost good. I left some minor comments. Please check. Thanks again!

tfexec/terraform_test.go Outdated Show resolved Hide resolved
ok: true,
},
{
desc: "with existing workspace",
Copy link
Owner

Choose a reason for hiding this comment

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

desc: "with an error",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure what you mean here because the second test case doesn't expect an error from the terraform command. I've added a third test case to test for an error returned by terraform workspace select

Copy link
Owner

Choose a reason for hiding this comment

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

My mistake. I guess when I wrote this comment, I probably misread the 2nd test case returns an error, but now I'm reading the 2nd test case with fresh eyes, apparently I was wrong. Sorry. Although, it's good to add the 3rd error case you suggested. Thanks!

if changed != tc.toStateExpectChange {
t.Fatalf("expected change in toDir is %t but actual value is %t", tc.toStateExpectChange, changed)
}
})
Copy link
Owner

Choose a reason for hiding this comment

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

Looks good 😄

* [multi_state mv](#multi_state-mv)
* [License](#license)
<!--te-->

Copy link
Owner

Choose a reason for hiding this comment

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

Cool 👍

Copy link
Owner

@minamijoyo minamijoyo left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@minamijoyo minamijoyo merged commit acf1118 into minamijoyo:master Aug 3, 2021
@minamijoyo
Copy link
Owner

@dhaven Shipped it in v0.2.6. Thank you for your great work 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants