From 954b4ee3db14dbb98ffd23cd424d73006c9892cf Mon Sep 17 00:00:00 2001 From: danischm Date: Mon, 22 Jan 2024 13:51:52 +0100 Subject: [PATCH] Add requires replace attribute option --- gen/generator.go | 1 + gen/schema/schema.yaml | 1 + gen/templates/resource.go | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/gen/generator.go b/gen/generator.go index 2d7ac49..8370eb1 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -139,6 +139,7 @@ type YamlConfigAttribute struct { StringPatterns []string `yaml:"string_patterns"` StringMinLength int64 `yaml:"string_min_length"` StringMaxLength int64 `yaml:"string_max_length"` + RequiresReplace bool `yaml:"requires_replace"` DefaultValue string `yaml:"default_value"` Value string `yaml:"value"` TestValue string `yaml:"test_value"` diff --git a/gen/schema/schema.yaml b/gen/schema/schema.yaml index 62f1a8f..5867a91 100644 --- a/gen/schema/schema.yaml +++ b/gen/schema/schema.yaml @@ -42,6 +42,7 @@ attribute: string_patterns: list(str(), required=False) # List of regular expressions that the string must match, only relevant if type is "String" string_min_length: int(required=False) # Minimum length of a string, only relevant if type is "String" string_max_length: int(required=False) # Maximum length of a string, only relevant if type is "String" + requires_replace: bool(required=False) # Set to true if the attribute requires a replace operation default_value: any(str(), int(), bool(), required=False) # Default value for the attribute value: any(str(), int(), bool(), required=False) # Hardcoded value for the attribute test_value: str(required=False) # Value used for acceptance test diff --git a/gen/templates/resource.go b/gen/templates/resource.go index 1f35409..a8ed55a 100644 --- a/gen/templates/resource.go +++ b/gen/templates/resource.go @@ -34,6 +34,11 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/floatplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -133,7 +138,7 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- else if and (len .DefaultValue) (eq .Type "String")}} Default: stringdefault.StaticString("{{.DefaultValue}}"), {{- end}} - {{- if or .Id .Reference}} + {{- if or .Id .Reference .RequiresReplace}} PlanModifiers: []planmodifier.{{.Type}}{ {{snakeCase .Type}}planmodifier.RequiresReplace(), }, @@ -198,6 +203,11 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- else if and (len .DefaultValue) (eq .Type "String")}} Default: stringdefault.StaticString("{{.DefaultValue}}"), {{- end}} + {{- if .RequiresReplace}} + PlanModifiers: []planmodifier.{{.Type}}{ + {{snakeCase .Type}}planmodifier.RequiresReplace(), + }, + {{- end}} {{- if or (eq .Type "List") (eq .Type "Set")}} NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ @@ -258,6 +268,11 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- else if and (len .DefaultValue) (eq .Type "String")}} Default: stringdefault.StaticString("{{.DefaultValue}}"), {{- end}} + {{- if .RequiresReplace}} + PlanModifiers: []planmodifier.{{.Type}}{ + {{snakeCase .Type}}planmodifier.RequiresReplace(), + }, + {{- end}} {{- if or (eq .Type "List") (eq .Type "Set")}} NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ @@ -318,6 +333,11 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- else if and (len .DefaultValue) (eq .Type "String")}} Default: stringdefault.StaticString("{{.DefaultValue}}"), {{- end}} + {{- if .RequiresReplace}} + PlanModifiers: []planmodifier.{{.Type}}{ + {{snakeCase .Type}}planmodifier.RequiresReplace(), + }, + {{- end}} }, {{- end}} {{- end}} @@ -333,6 +353,11 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- end}} }, {{- end}} + {{- if .RequiresReplace}} + PlanModifiers: []planmodifier.{{.Type}}{ + {{snakeCase .Type}}planmodifier.RequiresReplace(), + }, + {{- end}} {{- end}} }, {{- end}} @@ -349,6 +374,11 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- end}} }, {{- end}} + {{- if .RequiresReplace}} + PlanModifiers: []planmodifier.{{.Type}}{ + {{snakeCase .Type}}planmodifier.RequiresReplace(), + }, + {{- end}} {{- end}} }, {{- end}} @@ -365,6 +395,11 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S {{- end}} }, {{- end}} + {{- if .RequiresReplace}} + PlanModifiers: []planmodifier.{{.Type}}{ + {{snakeCase .Type}}planmodifier.RequiresReplace(), + }, + {{- end}} {{- end}} }, {{- end}}