@@ -5,8 +5,11 @@ package pluggable
55
66import (
77 "errors"
8+ "fmt"
9+ "log"
810
911 "github.com/hashicorp/terraform/internal/backend"
12+ "github.com/hashicorp/terraform/internal/backend/pluggable/chunks"
1013 "github.com/hashicorp/terraform/internal/configs/configschema"
1114 "github.com/hashicorp/terraform/internal/providers"
1215 "github.com/hashicorp/terraform/internal/states/remote"
@@ -15,19 +18,22 @@ import (
1518 "github.com/zclconf/go-cty/cty"
1619)
1720
18- // NewPluggable returns an instance of the backend.Backend interface that
19- // contains a provider interface. These are the assumptions about that
20- // provider:
21+ // NewPluggable returns a Pluggable. A Pluggable fulfils the
22+ // backend.Backend interface and allows management of state via
23+ // a state store implemented in the provider that's within the Pluggable.
2124//
25+ // These are the assumptions about that
26+ // provider:
2227// * The provider implements at least one state store.
23- // * The provider has already been configured before using NewPluggable.
28+ // * The provider has already been fully configured before using NewPluggable.
2429//
2530// The state store could also be configured prior to using NewPluggable,
26- // or it could be configured using the relevant backend.Backend methods.
31+ // but preferably it will be configured via the Pluggable,
32+ // using the relevant backend.Backend methods.
2733//
2834// By wrapping a configured provider in a Pluggable we allow calling code
2935// to use the provider's gRPC methods when interacting with state.
30- func NewPluggable (p providers.Interface , typeName string ) (backend. Backend , error ) {
36+ func NewPluggable (p providers.Interface , typeName string ) (* Pluggable , error ) {
3137 if p == nil {
3238 return nil , errors .New ("Attempted to initialize pluggable state with a nil provider interface. This is a bug in Terraform and should be reported" )
3339 }
@@ -102,14 +108,47 @@ func (p *Pluggable) PrepareConfig(config cty.Value) (cty.Value, tfdiags.Diagnost
102108//
103109// Configure implements backend.Backend
104110func (p * Pluggable ) Configure (config cty.Value ) tfdiags.Diagnostics {
111+ var diags tfdiags.Diagnostics
105112 req := providers.ConfigureStateStoreRequest {
106113 TypeName : p .typeName ,
107114 Config : config ,
108115 Capabilities : providers.StateStoreClientCapabilities {
109- ChunkSize : DefaultStateStoreChunkSize ,
116+ // The core binary will always request the default chunk size from the provider to start
117+ ChunkSize : chunks .DefaultStateStoreChunkSize ,
110118 },
111119 }
112120 resp := p .provider .ConfigureStateStore (req )
121+ diags = diags .Append (resp .Diagnostics )
122+ if diags .HasErrors () {
123+ return diags
124+ }
125+
126+ // Validate the returned value from chunk size negotiation
127+ chunkSize := resp .Capabilities .ChunkSize
128+ if chunkSize == 0 || chunkSize > chunks .MaxStateStoreChunkSize {
129+ diags = diags .Append (fmt .Errorf ("Failed to negotiate acceptable chunk size. " +
130+ "Expected size > 0 and <= %d bytes, provider wants %d bytes" ,
131+ chunks .MaxStateStoreChunkSize , chunkSize ,
132+ ))
133+ return diags
134+ }
135+
136+ // Negotiated chunk size is valid, so set it in the provider server
137+ // that will use the value for future RPCs to read/write state.
138+ if cs , ok := p .provider .(providers.StateStoreChunkSizeSetter ); ok {
139+ cs .SetStateStoreChunkSize (p .typeName , int (chunkSize ))
140+ } else {
141+ // TODO: Remove
142+ // I've put this here to try and fish for types of test setup that
143+ // use other things that should implement SetStateStoreChunkSize but
144+ // don't yet.
145+ panic ("provider does not implement providers.StateStoreChunkSizeSetter interface. This is a bug in Terraform and should be reported." )
146+ }
147+ log .Printf ("[TRACE] Pluggable.Configure: negotiated a chunk size of %v when configuring state store %s" ,
148+ chunkSize ,
149+ p .typeName ,
150+ )
151+
113152 return resp .Diagnostics
114153}
115154
0 commit comments