diff --git a/internal/provider/curl_action.go b/internal/provider/curl_action.go index d12bb98..94426c3 100644 --- a/internal/provider/curl_action.go +++ b/internal/provider/curl_action.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "net/http" - "strconv" "time" "github.com/hashicorp/terraform-plugin-framework-validators/actionvalidator" @@ -192,13 +191,6 @@ func (c *CurlAction) Invoke(ctx context.Context, req action.InvokeRequest, resp tflog.Debug(ctx, fmt.Sprintf("Invoke Action Call: \nURL: %s\nHeaders: %s\nMethod: %s\nRequest Body: %s\n", request.URL.String(), request.Header, request.Method, request.Body)) - var responseCodes []string - for _, v := range data.ResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - responseCodes = append(responseCodes, strVal.ValueString()) - } - } - timeout := 10 * time.Second if !data.Timeout.IsNull() { timeout = time.Duration(data.Timeout.ValueInt64()) * time.Second @@ -224,7 +216,7 @@ func (c *CurlAction) Invoke(ctx context.Context, req action.InvokeRequest, resp _, _ = io.Copy(io.Discard, response.Body) _ = response.Body.Close() - if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(data.ResponseCodes, statusCode) { return } diff --git a/internal/provider/curl_data_source.go b/internal/provider/curl_data_source.go index bab6199..c7c1ef1 100644 --- a/internal/provider/curl_data_source.go +++ b/internal/provider/curl_data_source.go @@ -277,14 +277,7 @@ func (d *CurlDataSource) Read(ctx context.Context, req datasource.ReadRequest, r bodyString = "{}" } - var responseCodes []string - for _, v := range data.ResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - responseCodes = append(responseCodes, strVal.ValueString()) - } - } - - if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(data.ResponseCodes, statusCode) { break } diff --git a/internal/provider/curl_ephemeral_resource.go b/internal/provider/curl_ephemeral_resource.go index 38a94db..82cf30b 100644 --- a/internal/provider/curl_ephemeral_resource.go +++ b/internal/provider/curl_ephemeral_resource.go @@ -517,14 +517,7 @@ func (e *EphemeralCurlResource) Open(ctx context.Context, req ephemeral.OpenRequ bodyString = "{}" } - var responseCodes []string - for _, v := range data.ResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - responseCodes = append(responseCodes, strVal.ValueString()) - } - } - - if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(data.ResponseCodes, statusCode) { break } @@ -1171,14 +1164,7 @@ func (e *EphemeralCurlResource) Renew(ctx context.Context, req ephemeral.RenewRe bodyString = "{}" } - var responseCodes []string - for _, v := range privateData.RenewResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - responseCodes = append(responseCodes, strVal.ValueString()) - } - } - - if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(privateData.RenewResponseCodes, statusCode) { break } @@ -1525,19 +1511,10 @@ func (e *EphemeralCurlResource) Close(ctx context.Context, req ephemeral.CloseRe return } - var expectedCodes []string tflog.Debug(ctx, fmt.Sprintf("private data response code list: %v", privateData.CloseResponseCodes.Elements())) - for _, v := range privateData.CloseResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - expectedCodes = append(expectedCodes, strVal.ValueString()) - } - } - - tflog.Debug(ctx, fmt.Sprintf("response code received: %v", statusCode)) - tflog.Debug(ctx, fmt.Sprintf("expected response code received: %v", expectedCodes)) // Validate Response Code - if responseCodeChecker(expectedCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(privateData.CloseResponseCodes, statusCode) { tflog.Debug(ctx, "Close request completed successfully") break } else { diff --git a/internal/provider/curl_resource.go b/internal/provider/curl_resource.go index 5334fa1..40fff52 100644 --- a/internal/provider/curl_resource.go +++ b/internal/provider/curl_resource.go @@ -577,14 +577,7 @@ func (r *CurlResource) Create(ctx context.Context, req resource.CreateRequest, r bodyString = "{}" } - var responseCodes []string - for _, v := range data.ResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - responseCodes = append(responseCodes, strVal.ValueString()) - } - } - - if responseCodeChecker(responseCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(data.ResponseCodes, statusCode) { break } @@ -739,7 +732,7 @@ func (r *CurlResource) Read(ctx context.Context, req resource.ReadRequest, resp } // Drift detection - if oldSanitized != sanitizedResponse { + if !responseCodeChecker(data.ReadResponseCodes, httpResp.StatusCode) || (oldSanitized != "null" && oldSanitized != sanitizedResponse) { tflog.Warn(ctx, "Drift detected: Response has changed, marking for recreation.") data.DriftMarker = types.StringValue(time.Now().Format(time.RFC3339Nano)) } else { @@ -878,15 +871,8 @@ func (r *CurlResource) Delete(ctx context.Context, req resource.DeleteRequest, r return } - var expectedCodes []string - for _, v := range data.DestroyResponseCodes.Elements() { - if strVal, ok := v.(types.String); ok { - expectedCodes = append(expectedCodes, strVal.ValueString()) - } - } - // Validate Response Code - if responseCodeChecker(expectedCodes, strconv.Itoa(statusCode)) { + if responseCodeChecker(data.DestroyResponseCodes, statusCode) { tflog.Debug(ctx, "Destroy request completed successfully") break } else { diff --git a/internal/provider/utilities.go b/internal/provider/utilities.go index a527ff0..e968b91 100644 --- a/internal/provider/utilities.go +++ b/internal/provider/utilities.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "os" + "strconv" "strings" "time" @@ -115,9 +116,18 @@ func setDataSourceResponseValues(data *CurlDataSourceModel, body string) { ) } -func responseCodeChecker(s []string, str string) bool { - for _, v := range s { - if v == str { +func responseCodeChecker(expectedStatusCodes types.List, receivedStatusCode int) bool { + var responseStatusCodes []string + for _, v := range expectedStatusCodes.Elements() { + if strVal, ok := v.(types.String); ok { + responseStatusCodes = append(responseStatusCodes, strVal.ValueString()) + } + } + + receivedStatusCodeAsInt := strconv.Itoa(receivedStatusCode) + + for _, v := range responseStatusCodes { + if v == receivedStatusCodeAsInt { return true } } diff --git a/internal/provider/utilities_test.go b/internal/provider/utilities_test.go index 2180a9b..c44b4f2 100644 --- a/internal/provider/utilities_test.go +++ b/internal/provider/utilities_test.go @@ -1,6 +1,8 @@ package provider import ( + "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/types" "io" "net/http" "net/http/httptest" @@ -54,17 +56,19 @@ func TestSanitizeResponse(t *testing.T) { func TestResponseCodeChecker(t *testing.T) { tests := []struct { name string - codes []string - input string + codes []attr.Value + input int expected bool }{ - {"Value Present", []string{"200", "404", "500"}, "404", true}, - {"Value Absent", []string{"200", "500"}, "404", false}, + {"Value Present", []attr.Value{types.StringValue("200"), types.StringValue("404"), types.StringValue("500")}, 404, true}, + {"Value Absent", []attr.Value{types.StringValue("200"), types.StringValue("500")}, 404, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := responseCodeChecker(tt.codes, tt.input) + var listValue types.List + listValue, _ = types.ListValue(types.StringType, tt.codes) + result := responseCodeChecker(listValue, tt.input) if result != tt.expected { t.Errorf("Expected %v, got %v", tt.expected, result) }