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
Changes from 1 commit
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