Skip to content

Commit

Permalink
improve access_list error handling (#364)
Browse files Browse the repository at this point in the history
Propagate error to the user when there is a problem creating an access
list.
Fixes #361 

This is caused by the Astra client library swallowing parse errors, so
that the user doesn't see the actual error message. This should be fixed
in the client library at some point.
datastax/astra-client-go#32
  • Loading branch information
pgier authored Feb 20, 2024
1 parent b63668b commit e42b39b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions internal/provider/resource_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"errors"
"io"
"strings"

"github.com/datastax/astra-client-go/v2/astra"
Expand Down Expand Up @@ -77,21 +78,22 @@ func resourceAccessListCreate(ctx context.Context, d *schema.ResourceData, meta
restricted := d.Get("enabled").(bool)
addressList := getAddressList(addresses)

addResp, err := client.AddAddressesToAccessListForDatabaseWithResponse(ctx,
addResp, err := client.AddAddressesToAccessListForDatabase(ctx,
astra.DatabaseIdParam(databaseID),
addressList,
)

if err != nil {
return diag.FromErr(err)
} else if addResp.StatusCode() >= 400 {
return diag.Errorf("error adding access list to database: (%d) %s", addResp.StatusCode(), addResp.Body)
} else if addResp.StatusCode >= 400 {
respBody, _ := io.ReadAll(addResp.Body)
return diag.Errorf("failed to create access list, unexpected status code: %v, message: '%s'", addResp.StatusCode, respBody)
}

d.SetId(databaseID)

accessListConfig := astra.AccessListConfigurations{AccessListEnabled: restricted}
updResp, err := client.UpdateAccessListForDatabaseWithResponse(ctx,
updResp, err := client.UpdateAccessListForDatabase(ctx,
astra.DatabaseIdParam(databaseID),
astra.UpdateAccessListForDatabaseJSONRequestBody{
Addresses: &addressList,
Expand All @@ -100,8 +102,9 @@ func resourceAccessListCreate(ctx context.Context, d *schema.ResourceData, meta
)
if err != nil {
return diag.FromErr(err)
} else if updResp.StatusCode() >= 400 {
return diag.Errorf("error updating access list configuration: %d\n%s", updResp.StatusCode(), updResp.Body)
} else if updResp.StatusCode >= 400 {
respBody, _ := io.ReadAll(addResp.Body)
return diag.Errorf("error updating access list configuration: %d\n%s", updResp.StatusCode, respBody)
}

return nil
Expand Down

0 comments on commit e42b39b

Please sign in to comment.