Skip to content

Commit

Permalink
Removing TransformDefaults() from tfsdk.State so as not to expose it …
Browse files Browse the repository at this point in the history
…in the public API (#668)
  • Loading branch information
bendbennett committed Mar 6, 2023
1 parent 0438d70 commit d2ae08e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
22 changes: 21 additions & 1 deletion internal/fwserver/server_planresourcechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -105,8 +106,27 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange

resp.PlannedState = planToState(*req.ProposedNewState)

// Set Defaults.
//
// If the planned state is not null (i.e., not a destroy operation) we traverse the schema,
// identifying any attributes which are null, and if the attribute has a default value
// specified by the `Default` field on the attribute then the default value is assigned.
if !resp.PlannedState.Raw.IsNull() {
resp.PlannedState.TransformDefaults(ctx, req.Config.Raw)
data := fwschemadata.Data{
Description: fwschemadata.DataDescriptionState,
Schema: resp.PlannedState.Schema,
TerraformValue: resp.PlannedState.Raw,
}

diags := data.TransformDefaults(ctx, req.Config.Raw)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

resp.PlannedState.Raw = data.TerraformValue
}

// Execute any AttributePlanModifiers.
Expand Down
36 changes: 26 additions & 10 deletions internal/fwserver/server_planresourcechange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ func TestServerPlanResourceChange(t *testing.T) {
},
}

testSchemaTypeComputed := tftypes.Object{
testSchemaTypeComputedRequired := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_computed": tftypes.String,
"test_required": tftypes.String,
},
}

Expand Down Expand Up @@ -664,6 +665,9 @@ func TestServerPlanResourceChange(t *testing.T) {
},
},
},
"test_required": schema.StringAttribute{
Required: true,
},
},
}

Expand All @@ -681,6 +685,9 @@ func TestServerPlanResourceChange(t *testing.T) {
},
},
},
"test_required": schema.StringAttribute{
Required: true,
},
},
}

Expand Down Expand Up @@ -1740,14 +1747,16 @@ func TestServerPlanResourceChange(t *testing.T) {
},
request: &fwserver.PlanResourceChangeRequest{
Config: &tfsdk.Config{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, nil),
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
ProposedNewState: &tfsdk.Plan{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, nil),
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
Expand All @@ -1770,8 +1779,9 @@ func TestServerPlanResourceChange(t *testing.T) {
},
expectedResponse: &fwserver.PlanResourceChangeResponse{
PlannedState: &tfsdk.State{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
Expand Down Expand Up @@ -2102,15 +2112,17 @@ func TestServerPlanResourceChange(t *testing.T) {
},
request: &fwserver.PlanResourceChangeRequest{
Config: &tfsdk.Config{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, nil),
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
ProposedNewState: testEmptyPlan,
PriorState: &tfsdk.State{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, nil),
"test_required": tftypes.NewValue(tftypes.String, "test-state-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
Expand Down Expand Up @@ -3356,20 +3368,23 @@ func TestServerPlanResourceChange(t *testing.T) {
},
request: &fwserver.PlanResourceChangeRequest{
Config: &tfsdk.Config{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
ProposedNewState: &tfsdk.Plan{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
PriorState: &tfsdk.State{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, "test-old-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-old-value"),
}),
Schema: testSchema,
},
Expand All @@ -3384,8 +3399,9 @@ func TestServerPlanResourceChange(t *testing.T) {
},
expectedResponse: &fwserver.PlanResourceChangeResponse{
PlannedState: &tfsdk.State{
Raw: tftypes.NewValue(testSchemaTypeComputed, map[string]tftypes.Value{
Raw: tftypes.NewValue(testSchemaTypeComputedRequired, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
}),
Schema: testSchemaAttributePlanModifierPrivatePlanResponse,
},
Expand Down
16 changes: 0 additions & 16 deletions tfsdk/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,6 @@ func (s *State) RemoveResource(ctx context.Context) {
s.Raw = tftypes.NewValue(s.Schema.Type().TerraformType(ctx), nil)
}

// TransformDefaults traverses the schema, identifies any attributes which are null, and
// if the attribute has a default value specified by the `Default` field on the attribute
// then the default value is assigned.
func (s *State) TransformDefaults(ctx context.Context, configRaw tftypes.Value) diag.Diagnostics {
data := s.data()
diags := data.TransformDefaults(ctx, configRaw)

if diags.HasError() {
return diags
}

s.Raw = data.TerraformValue

return diags
}

func (s State) data() fwschemadata.Data {
return fwschemadata.Data{
Description: fwschemadata.DataDescriptionState,
Expand Down

0 comments on commit d2ae08e

Please sign in to comment.