Skip to content

Commit 012ae7a

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 c04c107 commit 012ae7a

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
@@ -1556,6 +1556,7 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15561556
chunk, err := client.Recv()
15571557
if err == io.EOF {
15581558
// End of stream, we're done
1559+
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(chunk.Diagnostics))
15591560
break
15601561
}
15611562
if err != nil {

internal/plugin6/grpc_provider_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,11 +2587,11 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
25872587
chunks := []string{"hello", "world"}
25882588
totalLength := len(chunks[0]) + len(chunks[1])
25892589
mockResp := map[int]struct {
2590-
resp *proto.ReadStateBytes_ResponseChunk
2590+
resp *proto.ReadStateBytes_Response
25912591
err error
25922592
}{
25932593
0: {
2594-
resp: &proto.ReadStateBytes_ResponseChunk{
2594+
resp: &proto.ReadStateBytes_Response{
25952595
Bytes: []byte(chunks[0]),
25962596
TotalLength: int64(totalLength),
25972597
Range: &proto.StateRange{
@@ -2602,7 +2602,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26022602
err: nil,
26032603
},
26042604
1: {
2605-
resp: &proto.ReadStateBytes_ResponseChunk{
2605+
resp: &proto.ReadStateBytes_Response{
26062606
Bytes: []byte(chunks[1]),
26072607
TotalLength: int64(totalLength),
26082608
Range: &proto.StateRange{
@@ -2613,12 +2613,12 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26132613
err: nil,
26142614
},
26152615
2: {
2616-
resp: nil,
2616+
resp: &proto.ReadStateBytes_Response{},
26172617
err: io.EOF,
26182618
},
26192619
}
26202620
var count int
2621-
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_ResponseChunk, error) {
2621+
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_Response, error) {
26222622
ret := mockResp[count]
26232623
count++
26242624
return ret.resp, ret.err
@@ -2663,11 +2663,11 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26632663
var incorrectLength int64 = 999
26642664
correctLength := len(chunks[0]) + len(chunks[1])
26652665
mockResp := map[int]struct {
2666-
resp *proto.ReadStateBytes_ResponseChunk
2666+
resp *proto.ReadStateBytes_Response
26672667
err error
26682668
}{
26692669
0: {
2670-
resp: &proto.ReadStateBytes_ResponseChunk{
2670+
resp: &proto.ReadStateBytes_Response{
26712671
Bytes: []byte(chunks[0]),
26722672
TotalLength: incorrectLength,
26732673
Range: &proto.StateRange{
@@ -2678,7 +2678,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26782678
err: nil,
26792679
},
26802680
1: {
2681-
resp: &proto.ReadStateBytes_ResponseChunk{
2681+
resp: &proto.ReadStateBytes_Response{
26822682
Bytes: []byte(chunks[1]),
26832683
TotalLength: incorrectLength,
26842684
Range: &proto.StateRange{
@@ -2689,12 +2689,12 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
26892689
err: nil,
26902690
},
26912691
2: {
2692-
resp: nil,
2692+
resp: &proto.ReadStateBytes_Response{},
26932693
err: io.EOF,
26942694
},
26952695
}
26962696
var count int
2697-
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_ResponseChunk, error) {
2697+
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_Response, error) {
26982698
ret := mockResp[count]
26992699
count++
27002700
return ret.resp, ret.err
@@ -2770,7 +2770,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
27702770
).Return(mockReadBytesClient, nil)
27712771

27722772
// Define what will be returned by each call to Recv
2773-
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{
2773+
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{
27742774
Diagnostics: []*proto.Diagnostic{
27752775
&proto.Diagnostic{
27762776
Severity: proto.Diagnostic_ERROR,
@@ -2820,15 +2820,15 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
28202820
).Return(mockReadBytesClient, nil)
28212821

28222822
// Define what will be returned by each call to Recv
2823-
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{
2823+
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{
28242824
Diagnostics: []*proto.Diagnostic{
28252825
&proto.Diagnostic{
28262826
Severity: proto.Diagnostic_WARNING,
28272827
Summary: "Warning from test",
28282828
Detail: "This warning is forced by the test case",
28292829
},
28302830
},
2831-
}, nil)
2831+
}, io.EOF)
28322832

28332833
// Act
28342834
request := providers.ReadStateBytesRequest{
@@ -2869,7 +2869,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
28692869
).Return(mockClient, nil)
28702870

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

28742874
// Act
28752875
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)