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

Allow Importing workspaces using workspace name #401

Merged
merged 4 commits into from
Jan 7, 2022

Conversation

sebasslash
Copy link
Contributor

@sebasslash sebasslash commented Jan 4, 2022

Description

This PR adds support for importing workspaces using a <ORGANIZATION NAME>/<WORKSPACE NAME> nested combo. You can still pass a workspace external id.

An example import command looks as follows:

terraform import tfe_workspace.test my-org-name/my-wkspace-name

How to test

To run acceptance tests:

TESTARGS="-run TestAccTFEWorkspace_import" make testacc

To smoke test:

  1. Create a Terraform configuration file, setting the tfe provider and the workspace resource we'll import to.
provider "tfe" {
  hostname = "my-tfe-host"
  token = "my-tfe-token
}

resource "tfe_workspace" "test" {}

Ensure you have the dev overrides set to the compiled binary of this PR in your go/bin.

  1. Create an organization in your TFE host and workspace.
  2. Import!
terraform import tfe_workspace.test org-name/wkspace-name

@sebasslash sebasslash requested a review from a team as a code owner January 4, 2022 23:02
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

I tried this and it worked with both methods. I think you can test import by using the ImportState/ImportStateId property of resource.TestStep.

tfeClient := meta.(*tfe.Client)

s := strings.SplitN(d.Id(), "/", 2)
if len(s) >= 3 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

At most 2 strings will be returned by strings.SplitN(d.Id(), "/", 2)

}

d.SetId(workspaceID)
return []*schema.ResourceData{d}, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line can be omitted

This changes the Importer function for the Workspace resource to handle passing an
organization name and workspace name pair to import the workspace. If no pair is
specified the importer function will assume an opaque external id was passed.

An example import command looks as follows:

terraform import tfe_workspace.test my-org-name/my-wkspace-name
@brandonc brandonc force-pushed the sebasslash/allow-wkspace-import-by-name branch from 4c3f400 to 3c5f110 Compare January 7, 2022 03:15
@brandonc brandonc merged commit f5aee0a into main Jan 7, 2022
@brandonc brandonc deleted the sebasslash/allow-wkspace-import-by-name branch January 7, 2022 03:28
@chrisarcand
Copy link
Member

Nice! 👏🏻

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.

3 participants