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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion tfe/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func resourceTFEWorkspace() *schema.Resource {
Update: resourceTFEWorkspaceUpdate,
Delete: resourceTFEWorkspaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceTFEWorkspaceImporter,
},

SchemaVersion: 1,
Expand Down Expand Up @@ -642,3 +642,25 @@ func validateRemoteState(_ context.Context, d *schema.ResourceDiff, meta interfa

return nil
}

func resourceTFEWorkspaceImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tfeClient := meta.(*tfe.Client)

s := strings.Split(d.Id(), "/")
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)

return nil, fmt.Errorf(
"invalid workspace input format: %s (expected <ORGANIZATION>/<WORKSPACE NAME> or <WORKSPACE ID>)",
d.Id(),
)
} else if len(s) == 2 {
workspaceID, err := fetchWorkspaceExternalID(s[0]+"/"+s[1], tfeClient)
if err != nil {
return nil, fmt.Errorf(
"error retrieving workspace with name %s from organization %s %v", s[1], s[0], err)
}

d.SetId(workspaceID)
}

return []*schema.ResourceData{d}, nil
}
6 changes: 6 additions & 0 deletions tfe/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@ func TestAccTFEWorkspace_import(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "tfe_workspace.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("tst-terraform-%d/workspace-test", rInt),
ImportStateVerify: true,
},
},
})
}
Expand Down
9 changes: 5 additions & 4 deletions website/docs/r/workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ In addition to all arguments above, the following attributes are exported:

## Import

~> **NOTE** In versions < 0.15.1, the import ID was in the format `<ORGANIZATION NAME>/<WORKSPACE NAME>`.
This format has been deprecated in favor of the immutable workspace ID in the format `ws-<RANDOM STRING>`.

Workspaces can be imported; use `<WORKSPACE ID>` as the
Workspaces can be imported; use `<WORKSPACE ID>` or `<ORGANIZATION NAME>/<WORKSPACE NAME>` as the
import ID. For example:

```shell
terraform import tfe_workspace.test ws-CH5in3chf8RJjrVd
```

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