Skip to content

Commit 7e0fd48

Browse files
committed
refactor(utils): improve the status code checker
The responseCodeChecker method now takes raw parameters and does more work: convert them and check the values The resulting code is easier to use Signed-off-by: Alexandre Bruyelles <git@jack.fr.eu.org>
1 parent 304e5b6 commit 7e0fd48

5 files changed

Lines changed: 28 additions & 58 deletions

File tree

internal/provider/curl_data_source.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,7 @@ func (d *CurlDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
256256
bodyString = "{}"
257257
}
258258

259-
var responseCodes []string
260-
for _, v := range data.ResponseCodes.Elements() {
261-
if strVal, ok := v.(types.String); ok {
262-
responseCodes = append(responseCodes, strVal.ValueString())
263-
}
264-
}
265-
266-
if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) {
259+
if responseCodeChecker(data.ResponseCodes, statusCode) {
267260
break
268261
}
269262

internal/provider/curl_ephemeral_resource.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,7 @@ func (e *EphemeralCurlResource) Open(ctx context.Context, req ephemeral.OpenRequ
483483
bodyString = "{}"
484484
}
485485

486-
var responseCodes []string
487-
for _, v := range data.ResponseCodes.Elements() {
488-
if strVal, ok := v.(types.String); ok {
489-
responseCodes = append(responseCodes, strVal.ValueString())
490-
}
491-
}
492-
493-
if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) {
486+
if responseCodeChecker(data.ResponseCodes, statusCode) {
494487
break
495488
}
496489

@@ -1143,14 +1136,7 @@ func (e *EphemeralCurlResource) Renew(ctx context.Context, req ephemeral.RenewRe
11431136
bodyString = "{}"
11441137
}
11451138

1146-
var responseCodes []string
1147-
for _, v := range privateData.RenewResponseCodes.Elements() {
1148-
if strVal, ok := v.(types.String); ok {
1149-
responseCodes = append(responseCodes, strVal.ValueString())
1150-
}
1151-
}
1152-
1153-
if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) {
1139+
if responseCodeChecker(privateData.RenewResponseCodes, statusCode) {
11541140
break
11551141
}
11561142

@@ -1497,19 +1483,10 @@ func (e *EphemeralCurlResource) Close(ctx context.Context, req ephemeral.CloseRe
14971483
return
14981484
}
14991485

1500-
var expectedCodes []string
15011486
tflog.Debug(ctx, fmt.Sprintf("private data response code list: %v", privateData.CloseResponseCodes.Elements()))
1502-
for _, v := range privateData.CloseResponseCodes.Elements() {
1503-
if strVal, ok := v.(types.String); ok {
1504-
expectedCodes = append(expectedCodes, strVal.ValueString())
1505-
}
1506-
}
1507-
1508-
tflog.Debug(ctx, fmt.Sprintf("response code received: %v", statusCode))
1509-
tflog.Debug(ctx, fmt.Sprintf("expected response code received: %v", expectedCodes))
15101487

15111488
// Validate Response Code
1512-
if responseCodeChecker(expectedCodes, strconv.Itoa(statusCode)) {
1489+
if responseCodeChecker(privateData.CloseResponseCodes, statusCode) {
15131490
tflog.Debug(ctx, "Close request completed successfully")
15141491
break
15151492
} else {

internal/provider/curl_resource.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,7 @@ func (r *CurlResource) Create(ctx context.Context, req resource.CreateRequest, r
574574
bodyString = "{}"
575575
}
576576

577-
var responseCodes []string
578-
for _, v := range data.ResponseCodes.Elements() {
579-
if strVal, ok := v.(types.String); ok {
580-
responseCodes = append(responseCodes, strVal.ValueString())
581-
}
582-
}
583-
584-
if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) {
577+
if responseCodeChecker(data.ResponseCodes, statusCode) {
585578
break
586579
}
587580

@@ -875,15 +868,8 @@ func (r *CurlResource) Delete(ctx context.Context, req resource.DeleteRequest, r
875868
return
876869
}
877870

878-
var expectedCodes []string
879-
for _, v := range data.DestroyResponseCodes.Elements() {
880-
if strVal, ok := v.(types.String); ok {
881-
expectedCodes = append(expectedCodes, strVal.ValueString())
882-
}
883-
}
884-
885871
// Validate Response Code
886-
if responseCodeChecker(expectedCodes, strconv.Itoa(statusCode)) {
872+
if responseCodeChecker(data.DestroyResponseCodes, statusCode) {
887873
tflog.Debug(ctx, "Destroy request completed successfully")
888874
break
889875
} else {

internal/provider/utilities.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/types"
1010
"net/http"
1111
"os"
12+
"strconv"
1213
"time"
1314
)
1415

@@ -39,9 +40,18 @@ func sanitizeResponse(response string, fieldsToIgnore []string) (string, error)
3940
return string(filteredBytes), nil
4041
}
4142

42-
func responseCodeChecker(s []string, str string) bool {
43-
for _, v := range s {
44-
if v == str {
43+
func responseCodeChecker(expectedStatusCodes types.List, receivedStatusCode int) bool {
44+
var responseStatusCodes []string
45+
for _, v := range expectedStatusCodes.Elements() {
46+
if strVal, ok := v.(types.String); ok {
47+
responseStatusCodes = append(responseStatusCodes, strVal.ValueString())
48+
}
49+
}
50+
51+
var receivedStatusCodeAsInt = strconv.Itoa(receivedStatusCode)
52+
53+
for _, v := range responseStatusCodes {
54+
if v == receivedStatusCodeAsInt {
4555
return true
4656
}
4757
}

internal/provider/utilities_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package provider
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-framework/attr"
5+
"github.com/hashicorp/terraform-plugin-framework/types"
46
"io"
57
"net/http"
68
"net/http/httptest"
@@ -51,17 +53,19 @@ func TestSanitizeResponse(t *testing.T) {
5153
func TestResponseCodeChecker(t *testing.T) {
5254
tests := []struct {
5355
name string
54-
codes []string
55-
input string
56+
codes []attr.Value
57+
input int
5658
expected bool
5759
}{
58-
{"Value Present", []string{"200", "404", "500"}, "404", true},
59-
{"Value Absent", []string{"200", "500"}, "404", false},
60+
{"Value Present", []attr.Value{types.StringValue("200"), types.StringValue("404"), types.StringValue("500")}, 404, true},
61+
{"Value Absent", []attr.Value{types.StringValue("200"), types.StringValue("500")}, 404, false},
6062
}
6163

6264
for _, tt := range tests {
6365
t.Run(tt.name, func(t *testing.T) {
64-
result := responseCodeChecker(tt.codes, tt.input)
66+
var listValue types.List
67+
listValue, _ = types.ListValue(types.StringType, tt.codes)
68+
result := responseCodeChecker(listValue, tt.input)
6569
if result != tt.expected {
6670
t.Errorf("Expected %v, got %v", tt.expected, result)
6771
}

0 commit comments

Comments
 (0)