|
| 1 | +// Copyright IBM Corp. 2021, 2026 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package proto6server |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/internal/logging" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/toproto6" |
| 14 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 15 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 16 | + "github.com/hashicorp/terraform-plugin-log/tfsdklog" |
| 17 | +) |
| 18 | + |
| 19 | +func (s *Server) WriteStateBytes(ctx context.Context, proto6Req *tfprotov6.WriteStateBytesStream) (*tfprotov6.WriteStateBytesResponse, error) { |
| 20 | + ctx = s.registerContext(ctx) |
| 21 | + ctx = logging.InitContext(ctx) |
| 22 | + |
| 23 | + fwResp := &fwserver.WriteStateBytesResponse{} |
| 24 | + |
| 25 | + var typeName string |
| 26 | + var stateID string |
| 27 | + var stateBuffer bytes.Buffer |
| 28 | + |
| 29 | + // Chunk size is set on the server during the ConfigureStateStore RPC |
| 30 | + configuredChunkSize := s.FrameworkServer.StateStoreConfigureData.ServerCapabilities.ChunkSize |
| 31 | + if configuredChunkSize <= 0 { |
| 32 | + fwResp.Diagnostics.AddError( |
| 33 | + "Error Writing State", |
| 34 | + "The provider server does not have a chunk size configured. This is a bug in either Terraform core or terraform-plugin-framework and should be reported.", |
| 35 | + ) |
| 36 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 37 | + } |
| 38 | + |
| 39 | + // This field will be collected from one of the chunks |
| 40 | + var expectedTotalLength int64 = 0 |
| 41 | + |
| 42 | + for chunk, diags := range proto6Req.Chunks { |
| 43 | + // Any diagnostics here are either a GRPC communication error or invalid data from the client. |
| 44 | + if len(diags) > 0 { |
| 45 | + return &tfprotov6.WriteStateBytesResponse{Diagnostics: diags}, nil |
| 46 | + } |
| 47 | + |
| 48 | + // Only the first chunk will have meta set |
| 49 | + if chunk.Meta != nil { |
| 50 | + typeName = chunk.Meta.TypeName |
| 51 | + stateID = chunk.Meta.StateID |
| 52 | + } |
| 53 | + |
| 54 | + if chunk.Range.End < chunk.TotalLength-1 { |
| 55 | + // Ensure each chunk (except the last) exactly match the configured size. |
| 56 | + if int64(len(chunk.Bytes)) != configuredChunkSize { |
| 57 | + fwResp.Diagnostics.AddError( |
| 58 | + "Error Writing State", |
| 59 | + fmt.Sprintf("Unexpected chunk of size %d was received from Terraform, expected chunk size was %d. This is a bug in Terraform core and should be reported.", len(chunk.Bytes), configuredChunkSize), |
| 60 | + ) |
| 61 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 62 | + } |
| 63 | + } else { |
| 64 | + // Ensure the last chunk is within the configured size. |
| 65 | + if int64(len(chunk.Bytes)) > configuredChunkSize { |
| 66 | + fwResp.Diagnostics.AddError( |
| 67 | + "Error Writing State", |
| 68 | + fmt.Sprintf("Unexpected final chunk of size %d was received from Terraform, which exceeds the configured chunk size of %d. This is a bug in Terraform core and should be reported.", len(chunk.Bytes), configuredChunkSize), |
| 69 | + ) |
| 70 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if expectedTotalLength == 0 { |
| 75 | + expectedTotalLength = chunk.TotalLength |
| 76 | + } |
| 77 | + |
| 78 | + stateBuffer.Write(chunk.Bytes) |
| 79 | + } |
| 80 | + |
| 81 | + // MAINTAINER NOTE: Typically these fields are set in terraform-plugin-go (see link below), however because |
| 82 | + // the type name is not extracted until the stream is consumed, it's easier to set the logger fields here. |
| 83 | + // |
| 84 | + // https://github.com/hashicorp/terraform-plugin-go/blob/14fe65ea923b5e306dbb4f67f2bb861f74b9e3ec/internal/logging/context.go#L112-L119 |
| 85 | + ctx = tfsdklog.SetField(ctx, "tf_state_store_type", typeName) |
| 86 | + ctx = tfsdklog.SubsystemSetField(ctx, "proto", "tf_state_store_type", typeName) |
| 87 | + ctx = tflog.SetField(ctx, "tf_state_store_type", typeName) |
| 88 | + |
| 89 | + if stateBuffer.Len() == 0 { |
| 90 | + fwResp.Diagnostics.AddError( |
| 91 | + "Error Writing State", |
| 92 | + "No state data was received from Terraform. This is a bug in Terraform core and should be reported.", |
| 93 | + ) |
| 94 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 95 | + } |
| 96 | + |
| 97 | + if int64(stateBuffer.Len()) != expectedTotalLength { |
| 98 | + fwResp.Diagnostics.AddError( |
| 99 | + "Error Writing State", |
| 100 | + fmt.Sprintf("Unexpected size of state data received from Terraform, got: %d, expected: %d. This is a bug in Terraform core and should be reported.", stateBuffer.Len(), expectedTotalLength), |
| 101 | + ) |
| 102 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 103 | + } |
| 104 | + |
| 105 | + if stateID == "" { |
| 106 | + fwResp.Diagnostics.AddError( |
| 107 | + "Error Writing State", |
| 108 | + "No state ID was received from Terraform. This is a bug in Terraform core and should be reported.", |
| 109 | + ) |
| 110 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 111 | + } |
| 112 | + |
| 113 | + stateStore, diags := s.FrameworkServer.StateStore(ctx, typeName) |
| 114 | + |
| 115 | + fwResp.Diagnostics.Append(diags...) |
| 116 | + |
| 117 | + if fwResp.Diagnostics.HasError() { |
| 118 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 119 | + } |
| 120 | + |
| 121 | + fwReq := &fwserver.WriteStateBytesRequest{ |
| 122 | + StateStore: stateStore, |
| 123 | + StateID: stateID, |
| 124 | + StateBytes: stateBuffer.Bytes(), |
| 125 | + } |
| 126 | + |
| 127 | + s.FrameworkServer.WriteStateBytes(ctx, fwReq, fwResp) |
| 128 | + |
| 129 | + return toproto6.WriteStateBytesResponse(ctx, fwResp), nil |
| 130 | +} |
0 commit comments