Skip to content

Commit c833aa7

Browse files
committed
WIP
1 parent 32a9788 commit c833aa7

File tree

15 files changed

+194
-0
lines changed

15 files changed

+194
-0
lines changed

internal/builtin/providers/terraform/provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (p *Provider) GetProviderSchema() providers.GetProviderSchemaResponse {
7373
ReturnType: cty.String,
7474
},
7575
},
76+
StateStores: map[string]providers.Schema{},
7677
}
7778
providers.SchemaCache.Set(tfaddr.NewProvider(tfaddr.BuiltInProviderHost, tfaddr.BuiltInProviderNamespace, "terraform"), resp)
7879
return resp
@@ -275,3 +276,9 @@ func (p *Provider) CallFunction(req providers.CallFunctionRequest) providers.Cal
275276
func (p *Provider) Close() error {
276277
return nil
277278
}
279+
280+
func (p *Provider) ValidateStorageConfig(req providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
281+
var resp providers.ValidateStorageConfigResponse
282+
resp.Diagnostics.Append(fmt.Errorf("unsupported storage type %q", req.TypeName))
283+
return resp
284+
}

internal/command/testing/test_provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ var (
9090
ReturnType: cty.Bool,
9191
},
9292
},
93+
// TODO - add a State Stores map here?
9394
}
9495
)
9596

internal/grpcwrap/provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (p *provider) GetSchema(_ context.Context, req *tfplugin5.GetProviderSchema
4646
DataSourceSchemas: make(map[string]*tfplugin5.Schema),
4747
EphemeralResourceSchemas: make(map[string]*tfplugin5.Schema),
4848
ListResourceSchemas: make(map[string]*tfplugin5.Schema),
49+
StateStoreSchemas: make(map[string]*tfplugin5.Schema),
4950
}
5051

5152
resp.Provider = &tfplugin5.Schema{
@@ -86,6 +87,12 @@ func (p *provider) GetSchema(_ context.Context, req *tfplugin5.GetProviderSchema
8687
Block: convert.ConfigSchemaToProto(dat.Body),
8788
}
8889
}
90+
for typ, dat := range p.schema.StateStores {
91+
resp.StateStoreSchemas[typ] = &tfplugin5.Schema{
92+
Version: int64(dat.Version),
93+
Block: convert.ConfigSchemaToProto(dat.Body),
94+
}
95+
}
8996
if decls, err := convert.FunctionDeclsToProto(p.schema.Functions); err == nil {
9097
resp.Functions = decls
9198
} else {

internal/grpcwrap/provider6.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (p *provider6) GetProviderSchema(_ context.Context, req *tfplugin6.GetProvi
4848
EphemeralResourceSchemas: make(map[string]*tfplugin6.Schema),
4949
Functions: make(map[string]*tfplugin6.Function),
5050
ListResourceSchemas: make(map[string]*tfplugin6.Schema),
51+
StateStoreSchemas: make(map[string]*tfplugin6.Schema),
5152
}
5253

5354
resp.Provider = &tfplugin6.Schema{
@@ -88,6 +89,13 @@ func (p *provider6) GetProviderSchema(_ context.Context, req *tfplugin6.GetProvi
8889
Block: convert.ConfigSchemaToProto(dat.Body),
8990
}
9091
}
92+
93+
for typ, dat := range p.schema.StateStores {
94+
resp.StateStoreSchemas[typ] = &tfplugin6.Schema{
95+
Version: int64(dat.Version),
96+
Block: convert.ConfigSchemaToProto(dat.Body),
97+
}
98+
}
9199
if decls, err := convert.FunctionDeclsToProto(p.schema.Functions); err == nil {
92100
resp.Functions = decls
93101
} else {
@@ -814,6 +822,7 @@ func (p *provider6) ListResource(*tfplugin6.ListResource_Request, tfplugin6.Prov
814822
panic("not implemented")
815823
}
816824

825+
<<<<<<< HEAD
817826
func (p *provider6) ValidateStateStoreConfig(ctx context.Context, req *tfplugin6.ValidateStateStore_Request) (*tfplugin6.ValidateStateStore_Response, error) {
818827
panic("not implemented")
819828
}
@@ -844,6 +853,46 @@ func (p *provider6) GetStates(ctx context.Context, req *tfplugin6.GetStates_Requ
844853

845854
func (p *provider6) DeleteState(ctx context.Context, req *tfplugin6.DeleteState_Request) (*tfplugin6.DeleteState_Response, error) {
846855
panic("not implemented")
856+
=======
857+
func (p *provider6) ValidateStorageConfig(ctx context.Context, req *tfplugin6.ValidateStorage_Request) (*tfplugin6.ValidateStorage_Response, error) {
858+
// TODO
859+
return nil, nil
860+
}
861+
862+
func (p *provider6) ConfigureStorage(ctx context.Context, req *tfplugin6.ConfigureStorage_Request) (*tfplugin6.ConfigureStorage_Response, error) {
863+
// TODO
864+
return nil, nil
865+
}
866+
867+
func (p *provider6) ReadState(req *tfplugin6.ReadState_Request, srv tfplugin6.Provider_ReadStateServer) error {
868+
// TODO
869+
return nil
870+
}
871+
872+
func (p *provider6) WriteState(srv tfplugin6.Provider_WriteStateServer) error {
873+
// TODO
874+
return nil
875+
}
876+
877+
func (p *provider6) LockState(ctx context.Context, req *tfplugin6.LockState_Request) (*tfplugin6.LockState_Response, error) {
878+
// TODO
879+
return nil, nil
880+
}
881+
882+
func (p *provider6) UnlockState(ctx context.Context, req *tfplugin6.UnlockState_Request) (*tfplugin6.UnlockState_Response, error) {
883+
// TODO
884+
return nil, nil
885+
}
886+
887+
func (p *provider6) GetStates(ctx context.Context, req *tfplugin6.GetStates_Request) (*tfplugin6.GetStates_Response, error) {
888+
// TODO
889+
return nil, nil
890+
}
891+
892+
func (p *provider6) DeleteState(ctx context.Context, req *tfplugin6.DeleteState_Request) (*tfplugin6.DeleteState_Response, error) {
893+
// TODO
894+
return nil, nil
895+
>>>>>>> d7931111d1 (WIP)
847896
}
848897

849898
func (p *provider6) StopProvider(context.Context, *tfplugin6.StopProvider_Request) (*tfplugin6.StopProvider_Response, error) {

internal/plugin/grpc_provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (p *GRPCProviderPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Serve
4646
return nil
4747
}
4848

49+
var _ providers.Interface = &GRPCProvider{}
50+
4951
// GRPCProvider handles the client, or core side of the plugin rpc connection.
5052
// The GRPCProvider methods are mostly a translation layer between the
5153
// terraform providers types and the grpc proto types, directly converting
@@ -102,6 +104,7 @@ func (p *GRPCProvider) GetProviderSchema() providers.GetProviderSchemaResponse {
102104
resp.DataSources = make(map[string]providers.Schema)
103105
resp.EphemeralResourceTypes = make(map[string]providers.Schema)
104106
resp.ListResourceTypes = make(map[string]providers.Schema)
107+
// TODO: resp.StateStores = make(map[string]providers.Schema)
105108

106109
// Some providers may generate quite large schemas, and the internal default
107110
// grpc response size limit is 4MB. 64MB should cover most any use case, and
@@ -1301,3 +1304,8 @@ func clientCapabilitiesToProto(c providers.ClientCapabilities) *proto.ClientCapa
13011304
WriteOnlyAttributesAllowed: c.WriteOnlyAttributesAllowed,
13021305
}
13031306
}
1307+
1308+
func (p *GRPCProvider) ValidateStorageConfig(r providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
1309+
// TODO
1310+
return providers.ValidateStorageConfigResponse{}
1311+
}

internal/plugin6/grpc_provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (p *GRPCProviderPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Serve
4646
return nil
4747
}
4848

49+
var _ providers.Interface = &GRPCProvider{}
50+
4951
// GRPCProvider handles the client, or core side of the plugin rpc connection.
5052
// The GRPCProvider methods are mostly a translation layer between the
5153
// terraform providers types and the grpc proto types, directly converting
@@ -1246,6 +1248,11 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) error {
12461248
panic("not implemented")
12471249
}
12481250

1251+
func (p *GRPCProvider) ValidateStorageConfig(r providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
1252+
// TODO
1253+
return providers.ValidateStorageConfigResponse{}
1254+
}
1255+
12491256
// closing the grpc connection is final, and terraform will call it at the end of every phase.
12501257
func (p *GRPCProvider) Close() error {
12511258
logger.Trace("GRPCProvider.v6: Close")

internal/provider-simple-v6/provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type simple struct {
2121
schema providers.GetProviderSchemaResponse
2222
}
2323

24+
var _ providers.Interface = simple{}
25+
2426
func Provider() providers.Interface {
2527
simpleResource := providers.Schema{
2628
Body: &configschema.Block{
@@ -251,6 +253,11 @@ func (s simple) CallFunction(req providers.CallFunctionRequest) (resp providers.
251253
return resp
252254
}
253255

256+
func (s simple) ValidateStorageConfig(req providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
257+
// TODO
258+
return providers.ValidateStorageConfigResponse{}
259+
}
260+
254261
func (s simple) Close() error {
255262
return nil
256263
}

internal/provider-simple/provider.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func Provider() providers.Interface {
4949
EphemeralResourceTypes: map[string]providers.Schema{
5050
"simple_resource": simpleResource,
5151
},
52+
StateStores: map[string]providers.Schema{
53+
"simple_store": simpleResource,
54+
},
5255
ServerCapabilities: providers.ServerCapabilities{
5356
PlanDestroy: true,
5457
},
@@ -211,6 +214,12 @@ func (s simple) CallFunction(req providers.CallFunctionRequest) (resp providers.
211214
panic("CallFunction on provider that didn't declare any functions")
212215
}
213216

217+
func (s simple) ValidateStorageConfig(req providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
218+
// Our schema doesn't include any storages, so it should be impossible
219+
// to get in here.
220+
panic("ValidateStorageConfig on provider that didn't declare any storages")
221+
}
222+
214223
func (s simple) Close() error {
215224
return nil
216225
}

internal/providers/mock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ func (m *Mock) CallFunction(request CallFunctionRequest) CallFunctionResponse {
413413
return m.Provider.CallFunction(request)
414414
}
415415

416+
func (m *Mock) ValidateStorageConfig(req ValidateStorageConfigRequest) ValidateStorageConfigResponse {
417+
return m.Provider.ValidateStorageConfig(req)
418+
}
419+
416420
func (m *Mock) Close() error {
417421
return m.Provider.Close()
418422
}

internal/providers/provider.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ type Interface interface {
104104
// CallFunction calls a provider-contributed function.
105105
CallFunction(CallFunctionRequest) CallFunctionResponse
106106

107+
// ValidateStorageConfig allows the provider to validate storage configuration values
108+
ValidateStorageConfig(ValidateStorageConfigRequest) ValidateStorageConfigResponse
109+
// ConfigureStorage(ConfigureStorageRequest) ConfigureStorageResponse
110+
111+
// ReadState(ReadStateRequest, srv ProviderReadStateServer)
112+
// WriteState(srv ProviderWriteStateServer)
113+
114+
// LockState(LockStateRequest) LockStateResponse
115+
// UnlockState(UnlockStateRequest) UnlockStateResponse
116+
// GetStates(GetStatesRequest) GetStatesResponse
117+
// DeleteState(DeleteStateRequest) DeleteStateResponse
118+
107119
// Close shuts down the plugin process if applicable.
108120
Close() error
109121
}
@@ -138,6 +150,9 @@ type GetProviderSchemaResponse struct {
138150
// prefix) to the declaration of a function.
139151
Functions map[string]FunctionDecl
140152

153+
// StateStores maps the backend type name to that type's schema.
154+
StateStores map[string]Schema
155+
141156
// Diagnostics contains any warnings or errors from the method call.
142157
Diagnostics tfdiags.Diagnostics
143158

@@ -721,3 +736,16 @@ type ListResourceRequest struct {
721736
// the full resource object for each result
722737
IncludeResourceObject bool
723738
}
739+
740+
type ValidateStorageConfigRequest struct {
741+
// TypeName is the name of the storage type to validate.
742+
TypeName string
743+
744+
// Config is the configuration value to validate.
745+
Config cty.Value
746+
}
747+
748+
type ValidateStorageConfigResponse struct {
749+
// Diagnostics contains any warnings or errors from the method call.
750+
Diagnostics tfdiags.Diagnostics
751+
}

internal/providers/testing/provider_mock.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ type MockProvider struct {
126126
CallFunctionRequest providers.CallFunctionRequest
127127
CallFunctionFn func(providers.CallFunctionRequest) providers.CallFunctionResponse
128128

129+
ValidateStorageConfigCalled bool
130+
ValidateStorageConfigResponse *providers.ValidateStorageConfigResponse
131+
ValidateStorageConfigRequest providers.ValidateStorageConfigRequest
132+
ValidateStorageConfigFn func(providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse
133+
129134
CloseCalled bool
130135
CloseError error
131136
}
@@ -149,6 +154,7 @@ func (p *MockProvider) getProviderSchema() providers.GetProviderSchemaResponse {
149154
Provider: providers.Schema{},
150155
DataSources: map[string]providers.Schema{},
151156
ResourceTypes: map[string]providers.Schema{},
157+
StateStores: map[string]providers.Schema{},
152158
}
153159
}
154160

@@ -827,6 +833,38 @@ func (p *MockProvider) CallFunction(r providers.CallFunctionRequest) providers.C
827833
return p.CallFunctionResponse
828834
}
829835

836+
func (p *MockProvider) ValidateStorageConfig(r providers.ValidateStorageConfigRequest) (resp providers.ValidateStorageConfigResponse) {
837+
p.Lock()
838+
defer p.Unlock()
839+
840+
p.ValidateStorageConfigCalled = true
841+
p.ValidateStorageConfigRequest = r
842+
843+
// Marshall the value to replicate behavior by the GRPC protocol,
844+
// and return any relevant errors
845+
storageSchema, ok := p.getProviderSchema().StateStores[r.TypeName]
846+
if !ok {
847+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
848+
return resp
849+
}
850+
851+
_, err := msgpack.Marshal(r.Config, storageSchema.Body.ImpliedType())
852+
if err != nil {
853+
resp.Diagnostics = resp.Diagnostics.Append(err)
854+
return resp
855+
}
856+
857+
if p.ValidateStorageConfigFn != nil {
858+
return p.ValidateStorageConfigFn(r)
859+
}
860+
861+
if p.ValidateStorageConfigResponse != nil {
862+
return *p.ValidateStorageConfigResponse
863+
}
864+
865+
return resp
866+
}
867+
830868
func (p *MockProvider) Close() error {
831869
p.Lock()
832870
defer p.Unlock()

internal/refactoring/mock_provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func (provider *mockProvider) CallFunction(providers.CallFunctionRequest) provid
118118
panic("not implemented in mock")
119119
}
120120

121+
func (provider *mockProvider) ValidateStorageConfig(req providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
122+
panic("not implemented in mock")
123+
}
124+
121125
func (provider *mockProvider) Close() error {
122126
return nil // do nothing
123127
}

internal/stacks/stackruntime/internal/stackeval/stubs/errored.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,9 @@ func (p *erroredProvider) ValidateListResourceConfig(providers.ValidateListResou
244244
Diagnostics: nil,
245245
}
246246
}
247+
248+
func (p *erroredProvider) ValidateStorageConfig(req providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
249+
return providers.ValidateStorageConfigResponse{
250+
Diagnostics: nil,
251+
}
252+
}

internal/stacks/stackruntime/internal/stackeval/stubs/offline.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,19 @@ func (o *offlineProvider) CallFunction(request providers.CallFunctionRequest) pr
249249
return o.unconfiguredClient.CallFunction(request)
250250
}
251251

252+
func (o *offlineProvider) ValidateStorageConfig(req providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
253+
var diags tfdiags.Diagnostics
254+
diags = diags.Append(tfdiags.AttributeValue(
255+
tfdiags.Error,
256+
"Called ValidateStorageConfig on an unconfigured provider",
257+
"Cannot validate storage configuration because this provider is not configured. This is a bug in Terraform - please report it.",
258+
nil, // nil attribute path means the overall configuration block
259+
))
260+
return providers.ValidateStorageConfigResponse{
261+
Diagnostics: diags,
262+
}
263+
}
264+
252265
func (o *offlineProvider) Close() error {
253266
// pass the close call to the underlying unconfigured client
254267
return o.unconfiguredClient.Close()

internal/stacks/stackruntime/internal/stackeval/stubs/unknown.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ func (u *unknownProvider) CallFunction(_ providers.CallFunctionRequest) provider
297297
}
298298
}
299299

300+
func (u *unknownProvider) ValidateStorageConfig(request providers.ValidateStorageConfigRequest) providers.ValidateStorageConfigResponse {
301+
// This is offline functionality, so we can hand it off to the unconfigured
302+
// client.
303+
return u.unconfiguredClient.ValidateStorageConfig(request)
304+
}
305+
300306
func (u *unknownProvider) Close() error {
301307
// the underlying unconfiguredClient is managed elsewhere.
302308
return nil

0 commit comments

Comments
 (0)