Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: delete resources & data sources if not found #102

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

Merged
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
10 changes: 10 additions & 0 deletions internal/provider/group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
groupID := data.ID.ValueUUID()
group, err = client.Group(ctx, groupID)
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group by ID, got error: %s", err))
return
}
Expand All @@ -195,6 +200,11 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
} else {
group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with name %s not found in organization with ID %s. Marking as deleted.", data.Name.ValueString(), data.OrganizationID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Failed to get group by name and org ID", err.Error())
return
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp

group, err := client.Group(ctx, groupID)
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err))
return
}
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/license_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func (r *LicenseResource) Read(ctx context.Context, req resource.ReadRequest, re
}
}
if !found {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("License with ID %d not found", data.ID.ValueInt32()))
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("License with ID %d not found. Marking as deleted.", data.ID.ValueInt32()))
resp.State.RemoveResource(ctx)
return
}

// Save updated data into Terraform state
Expand Down
15 changes: 15 additions & 0 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
orgID := data.ID.ValueUUID()
org, err = client.Organization(ctx, orgID)
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Organization with ID %s not found. Marking as deleted.", data.ID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by ID, got error: %s", err))
return
}
Expand All @@ -137,6 +142,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
} else if data.IsDefault.ValueBool() { // Get Default
org, err = client.OrganizationByName(ctx, "default")
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", "Default organization not found. Marking as deleted.")
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get default organization, got error: %s", err))
return
}
Expand All @@ -147,6 +157,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
} else { // By Name
org, err = client.OrganizationByName(ctx, data.Name.ValueString())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Organization with name %s not found. Marking as deleted.", data.Name))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by name, got error: %s", err))
return
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/template_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadReques
template, err = client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
}
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", "Template not found. Marking as deleted.")
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get template, got error: %s", err))
return
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r

template, err := client.Template(ctx, templateID)
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Template with ID %s not found. Marking as deleted.", templateID.String()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template: %s", err))
return
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
}
user, err := client.User(ctx, ident)
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("User with identifier %q not found. Marking as deleted.", ident))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
return
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp

user, err := client.User(ctx, data.ID.ValueString())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("User with ID %q not found. Marking as deleted.", data.ID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
return
}
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package provider
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"

"github.com/coder/coder/v2/codersdk"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -113,3 +116,8 @@ func memberDiff(curMembers []uuid.UUID, plannedMembers []UUID) (add, remove []st
}
return add, remove
}

func isNotFound(err error) bool {
var sdkErr *codersdk.Error
return errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound
}
5 changes: 5 additions & 0 deletions internal/provider/workspace_proxy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ func (r *WorkspaceProxyResource) Read(ctx context.Context, req resource.ReadRequ
client := r.data.Client
wsp, err := client.WorkspaceProxyByID(ctx, data.ID.ValueUUID())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Workspace proxy with ID %s not found. Marking as deleted.", data.ID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to read workspace proxy: %v", err))
return
}
Expand Down
Loading