Skip to content

Commit cbcb325

Browse files
authored
fix(zero_trust_dlp_custom_profile): add sweepers for dlp_custom_profile
1 parent adba156 commit cbcb325

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

internal/services/zero_trust_dlp_custom_profile/resource_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strings"
78
"testing"
89

910
"github.com/cloudflare/cloudflare-go/v6"
@@ -24,6 +25,79 @@ const (
2425
EnvTfAcc = "TF_ACC"
2526
)
2627

28+
func TestMain(m *testing.M) {
29+
resource.TestMain(m)
30+
}
31+
32+
func init() {
33+
resource.AddTestSweepers("cloudflare_zero_trust_dlp_custom_profile", &resource.Sweeper{
34+
Name: "cloudflare_zero_trust_dlp_custom_profile",
35+
F: testSweepCloudflareZeroTrustDlpCustomProfile,
36+
})
37+
}
38+
39+
// testSweepCloudflareZeroTrustDlpCustomProfile removes test DLP custom profiles created during acceptance tests.
40+
//
41+
// This sweeper:
42+
// - Lists all DLP profiles in the test account
43+
// - Filters for custom profiles with test name prefixes (tf-acc-test-, tf-acctest-, tfmigrate-e2e-)
44+
// - Deletes matching custom profiles
45+
// - Continues on errors to sweep as many resources as possible
46+
//
47+
// Run with: go test ./internal/services/zero_trust_dlp_custom_profile/ -v -sweep=all
48+
//
49+
// Requires:
50+
// - CLOUDFLARE_ACCOUNT_ID (for account-scoped resources)
51+
// - CLOUDFLARE_EMAIL + CLOUDFLARE_API_KEY or CLOUDFLARE_API_TOKEN
52+
func testSweepCloudflareZeroTrustDlpCustomProfile(r string) error {
53+
ctx := context.Background()
54+
client := acctest.SharedClient()
55+
56+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
57+
if accountID == "" {
58+
return nil // Skip if no account ID set
59+
}
60+
61+
// List all DLP profiles (both custom and predefined)
62+
list, err := client.ZeroTrust.DLP.Profiles.List(ctx, zero_trust.DLPProfileListParams{
63+
AccountID: cloudflare.F(accountID),
64+
})
65+
if err != nil {
66+
return fmt.Errorf("failed to list DLP profiles: %w", err)
67+
}
68+
69+
// Delete test custom profiles only
70+
for _, profile := range list.Result {
71+
// Only delete custom profiles (not predefined)
72+
if profile.Type != "custom" {
73+
continue
74+
}
75+
76+
// Filter by test naming convention
77+
if !isTestResource(profile.Name) {
78+
continue
79+
}
80+
81+
_, err := client.ZeroTrust.DLP.Profiles.Custom.Delete(ctx, profile.ID, zero_trust.DLPProfileCustomDeleteParams{
82+
AccountID: cloudflare.F(accountID),
83+
})
84+
if err != nil {
85+
// Log but don't fail - continue sweeping
86+
fmt.Printf("Failed to delete DLP custom profile %s (%s): %v\n", profile.Name, profile.ID, err)
87+
}
88+
}
89+
90+
return nil
91+
}
92+
93+
// isTestResource checks if a resource name matches test naming conventions
94+
func isTestResource(name string) bool {
95+
return strings.HasPrefix(name, "tf-acc-test-") ||
96+
strings.HasPrefix(name, "tf-acctest-") ||
97+
strings.HasPrefix(name, "test-") ||
98+
strings.HasPrefix(name, "tfmigrate-e2e-")
99+
}
100+
27101
// setupDLPCustomProfileTest handles common test setup and TF_ACC environment check
28102
func setupDLPCustomProfileTest(t *testing.T) (string, string) {
29103
if os.Getenv(EnvTfAcc) == "" {

0 commit comments

Comments
 (0)