diff --git a/tfe/resource_tfe_workspace.go b/tfe/resource_tfe_workspace.go index ebfbbfea0..0087527b0 100644 --- a/tfe/resource_tfe_workspace.go +++ b/tfe/resource_tfe_workspace.go @@ -21,7 +21,7 @@ func resourceTFEWorkspace() *schema.Resource { Update: resourceTFEWorkspaceUpdate, Delete: resourceTFEWorkspaceDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceTFEWorkspaceImporter, }, SchemaVersion: 1, @@ -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 { + return nil, fmt.Errorf( + "invalid workspace input format: %s (expected / or )", + 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 +} diff --git a/tfe/resource_tfe_workspace_test.go b/tfe/resource_tfe_workspace_test.go index 948eeb977..fa561125f 100644 --- a/tfe/resource_tfe_workspace_test.go +++ b/tfe/resource_tfe_workspace_test.go @@ -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, + }, }, }) } diff --git a/website/docs/r/workspace.html.markdown b/website/docs/r/workspace.html.markdown index 886417632..822058362 100644 --- a/website/docs/r/workspace.html.markdown +++ b/website/docs/r/workspace.html.markdown @@ -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 `/`. -This format has been deprecated in favor of the immutable workspace ID in the format `ws-`. - -Workspaces can be imported; use `` as the +Workspaces can be imported; use `` or `/` 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 +```