Skip to content

Commit ef58611

Browse files
authored
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 bd2039e commit ef58611

File tree

5 files changed

+65
-62
lines changed

5 files changed

+65
-62
lines changed

docs/plugin-protocol/tfplugin6.proto

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

428428
// ReadStateBytes streams byte chunks of a given state file from a state store
429-
rpc ReadStateBytes(ReadStateBytes.Request) returns (stream ReadStateBytes.ResponseChunk);
429+
rpc ReadStateBytes(ReadStateBytes.Request) returns (stream ReadStateBytes.Response);
430430
// WriteStateBytes streams byte chunks of a given state file into a state store
431431
rpc WriteStateBytes(stream WriteStateBytes.RequestChunk) returns (WriteStateBytes.Response);
432432

@@ -923,7 +923,6 @@ message ValidateListResourceConfig {
923923
}
924924
}
925925

926-
927926
message ValidateStateStore {
928927
message Request {
929928
string type_name = 1;
@@ -949,7 +948,7 @@ message ReadStateBytes {
949948
string type_name = 1;
950949
string state_id = 2;
951950
}
952-
message ResponseChunk {
951+
message Response {
953952
bytes bytes = 1;
954953
int64 total_length = 2;
955954
StateRange range = 3;
@@ -959,9 +958,11 @@ message ReadStateBytes {
959958

960959
message WriteStateBytes {
961960
message RequestChunk {
961+
// TODO: Can we decouple this outside of the stream?
962962
string type_name = 1;
963-
bytes bytes = 2;
964963
string state_id = 3;
964+
965+
bytes bytes = 2;
965966
int64 total_length = 4;
966967
StateRange range = 5;
967968
}

internal/plugin6/grpc_provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,7 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15161516
chunk, err := client.Recv()
15171517
if err == io.EOF {
15181518
// End of stream, we're done
1519+
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(chunk.Diagnostics))
15191520
break
15201521
}
15211522
if err != nil {

internal/plugin6/grpc_provider_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,11 +3519,11 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
35193519
chunks := []string{"hello", "world"}
35203520
totalLength := len(chunks[0]) + len(chunks[1])
35213521
mockResp := map[int]struct {
3522-
resp *proto.ReadStateBytes_ResponseChunk
3522+
resp *proto.ReadStateBytes_Response
35233523
err error
35243524
}{
35253525
0: {
3526-
resp: &proto.ReadStateBytes_ResponseChunk{
3526+
resp: &proto.ReadStateBytes_Response{
35273527
Bytes: []byte(chunks[0]),
35283528
TotalLength: int64(totalLength),
35293529
Range: &proto.StateRange{
@@ -3534,7 +3534,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
35343534
err: nil,
35353535
},
35363536
1: {
3537-
resp: &proto.ReadStateBytes_ResponseChunk{
3537+
resp: &proto.ReadStateBytes_Response{
35383538
Bytes: []byte(chunks[1]),
35393539
TotalLength: int64(totalLength),
35403540
Range: &proto.StateRange{
@@ -3545,12 +3545,12 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
35453545
err: nil,
35463546
},
35473547
2: {
3548-
resp: nil,
3548+
resp: &proto.ReadStateBytes_Response{},
35493549
err: io.EOF,
35503550
},
35513551
}
35523552
var count int
3553-
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_ResponseChunk, error) {
3553+
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_Response, error) {
35543554
ret := mockResp[count]
35553555
count++
35563556
return ret.resp, ret.err
@@ -3595,11 +3595,11 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
35953595
var incorrectLength int64 = 999
35963596
correctLength := len(chunks[0]) + len(chunks[1])
35973597
mockResp := map[int]struct {
3598-
resp *proto.ReadStateBytes_ResponseChunk
3598+
resp *proto.ReadStateBytes_Response
35993599
err error
36003600
}{
36013601
0: {
3602-
resp: &proto.ReadStateBytes_ResponseChunk{
3602+
resp: &proto.ReadStateBytes_Response{
36033603
Bytes: []byte(chunks[0]),
36043604
TotalLength: incorrectLength,
36053605
Range: &proto.StateRange{
@@ -3610,7 +3610,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
36103610
err: nil,
36113611
},
36123612
1: {
3613-
resp: &proto.ReadStateBytes_ResponseChunk{
3613+
resp: &proto.ReadStateBytes_Response{
36143614
Bytes: []byte(chunks[1]),
36153615
TotalLength: incorrectLength,
36163616
Range: &proto.StateRange{
@@ -3621,12 +3621,12 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
36213621
err: nil,
36223622
},
36233623
2: {
3624-
resp: nil,
3624+
resp: &proto.ReadStateBytes_Response{},
36253625
err: io.EOF,
36263626
},
36273627
}
36283628
var count int
3629-
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_ResponseChunk, error) {
3629+
mockReadBytesClient.EXPECT().Recv().DoAndReturn(func() (*proto.ReadStateBytes_Response, error) {
36303630
ret := mockResp[count]
36313631
count++
36323632
return ret.resp, ret.err
@@ -3702,7 +3702,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
37023702
).Return(mockReadBytesClient, nil)
37033703

37043704
// Define what will be returned by each call to Recv
3705-
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{
3705+
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{
37063706
Diagnostics: []*proto.Diagnostic{
37073707
&proto.Diagnostic{
37083708
Severity: proto.Diagnostic_ERROR,
@@ -3752,15 +3752,15 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
37523752
).Return(mockReadBytesClient, nil)
37533753

37543754
// Define what will be returned by each call to Recv
3755-
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_ResponseChunk{
3755+
mockReadBytesClient.EXPECT().Recv().Return(&proto.ReadStateBytes_Response{
37563756
Diagnostics: []*proto.Diagnostic{
37573757
&proto.Diagnostic{
37583758
Severity: proto.Diagnostic_WARNING,
37593759
Summary: "Warning from test",
37603760
Detail: "This warning is forced by the test case",
37613761
},
37623762
},
3763-
}, nil)
3763+
}, io.EOF)
37643764

37653765
// Act
37663766
request := providers.ReadStateBytesRequest{
@@ -3801,7 +3801,7 @@ func TestGRPCProvider_ReadStateBytes(t *testing.T) {
38013801
).Return(mockClient, nil)
38023802

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

38063806
// Act
38073807
request := providers.ReadStateBytesRequest{

internal/plugin6/mock_proto/mock.go

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

0 commit comments

Comments
 (0)