Skip to content

Commit 8c538fa

Browse files
committed
refactor: make func re-usable
1 parent 671252c commit 8c538fa

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

internal/services/dns_record/schema.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ package dns_record
44

55
import (
66
"context"
7-
"os"
8-
7+
98
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
109
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
1110
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
@@ -23,34 +22,14 @@ import (
2322

2423
"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
2524
"github.com/cloudflare/terraform-provider-cloudflare/internal/customvalidator"
25+
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
2626
)
2727

2828
var _ resource.ResourceWithConfigValidators = (*DNSRecordResource)(nil)
2929

30-
// GetSchemaVersion returns the appropriate schema version based on TF_MIG_TEST environment variable.
31-
//
32-
// This function allows controlled rollout of StateUpgrader migrations:
33-
// - During development/testing: Set TF_MIG_TEST=1 to enable migrations (returns postMigration version)
34-
// - In production: StateUpgraders remain dormant (returns preMigration version)
35-
// - For coordinated release: Remove this wrapper and set Version directly to enable all migrations at once
36-
//
37-
// Parameters:
38-
// - preMigration: The version to use when migrations are disabled (typically 0)
39-
// - postMigration: The version to use when migrations are enabled (typically 500)
40-
//
41-
// Example usage:
42-
//
43-
// Version: GetSchemaVersion(0, 500) // Returns 0 normally, 500 when TF_MIG_TEST=1
44-
func GetSchemaVersion(preMigration, postMigration int64) int64 {
45-
if os.Getenv("TF_MIG_TEST") == "" {
46-
return preMigration
47-
}
48-
return postMigration
49-
}
50-
5130
func ResourceSchema(ctx context.Context) schema.Schema {
5231
return schema.Schema{
53-
Version: GetSchemaVersion(0, 500),
32+
Version: utils.GetSchemaVersion(0, 500),
5433
Attributes: map[string]schema.Attribute{
5534
"id": schema.StringAttribute{
5635
Description: "Identifier.",

internal/services/load_balancer_pool/schema.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package load_balancer_pool
44

55
import (
66
"context"
7-
"os"
87

98
"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
9+
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
1010
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
1111
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
1212
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
@@ -25,30 +25,9 @@ import (
2525

2626
var _ resource.ResourceWithConfigValidators = (*LoadBalancerPoolResource)(nil)
2727

28-
// GetSchemaVersion returns the appropriate schema version based on TF_MIG_TEST environment variable.
29-
//
30-
// This function allows controlled rollout of StateUpgrader migrations:
31-
// - During development/testing: Set TF_MIG_TEST=1 to enable migrations (returns postMigration version)
32-
// - In production: StateUpgraders remain dormant (returns preMigration version)
33-
// - For coordinated release: Remove this wrapper and set Version directly to enable all migrations at once
34-
//
35-
// Parameters:
36-
// - preMigration: The version to use when migrations are disabled (typically 0)
37-
// - postMigration: The version to use when migrations are enabled (typically 500)
38-
//
39-
// Example usage:
40-
//
41-
// Version: GetSchemaVersion(0, 500) // Returns 0 normally, 500 when TF_MIG_TEST=1
42-
func GetSchemaVersion(preMigration, postMigration int64) int64 {
43-
if os.Getenv("TF_MIG_TEST") == "" {
44-
return preMigration
45-
}
46-
return postMigration
47-
}
48-
4928
func ResourceSchema(ctx context.Context) schema.Schema {
5029
return schema.Schema{
51-
Version: GetSchemaVersion(0, 500),
30+
Version: utils.GetSchemaVersion(0, 500),
5231
Attributes: map[string]schema.Attribute{
5332
"id": schema.StringAttribute{
5433
Computed: true,

internal/services/page_rule/schema.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ package page_rule
44

55
import (
66
"context"
7+
78
"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
9+
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
810
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
911
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1012
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -25,7 +27,7 @@ var _ resource.ResourceWithConfigValidators = (*PageRuleResource)(nil)
2527

2628
func ResourceSchema(ctx context.Context) schema.Schema {
2729
return schema.Schema{
28-
Version: 500,
30+
Version: utils.GetSchemaVersion(0, 500),
2931
Attributes: map[string]schema.Attribute{
3032
"id": schema.StringAttribute{
3133
Description: "Identifier.",

internal/utils/schema_version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package utils
2+
3+
import "os"
4+
5+
// GetSchemaVersion returns the appropriate schema version based on TF_MIG_TEST environment variable.
6+
//
7+
// This function allows controlled rollout of StateUpgrader migrations:
8+
// - During development/testing: Set TF_MIG_TEST=1 to enable migrations (returns postMigration version)
9+
// - In production: StateUpgraders remain dormant (returns preMigration version)
10+
// - For coordinated release: Remove this wrapper and set Version directly to enable all migrations at once
11+
//
12+
// Parameters:
13+
// - preMigration: The version to use when migrations are disabled (typically 0)
14+
// - postMigration: The version to use when migrations are enabled (typically 500)
15+
//
16+
// Example usage:
17+
//
18+
// Version: GetSchemaVersion(0, 500) // Returns 0 normally, 500 when TF_MIG_TEST=1
19+
func GetSchemaVersion(preMigration, postMigration int64) int64 {
20+
if os.Getenv("TF_MIG_TEST") == "" {
21+
return preMigration
22+
}
23+
return postMigration
24+
}

0 commit comments

Comments
 (0)