Skip to content

Commit b8b455b

Browse files
committed
Implement ValidateStateStoreConfig + ConfigureStateStore methods
1 parent a498eaf commit b8b455b

File tree

9 files changed

+379
-3
lines changed

9 files changed

+379
-3
lines changed

internal/logging/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ func ActionContext(ctx context.Context, action string) context.Context {
109109
return ctx
110110
}
111111

112+
// StateStoreContext injects the state store type into logger contexts.
113+
func StateStoreContext(ctx context.Context, stateStore string) context.Context {
114+
ctx = tfsdklog.SetField(ctx, KeyStateStoreType, stateStore)
115+
ctx = tfsdklog.SubsystemSetField(ctx, SubsystemProto, KeyStateStoreType, stateStore)
116+
ctx = tflog.SetField(ctx, KeyStateStoreType, stateStore)
117+
118+
return ctx
119+
}
120+
112121
// RpcContext injects the RPC name into logger contexts.
113122
func RpcContext(ctx context.Context, rpc string) context.Context {
114123
ctx = tfsdklog.SetField(ctx, KeyRPC, rpc)

internal/logging/keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ const (
6666
// The action being operated on
6767
KeyActionType = "tf_action_type"
6868

69+
// The type of state store being operated on, such as "terraform_fs"
70+
KeyStateStoreType = "tf_state_store_type"
71+
6972
// Path to protocol data file, such as "/tmp/example.json"
7073
KeyProtocolDataFile = "tf_proto_data_file"
7174

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
9+
)
10+
11+
func ValidateStateStoreRequest(in *tfplugin6.ValidateStateStore_Request) *tfprotov6.ValidateStateStoreRequest {
12+
if in == nil {
13+
return nil
14+
}
15+
16+
return &tfprotov6.ValidateStateStoreRequest{
17+
TypeName: in.TypeName,
18+
Config: DynamicValue(in.Config),
19+
}
20+
}
21+
22+
func ConfigureStateStoreRequest(in *tfplugin6.ConfigureStateStore_Request) *tfprotov6.ConfigureStateStoreRequest {
23+
if in == nil {
24+
return nil
25+
}
26+
27+
return &tfprotov6.ConfigureStateStoreRequest{
28+
TypeName: in.TypeName,
29+
Config: DynamicValue(in.Config),
30+
}
31+
}
32+
33+
func GetStatesRequest(in *tfplugin6.GetStates_Request) *tfprotov6.GetStatesRequest {
34+
if in == nil {
35+
return nil
36+
}
37+
38+
return &tfprotov6.GetStatesRequest{
39+
TypeName: in.TypeName,
40+
}
41+
}
42+
43+
func DeleteStateRequest(in *tfplugin6.DeleteState_Request) *tfprotov6.DeleteStateRequest {
44+
if in == nil {
45+
return nil
46+
}
47+
48+
return &tfprotov6.DeleteStateRequest{
49+
TypeName: in.TypeName,
50+
StateId: in.StateId,
51+
}
52+
}

tfprotov6/internal/toproto/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func GetProviderSchema_Response(in *tfprotov6.GetProviderSchemaResponse) *tfplug
6262
Diagnostics: Diagnostics(in.Diagnostics),
6363
EphemeralResourceSchemas: make(map[string]*tfplugin6.Schema, len(in.EphemeralResourceSchemas)),
6464
ListResourceSchemas: make(map[string]*tfplugin6.Schema, len(in.ListResourceSchemas)),
65+
StateStoreSchemas: make(map[string]*tfplugin6.Schema, len(in.StateStoreSchemas)),
6566
Functions: make(map[string]*tfplugin6.Function, len(in.Functions)),
6667
Provider: Schema(in.Provider),
6768
ProviderMeta: Schema(in.ProviderMeta),
@@ -77,6 +78,10 @@ func GetProviderSchema_Response(in *tfprotov6.GetProviderSchemaResponse) *tfplug
7778
resp.ListResourceSchemas[name] = Schema(schema)
7879
}
7980

81+
for name, schema := range in.StateStoreSchemas {
82+
resp.StateStoreSchemas[name] = Schema(schema)
83+
}
84+
8085
for name, schema := range in.ResourceSchemas {
8186
resp.ResourceSchemas[name] = Schema(schema)
8287
}

tfprotov6/internal/toproto/provider_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
310310
Diagnostics: []*tfplugin6.Diagnostic{},
311311
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
312312
ListResourceSchemas: map[string]*tfplugin6.Schema{},
313+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
313314
Functions: map[string]*tfplugin6.Function{},
314315
ResourceSchemas: map[string]*tfplugin6.Schema{},
315316
},
@@ -349,6 +350,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
349350
Diagnostics: []*tfplugin6.Diagnostic{},
350351
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
351352
ListResourceSchemas: map[string]*tfplugin6.Schema{},
353+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
352354
Functions: map[string]*tfplugin6.Function{},
353355
ResourceSchemas: map[string]*tfplugin6.Schema{},
354356
},
@@ -384,6 +386,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
384386
Diagnostics: []*tfplugin6.Diagnostic{},
385387
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
386388
ListResourceSchemas: map[string]*tfplugin6.Schema{},
389+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
387390
Functions: map[string]*tfplugin6.Function{},
388391
ResourceSchemas: map[string]*tfplugin6.Schema{},
389392
},
@@ -402,6 +405,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
402405
},
403406
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
404407
ListResourceSchemas: map[string]*tfplugin6.Schema{},
408+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
405409
Functions: map[string]*tfplugin6.Function{},
406410
ResourceSchemas: map[string]*tfplugin6.Schema{},
407411
},
@@ -437,6 +441,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
437441
},
438442
},
439443
ListResourceSchemas: map[string]*tfplugin6.Schema{},
444+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
440445
Functions: map[string]*tfplugin6.Function{},
441446
ResourceSchemas: map[string]*tfplugin6.Schema{},
442447
},
@@ -457,6 +462,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
457462
Diagnostics: []*tfplugin6.Diagnostic{},
458463
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
459464
ListResourceSchemas: map[string]*tfplugin6.Schema{},
465+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
460466
Functions: map[string]*tfplugin6.Function{
461467
"test": {
462468
Parameters: []*tfplugin6.Function_Parameter{},
@@ -499,8 +505,9 @@ func TestGetProviderSchema_Response(t *testing.T) {
499505
},
500506
},
501507
},
502-
Functions: map[string]*tfplugin6.Function{},
503-
ResourceSchemas: map[string]*tfplugin6.Schema{},
508+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
509+
Functions: map[string]*tfplugin6.Function{},
510+
ResourceSchemas: map[string]*tfplugin6.Schema{},
504511
},
505512
},
506513
"Provider": {
@@ -521,6 +528,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
521528
Diagnostics: []*tfplugin6.Diagnostic{},
522529
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
523530
ListResourceSchemas: map[string]*tfplugin6.Schema{},
531+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
524532
Functions: map[string]*tfplugin6.Function{},
525533
Provider: &tfplugin6.Schema{
526534
Block: &tfplugin6.Schema_Block{
@@ -553,6 +561,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
553561
Diagnostics: []*tfplugin6.Diagnostic{},
554562
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
555563
ListResourceSchemas: map[string]*tfplugin6.Schema{},
564+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
556565
Functions: map[string]*tfplugin6.Function{},
557566
ProviderMeta: &tfplugin6.Schema{
558567
Block: &tfplugin6.Schema_Block{
@@ -587,6 +596,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
587596
Diagnostics: []*tfplugin6.Diagnostic{},
588597
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
589598
ListResourceSchemas: map[string]*tfplugin6.Schema{},
599+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
590600
Functions: map[string]*tfplugin6.Function{},
591601
ResourceSchemas: map[string]*tfplugin6.Schema{
592602
"test": {
@@ -614,6 +624,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
614624
Diagnostics: []*tfplugin6.Diagnostic{},
615625
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
616626
ListResourceSchemas: map[string]*tfplugin6.Schema{},
627+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
617628
Functions: map[string]*tfplugin6.Function{},
618629
ResourceSchemas: map[string]*tfplugin6.Schema{},
619630
ServerCapabilities: &tfplugin6.ServerCapabilities{
@@ -645,7 +656,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
645656
tfplugin6.ServerCapabilities{},
646657
)
647658

648-
if diff := cmp.Diff(got, testCase.expected, diffOpts); diff != "" {
659+
if diff := cmp.Diff(testCase.expected, got, diffOpts); diff != "" {
649660
t.Errorf("unexpected difference: %s", diff)
650661
}
651662
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package toproto
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
9+
)
10+
11+
func ValidateStateStoreConfig_Response(in *tfprotov6.ValidateStateStoreResponse) *tfplugin6.ValidateStateStore_Response {
12+
if in == nil {
13+
return nil
14+
}
15+
16+
return &tfplugin6.ValidateStateStore_Response{
17+
Diagnostics: Diagnostics(in.Diagnostics),
18+
}
19+
}
20+
21+
func ConfigureStateStore_Response(in *tfprotov6.ConfigureStateStoreResponse) *tfplugin6.ConfigureStateStore_Response {
22+
if in == nil {
23+
return nil
24+
}
25+
26+
return &tfplugin6.ConfigureStateStore_Response{
27+
Diagnostics: Diagnostics(in.Diagnostics),
28+
}
29+
}
30+
31+
func GetStates_Response(in *tfprotov6.GetStatesResponse) *tfplugin6.GetStates_Response {
32+
if in == nil {
33+
return nil
34+
}
35+
36+
return &tfplugin6.GetStates_Response{
37+
StateId: in.StateId,
38+
Diagnostics: Diagnostics(in.Diagnostics),
39+
}
40+
}
41+
42+
func DeleteState_Response(in *tfprotov6.DeleteStateResponse) *tfplugin6.DeleteState_Response {
43+
if in == nil {
44+
return nil
45+
}
46+
47+
return &tfplugin6.DeleteState_Response{
48+
Diagnostics: Diagnostics(in.Diagnostics),
49+
}
50+
}

tfprotov6/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ type GetProviderSchemaResponse struct {
201201
// shortname and an underscore.
202202
ActionSchemas map[string]*ActionSchema
203203

204+
// StateStoreSchemas is a map of state store name and its schema
205+
StateStoreSchemas map[string]*Schema
206+
204207
// Diagnostics report errors or warnings related to returning the
205208
// provider's schemas. Returning an empty slice indicates success, with
206209
// no errors or warnings generated.

tfprotov6/state_store.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfprotov6
5+
6+
import "context"
7+
8+
// StateStoreServer is an interface containing the methods an list resource
9+
// implementation needs to fill.
10+
type StateStoreServer interface {
11+
// ValidateStateStoreConfig performs configuration validation
12+
ValidateStateStoreConfig(context.Context, *ValidateStateStoreRequest) (*ValidateStateStoreResponse, error)
13+
14+
// ConfigureStateStore configures the state store, such as S3 connection in the context of already configured provider
15+
ConfigureStateStore(context.Context, *ConfigureStateStoreRequest) (*ConfigureStateStoreResponse, error)
16+
17+
// GetStates returns a list of all states (i.e. CE workspaces) managed by a given state store
18+
GetStates(context.Context, *GetStatesRequest) (*GetStatesResponse, error)
19+
20+
// DeleteState instructs a given state store to delete a specific state (i.e. a CE workspace)
21+
DeleteState(context.Context, *DeleteStateRequest) (*DeleteStateResponse, error)
22+
}
23+
24+
type ValidateStateStoreRequest struct {
25+
TypeName string
26+
Config *DynamicValue
27+
}
28+
29+
type ValidateStateStoreResponse struct {
30+
Diagnostics []*Diagnostic
31+
}
32+
33+
type ConfigureStateStoreRequest struct {
34+
TypeName string
35+
Config *DynamicValue
36+
}
37+
38+
type ConfigureStateStoreResponse struct {
39+
Diagnostics []*Diagnostic
40+
}
41+
42+
type GetStatesRequest struct {
43+
TypeName string
44+
}
45+
46+
type GetStatesResponse struct {
47+
StateId []string
48+
Diagnostics []*Diagnostic
49+
}
50+
51+
type DeleteStateRequest struct {
52+
TypeName string
53+
StateId string
54+
}
55+
56+
type DeleteStateResponse struct {
57+
Diagnostics []*Diagnostic
58+
}

0 commit comments

Comments
 (0)