From d9d18f5b2a549ad7bd5a95b093f276826f98da09 Mon Sep 17 00:00:00 2001 From: Paul Gier Date: Tue, 20 Feb 2024 15:31:45 -0600 Subject: [PATCH] improve access_list error handling Propagate error to the user when there is a problem creating an access list. --- internal/provider/resource_access_list.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/provider/resource_access_list.go b/internal/provider/resource_access_list.go index 7abf909..3c1b6f3 100644 --- a/internal/provider/resource_access_list.go +++ b/internal/provider/resource_access_list.go @@ -3,6 +3,7 @@ package provider import ( "context" "errors" + "io" "strings" "github.com/datastax/astra-client-go/v2/astra" @@ -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, @@ -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