|
1 | 1 | package url_normalization_settings_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
| 6 | + "github.com/cloudflare/cloudflare-go/v5/url_normalization" |
| 7 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 8 | + "log" |
5 | 9 | "os"
|
6 | 10 | "regexp"
|
7 | 11 | "testing"
|
8 | 12 |
|
| 13 | + cloudflare "github.com/cloudflare/cloudflare-go/v5" |
9 | 14 | "github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
|
10 | 15 | "github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
|
11 | 16 | "github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
|
12 | 17 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
13 | 18 | "github.com/hashicorp/terraform-plugin-testing/knownvalue"
|
14 | 19 | "github.com/hashicorp/terraform-plugin-testing/statecheck"
|
15 | 20 | "github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
|
| 21 | + "github.com/pkg/errors" |
16 | 22 | )
|
17 | 23 |
|
| 24 | +func init() { |
| 25 | + resource.AddTestSweepers("cloudflare_url_normalization_settings", &resource.Sweeper{ |
| 26 | + Name: "cloudflare_url_normalization_settings", |
| 27 | + F: testSweepCloudflareURLNormalizationSettings, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func testSweepCloudflareURLNormalizationSettings(r string) error { |
| 32 | + ctx := context.Background() |
| 33 | + client := acctest.SharedClient() |
| 34 | + |
| 35 | + // Clean up the account level rulesets |
| 36 | + zoneID := os.Getenv("CLOUDFLARE_ZONE_ID") |
| 37 | + if zoneID == "" { |
| 38 | + return errors.New("CLOUDFLARE_ZONE_ID must be set") |
| 39 | + } |
| 40 | + |
| 41 | + settings, err := client.URLNormalization.Get(context.Background(), url_normalization.URLNormalizationGetParams{ |
| 42 | + ZoneID: cloudflare.F(zoneID), |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare url normalization settings: %s", err)) |
| 46 | + } |
| 47 | + |
| 48 | + if settings == nil { |
| 49 | + log.Print("[DEBUG] No Cloudflare url normalization settings to sweep") |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + err = client.URLNormalization.Delete(context.Background(), url_normalization.URLNormalizationDeleteParams{ |
| 54 | + ZoneID: cloudflare.F(zoneID), |
| 55 | + }) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + tflog.Error(ctx, fmt.Sprintf("Failed to delete Cloudflare url normalization settings: %s", err)) |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
18 | 64 | func TestAccCloudflareURLNormalizationSettings_CreateThenUpdate(t *testing.T) {
|
19 | 65 |
|
20 | 66 | zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
|
|
0 commit comments