Skip to content

Commit

Permalink
fix: api shield tests and test data
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaishak Dinesh committed Dec 20, 2024
1 parent 3721d34 commit 5cc721a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
10 changes: 10 additions & 0 deletions internal/services/api_shield_operation/custom.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 17 additions & 10 deletions internal/services/api_shield_operation/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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"),
),
},
{
Expand All @@ -79,35 +86,35 @@ 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"),
),
},
},
})
}

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, &notFoundError) {
return fmt.Errorf("expected not found error but got: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
]
}

0 comments on commit 5cc721a

Please sign in to comment.