Skip to content

Commit f7660c3

Browse files
committed
Implement ValidateStateStoreConfig + ConfigureStateStore methods
1 parent 33a7c2c commit f7660c3

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
},
@@ -353,6 +354,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
353354
Diagnostics: []*tfplugin6.Diagnostic{},
354355
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
355356
ListResourceSchemas: map[string]*tfplugin6.Schema{},
357+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
356358
Functions: map[string]*tfplugin6.Function{},
357359
ResourceSchemas: map[string]*tfplugin6.Schema{},
358360
},
@@ -388,6 +390,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
388390
Diagnostics: []*tfplugin6.Diagnostic{},
389391
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
390392
ListResourceSchemas: map[string]*tfplugin6.Schema{},
393+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
391394
Functions: map[string]*tfplugin6.Function{},
392395
ResourceSchemas: map[string]*tfplugin6.Schema{},
393396
},
@@ -406,6 +409,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
406409
},
407410
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
408411
ListResourceSchemas: map[string]*tfplugin6.Schema{},
412+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
409413
Functions: map[string]*tfplugin6.Function{},
410414
ResourceSchemas: map[string]*tfplugin6.Schema{},
411415
},
@@ -441,6 +445,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
441445
},
442446
},
443447
ListResourceSchemas: map[string]*tfplugin6.Schema{},
448+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
444449
Functions: map[string]*tfplugin6.Function{},
445450
ResourceSchemas: map[string]*tfplugin6.Schema{},
446451
},
@@ -461,6 +466,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
461466
Diagnostics: []*tfplugin6.Diagnostic{},
462467
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
463468
ListResourceSchemas: map[string]*tfplugin6.Schema{},
469+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
464470
Functions: map[string]*tfplugin6.Function{
465471
"test": {
466472
Parameters: []*tfplugin6.Function_Parameter{},
@@ -503,8 +509,9 @@ func TestGetProviderSchema_Response(t *testing.T) {
503509
},
504510
},
505511
},
506-
Functions: map[string]*tfplugin6.Function{},
507-
ResourceSchemas: map[string]*tfplugin6.Schema{},
512+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
513+
Functions: map[string]*tfplugin6.Function{},
514+
ResourceSchemas: map[string]*tfplugin6.Schema{},
508515
},
509516
},
510517
"Provider": {
@@ -525,6 +532,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
525532
Diagnostics: []*tfplugin6.Diagnostic{},
526533
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
527534
ListResourceSchemas: map[string]*tfplugin6.Schema{},
535+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
528536
Functions: map[string]*tfplugin6.Function{},
529537
Provider: &tfplugin6.Schema{
530538
Block: &tfplugin6.Schema_Block{
@@ -557,6 +565,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
557565
Diagnostics: []*tfplugin6.Diagnostic{},
558566
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
559567
ListResourceSchemas: map[string]*tfplugin6.Schema{},
568+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
560569
Functions: map[string]*tfplugin6.Function{},
561570
ProviderMeta: &tfplugin6.Schema{
562571
Block: &tfplugin6.Schema_Block{
@@ -591,6 +600,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
591600
Diagnostics: []*tfplugin6.Diagnostic{},
592601
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
593602
ListResourceSchemas: map[string]*tfplugin6.Schema{},
603+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
594604
Functions: map[string]*tfplugin6.Function{},
595605
ResourceSchemas: map[string]*tfplugin6.Schema{
596606
"test": {
@@ -618,6 +628,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
618628
Diagnostics: []*tfplugin6.Diagnostic{},
619629
EphemeralResourceSchemas: map[string]*tfplugin6.Schema{},
620630
ListResourceSchemas: map[string]*tfplugin6.Schema{},
631+
StateStoreSchemas: map[string]*tfplugin6.Schema{},
621632
Functions: map[string]*tfplugin6.Function{},
622633
ResourceSchemas: map[string]*tfplugin6.Schema{},
623634
ServerCapabilities: &tfplugin6.ServerCapabilities{
@@ -651,7 +662,7 @@ func TestGetProviderSchema_Response(t *testing.T) {
651662
tfplugin6.ServerCapabilities{},
652663
)
653664

654-
if diff := cmp.Diff(got, testCase.expected, diffOpts); diff != "" {
665+
if diff := cmp.Diff(testCase.expected, got, diffOpts); diff != "" {
655666
t.Errorf("unexpected difference: %s", diff)
656667
}
657668
})
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)