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

issue-227: improve error handling for access list resource #298

Merged
merged 1 commit into from
Sep 6, 2023
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
25 changes: 16 additions & 9 deletions internal/provider/data_source_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package provider

import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/datastax/astra-client-go/v2/astra"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -82,28 +85,32 @@ func dataSourceAccessListRead(ctx context.Context, d *schema.ResourceData, meta
}

func listAccessList(ctx context.Context, client *astra.ClientWithResponses, databaseID string) (*astra.AccessListResponse, error) {
resp, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
dbResp, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
if err != nil {
return nil, err
}

db := resp.JSON200
db := dbResp.JSON200
if db == nil {
return nil, fmt.Errorf("error fetching database: %s", string(resp.Body))
return nil, fmt.Errorf("error fetching database: %s", string(dbResp.Body))
}

// If the database is terminated then the private links have been deleted.
// If the database is terminated then the access list has been deleted.
if db.Status == astra.TERMINATING || db.Status == astra.TERMINATED {
return nil, nil
}

alResponse, err := client.GetAccessListForDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))

resp, err := client.GetAccessListForDatabase(ctx, astra.DatabaseIdParam(databaseID))
if err != nil {
return nil, err
} else if resp.StatusCode > 299 {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("unexpected status code: %v, message: '%s'", resp.StatusCode, body)
}
accessList := astra.AccessListResponse{}
if err := json.NewDecoder(resp.Body).Decode(&accessList); err != nil {
return nil, fmt.Errorf("failed to parse access list response: %w", err)
}

accessListOutput := alResponse.JSON200

return accessListOutput, err
return &accessList, nil
}
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func configure(providerVersion string, p *schema.Provider) func(context.Context,
retryClient.RetryMax = 10
retryClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
// Never retry POST requests because of side effects
if resp.Request.Method == "POST" {
if resp != nil && resp.Request.Method == "POST" {
return false, err
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func resourceAccessListRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

if string(*accessList.DatabaseId) == databaseID {
if accessList != nil && string(*accessList.DatabaseId) == databaseID {
if err := setAccessListData(d, accessList); err != nil {
return diag.FromErr(err)
}
Expand Down
1 change: 1 addition & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ setup_env() {
TEST_ENV_FILE="${SCRIPT_PATH}/${DEFAULT_TEST_ENV_FILE}"
fi
if [ -f "$TEST_ENV_FILE" ]; then
echo "loading config from file $TEST_ENV_FILE"
source "$TEST_ENV_FILE"
else
echo "file '$TEST_ENV_FILE' not found, some tests may be skipped"
Expand Down
Loading