From 843f05f0e5e178fe749c09af5dfc2cc7b17f3382 Mon Sep 17 00:00:00 2001 From: Evan Rademacher <44209290+erademacher@users.noreply.github.com> Date: Thu, 5 Jan 2023 11:39:17 -0800 Subject: [PATCH] Misc bug fixes (#70) --- .../provider/cockroach_cluster_data_source.go | 8 +++-- internal/provider/networking_resource.go | 35 ++++++++++++------ internal/provider/sql_user_resource.go | 36 ++++++++++++------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/internal/provider/cockroach_cluster_data_source.go b/internal/provider/cockroach_cluster_data_source.go index 13373919..c49e6497 100644 --- a/internal/provider/cockroach_cluster_data_source.go +++ b/internal/provider/cockroach_cluster_data_source.go @@ -23,7 +23,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -121,7 +120,7 @@ func (d *clusterDataSource) Metadata(_ context.Context, req datasource.MetadataR resp.TypeName = req.ProviderTypeName + "_cluster" } -func (d *clusterDataSource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { +func (d *clusterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { if req.ProviderData == nil { return } @@ -133,6 +132,11 @@ func (d *clusterDataSource) Configure(_ context.Context, req resource.ConfigureR } func (d *clusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + if d.provider == nil || !d.provider.configured { + addConfigureProviderErr(&resp.Diagnostics) + return + } + var cluster CockroachCluster diags := req.Config.Get(ctx, &cluster) diff --git a/internal/provider/networking_resource.go b/internal/provider/networking_resource.go index 62ab02e2..3676ff8c 100644 --- a/internal/provider/networking_resource.go +++ b/internal/provider/networking_resource.go @@ -19,6 +19,7 @@ package provider import ( "context" "fmt" + "net/http" "regexp" "strconv" @@ -175,12 +176,20 @@ func (r *allowListResource) Read(ctx context.Context, req resource.ReadRequest, // Since the state may have come from an import, we need to retrieve // the actual entry list and make sure this one is in there. - apiResp, _, err := r.provider.service.ListAllowlistEntries(ctx, state.ClusterId.ValueString(), &client.ListAllowlistEntriesOptions{}) + apiResp, httpResp, err := r.provider.service.ListAllowlistEntries(ctx, state.ClusterId.ValueString(), &client.ListAllowlistEntriesOptions{}) if err != nil { - resp.Diagnostics.AddError( - "Couldn't retrieve allowlist entries", - fmt.Sprintf("Unexpected error retrieving allowlist entries: %s", formatAPIErrorMessage(err)), - ) + if httpResp != nil && httpResp.StatusCode == http.StatusNotFound { + resp.Diagnostics.AddWarning( + "Cluster not found", + fmt.Sprintf("Allowlist's parent cluster with clusterID %s is not found. Removing from state.", state.ClusterId.ValueString())) + resp.State.RemoveResource(ctx) + } else { + resp.Diagnostics.AddError( + "Couldn't retrieve allowlist entries", + fmt.Sprintf("Unexpected error retrieving allowlist entries: %s", formatAPIErrorMessage(err)), + ) + } + return } if resp.Diagnostics.HasError() { return @@ -257,13 +266,17 @@ func (r *allowListResource) Delete(ctx context.Context, req resource.DeleteReque return } - _, _, err := r.provider.service.DeleteAllowlistEntry(ctx, state.ClusterId.ValueString(), state.CidrIp.ValueString(), int32(state.CidrMask.ValueInt64())) + _, httpResp, err := r.provider.service.DeleteAllowlistEntry(ctx, state.ClusterId.ValueString(), state.CidrIp.ValueString(), int32(state.CidrMask.ValueInt64())) if err != nil { - resp.Diagnostics.AddError( - "Error deleting network allowlist", - fmt.Sprintf("Could not delete network allowlist: %s", formatAPIErrorMessage(err)), - ) - return + if httpResp != nil && httpResp.StatusCode == http.StatusNotFound { + // Entry or cluster is already gone. Swallow the error. + } else { + resp.Diagnostics.AddError( + "Error deleting network allowlist", + fmt.Sprintf("Could not delete network allowlist: %s", formatAPIErrorMessage(err)), + ) + return + } } // Remove resource from state diff --git a/internal/provider/sql_user_resource.go b/internal/provider/sql_user_resource.go index 6dd92936..7cbebe54 100644 --- a/internal/provider/sql_user_resource.go +++ b/internal/provider/sql_user_resource.go @@ -19,6 +19,7 @@ package provider import ( "context" "fmt" + "net/http" "regexp" "github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client" @@ -145,14 +146,19 @@ func (r *sqlUserResource) Read(ctx context.Context, req resource.ReadRequest, re // Since the state may have come from an import, we need to retrieve // the actual user list and make sure this one is in there. - apiResp, _, err := r.provider.service.ListSQLUsers(ctx, state.ClusterId.ValueString(), &client.ListSQLUsersOptions{}) + apiResp, httpResp, err := r.provider.service.ListSQLUsers(ctx, state.ClusterId.ValueString(), &client.ListSQLUsersOptions{}) if err != nil { - resp.Diagnostics.AddError( - "Couldn't retrieve SQL users", - fmt.Sprintf("Unexpected error retrieving SQL users: %s", formatAPIErrorMessage(err)), - ) - } - if resp.Diagnostics.HasError() { + if httpResp != nil && httpResp.StatusCode == http.StatusNotFound { + resp.Diagnostics.AddWarning( + "Cluster not found", + fmt.Sprintf("SQL User's parent cluster with clusterID %s is not found. Removing from state.", state.ClusterId.ValueString())) + resp.State.RemoveResource(ctx) + } else { + resp.Diagnostics.AddError( + "Couldn't retrieve SQL users", + fmt.Sprintf("Unexpected error retrieving SQL users: %s", formatAPIErrorMessage(err)), + ) + } return } for _, user := range apiResp.GetUsers() { @@ -209,13 +215,17 @@ func (r *sqlUserResource) Delete(ctx context.Context, req resource.DeleteRequest return } - _, _, err := r.provider.service.DeleteSQLUser(ctx, state.ClusterId.ValueString(), state.Name.ValueString()) + _, httpResp, err := r.provider.service.DeleteSQLUser(ctx, state.ClusterId.ValueString(), state.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError( - "Error deleting sql user", - fmt.Sprintf("Could not delete sql user: %s", formatAPIErrorMessage(err)), - ) - return + if httpResp != nil && httpResp.StatusCode == http.StatusNotFound { + // User or cluster is already gone. Swallow the error. + } else { + resp.Diagnostics.AddError( + "Error deleting sql user", + fmt.Sprintf("Could not delete sql user: %s", formatAPIErrorMessage(err)), + ) + return + } } // Remove resource from state