diff --git a/internal/providers/pluginfw/tfschema/bool_attribute.go b/internal/providers/pluginfw/tfschema/bool_attribute.go index 1b9f6b8919..5dc4727293 100644 --- a/internal/providers/pluginfw/tfschema/bool_attribute.go +++ b/internal/providers/pluginfw/tfschema/bool_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -13,6 +14,7 @@ type BoolAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.Bool + PlanModifiers []planmodifier.Bool } func (a BoolAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -34,6 +36,7 @@ func (a BoolAttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -90,3 +93,8 @@ func (a BoolAttributeBuilder) AddValidator(v validator.Bool) AttributeBuilder { a.Validators = append(a.Validators, v) return a } + +func (a BoolAttributeBuilder) AddPlanModifier(v planmodifier.Bool) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/customizable_schema.go b/internal/providers/pluginfw/tfschema/customizable_schema.go index 129d8b161a..4f29f7765e 100644 --- a/internal/providers/pluginfw/tfschema/customizable_schema.go +++ b/internal/providers/pluginfw/tfschema/customizable_schema.go @@ -5,6 +5,7 @@ import ( "reflect" "github.com/databricks/terraform-provider-databricks/common" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -72,6 +73,37 @@ func (s *CustomizableSchema) AddValidator(v any, path ...string) *CustomizableSc return s } +func (s *CustomizableSchema) AddPlanModifier(v any, path ...string) *CustomizableSchema { + cb := func(attr AttributeBuilder) AttributeBuilder { + switch a := attr.(type) { + case BoolAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.Bool)) + case Float64AttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.Float64)) + case Int64AttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.Int64)) + case ListAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.List)) + case ListNestedAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.List)) + case MapAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.Map)) + case MapNestedAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.Map)) + case SingleNestedAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.Object)) + case StringAttributeBuilder: + return a.AddPlanModifier(v.(planmodifier.String)) + default: + panic(fmt.Errorf("cannot add planmodifier, attribute builder type is invalid: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage)) + } + } + + navigateSchemaWithCallback(&s.attr, cb, path...) + + return s +} + func (s *CustomizableSchema) SetOptional(path ...string) *CustomizableSchema { cb := func(attr AttributeBuilder) AttributeBuilder { return attr.SetOptional() diff --git a/internal/providers/pluginfw/tfschema/customizable_schema_test.go b/internal/providers/pluginfw/tfschema/customizable_schema_test.go index fc8a2078fb..e134b47c2a 100644 --- a/internal/providers/pluginfw/tfschema/customizable_schema_test.go +++ b/internal/providers/pluginfw/tfschema/customizable_schema_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stretchr/testify/assert" @@ -111,3 +112,12 @@ func TestCustomizeSchemaAddValidator(t *testing.T) { assert.True(t, len(scm.Attributes["description"].(schema.StringAttribute).Validators) == 1) } + +func TestCustomizeSchemaAddPlanModifier(t *testing.T) { + scm := ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema { + c.AddPlanModifier(stringplanmodifier.RequiresReplace(), "description") + return c + }) + + assert.True(t, len(scm.Attributes["description"].(schema.StringAttribute).PlanModifiers) == 1) +} diff --git a/internal/providers/pluginfw/tfschema/float64_attribute.go b/internal/providers/pluginfw/tfschema/float64_attribute.go index 43bfaf4830..913b747431 100644 --- a/internal/providers/pluginfw/tfschema/float64_attribute.go +++ b/internal/providers/pluginfw/tfschema/float64_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -13,6 +14,7 @@ type Float64AttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.Float64 + PlanModifiers []planmodifier.Float64 } func (a Float64AttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -34,6 +36,7 @@ func (a Float64AttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -90,3 +93,8 @@ func (a Float64AttributeBuilder) AddValidator(v validator.Float64) AttributeBuil a.Validators = append(a.Validators, v) return a } + +func (a Float64AttributeBuilder) AddPlanModifier(v planmodifier.Float64) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/int64_attribute.go b/internal/providers/pluginfw/tfschema/int64_attribute.go index e9311c3a07..5c8bd9693e 100644 --- a/internal/providers/pluginfw/tfschema/int64_attribute.go +++ b/internal/providers/pluginfw/tfschema/int64_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -13,6 +14,7 @@ type Int64AttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.Int64 + PlanModifiers []planmodifier.Int64 } func (a Int64AttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -34,6 +36,7 @@ func (a Int64AttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -90,3 +93,8 @@ func (a Int64AttributeBuilder) AddValidator(v validator.Int64) AttributeBuilder a.Validators = append(a.Validators, v) return a } + +func (a Int64AttributeBuilder) AddPlanModifier(v planmodifier.Int64) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/list_attribute.go b/internal/providers/pluginfw/tfschema/list_attribute.go index 5a822179ba..6e58165996 100644 --- a/internal/providers/pluginfw/tfschema/list_attribute.go +++ b/internal/providers/pluginfw/tfschema/list_attribute.go @@ -4,6 +4,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -16,6 +17,7 @@ type ListAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.List + PlanModifiers []planmodifier.List } func (a ListAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -39,6 +41,7 @@ func (a ListAttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -95,3 +98,8 @@ func (a ListAttributeBuilder) AddValidator(v validator.List) AttributeBuilder { a.Validators = append(a.Validators, v) return a } + +func (a ListAttributeBuilder) AddPlanModifier(v planmodifier.List) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/list_nested_attribute.go b/internal/providers/pluginfw/tfschema/list_nested_attribute.go index ab0adb38d4..5d80ec8500 100644 --- a/internal/providers/pluginfw/tfschema/list_nested_attribute.go +++ b/internal/providers/pluginfw/tfschema/list_nested_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -15,6 +16,7 @@ type ListNestedAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.List + PlanModifiers []planmodifier.List } func (a ListNestedAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -38,6 +40,7 @@ func (a ListNestedAttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -94,3 +97,8 @@ func (a ListNestedAttributeBuilder) AddValidator(v validator.List) AttributeBuil a.Validators = append(a.Validators, v) return a } + +func (a ListNestedAttributeBuilder) AddPlanModifier(v planmodifier.List) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/map_attribute.go b/internal/providers/pluginfw/tfschema/map_attribute.go index f02d1c55b8..3793b444bb 100644 --- a/internal/providers/pluginfw/tfschema/map_attribute.go +++ b/internal/providers/pluginfw/tfschema/map_attribute.go @@ -4,6 +4,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -16,6 +17,7 @@ type MapAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.Map + PlanModifiers []planmodifier.Map } func (a MapAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -39,6 +41,7 @@ func (a MapAttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -95,3 +98,8 @@ func (a MapAttributeBuilder) AddValidator(v validator.Map) AttributeBuilder { a.Validators = append(a.Validators, v) return a } + +func (a MapAttributeBuilder) AddPlanModifier(v planmodifier.Map) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/map_nested_attribute.go b/internal/providers/pluginfw/tfschema/map_nested_attribute.go index 1c13bfc23e..bfcf5da968 100644 --- a/internal/providers/pluginfw/tfschema/map_nested_attribute.go +++ b/internal/providers/pluginfw/tfschema/map_nested_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -15,6 +16,7 @@ type MapNestedAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.Map + PlanModifiers []planmodifier.Map } func (a MapNestedAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -38,6 +40,7 @@ func (a MapNestedAttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -94,3 +97,8 @@ func (a MapNestedAttributeBuilder) AddValidator(v validator.Map) AttributeBuilde a.Validators = append(a.Validators, v) return a } + +func (a MapNestedAttributeBuilder) AddPlanModifier(v planmodifier.Map) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/single_nested_attribute.go b/internal/providers/pluginfw/tfschema/single_nested_attribute.go index 95e0122167..ee234db903 100644 --- a/internal/providers/pluginfw/tfschema/single_nested_attribute.go +++ b/internal/providers/pluginfw/tfschema/single_nested_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -15,6 +16,7 @@ type SingleNestedAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.Object + PlanModifiers []planmodifier.Object } func (a SingleNestedAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -38,6 +40,7 @@ func (a SingleNestedAttributeBuilder) BuildResourceAttribute() schema.Attribute DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -94,3 +97,8 @@ func (a SingleNestedAttributeBuilder) AddValidator(v validator.Object) Attribute a.Validators = append(a.Validators, v) return a } + +func (a SingleNestedAttributeBuilder) AddPlanModifier(v planmodifier.Object) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +} diff --git a/internal/providers/pluginfw/tfschema/string_attribute.go b/internal/providers/pluginfw/tfschema/string_attribute.go index 7656f1a1a4..6b81b939f6 100644 --- a/internal/providers/pluginfw/tfschema/string_attribute.go +++ b/internal/providers/pluginfw/tfschema/string_attribute.go @@ -3,6 +3,7 @@ package tfschema import ( dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" ) @@ -13,6 +14,7 @@ type StringAttributeBuilder struct { Computed bool DeprecationMessage string Validators []validator.String + PlanModifiers []planmodifier.String } func (a StringAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { @@ -34,6 +36,7 @@ func (a StringAttributeBuilder) BuildResourceAttribute() schema.Attribute { DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators, + PlanModifiers: a.PlanModifiers, } } @@ -90,3 +93,8 @@ func (a StringAttributeBuilder) AddValidator(v validator.String) AttributeBuilde a.Validators = append(a.Validators, v) return a } + +func (a StringAttributeBuilder) AddPlanModifier(v planmodifier.String) AttributeBuilder { + a.PlanModifiers = append(a.PlanModifiers, v) + return a +}