Skip to content

Commit 5c9c15c

Browse files
committed
Move Required + WriteOnly validations from PlanResourceChange RPC to ValidateResourceConfig RPC
1 parent 4aa117c commit 5c9c15c

File tree

4 files changed

+11
-83
lines changed

4 files changed

+11
-83
lines changed

internal/fwserver/attribute_validation.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ func AttributeValidate(ctx context.Context, a fwschema.Attribute, req ValidateAt
120120
// until Terraform CLI versions 0.12 through the release containing the
121121
// checks are considered end-of-life.
122122
// Reference: https://github.com/hashicorp/terraform/issues/30669
123-
//
124-
// We don't validate Required + WriteOnly attributes here as that is
125-
// done in PlanResourceChange (only on create).
126-
if a.IsRequired() && !a.IsWriteOnly() && attributeConfig.IsNull() {
123+
if a.IsRequired() && attributeConfig.IsNull() {
127124
resp.Diagnostics.AddAttributeError(
128125
req.AttributePath,
129126
"Missing Configuration for Required Attribute",

internal/fwserver/attribute_validation_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,16 @@ func TestAttributeValidate(t *testing.T) {
17521752
},
17531753
},
17541754
},
1755-
resp: ValidateAttributeResponse{},
1755+
resp: ValidateAttributeResponse{
1756+
Diagnostics: diag.Diagnostics{
1757+
diag.NewAttributeErrorDiagnostic(
1758+
path.Root("test"),
1759+
"Missing Configuration for Required Attribute",
1760+
"Must set a configuration value for the test attribute as the provider has marked it as required.\n\n"+
1761+
"Refer to the provider documentation or contact the provider developers for additional information about configurable attributes that are required.",
1762+
),
1763+
},
1764+
},
17561765
},
17571766
"write-only-attr-with-optional": {
17581767
req: ValidateAttributeRequest{

internal/fwserver/server_planresourcechange.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,6 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
7272
return
7373
}
7474

75-
// If the resource is planned for creation, verify that
76-
// WriteOnly + Required attributes have a configuration
77-
// value.
78-
if req.PriorState.Raw.IsNull() && !req.Config.Raw.IsNull() {
79-
var reqWriteOnlyPaths path.Paths
80-
81-
err := tftypes.Walk(req.Config.Raw, RequiredWriteOnlyNilsAttributePaths(ctx, req.Config.Schema, &reqWriteOnlyPaths))
82-
if err != nil {
83-
resp.Diagnostics.AddError(
84-
"Error Validating Plan",
85-
"There was an unexpected error validating the plan. This is always a problem with the provider. Please report the following to the provider developer:\n\n"+err.Error(),
86-
)
87-
return
88-
}
89-
90-
for _, p := range reqWriteOnlyPaths {
91-
resp.Diagnostics.AddAttributeError(
92-
p,
93-
"Invalid writeOnly attribute plan",
94-
"Required + WriteOnly attributes must have a non-null configuration value during Create.",
95-
)
96-
}
97-
98-
if resp.Diagnostics.HasError() {
99-
return
100-
}
101-
}
102-
10375
if resourceWithConfigure, ok := req.Resource.(resource.ResourceWithConfigure); ok {
10476
logging.FrameworkTrace(ctx, "Resource implements ResourceWithConfigure")
10577

internal/fwserver/server_planresourcechange_test.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -856,13 +856,6 @@ func TestServerPlanResourceChange(t *testing.T) {
856856
},
857857
}
858858

859-
testSchemaTypeWriteOnly := tftypes.Object{
860-
AttributeTypes: map[string]tftypes.Type{
861-
"test_optional_write_only": tftypes.String,
862-
"test_required_write_only": tftypes.String,
863-
},
864-
}
865-
866859
testSchema := schema.Schema{
867860
Attributes: map[string]schema.Attribute{
868861
"test_computed": schema.StringAttribute{
@@ -1367,19 +1360,6 @@ func TestServerPlanResourceChange(t *testing.T) {
13671360
},
13681361
}
13691362

1370-
testSchemaWriteOnly := schema.Schema{
1371-
Attributes: map[string]schema.Attribute{
1372-
"test_optional_write_only": schema.StringAttribute{
1373-
Optional: true,
1374-
WriteOnly: true,
1375-
},
1376-
"test_required_write_only": schema.StringAttribute{
1377-
Required: true,
1378-
WriteOnly: true,
1379-
},
1380-
},
1381-
}
1382-
13831363
testEmptyPlan := &tfsdk.Plan{
13841364
Raw: tftypes.NewValue(testSchemaType, nil),
13851365
Schema: testSchema,
@@ -3765,36 +3745,6 @@ func TestServerPlanResourceChange(t *testing.T) {
37653745
PlannedPrivate: testEmptyPrivate,
37663746
},
37673747
},
3768-
"create-required-write-only-null-diag": {
3769-
server: &fwserver.Server{
3770-
Provider: &testprovider.Provider{},
3771-
},
3772-
request: &fwserver.PlanResourceChangeRequest{
3773-
Config: &tfsdk.Config{
3774-
Raw: tftypes.NewValue(testSchemaTypeWriteOnly, map[string]tftypes.Value{
3775-
"test_optional_write_only": tftypes.NewValue(tftypes.String, "test-config-value"),
3776-
"test_required_write_only": tftypes.NewValue(tftypes.String, nil),
3777-
}),
3778-
Schema: testSchemaWriteOnly,
3779-
},
3780-
ProposedNewState: &tfsdk.Plan{
3781-
Raw: tftypes.NewValue(testSchemaTypeWriteOnly, map[string]tftypes.Value{
3782-
"test_optional_write_only": tftypes.NewValue(tftypes.String, "test-config-value"),
3783-
"test_required_write_only": tftypes.NewValue(tftypes.String, nil),
3784-
}),
3785-
Schema: testSchemaWriteOnly,
3786-
},
3787-
PriorState: testEmptyState,
3788-
ResourceSchema: testSchemaWriteOnly,
3789-
Resource: &testprovider.Resource{},
3790-
},
3791-
expectedResponse: &fwserver.PlanResourceChangeResponse{
3792-
Diagnostics: diag.Diagnostics{
3793-
diag.NewAttributeErrorDiagnostic(path.Root("test_required_write_only"),
3794-
"Invalid writeOnly attribute plan", "Required + WriteOnly attributes must have a non-null configuration value during Create."),
3795-
},
3796-
},
3797-
},
37983748
"create-resourcewithmodifyplan-attributeplanmodifier-private": {
37993749
server: &fwserver.Server{
38003750
Provider: &testprovider.Provider{},

0 commit comments

Comments
 (0)