From ff943fcc13f0ce9b6177bb64197ccff07de31a80 Mon Sep 17 00:00:00 2001 From: Vaishak Dinesh Date: Wed, 18 Dec 2024 14:28:48 -0800 Subject: [PATCH 1/2] fix: api shield tests and test data --- .../services/api_shield_operation/custom.go | 10 +++++++ .../api_shield_operation/resource_test.go | 27 ++++++++++++------- .../testdata/apishieldoperation.tf | 17 +++++++----- 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 internal/services/api_shield_operation/custom.go diff --git a/internal/services/api_shield_operation/custom.go b/internal/services/api_shield_operation/custom.go new file mode 100644 index 0000000000..425c16379d --- /dev/null +++ b/internal/services/api_shield_operation/custom.go @@ -0,0 +1,10 @@ +package api_shield_operation + +func setOperationValues(data *APIShieldOperationModel, env APIShieldOperationResultEnvelope) { + data.Endpoint = (*env.Result)[0].Endpoint + data.Host = (*env.Result)[0].Host + data.Method = (*env.Result)[0].Method + // TODO:: after schema changes land + //data.LastUpdated = (*env.Result)[0].LastUpdated + //data.OperationID = (*env.Result)[0].OperationID +} diff --git a/internal/services/api_shield_operation/resource_test.go b/internal/services/api_shield_operation/resource_test.go index d1e7f54492..93574250b8 100644 --- a/internal/services/api_shield_operation/resource_test.go +++ b/internal/services/api_shield_operation/resource_test.go @@ -8,10 +8,11 @@ import ( "testing" "github.com/cloudflare/cloudflare-go" + cfv3 "github.com/cloudflare/cloudflare-go/v3" + "github.com/cloudflare/cloudflare-go/v3/api_gateway" "github.com/cloudflare/terraform-provider-cloudflare/internal/acctest" "github.com/cloudflare/terraform-provider-cloudflare/internal/consts" "github.com/cloudflare/terraform-provider-cloudflare/internal/utils" - "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" ) @@ -40,6 +41,9 @@ func TestAccCloudflareAPIShieldOperation_Create(t *testing.T) { resource.TestCheckResourceAttr(resourceID, "method", "GET"), resource.TestCheckResourceAttr(resourceID, "host", domain), resource.TestCheckResourceAttr(resourceID, "endpoint", "/example/path"), + resource.TestCheckResourceAttr(resourceID, "operations.0.method", "GET"), + resource.TestCheckResourceAttr(resourceID, "operations.0.host", domain), + resource.TestCheckResourceAttr(resourceID, "operations.0.endpoint", "/example/path"), ), }, }, @@ -70,6 +74,9 @@ func TestAccCloudflareAPIShieldOperation_ForceNew(t *testing.T) { resource.TestCheckResourceAttr(resourceID, "method", "GET"), resource.TestCheckResourceAttr(resourceID, "host", domain), resource.TestCheckResourceAttr(resourceID, "endpoint", "/example/path"), + resource.TestCheckResourceAttr(resourceID, "operations.0.method", "GET"), + resource.TestCheckResourceAttr(resourceID, "operations.0.host", domain), + resource.TestCheckResourceAttr(resourceID, "operations.0.endpoint", "/example/path"), ), }, { @@ -79,6 +86,9 @@ func TestAccCloudflareAPIShieldOperation_ForceNew(t *testing.T) { resource.TestCheckResourceAttr(resourceID, "method", "POST"), // check that we've 'updated' the value resource.TestCheckResourceAttr(resourceID, "host", domain), resource.TestCheckResourceAttr(resourceID, "endpoint", "/example/path"), + resource.TestCheckResourceAttr(resourceID, "operations.0.method", "POST"), // check that we've 'updated' the value + resource.TestCheckResourceAttr(resourceID, "operations.0.host", domain), + resource.TestCheckResourceAttr(resourceID, "operations.0.endpoint", "/example/path"), ), }, }, @@ -86,28 +96,25 @@ func TestAccCloudflareAPIShieldOperation_ForceNew(t *testing.T) { } func testAccCheckAPIShieldOperationDelete(s *terraform.State) error { - client, clientErr := acctest.SharedV1Client() // TODO(terraform): replace with SharedV2Clent - if clientErr != nil { - tflog.Error(context.TODO(), fmt.Sprintf("failed to create Cloudflare client: %s", clientErr)) - } + client := acctest.SharedClient() // TODO(terraform): replace with SharedV2Clent for _, rs := range s.RootModule().Resources { if rs.Type != "cloudflare_api_shield_operation" { continue } - _, err := client.GetAPIShieldOperation( + _, err := client.APIGateway.Operations.Get( context.Background(), - cloudflare.ZoneIdentifier(rs.Primary.Attributes[consts.ZoneIDSchemaKey]), - cloudflare.GetAPIShieldOperationParams{ - OperationID: rs.Primary.Attributes["id"], + rs.Primary.Attributes["operation_id"], + api_gateway.OperationGetParams{ + ZoneID: cfv3.F(rs.Primary.Attributes[consts.ZoneIDSchemaKey]), }, ) if err == nil { return fmt.Errorf("operation still exists") } - var notFoundError *cloudflare.NotFoundError + var notFoundError *cfv3.Error if !errors.As(err, ¬FoundError) { return fmt.Errorf("expected not found error but got: %w", err) } diff --git a/internal/services/api_shield_operation/testdata/apishieldoperation.tf b/internal/services/api_shield_operation/testdata/apishieldoperation.tf index 838c581a9c..c2e0835b6b 100644 --- a/internal/services/api_shield_operation/testdata/apishieldoperation.tf +++ b/internal/services/api_shield_operation/testdata/apishieldoperation.tf @@ -1,7 +1,10 @@ - - resource "cloudflare_api_shield_operation" "%[1]s" { - zone_id = "%[2]s" - method = "%[3]s" - host = "%[4]s" - endpoint = "%[5]s" - } +resource "cloudflare_api_shield_operation" "%[1]s" { + zone_id = "%[2]s" + operations = [ + { + method = "%[3]s" + host = "%[4]s" + endpoint = "%[5]s" + } + ] +} \ No newline at end of file From 87cced06e0ebda40fe9b0700a40893509c75c75c Mon Sep 17 00:00:00 2001 From: Vaishak Dinesh Date: Fri, 20 Dec 2024 11:40:23 -0800 Subject: [PATCH 2/2] fix: refactor tests --- internal/services/api_shield_operation/custom.go | 10 ---------- .../services/api_shield_operation/resource_test.go | 11 +---------- .../testdata/apishieldoperation.tf | 14 +++++--------- 3 files changed, 6 insertions(+), 29 deletions(-) delete mode 100644 internal/services/api_shield_operation/custom.go diff --git a/internal/services/api_shield_operation/custom.go b/internal/services/api_shield_operation/custom.go deleted file mode 100644 index 425c16379d..0000000000 --- a/internal/services/api_shield_operation/custom.go +++ /dev/null @@ -1,10 +0,0 @@ -package api_shield_operation - -func setOperationValues(data *APIShieldOperationModel, env APIShieldOperationResultEnvelope) { - data.Endpoint = (*env.Result)[0].Endpoint - data.Host = (*env.Result)[0].Host - data.Method = (*env.Result)[0].Method - // TODO:: after schema changes land - //data.LastUpdated = (*env.Result)[0].LastUpdated - //data.OperationID = (*env.Result)[0].OperationID -} diff --git a/internal/services/api_shield_operation/resource_test.go b/internal/services/api_shield_operation/resource_test.go index 93574250b8..1806e1c661 100644 --- a/internal/services/api_shield_operation/resource_test.go +++ b/internal/services/api_shield_operation/resource_test.go @@ -41,9 +41,6 @@ func TestAccCloudflareAPIShieldOperation_Create(t *testing.T) { resource.TestCheckResourceAttr(resourceID, "method", "GET"), resource.TestCheckResourceAttr(resourceID, "host", domain), resource.TestCheckResourceAttr(resourceID, "endpoint", "/example/path"), - resource.TestCheckResourceAttr(resourceID, "operations.0.method", "GET"), - resource.TestCheckResourceAttr(resourceID, "operations.0.host", domain), - resource.TestCheckResourceAttr(resourceID, "operations.0.endpoint", "/example/path"), ), }, }, @@ -74,9 +71,6 @@ func TestAccCloudflareAPIShieldOperation_ForceNew(t *testing.T) { resource.TestCheckResourceAttr(resourceID, "method", "GET"), resource.TestCheckResourceAttr(resourceID, "host", domain), resource.TestCheckResourceAttr(resourceID, "endpoint", "/example/path"), - resource.TestCheckResourceAttr(resourceID, "operations.0.method", "GET"), - resource.TestCheckResourceAttr(resourceID, "operations.0.host", domain), - resource.TestCheckResourceAttr(resourceID, "operations.0.endpoint", "/example/path"), ), }, { @@ -86,9 +80,6 @@ func TestAccCloudflareAPIShieldOperation_ForceNew(t *testing.T) { resource.TestCheckResourceAttr(resourceID, "method", "POST"), // check that we've 'updated' the value resource.TestCheckResourceAttr(resourceID, "host", domain), resource.TestCheckResourceAttr(resourceID, "endpoint", "/example/path"), - resource.TestCheckResourceAttr(resourceID, "operations.0.method", "POST"), // check that we've 'updated' the value - resource.TestCheckResourceAttr(resourceID, "operations.0.host", domain), - resource.TestCheckResourceAttr(resourceID, "operations.0.endpoint", "/example/path"), ), }, }, @@ -96,7 +87,7 @@ func TestAccCloudflareAPIShieldOperation_ForceNew(t *testing.T) { } func testAccCheckAPIShieldOperationDelete(s *terraform.State) error { - client := acctest.SharedClient() // TODO(terraform): replace with SharedV2Clent + client := acctest.SharedClient() for _, rs := range s.RootModule().Resources { if rs.Type != "cloudflare_api_shield_operation" { diff --git a/internal/services/api_shield_operation/testdata/apishieldoperation.tf b/internal/services/api_shield_operation/testdata/apishieldoperation.tf index c2e0835b6b..a749df2dc0 100644 --- a/internal/services/api_shield_operation/testdata/apishieldoperation.tf +++ b/internal/services/api_shield_operation/testdata/apishieldoperation.tf @@ -1,10 +1,6 @@ resource "cloudflare_api_shield_operation" "%[1]s" { - zone_id = "%[2]s" - operations = [ - { - method = "%[3]s" - host = "%[4]s" - endpoint = "%[5]s" - } - ] -} \ No newline at end of file + zone_id = "%[2]s" + method = "%[3]s" + host = "%[4]s" + endpoint = "%[5]s" +}