Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to prior state in responses. #74

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/74.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Responses now default to returning the current state, meaning state will only change when provider developers actively change it. Previously, an empty state value would be returned, which caused problems.
```
23 changes: 10 additions & 13 deletions tfsdk/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func (s *server) ReadResource(ctx context.Context, req *tfprotov6.ReadResourceRe
}
readResp := ReadResourceResponse{
State: State{
Raw: state,
Schema: resourceSchema,
},
Diagnostics: resp.Diagnostics,
Expand Down Expand Up @@ -583,15 +584,12 @@ func (s *server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
createResp := CreateResourceResponse{
State: State{
Schema: resourceSchema,
Raw: priorState,
},
Diagnostics: resp.Diagnostics,
}
resource.Create(ctx, createReq, &createResp)
resp.Diagnostics = createResp.Diagnostics
// TODO: set partial state before returning error
if diagsHasErrors(resp.Diagnostics) {
return resp, nil
}
newState, err := tfprotov6.NewDynamicValue(resourceSchema.TerraformType(ctx), createResp.State.Raw)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Expand Down Expand Up @@ -647,15 +645,12 @@ func (s *server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
updateResp := UpdateResourceResponse{
State: State{
Schema: resourceSchema,
Raw: priorState,
},
Diagnostics: resp.Diagnostics,
}
resource.Update(ctx, updateReq, &updateResp)
resp.Diagnostics = updateResp.Diagnostics
// TODO: set partial state before returning error
if diagsHasErrors(resp.Diagnostics) {
return resp, nil
}
newState, err := tfprotov6.NewDynamicValue(resourceSchema.TerraformType(ctx), updateResp.State.Raw)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Expand All @@ -666,6 +661,7 @@ func (s *server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
return resp, nil
}
resp.NewState = &newState
return resp, nil
case !create && !update && destroy:
destroyReq := DeleteResourceRequest{
State: State{
Expand Down Expand Up @@ -702,15 +698,12 @@ func (s *server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
destroyResp := DeleteResourceResponse{
State: State{
Schema: resourceSchema,
Raw: priorState,
},
Diagnostics: resp.Diagnostics,
}
resource.Delete(ctx, destroyReq, &destroyResp)
resp.Diagnostics = destroyResp.Diagnostics
// TODO: set partial state before returning error
if diagsHasErrors(resp.Diagnostics) {
return resp, nil
}
newState, err := tfprotov6.NewDynamicValue(resourceSchema.TerraformType(ctx), destroyResp.State.Raw)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Expand All @@ -721,6 +714,7 @@ func (s *server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
return resp, nil
}
resp.NewState = &newState
return resp, nil
default:
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Expand All @@ -729,7 +723,6 @@ func (s *server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
})
return resp, nil
}
return resp, nil
}

func (s *server) ImportResourceState(ctx context.Context, _ *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
Expand Down Expand Up @@ -811,6 +804,10 @@ func (s *server) ReadDataSource(ctx context.Context, req *tfprotov6.ReadDataSour
readResp := ReadDataSourceResponse{
State: State{
Schema: dataSourceSchema,
// default to the config values
// they should be of the same type
// we just want SetAttribute to not find an empty value
Raw: config,
},
Diagnostics: resp.Diagnostics,
}
Expand Down
2 changes: 2 additions & 0 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,8 @@ func TestServerApplyResourceChange(t *testing.T) {
"name": tftypes.NewValue(tftypes.String, "hello, world"),
"favorite_colors": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{
tftypes.NewValue(tftypes.String, "red"),
tftypes.NewValue(tftypes.String, "orange"),
tftypes.NewValue(tftypes.String, "yellow"),
}),
"created_timestamp": tftypes.NewValue(tftypes.String, "right now I guess"),
}),
Expand Down