Change backend.ValidateConfig to PrepareConfig

This mirrors the change made for providers, so that default values can
be inserted into the config by the backend implementation. This is only
the interface and method name changes, it does not yet add any default
values.
This commit is contained in:
James Bardin 2019-02-25 18:37:20 -05:00
parent 2b9e2b4c2b
commit c814f2da37
16 changed files with 47 additions and 40 deletions

View File

@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/state/remote" "github.com/hashicorp/terraform/state/remote"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -43,9 +42,6 @@ type Backend struct {
// stateClient is the legacy state client, setup in Configure // stateClient is the legacy state client, setup in Configure
stateClient *stateClient stateClient *stateClient
// schema is the schema for configuration, set by init
schema *schema.Backend
// opLock locks operations // opLock locks operations
opLock sync.Mutex opLock sync.Mutex
} }
@ -79,7 +75,7 @@ func (b *Backend) ConfigSchema() *configschema.Block {
} }
} }
func (b *Backend) ValidateConfig(obj cty.Value) tfdiags.Diagnostics { func (b *Backend) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
name := obj.GetAttr("name").AsString() name := obj.GetAttr("name").AsString()
@ -105,7 +101,7 @@ func (b *Backend) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
} }
} }
return diags return obj, diags
} }
func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics { func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics {
@ -116,7 +112,7 @@ func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics {
RunId: os.Getenv("ATLAS_RUN_ID"), RunId: os.Getenv("ATLAS_RUN_ID"),
} }
name := obj.GetAttr("name").AsString() // assumed valid due to ValidateConfig method name := obj.GetAttr("name").AsString() // assumed valid due to PrepareConfig method
slashIdx := strings.Index(name, "/") slashIdx := strings.Index(name, "/")
client.User = name[:slashIdx] client.User = name[:slashIdx]
client.Name = name[slashIdx+1:] client.Name = name[slashIdx+1:]
@ -139,7 +135,7 @@ func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics {
addr := v.AsString() addr := v.AsString()
addrURL, err := url.Parse(addr) addrURL, err := url.Parse(addr)
if err != nil { if err != nil {
// We already validated the URL in ValidateConfig, so this shouldn't happen // We already validated the URL in PrepareConfig, so this shouldn't happen
panic(err) panic(err)
} }
client.Server = addr client.Server = addr

View File

@ -59,9 +59,10 @@ type Backend interface {
// be safely used before configuring. // be safely used before configuring.
ConfigSchema() *configschema.Block ConfigSchema() *configschema.Block
// ValidateConfig checks the validity of the values in the given // PrepareConfig checks the validity of the values in the given
// configuration, assuming that its structure has already been validated // configuration, and inserts any missing defaults, assuming that its
// per the schema returned by ConfigSchema. // structure has already been validated per the schema returned by
// ConfigSchema.
// //
// This method does not have any side-effects for the backend and can // This method does not have any side-effects for the backend and can
// be safely used before configuring. It also does not consult any // be safely used before configuring. It also does not consult any
@ -76,14 +77,14 @@ type Backend interface {
// as tfdiags.AttributeValue, and so the caller should provide the // as tfdiags.AttributeValue, and so the caller should provide the
// necessary context via the diags.InConfigBody method before returning // necessary context via the diags.InConfigBody method before returning
// diagnostics to the user. // diagnostics to the user.
ValidateConfig(cty.Value) tfdiags.Diagnostics PrepareConfig(cty.Value) (cty.Value, tfdiags.Diagnostics)
// Configure uses the provided configuration to set configuration fields // Configure uses the provided configuration to set configuration fields
// within the backend. // within the backend.
// //
// The given configuration is assumed to have already been validated // The given configuration is assumed to have already been validated
// against the schema returned by ConfigSchema and passed validation // against the schema returned by ConfigSchema and passed validation
// via ValidateConfig. // via PrepareConfig.
// //
// This method may be called only once per backend instance, and must be // This method may be called only once per backend instance, and must be
// called before all other methods except where otherwise stated. // called before all other methods except where otherwise stated.

View File

@ -14,7 +14,7 @@ func TestDeprecateBackend(t *testing.T) {
deprecateMessage, deprecateMessage,
) )
diags := deprecatedBackend.ValidateConfig(cty.EmptyObjectVal) _, diags := deprecatedBackend.PrepareConfig(cty.EmptyObjectVal)
if len(diags) != 1 { if len(diags) != 1 {
t.Errorf("got %d diagnostics; want 1", len(diags)) t.Errorf("got %d diagnostics; want 1", len(diags))
for _, diag := range diags { for _, diag := range diags {

View File

@ -108,11 +108,11 @@ type deprecatedBackendShim struct {
Message string Message string
} }
// ValidateConfig delegates to the wrapped backend to validate its config // PrepareConfig delegates to the wrapped backend to validate its config
// and then appends shim's deprecation warning. // and then appends shim's deprecation warning.
func (b deprecatedBackendShim) ValidateConfig(obj cty.Value) tfdiags.Diagnostics { func (b deprecatedBackendShim) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
diags := b.Backend.ValidateConfig(obj) newObj, diags := b.Backend.PrepareConfig(obj)
return diags.Append(tfdiags.SimpleWarning(b.Message)) return newObj, diags.Append(tfdiags.SimpleWarning(b.Message))
} }
// DeprecateBackend can be used to wrap a backend to retrun a deprecation // DeprecateBackend can be used to wrap a backend to retrun a deprecation

View File

@ -139,9 +139,9 @@ func (b *Local) ConfigSchema() *configschema.Block {
} }
} }
func (b *Local) ValidateConfig(obj cty.Value) tfdiags.Diagnostics { func (b *Local) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
if b.Backend != nil { if b.Backend != nil {
return b.Backend.ValidateConfig(obj) return b.Backend.PrepareConfig(obj)
} }
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
@ -170,7 +170,7 @@ func (b *Local) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
} }
} }
return diags return obj, diags
} }
func (b *Local) Configure(obj cty.Value) tfdiags.Diagnostics { func (b *Local) Configure(obj cty.Value) tfdiags.Diagnostics {

View File

@ -17,8 +17,8 @@ func (Nil) ConfigSchema() *configschema.Block {
return &configschema.Block{} return &configschema.Block{}
} }
func (Nil) ValidateConfig(cty.Value) tfdiags.Diagnostics { func (Nil) PrepareConfig(v cty.Value) (cty.Value, tfdiags.Diagnostics) {
return nil return v, nil
} }
func (Nil) Configure(cty.Value) tfdiags.Diagnostics { func (Nil) Configure(cty.Value) tfdiags.Diagnostics {

View File

@ -76,7 +76,7 @@ func TestBackendConfig_invalidKey(t *testing.T) {
"dynamodb_table": "dynamoTable", "dynamodb_table": "dynamoTable",
}) })
diags := New().ValidateConfig(cfg) _, diags := New().PrepareConfig(cfg)
if !diags.HasErrors() { if !diags.HasErrors() {
t.Fatal("expected config validation error") t.Fatal("expected config validation error")
} }

View File

@ -139,8 +139,8 @@ func (b *Remote) ConfigSchema() *configschema.Block {
} }
} }
// ValidateConfig implements backend.Enhanced. // PrepareConfig implements backend.Backend.
func (b *Remote) ValidateConfig(obj cty.Value) tfdiags.Diagnostics { func (b *Remote) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
if val := obj.GetAttr("organization"); val.IsNull() || val.AsString() == "" { if val := obj.GetAttr("organization"); val.IsNull() || val.AsString() == "" {
@ -182,7 +182,7 @@ func (b *Remote) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
)) ))
} }
return diags return obj, diags
} }
// Configure implements backend.Enhanced. // Configure implements backend.Enhanced.

View File

@ -117,7 +117,7 @@ func TestRemote_config(t *testing.T) {
b := New(testDisco(s)) b := New(testDisco(s))
// Validate // Validate
valDiags := b.ValidateConfig(tc.config) _, valDiags := b.PrepareConfig(tc.config)
if (valDiags.Err() != nil || tc.valErr != "") && if (valDiags.Err() != nil || tc.valErr != "") &&
(valDiags.Err() == nil || !strings.Contains(valDiags.Err().Error(), tc.valErr)) { (valDiags.Err() == nil || !strings.Contains(valDiags.Err().Error(), tc.valErr)) {
t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err()) t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err())
@ -196,7 +196,7 @@ func TestRemote_versionConstraints(t *testing.T) {
version.Version = tc.version version.Version = tc.version
// Validate // Validate
valDiags := b.ValidateConfig(tc.config) _, valDiags := b.PrepareConfig(tc.config)
if valDiags.HasErrors() { if valDiags.HasErrors() {
t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err()) t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err())
} }

View File

@ -97,10 +97,11 @@ func testBackend(t *testing.T, obj cty.Value) (*Remote, func()) {
b := New(testDisco(s)) b := New(testDisco(s))
// Configure the backend so the client is created. // Configure the backend so the client is created.
valDiags := b.ValidateConfig(obj) newObj, valDiags := b.PrepareConfig(obj)
if len(valDiags) != 0 { if len(valDiags) != 0 {
t.Fatal(valDiags.ErrWithWarnings()) t.Fatal(valDiags.ErrWithWarnings())
} }
obj = newObj
confDiags := b.Configure(obj) confDiags := b.Configure(obj)
if len(confDiags) != 0 { if len(confDiags) != 0 {

View File

@ -39,13 +39,15 @@ func TestBackendConfig(t *testing.T, b Backend, c hcl.Body) Backend {
obj, decDiags := hcldec.Decode(c, spec, nil) obj, decDiags := hcldec.Decode(c, spec, nil)
diags = diags.Append(decDiags) diags = diags.Append(decDiags)
valDiags := b.ValidateConfig(obj) newObj, valDiags := b.PrepareConfig(obj)
diags = diags.Append(valDiags.InConfigBody(c)) diags = diags.Append(valDiags.InConfigBody(c))
if len(diags) != 0 { if len(diags) != 0 {
t.Fatal(diags.ErrWithWarnings()) t.Fatal(diags.ErrWithWarnings())
} }
obj = newObj
confDiags := b.Configure(obj) confDiags := b.Configure(obj)
if len(confDiags) != 0 { if len(confDiags) != 0 {
confDiags = confDiags.InConfigBody(c) confDiags = confDiags.InConfigBody(c)

View File

@ -92,11 +92,12 @@ func dataSourceRemoteStateRead(d *cty.Value) (cty.Value, tfdiags.Diagnostics) {
return cty.NilVal, diags return cty.NilVal, diags
} }
validateDiags := b.ValidateConfig(configVal) newVal, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags) diags = diags.Append(validateDiags)
if validateDiags.HasErrors() { if validateDiags.HasErrors() {
return cty.NilVal, diags return cty.NilVal, diags
} }
configVal = newVal
configureDiags := b.Configure(configVal) configureDiags := b.Configure(configVal)
if configureDiags.HasErrors() { if configureDiags.HasErrors() {

View File

@ -446,6 +446,7 @@ func (c *InitCommand) initBackend(root *configs.Module, extraConfig rawFlags) (b
ConfigOverride: backendConfigOverride, ConfigOverride: backendConfigOverride,
Init: true, Init: true,
} }
back, backDiags := c.Backend(opts) back, backDiags := c.Backend(opts)
diags = diags.Append(backDiags) diags = diags.Append(backDiags)
return back, true, diags return back, true, diags

View File

@ -180,11 +180,12 @@ func (m *Meta) BackendForPlan(settings plans.Backend) (backend.Enhanced, tfdiags
return nil, diags return nil, diags
} }
validateDiags := b.ValidateConfig(configVal) newVal, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags) diags = diags.Append(validateDiags)
if validateDiags.HasErrors() { if validateDiags.HasErrors() {
return nil, diags return nil, diags
} }
configVal = newVal
configureDiags := b.Configure(configVal) configureDiags := b.Configure(configVal)
diags = diags.Append(configureDiags) diags = diags.Append(configureDiags)
@ -917,11 +918,13 @@ func (m *Meta) backend_C_r_S_unchanged(c *configs.Backend, cHash int, sMgr *stat
} }
// Validate the config and then configure the backend // Validate the config and then configure the backend
validDiags := b.ValidateConfig(configVal) newVal, validDiags := b.PrepareConfig(configVal)
diags = diags.Append(validDiags) diags = diags.Append(validDiags)
if validDiags.HasErrors() { if validDiags.HasErrors() {
return nil, diags return nil, diags
} }
configVal = newVal
configDiags := b.Configure(configVal) configDiags := b.Configure(configVal)
diags = diags.Append(configDiags) diags = diags.Append(configDiags)
if configDiags.HasErrors() { if configDiags.HasErrors() {
@ -1037,11 +1040,12 @@ func (m *Meta) backendInitFromConfig(c *configs.Backend) (backend.Backend, cty.V
} }
} }
validateDiags := b.ValidateConfig(configVal) newVal, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags.InConfigBody(c.Config)) diags = diags.Append(validateDiags.InConfigBody(c.Config))
if validateDiags.HasErrors() { if validateDiags.HasErrors() {
return nil, cty.NilVal, diags return nil, cty.NilVal, diags
} }
configVal = newVal
configureDiags := b.Configure(configVal) configureDiags := b.Configure(configVal)
diags = diags.Append(configureDiags.InConfigBody(c.Config)) diags = diags.Append(configureDiags.InConfigBody(c.Config))
@ -1067,11 +1071,12 @@ func (m *Meta) backendInitFromSaved(s *terraform.BackendState) (backend.Backend,
return nil, diags return nil, diags
} }
validateDiags := b.ValidateConfig(configVal) newVal, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags) diags = diags.Append(validateDiags)
if validateDiags.HasErrors() { if validateDiags.HasErrors() {
return nil, diags return nil, diags
} }
configVal = newVal
configureDiags := b.Configure(configVal) configureDiags := b.Configure(configVal)
diags = diags.Append(configureDiags) diags = diags.Append(configureDiags)

View File

@ -49,9 +49,9 @@ func (b *Backend) ConfigSchema() *configschema.Block {
return b.CoreConfigSchema() return b.CoreConfigSchema()
} }
func (b *Backend) ValidateConfig(obj cty.Value) tfdiags.Diagnostics { func (b *Backend) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
if b == nil { if b == nil {
return nil return obj, nil
} }
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
@ -63,7 +63,7 @@ func (b *Backend) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
for _, err := range errs { for _, err := range errs {
diags = diags.Append(err) diags = diags.Append(err)
} }
return diags return obj, diags
} }
func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics { func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics {

View File

@ -48,7 +48,7 @@ func TestBackendValidate(t *testing.T) {
for i, tc := range cases { for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) { t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
diags := tc.B.ValidateConfig(cty.ObjectVal(tc.Config)) _, diags := tc.B.PrepareConfig(cty.ObjectVal(tc.Config))
if diags.HasErrors() != tc.Err { if diags.HasErrors() != tc.Err {
t.Errorf("wrong number of diagnostics") t.Errorf("wrong number of diagnostics")
} }