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

Try to find a workspace by external ID before removing it #51

Merged
merged 2 commits into from Jan 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 38 additions & 3 deletions tfe/resource_tfe_workspace.go
Expand Up @@ -176,13 +176,48 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {

log.Printf("[DEBUG] Read configuration of workspace: %s", name)
workspace, err := tfeClient.Workspaces.Read(ctx, organization, name)
if err != nil {
if err == tfe.ErrResourceNotFound {
if err != nil && err != tfe.ErrResourceNotFound {
return fmt.Errorf("Error reading configuration of workspace %s: %v", name, err)
}

// If we cannot find the workspace, it either doesn't exist anymore or is
// renamed. To make sure the workspace is really gone before we delete it
// from our state, we will list all workspaces and try to find it using
// the external ID.
if err == tfe.ErrResourceNotFound {
// Set the workspace to nil so we can check if we found one later.
workspace = nil

options := tfe.WorkspaceListOptions{}
externalID := d.Get("external_id").(string)
for {
wl, err := tfeClient.Workspaces.List(ctx, organization, options)
if err != nil {
return fmt.Errorf("Error retrieving workspaces: %v", err)
}

for _, w := range wl.Items {
if externalID == w.ID {
workspace = w
break
}
}

// Exit the loop if we found the workspace or have seen all pages.
if workspace != nil || wl.CurrentPage >= wl.TotalPages {
break
}

// Update the page number to get the next page.
options.PageNumber = wl.NextPage
}

// Return if we didn't find a matching workspace.
if workspace == nil {
log.Printf("[DEBUG] Workspace %s does no longer exist", name)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of workspace %s: %v", name, err)
}

// Update the config.
Expand Down
78 changes: 78 additions & 0 deletions tfe/resource_tfe_workspace_test.go
@@ -1,7 +1,9 @@
package tfe

import (
"context"
"fmt"
"log"
"testing"

tfe "github.com/hashicorp/go-tfe"
Expand Down Expand Up @@ -117,6 +119,52 @@ func TestAccTFEWorkspace_basic(t *testing.T) {
})
}

func TestAccTFEWorkspace_renamed(t *testing.T) {
workspace := &tfe.Workspace{}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceAttributes(workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "name", "workspace-test"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "auto_apply", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "queue_all_runs", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "working_directory", ""),
),
},

{
PreConfig: testAccCheckTFEWorkspaceRename,
Config: testAccTFEWorkspace_renamed,
PlanOnly: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceAttributes(workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "name", "workspace-test"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "auto_apply", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "queue_all_runs", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "working_directory", ""),
),
},
},
})
}
func TestAccTFEWorkspace_update(t *testing.T) {
workspace := &tfe.Workspace{}

Expand Down Expand Up @@ -288,6 +336,24 @@ func testAccCheckTFEWorkspaceAttributes(
}
}

func testAccCheckTFEWorkspaceRename() {
tfeClient := testAccProvider.Meta().(*tfe.Client)

w, err := tfeClient.Workspaces.Update(
context.Background(),
"terraform-test",
"workspace-test",
tfe.WorkspaceUpdateOptions{Name: tfe.String("renamed-out-of-band")},
)
if err != nil {
log.Fatalf("Could not rename the workspace out of band: %v", err)
}

if w.Name != "renamed-out-of-band" {
log.Fatalf("Failed to rename the workspace out of band: %v", err)
}
}

func testAccCheckTFEWorkspaceAttributesUpdated(
workspace *tfe.Workspace) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -369,6 +435,18 @@ resource "tfe_workspace" "foobar" {
auto_apply = true
}`

const testAccTFEWorkspace_renamed = `
resource "tfe_organization" "foobar" {
name = "terraform-test"
email = "admin@company.com"
}

resource "tfe_workspace" "foobar" {
name = "renamed-out-of-band"
organization = "${tfe_organization.foobar.id}"
auto_apply = true
}`

const testAccTFEWorkspace_update = `
resource "tfe_organization" "foobar" {
name = "terraform-test"
Expand Down