Skip to content

Commit 0114185

Browse files
SarahFrenchradeksimko
authored andcommitted
Update proto file definition of Read/WriteStateBytes RPCs (#37529)
* Update Read/WriteStateBytes RPCs to match hashicorp/terraform-plugin-go#531 * Run `make protobuf` * Run `make generate` * Update use of `proto.ReadStateBytes_ResponseChunk` in tests * Fix how diagnostics are handled alongside EOF error, update ReadStateBytes test * More fixes - test setup was incorrect I think? I assume that a response would be returned full of zero-values when EOF is encountered.
1 parent 7a693da commit 0114185

File tree

5 files changed

+97
-92
lines changed

5 files changed

+97
-92
lines changed

docs/plugin-protocol/tfplugin6.proto

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ service Provider {
391391
rpc ConfigureStateStore(ConfigureStateStore.Request) returns (ConfigureStateStore.Response);
392392

393393
// ReadStateBytes streams byte chunks of a given state file from a state store
394-
rpc ReadStateBytes(ReadStateBytes.Request) returns (stream ReadStateBytes.ResponseChunk);
394+
rpc ReadStateBytes(ReadStateBytes.Request) returns (stream ReadStateBytes.Response);
395395
// WriteStateBytes streams byte chunks of a given state file into a state store
396396
rpc WriteStateBytes(stream WriteStateBytes.RequestChunk) returns (WriteStateBytes.Response);
397397

@@ -901,7 +901,6 @@ message ValidateListResourceConfig {
901901
}
902902
}
903903

904-
905904
message ValidateStateStore {
906905
message Request {
907906
string type_name = 1;
@@ -927,7 +926,7 @@ message ReadStateBytes {
927926
string type_name = 1;
928927
string state_id = 2;
929928
}
930-
message ResponseChunk {
929+
message Response {
931930
bytes bytes = 1;
932931
int64 total_length = 2;
933932
StateRange range = 3;
@@ -937,9 +936,11 @@ message ReadStateBytes {
937936

938937
message WriteStateBytes {
939938
message RequestChunk {
939+
// TODO: Can we decouple this outside of the stream?
940940
string type_name = 1;
941-
bytes bytes = 2;
942941
string state_id = 3;
942+
943+
bytes bytes = 2;
943944
int64 total_length = 4;
944945
StateRange range = 5;
945946
}

internal/plugin6/grpc_provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15431543
chunk, err := client.Recv()
15441544
if err == io.EOF {
15451545
// End of stream, we're done
1546+
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(chunk.Diagnostics))
15461547
break
15471548
}
15481549
if err != nil {

internal/plugin6/grpc_provider_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,11 +2594,11 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
25942594
chunks := []string{"hello", "world"}
25952595
totalLength := len(chunks[0]) + len(chunks[1])
25962596
mockResp := map[int]struct {
2597-
resp *proto.ReadStateBytes_ResponseChunk
2597+
resp *proto.ReadStateBytes_Response
25982598
err error
25992599
}{
26002600
0: {
2601-
resp: &proto.ReadStateBytes_ResponseChunk{
2601+
resp: &proto.ReadStateBytes_Response{
26022602
Bytes: []byte(chunks[0]),
26032603
TotalLength: int64(totalLength),
26042604
Range: &proto.StateRange{
@@ -2609,7 +2609,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26092609
err: nil,
26102610
},
26112611
1: {
2612-
resp: &proto.ReadStateBytes_ResponseChunk{
2612+
resp: &proto.ReadStateBytes_Response{
26132613
Bytes: []byte(chunks[1]),
26142614
TotalLength: int64(totalLength),
26152615
Range: &proto.StateRange{
@@ -2620,12 +2620,12 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26202620
err: nil,
26212621
},
26222622
2: {
2623-
resp: nil,
2623+
resp: &proto.ReadStateBytes_Response{},
26242624
err: io.EOF,
26252625
},
26262626
}
26272627
var count int
2628-
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_ResponseChunk, error) {
2628+
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_Response, error) {
26292629
ret := mockResp[count]
26302630
count++
26312631
return ret.resp, ret.err
@@ -2670,11 +2670,11 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26702670
var incorrectLength int64 = 999
26712671
correctLength := len(chunks[0]) + len(chunks[1])
26722672
mockResp := map[int]struct {
2673-
resp *proto.ReadStateBytes_ResponseChunk
2673+
resp *proto.ReadStateBytes_Response
26742674
err error
26752675
}{
26762676
0: {
2677-
resp: &proto.ReadStateBytes_ResponseChunk{
2677+
resp: &proto.ReadStateBytes_Response{
26782678
Bytes: []byte(chunks[0]),
26792679
TotalLength: incorrectLength,
26802680
Range: &proto.StateRange{
@@ -2685,7 +2685,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26852685
err: nil,
26862686
},
26872687
1: {
2688-
resp: &proto.ReadStateBytes_ResponseChunk{
2688+
resp: &proto.ReadStateBytes_Response{
26892689
Bytes: []byte(chunks[1]),
26902690
TotalLength: incorrectLength,
26912691
Range: &proto.StateRange{
@@ -2696,12 +2696,12 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26962696
err: nil,
26972697
},
26982698
2: {
2699-
resp: nil,
2699+
resp: &proto.ReadStateBytes_Response{},
27002700
err: io.EOF,
27012701
},
27022702
}
27032703
var count int
2704-
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_ResponseChunk, error) {
2704+
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_Response, error) {
27052705
ret := mockResp[count]
27062706
count++
27072707
return ret.resp, ret.err
@@ -2777,7 +2777,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
27772777
).Return(mockReadBytesClient, nil)
27782778

27792779
// Define what will be returned by each call to Recv
2780-
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{
2780+
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{
27812781
Diagnostics: []*proto.Diagnostic{
27822782
&proto.Diagnostic{
27832783
Severity: proto.Diagnostic_ERROR,
@@ -2827,15 +2827,15 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
28272827
).Return(mockReadBytesClient, nil)
28282828

28292829
// Define what will be returned by each call to Recv
2830-
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{
2830+
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{
28312831
Diagnostics: []*proto.Diagnostic{
28322832
&proto.Diagnostic{
28332833
Severity: proto.Diagnostic_WARNING,
28342834
Summary: "Warning from test",
28352835
Detail: "This warning is forced by the test case",
28362836
},
28372837
},
2838-
}, nil)
2838+
}, io.EOF)
28392839

28402840
// Act
28412841
request := providers.ReadStateBytesRequest{
@@ -2876,7 +2876,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
28762876
).Return(mockClient, nil)
28772877

28782878
mockError := errors.New("grpc error forced in test")
2879-
mockClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{}, mockError)
2879+
mockClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{}, mockError)
28802880

28812881
// Act
28822882
request := providers.ReadStateBytesRequest{

internal/plugin6/mock_proto/mock.go

Lines changed: 34 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)