Skip to content

Commit

Permalink
resource/schema/planmodifier: New type-specific plan modifiers package (
Browse files Browse the repository at this point in the history
#557)

Reference: #132

As part of upcoming effort to split schema functionality into the `datasource`, `provider`, and `resource` packages, there are some improvements that will land in the new implementations rather than breaking the existing `tfsdk` package schema functionality.

One area which has caused developer burden is that "attribute" plan modifiers, currently implementations of the `tfsdk.AttributePlanModifier` interface,  receive generic `attr.Value` as the configuration, plan, and state values to perform modification logic. This means that implementors must currently handle validating and converting the value into the concrete type they expect.

The upcoming split schemas handling will introduce separate attribute/block types that will enable to framework to strongly type validators and other future schema enhancements. This change prepares the exported interfaces and internal validation logic for those enhancements. Plan modifiers are only available for resources, so this package is explicitly placed under that structure to further reduce usage confusion.
  • Loading branch information
bflad committed Nov 29, 2022
1 parent 1dfcd30 commit 28f4804
Show file tree
Hide file tree
Showing 46 changed files with 15,432 additions and 516 deletions.
3 changes: 3 additions & 0 deletions .changelog/557.txt
@@ -0,0 +1,3 @@
```release-note:feature
resource/schema/planmodifier: New package which contains type-specific schema plan modifier interfaces
```
82 changes: 82 additions & 0 deletions internal/fwschema/fwxschema/attribute_plan_modification.go
Expand Up @@ -2,6 +2,7 @@ package fwxschema

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand All @@ -17,3 +18,84 @@ type AttributeWithPlanModifiers interface {
// the tfsdk.Attribute field name.
GetPlanModifiers() tfsdk.AttributePlanModifiers
}

// AttributeWithBoolPlanModifiers is an optional interface on Attribute which
// enables Bool plan modifier support.
type AttributeWithBoolPlanModifiers interface {
fwschema.Attribute

// BoolPlanModifiers should return a list of Bool plan modifiers.
BoolPlanModifiers() []planmodifier.Bool
}

// AttributeWithFloat64PlanModifiers is an optional interface on Attribute which
// enables Float64 plan modifier support.
type AttributeWithFloat64PlanModifiers interface {
fwschema.Attribute

// Float64PlanModifiers should return a list of Float64 plan modifiers.
Float64PlanModifiers() []planmodifier.Float64
}

// AttributeWithInt64PlanModifiers is an optional interface on Attribute which
// enables Int64 plan modifier support.
type AttributeWithInt64PlanModifiers interface {
fwschema.Attribute

// Int64PlanModifiers should return a list of Int64 plan modifiers.
Int64PlanModifiers() []planmodifier.Int64
}

// AttributeWithListPlanModifiers is an optional interface on Attribute which
// enables List plan modifier support.
type AttributeWithListPlanModifiers interface {
fwschema.Attribute

// ListPlanModifiers should return a list of List plan modifiers.
ListPlanModifiers() []planmodifier.List
}

// AttributeWithMapPlanModifiers is an optional interface on Attribute which
// enables Map plan modifier support.
type AttributeWithMapPlanModifiers interface {
fwschema.Attribute

// MapPlanModifiers should return a list of Map plan modifiers.
MapPlanModifiers() []planmodifier.Map
}

// AttributeWithNumberPlanModifiers is an optional interface on Attribute which
// enables Number plan modifier support.
type AttributeWithNumberPlanModifiers interface {
fwschema.Attribute

// NumberPlanModifiers should return a list of Number plan modifiers.
NumberPlanModifiers() []planmodifier.Number
}

// AttributeWithObjectPlanModifiers is an optional interface on Attribute which
// enables Object plan modifier support.
type AttributeWithObjectPlanModifiers interface {
fwschema.Attribute

// ObjectPlanModifiers should return a list of Object plan modifiers.
ObjectPlanModifiers() []planmodifier.Object
}

// AttributeWithSetPlanModifiers is an optional interface on Attribute which
// enables Set plan modifier support.
type AttributeWithSetPlanModifiers interface {
fwschema.Attribute

// SetPlanModifiers should return a list of Set plan modifiers.
SetPlanModifiers() []planmodifier.Set
}

// AttributeWithStringPlanModifiers is an optional interface on Attribute which
// enables String plan modifier support.
type AttributeWithStringPlanModifiers interface {
fwschema.Attribute

// StringPlanModifiers should return a list of String plan modifiers.
StringPlanModifiers() []planmodifier.String
}
28 changes: 28 additions & 0 deletions internal/fwschema/fwxschema/block_plan_modification.go
Expand Up @@ -2,6 +2,7 @@ package fwxschema

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand All @@ -17,3 +18,30 @@ type BlockWithPlanModifiers interface {
// the tfsdk.Block field name.
GetPlanModifiers() tfsdk.AttributePlanModifiers
}

// BlockWithListPlanModifiers is an optional interface on Block which
// enables List plan modifier support.
type BlockWithListPlanModifiers interface {
fwschema.Block

// ListPlanModifiers should return a list of List plan modifiers.
ListPlanModifiers() []planmodifier.List
}

// BlockWithObjectPlanModifiers is an optional interface on Block which
// enables Object plan modifier support.
type BlockWithObjectPlanModifiers interface {
fwschema.Block

// ObjectPlanModifiers should return a list of Object plan modifiers.
ObjectPlanModifiers() []planmodifier.Object
}

// BlockWithSetPlanModifiers is an optional interface on Block which
// enables Set plan modifier support.
type BlockWithSetPlanModifiers interface {
fwschema.Block

// SetPlanModifiers should return a list of Set plan modifiers.
SetPlanModifiers() []planmodifier.Set
}
@@ -0,0 +1,15 @@
package fwxschema

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)

// NestedAttributeObjectWithPlanModifiers is an optional interface on
// NestedAttributeObject which enables Object plan modification support.
type NestedAttributeObjectWithPlanModifiers interface {
fwschema.NestedAttributeObject

// ObjectPlanModifiers should return a list of Object plan modifiers.
ObjectPlanModifiers() []planmodifier.Object
}
@@ -0,0 +1,15 @@
package fwxschema

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)

// NestedBlockObjectWithPlanModifiers is an optional interface on
// NestedBlockObject which enables Object plan modification support.
type NestedBlockObjectWithPlanModifiers interface {
fwschema.NestedBlockObject

// ObjectPlanModifiers should return a list of Object plan modifiers.
ObjectPlanModifiers() []planmodifier.Object
}

0 comments on commit 28f4804

Please sign in to comment.