Skip to content

Commit 2ed457a

Browse files
authored
feat: sweepers for workers_kv and zero_trust_list (#6281)
1 parent 7c8a8ee commit 2ed457a

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

internal/services/workers_kv/resource_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package workers_kv_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"os"
78
"regexp"
89
"strings"
@@ -21,6 +22,59 @@ import (
2122
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
2223
)
2324

25+
func TestMain(m *testing.M) {
26+
resource.TestMain(m)
27+
}
28+
29+
func init() {
30+
resource.AddTestSweepers("cloudflare_workers_kv", &resource.Sweeper{
31+
Name: "cloudflare_workers_kv",
32+
F: testSweepCloudflareWorkersKV,
33+
})
34+
}
35+
36+
func testSweepCloudflareWorkersKV(r string) error {
37+
ctx := context.Background()
38+
client := acctest.SharedClient()
39+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
40+
41+
if accountID == "" {
42+
return nil
43+
}
44+
45+
// List all KV namespaces
46+
namespaces, err := client.KV.Namespaces.List(ctx, kv.NamespaceListParams{
47+
AccountID: cloudflare.F(accountID),
48+
})
49+
if err != nil {
50+
log.Printf("[ERROR] Failed to fetch KV namespaces: %s", err)
51+
return err
52+
}
53+
54+
for _, namespace := range namespaces.Result {
55+
// List keys in this namespace
56+
keys, err := client.KV.Namespaces.Keys.List(ctx, namespace.ID, kv.NamespaceKeyListParams{
57+
AccountID: cloudflare.F(accountID),
58+
})
59+
if err != nil {
60+
log.Printf("[ERROR] Failed to fetch KV keys for namespace %s: %s", namespace.ID, err)
61+
continue
62+
}
63+
64+
// Delete all keys in the namespace (sweepers clean up everything from test accounts)
65+
for _, key := range keys.Result {
66+
_, err := client.KV.Namespaces.Values.Delete(ctx, namespace.ID, key.Name, kv.NamespaceValueDeleteParams{
67+
AccountID: cloudflare.F(accountID),
68+
})
69+
if err != nil {
70+
log.Printf("[ERROR] Failed to delete KV key %s in namespace %s: %s", key.Name, namespace.ID, err)
71+
}
72+
}
73+
}
74+
75+
return nil
76+
}
77+
2478
func TestAccCloudflareWorkersKV_Basic(t *testing.T) {
2579
name := utils.GenerateRandomResourceName()
2680
key := utils.GenerateRandomResourceName()

internal/services/workers_kv_namespace/resource_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package workers_kv_namespace_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"os"
78
"regexp"
89
"strings"
@@ -21,6 +22,48 @@ import (
2122
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
2223
)
2324

25+
func TestMain(m *testing.M) {
26+
resource.TestMain(m)
27+
}
28+
29+
func init() {
30+
resource.AddTestSweepers("cloudflare_workers_kv_namespace", &resource.Sweeper{
31+
Name: "cloudflare_workers_kv_namespace",
32+
F: testSweepCloudflareWorkersKVNamespace,
33+
})
34+
}
35+
36+
func testSweepCloudflareWorkersKVNamespace(r string) error {
37+
ctx := context.Background()
38+
client := acctest.SharedClient()
39+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
40+
41+
if accountID == "" {
42+
return nil
43+
}
44+
45+
// List all KV namespaces
46+
namespaces, err := client.KV.Namespaces.List(ctx, kv.NamespaceListParams{
47+
AccountID: cloudflare.F(accountID),
48+
})
49+
if err != nil {
50+
log.Printf("[ERROR] Failed to fetch KV namespaces: %s", err)
51+
return err
52+
}
53+
54+
// Delete all namespaces (sweepers clean up everything from test accounts)
55+
for _, namespace := range namespaces.Result {
56+
_, err := client.KV.Namespaces.Delete(ctx, namespace.ID, kv.NamespaceDeleteParams{
57+
AccountID: cloudflare.F(accountID),
58+
})
59+
if err != nil {
60+
log.Printf("[ERROR] Failed to delete KV namespace %s (%s): %s", namespace.Title, namespace.ID, err)
61+
}
62+
}
63+
64+
return nil
65+
}
66+
2467
func TestAccCloudflareWorkersKVNamespace_Basic(t *testing.T) {
2568
rnd := utils.GenerateRandomResourceName()
2669
newRnd := utils.GenerateRandomResourceName()

internal/services/zero_trust_list/resource_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package zero_trust_list_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"os"
78
"strconv"
89
"strings"
910
"testing"
1011

1112
"github.com/cloudflare/cloudflare-go"
13+
cfv6 "github.com/cloudflare/cloudflare-go/v6"
14+
"github.com/cloudflare/cloudflare-go/v6/zero_trust"
1215
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
1316
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
1417
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
@@ -21,6 +24,48 @@ import (
2124
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
2225
)
2326

27+
func TestMain(m *testing.M) {
28+
resource.TestMain(m)
29+
}
30+
31+
func init() {
32+
resource.AddTestSweepers("cloudflare_zero_trust_list", &resource.Sweeper{
33+
Name: "cloudflare_zero_trust_list",
34+
F: testSweepCloudflareZeroTrustList,
35+
})
36+
}
37+
38+
func testSweepCloudflareZeroTrustList(r string) error {
39+
ctx := context.Background()
40+
client := acctest.SharedClient()
41+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
42+
43+
if accountID == "" {
44+
return nil
45+
}
46+
47+
// List all zero trust lists
48+
page, err := client.ZeroTrust.Gateway.Lists.List(ctx, zero_trust.GatewayListListParams{
49+
AccountID: cfv6.F(accountID),
50+
})
51+
if err != nil {
52+
log.Printf("[ERROR] Failed to fetch zero trust lists: %s", err)
53+
return err
54+
}
55+
56+
// Delete all lists (sweepers clean up everything from test accounts)
57+
for _, list := range page.Result {
58+
_, err := client.ZeroTrust.Gateway.Lists.Delete(ctx, list.ID, zero_trust.GatewayListDeleteParams{
59+
AccountID: cfv6.F(accountID),
60+
})
61+
if err != nil {
62+
log.Printf("[ERROR] Failed to delete zero trust list %s (%s): %s", list.Name, list.ID, err)
63+
}
64+
}
65+
66+
return nil
67+
}
68+
2469
func TestAccCloudflareTeamsList_Basic(t *testing.T) {
2570
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
2671
// service does not yet support the API tokens and it results in

0 commit comments

Comments
 (0)