Skip to content

Commit

Permalink
Misc bug fixes (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
erademacher committed Jan 5, 2023
1 parent b8aa8f4 commit 843f05f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
8 changes: 6 additions & 2 deletions internal/provider/cockroach_cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)

Expand Down
35 changes: 24 additions & 11 deletions internal/provider/networking_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package provider
import (
"context"
"fmt"
"net/http"
"regexp"
"strconv"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 23 additions & 13 deletions internal/provider/sql_user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package provider
import (
"context"
"fmt"
"net/http"
"regexp"

"github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 843f05f

Please sign in to comment.