Skip to content

Commit 510faf7

Browse files
Merge branch 'next' into remove-v4
2 parents d3f9d3e + b845463 commit 510faf7

File tree

8 files changed

+186
-10
lines changed

8 files changed

+186
-10
lines changed

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 1781
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-61ee15027103b7521f9e8af3fe324d0ae82f78ae06b698235d732132f8e24855.yml
3-
openapi_spec_hash: d12ccd191dabe55f50ce6a4ad15355a6
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-3ec774bdf5de1d462e90eef2ae707df91819ae8426bbd28e1968eae25f86b0a4.yml
3+
openapi_spec_hash: 494018bf95f397e1cdd863dd5c5a33a9
44
config_hash: 11218d4e895d6852fa70acc77ad5da3d

internal/acctest/acctest.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"os/exec"
89
"path/filepath"
910
"strings"
1011
"testing"
@@ -16,6 +17,8 @@ import (
1617
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
1718
"github.com/hashicorp/terraform-plugin-framework/providerserver"
1819
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
20+
"github.com/hashicorp/terraform-plugin-testing/config"
21+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1922
"github.com/hashicorp/terraform-plugin-testing/plancheck"
2023
"github.com/hashicorp/terraform-plugin-testing/terraform"
2124
)
@@ -317,3 +320,83 @@ var LogResourceDrift = []plancheck.PlanCheck{pc}
317320
func PtrTo[T any](v T) *T {
318321
return &v
319322
}
323+
324+
// RunMigrationCommand runs the migration script to transform config and state
325+
func RunMigrationCommand(t *testing.T, v4Config string, tmpDir string) {
326+
t.Helper()
327+
328+
// Get the current working directory to find the migration binary
329+
cwd, err := os.Getwd()
330+
if err != nil {
331+
t.Fatalf("Failed to get current working directory: %v", err)
332+
}
333+
334+
// Build the path to the migration binary
335+
// The test runs from internal/services/zone, so we need to go up to the root
336+
projectRoot := filepath.Join(cwd, "..", "..", "..")
337+
migratePath := filepath.Join(projectRoot, "cmd", "migrate")
338+
t.Logf("Migrate path: %s", migratePath)
339+
340+
// Write the v4 config to tmpDir/test_migration.tf
341+
testConfigPath := filepath.Join(tmpDir, "test_migration.tf")
342+
t.Logf("Writing v4 config to: %s", testConfigPath)
343+
344+
err = os.WriteFile(testConfigPath, []byte(v4Config), 0644)
345+
if err != nil {
346+
t.Fatalf("Failed to write test config file: %v", err)
347+
}
348+
t.Logf("Successfully wrote v4 config (%d bytes)", len(v4Config))
349+
350+
// Find state file in tmpDir
351+
entries, err := os.ReadDir(tmpDir)
352+
var stateDir string
353+
if err != nil {
354+
t.Logf("Failed to read test directory: %v", err)
355+
} else {
356+
for _, entry := range entries {
357+
if entry.IsDir() {
358+
inner_entries, _ := os.ReadDir(filepath.Join(tmpDir, entry.Name()))
359+
for _, inner_entry := range inner_entries {
360+
if inner_entry.Name() == "terraform.tfstate" {
361+
stateDir = filepath.Join(tmpDir, entry.Name())
362+
}
363+
}
364+
}
365+
366+
}
367+
}
368+
369+
// Run the migration command on tmpDir (for config) and terraform.tfstate (for state)
370+
t.Logf("StateDir: %s", stateDir)
371+
state, err := os.ReadFile(filepath.Join(stateDir, "terraform.tfstate"))
372+
if err != nil {
373+
t.Fatalf("Failed to read state file: %v", err)
374+
}
375+
t.Logf("State is: %s", string(state))
376+
cmd := exec.Command("go", "run", "-C", migratePath, ".", "-config", tmpDir, "-state", filepath.Join(stateDir, "terraform.tfstate"))
377+
cmd.Dir = tmpDir
378+
// Set environment variable so the migrate command can find local grit patterns
379+
patternsDir := filepath.Join(migratePath, "patterns")
380+
cmd.Env = append(os.Environ(), fmt.Sprintf("TF_MIGRATE_PATTERNS_DIR=%s", patternsDir))
381+
382+
// Capture output for debugging
383+
output, err := cmd.CombinedOutput()
384+
if err != nil {
385+
t.Logf("Migration command failed: %v", err)
386+
}
387+
388+
t.Logf("Migration output:\n%s", string(output))
389+
}
390+
391+
// MigrationTestStep creates a test step that runs the migration command and validates with v5 provider
392+
func MigrationTestStep(t *testing.T, v4Config string, tmpDir string) resource.TestStep {
393+
return resource.TestStep{
394+
PreConfig: func() {
395+
// Run the migration command to transform config and state
396+
RunMigrationCommand(t, v4Config, tmpDir)
397+
},
398+
ProtoV6ProviderFactories: TestAccProtoV6ProviderFactories,
399+
ConfigDirectory: config.StaticDirectory(tmpDir),
400+
PlanOnly: true, // Verify no changes needed after migration
401+
}
402+
}

internal/services/list/data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (d *ListDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
6767
env := ListResultDataSourceEnvelope{*data}
6868
_, err := d.client.Rules.Lists.Get(
6969
ctx,
70-
data.ListID.ValueString(),
70+
data.ID.ValueString(),
7171
params,
7272
option.WithResponseBodyInto(&res),
7373
option.WithMiddleware(logging.Middleware(ctx)),

internal/services/list/data_source_model.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ type ListResultDataSourceEnvelope struct {
1616
}
1717

1818
type ListDataSourceModel struct {
19-
ID types.String `tfsdk:"id" path:"list_id,computed"`
20-
ListID types.String `tfsdk:"list_id" path:"list_id,optional"`
19+
ID types.String `tfsdk:"id" path:"id,required"`
2120
AccountID types.String `tfsdk:"account_id" path:"account_id,required"`
2221
CreatedOn types.String `tfsdk:"created_on" json:"created_on,computed"`
2322
Description types.String `tfsdk:"description" json:"description,computed"`

internal/services/list/data_source_schema.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ func DataSourceSchema(ctx context.Context) schema.Schema {
1818
Attributes: map[string]schema.Attribute{
1919
"id": schema.StringAttribute{
2020
Description: "The unique ID of the list.",
21-
Computed: true,
22-
},
23-
"list_id": schema.StringAttribute{
24-
Description: "The unique ID of the list.",
25-
Optional: true,
21+
Required: true,
2622
},
2723
"account_id": schema.StringAttribute{
2824
Description: "The Account ID for this resource.",

internal/services/load_balancer_monitor/resource_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func TestAccCloudflareLoadBalancerMonitor_Basic(t *testing.T) {
7979
resource.TestCheckResourceAttr(name, "header.%", "0"),
8080
// also expect api to generate some values
8181
testAccCheckCloudflareLoadBalancerMonitorDates(name, &loadBalancerMonitor, testStartTime),
82+
resource.TestCheckResourceAttr(name, "port", "0"),
83+
resource.TestCheckResourceAttr(name, "consecutive_up", "0"),
84+
resource.TestCheckResourceAttr(name, "consecutive_down", "0"),
8285
),
8386
},
8487
},
@@ -261,6 +264,71 @@ func TestAccCloudflareLoadBalancerMonitor_Update(t *testing.T) {
261264
})
262265
}
263266

267+
func TestAccCloudflareLoadBalancerMonitor_ConsecutiveUpDownDrift(t *testing.T) {
268+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
269+
rnd := utils.GenerateRandomResourceName()
270+
name := "cloudflare_load_balancer_monitor." + rnd
271+
var loadBalancerMonitor cloudflare.LoadBalancerMonitor
272+
273+
resource.Test(t, resource.TestCase{
274+
PreCheck: func() { acctest.TestAccPreCheck(t) },
275+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
276+
CheckDestroy: testAccCheckCloudflareLoadBalancerMonitorDestroy,
277+
Steps: []resource.TestStep{
278+
{
279+
Config: testAccCheckCloudflareLoadBalancerMonitorConfigWithConsecutiveValues(rnd, accountID),
280+
Check: resource.ComposeTestCheckFunc(
281+
testAccCheckCloudflareLoadBalancerMonitorExists(name, &loadBalancerMonitor),
282+
resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID),
283+
resource.TestCheckResourceAttr(name, "consecutive_up", "0"),
284+
resource.TestCheckResourceAttr(name, "consecutive_down", "0"),
285+
resource.TestCheckResourceAttr(name, "port", "0"),
286+
),
287+
},
288+
{
289+
Config: testAccCheckCloudflareLoadBalancerMonitorConfigWithConsecutiveValues(rnd, accountID),
290+
ExpectNonEmptyPlan: false,
291+
},
292+
},
293+
})
294+
}
295+
296+
func TestAccCloudflareLoadBalancerMonitor_NonZeroConsecutiveValuesDrift(t *testing.T) {
297+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
298+
rnd := utils.GenerateRandomResourceName()
299+
name := "cloudflare_load_balancer_monitor." + rnd
300+
var loadBalancerMonitor cloudflare.LoadBalancerMonitor
301+
302+
resource.Test(t, resource.TestCase{
303+
PreCheck: func() { acctest.TestAccPreCheck(t) },
304+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
305+
CheckDestroy: testAccCheckCloudflareLoadBalancerMonitorDestroy,
306+
Steps: []resource.TestStep{
307+
{
308+
Config: testAccCheckCloudflareLoadBalancerMonitorConfigWithNonZeroConsecutiveValues(rnd, accountID, "Initial description"),
309+
Check: resource.ComposeTestCheckFunc(
310+
testAccCheckCloudflareLoadBalancerMonitorExists(name, &loadBalancerMonitor),
311+
resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID),
312+
resource.TestCheckResourceAttr(name, "consecutive_up", "2"),
313+
resource.TestCheckResourceAttr(name, "consecutive_down", "2"),
314+
resource.TestCheckResourceAttr(name, "port", "8080"),
315+
resource.TestCheckResourceAttr(name, "description", "Initial description"),
316+
),
317+
},
318+
{
319+
Config: testAccCheckCloudflareLoadBalancerMonitorConfigWithNonZeroConsecutiveValues(rnd, accountID, "Updated description"),
320+
Check: resource.ComposeTestCheckFunc(
321+
testAccCheckCloudflareLoadBalancerMonitorExists(name, &loadBalancerMonitor),
322+
resource.TestCheckResourceAttr(name, "description", "Updated description"),
323+
resource.TestCheckResourceAttr(name, "consecutive_up", "2"),
324+
resource.TestCheckResourceAttr(name, "consecutive_down", "2"),
325+
resource.TestCheckResourceAttr(name, "port", "8080"),
326+
),
327+
},
328+
},
329+
})
330+
}
331+
264332
func TestAccCloudflareLoadBalancerMonitor_ChangingHeadersCauseReplacement(t *testing.T) {
265333
domain := os.Getenv("CLOUDFLARE_DOMAIN")
266334
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
@@ -395,6 +463,14 @@ func testAccCheckCloudflareLoadBalancerMonitorConfigSMTP(resourceName, accountID
395463
return acctest.LoadTestCase("loadbalancermonitorconfigsmtp.tf", resourceName, accountID)
396464
}
397465

466+
func testAccCheckCloudflareLoadBalancerMonitorConfigWithConsecutiveValues(rnd, accountID string) string {
467+
return acctest.LoadTestCase("loadbalancermonitorconfigwithconsecutivevalues.tf", rnd, accountID)
468+
}
469+
470+
func testAccCheckCloudflareLoadBalancerMonitorConfigWithNonZeroConsecutiveValues(rnd, accountID, description string) string {
471+
return acctest.LoadTestCase("loadbalancermonitorconfigwithnonzeroconsecutivevalues.tf", rnd, accountID, description)
472+
}
473+
398474
func testAccCheckCloudflareLoadBalancerMonitorConfigMissingRequired(accountID string) string {
399475
return acctest.LoadTestCase("loadbalancermonitorconfigmissingrequired.tf", accountID)
400476
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "cloudflare_load_balancer_monitor" "%s" {
2+
account_id = "%s"
3+
type = "http"
4+
method = "GET"
5+
path = "/"
6+
expected_codes = "200"
7+
consecutive_up = 0
8+
consecutive_down = 0
9+
port = 0
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
resource "cloudflare_load_balancer_monitor" "%s" {
2+
account_id = "%s"
3+
type = "http"
4+
method = "GET"
5+
path = "/"
6+
expected_codes = "200"
7+
description = "%s"
8+
# Non-zero values for consecutive_up, consecutive_down, and port
9+
consecutive_up = 2
10+
consecutive_down = 2
11+
port = 8080
12+
}

0 commit comments

Comments
 (0)