Skip to content

Commit b198907

Browse files
committed
Nullify write-only attributes during Plan and Apply regardless of client capability
1 parent 6a5e0a1 commit b198907

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

internal/fwserver/server_createresource.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,15 @@ func (s *Server) CreateResource(ctx context.Context, req *CreateResourceRequest,
163163
resp.NewState.Raw = semanticEqualityResp.NewData.TerraformValue
164164
}
165165

166-
if req.ClientCapabilities.WriteOnlyAttributesAllowed {
167-
modifiedState, err := tftypes.Transform(resp.NewState.Raw, NullifyWriteOnlyAttributes(ctx, req.ResourceSchema))
168-
if err != nil {
169-
resp.Diagnostics.AddError(
170-
"Error modifying state",
171-
"There was an unexpected error modifying the NewState. This is always a problem with the provider. Please report the following to the provider developer:\n\n"+err.Error(),
172-
)
173-
return
174-
}
175-
176-
resp.NewState.Raw = modifiedState
166+
// Set any write-only attributes in the state to null
167+
modifiedState, err := tftypes.Transform(resp.NewState.Raw, NullifyWriteOnlyAttributes(ctx, req.ResourceSchema))
168+
if err != nil {
169+
resp.Diagnostics.AddError(
170+
"Error Modifying State",
171+
"There was an unexpected error modifying the NewState. This is always a problem with the provider. Please report the following to the provider developer:\n\n"+err.Error(),
172+
)
173+
return
177174
}
175+
176+
resp.NewState.Raw = modifiedState
178177
}

internal/fwserver/server_createresource_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,6 @@ func TestServerCreateResource(t *testing.T) {
535535
Provider: &testprovider.Provider{},
536536
},
537537
request: &fwserver.CreateResourceRequest{
538-
ClientCapabilities: fwserver.ApplyResourceChangeClientCapabilities{
539-
WriteOnlyAttributesAllowed: true,
540-
},
541538
PlannedState: &tfsdk.Plan{
542539
Raw: tftypes.NewValue(testSchemaTypeWriteOnly, map[string]tftypes.Value{
543540
"test_required": tftypes.NewValue(tftypes.String, "test-plannedstate-value"),

internal/fwserver/server_planresourcechange.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
8181
err := tftypes.Walk(req.Config.Raw, RequiredWriteOnlyNilsAttributePaths(ctx, req.Config.Schema, &reqWriteOnlyPaths))
8282
if err != nil {
8383
resp.Diagnostics.AddError(
84-
"Error validating plan",
84+
"Error Validating Plan",
8585
"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(),
8686
)
8787
return
@@ -367,6 +367,18 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
367367
"Ensure all resource plan modifiers do not attempt to change resource plan data from being a null value if the request plan is a null value.",
368368
)
369369
}
370+
371+
// Set any write-only attributes in the plan to null
372+
modifiedPlan, err := tftypes.Transform(resp.PlannedState.Raw, NullifyWriteOnlyAttributes(ctx, req.ResourceSchema))
373+
if err != nil {
374+
resp.Diagnostics.AddError(
375+
"Error Modifying Planned State",
376+
"There was an unexpected error modifying the PlannedState. This is always a problem with the provider. Please report the following to the provider developer:\n\n"+err.Error(),
377+
)
378+
return
379+
}
380+
381+
resp.PlannedState.Raw = modifiedPlan
370382
}
371383

372384
func MarkComputedNilsAsUnknown(ctx context.Context, config tftypes.Value, resourceSchema fwschema.Schema) func(*tftypes.AttributePath, tftypes.Value) (tftypes.Value, error) {

internal/fwserver/server_updateresource.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,15 @@ func (s *Server) UpdateResource(ctx context.Context, req *UpdateResourceRequest,
176176
resp.NewState.Raw = semanticEqualityResp.NewData.TerraformValue
177177
}
178178

179-
if req.ClientCapabilities.WriteOnlyAttributesAllowed {
180-
modifiedState, err := tftypes.Transform(resp.NewState.Raw, NullifyWriteOnlyAttributes(ctx, req.ResourceSchema))
181-
if err != nil {
182-
resp.Diagnostics.AddError(
183-
"Error modifying state",
184-
"There was an unexpected error modifying the NewState. This is always a problem with the provider. Please report the following to the provider developer:\n\n"+err.Error(),
185-
)
186-
return
187-
}
188-
189-
resp.NewState.Raw = modifiedState
179+
// Set any write-only attributes in the state to null
180+
modifiedState, err := tftypes.Transform(resp.NewState.Raw, NullifyWriteOnlyAttributes(ctx, req.ResourceSchema))
181+
if err != nil {
182+
resp.Diagnostics.AddError(
183+
"Error Modifying State",
184+
"There was an unexpected error modifying the NewState. This is always a problem with the provider. Please report the following to the provider developer:\n\n"+err.Error(),
185+
)
186+
return
190187
}
188+
189+
resp.NewState.Raw = modifiedState
191190
}

internal/fwserver/server_updateresource_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,6 @@ func TestServerUpdateResource(t *testing.T) {
806806
Provider: &testprovider.Provider{},
807807
},
808808
request: &fwserver.UpdateResourceRequest{
809-
ClientCapabilities: fwserver.ApplyResourceChangeClientCapabilities{
810-
WriteOnlyAttributesAllowed: true,
811-
},
812809
PlannedState: &tfsdk.Plan{
813810
Raw: tftypes.NewValue(testSchemaTypeWriteOnly, map[string]tftypes.Value{
814811
"test_required": tftypes.NewValue(tftypes.String, "test-plannedstate-value"),

0 commit comments

Comments
 (0)