Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/remote: change how to fall back to a local backend #19378

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions backend/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package init

import (
"os"
"sync"

"github.com/hashicorp/terraform/backend"
Expand Down Expand Up @@ -48,14 +47,8 @@ func Init(services *disco.Disco) {

backends = map[string]backend.InitFn{
// Enhanced backends.
"local": func() backend.Backend { return backendLocal.New() },
"remote": func() backend.Backend {
b := backendRemote.New(services)
if os.Getenv("TF_FORCE_LOCAL_BACKEND") != "" {
return backendLocal.NewWithBackend(b)
}
return b
},
"local": func() backend.Backend { return backendLocal.New() },
"remote": func() backend.Backend { return backendRemote.New(services) },

// Remote State backends.
"artifactory": func() backend.Backend { return backendArtifactory.New() },
Expand Down
42 changes: 0 additions & 42 deletions backend/init/init_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package init

import (
"os"
"reflect"
"testing"

backendLocal "github.com/hashicorp/terraform/backend/local"
)

func TestInit_backend(t *testing.T) {
Expand Down Expand Up @@ -44,42 +41,3 @@ func TestInit_backend(t *testing.T) {
})
}
}

func TestInit_forceLocalBackend(t *testing.T) {
// Initialize the backends map
Init(nil)

enhancedBackends := []struct {
Name string
Type string
}{
{"local", "nil"},
{"remote", "*remote.Remote"},
}

// Set the TF_FORCE_LOCAL_BACKEND flag so all enhanced backends will
// return a local.Local backend with themselves as embedded backend.
if err := os.Setenv("TF_FORCE_LOCAL_BACKEND", "1"); err != nil {
t.Fatalf("error setting environment variable TF_FORCE_LOCAL_BACKEND: %v", err)
}
defer os.Unsetenv("TF_FORCE_LOCAL_BACKEND")

// Make sure we always get the local backend.
for _, b := range enhancedBackends {
f := Backend(b.Name)

local, ok := f().(*backendLocal.Local)
if !ok {
t.Fatalf("expected backend %q to be \"*local.Local\", got: %T", b.Name, f())
}

bType := "nil"
if local.Backend != nil {
bType = reflect.TypeOf(local.Backend).String()
}

if bType != b.Type {
t.Fatalf("expected local.Backend to be %s, got: %s", b.Type, bType)
}
}
}
6 changes: 3 additions & 3 deletions backend/local/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,6 @@ func (b *Local) DeleteWorkspace(name string) error {
}

func (b *Local) StateMgr(name string) (statemgr.Full, error) {
statePath, stateOutPath, backupPath := b.StatePaths(name)
log.Printf("[TRACE] backend/local: state manager for workspace %q will:\n - read initial snapshot from %s\n - write new snapshots to %s\n - create any backup at %s", name, statePath, stateOutPath, backupPath)

// If we have a backend handling state, delegate to that.
if b.Backend != nil {
return b.Backend.StateMgr(name)
Expand All @@ -274,6 +271,9 @@ func (b *Local) StateMgr(name string) (statemgr.Full, error) {
return nil, err
}

statePath, stateOutPath, backupPath := b.StatePaths(name)
log.Printf("[TRACE] backend/local: state manager for workspace %q will:\n - read initial snapshot from %s\n - write new snapshots to %s\n - create any backup at %s", name, statePath, stateOutPath, backupPath)

s := statemgr.NewFilesystemBetweenPaths(statePath, stateOutPath)
if backupPath != "" {
s.SetBackupPath(backupPath)
Expand Down
5 changes: 3 additions & 2 deletions backend/local/backend_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
func TestLocal_applyBasic(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", applyFixtureSchema())

p := TestLocalProvider(t, b, "test", applyFixtureSchema())
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
"ami": cty.StringVal("bar"),
Expand Down Expand Up @@ -95,8 +95,8 @@ func TestLocal_applyEmptyDir(t *testing.T) {
func TestLocal_applyEmptyDirDestroy(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})

p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{}

op, configCleanup := testOperationApply(t, "./test-fixtures/empty")
Expand All @@ -122,6 +122,7 @@ func TestLocal_applyEmptyDirDestroy(t *testing.T) {
func TestLocal_applyError(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()

p := TestLocalProvider(t, b, "test", nil)
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
Expand Down
1 change: 0 additions & 1 deletion backend/local/backend_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func (b *Local) opPlan(
}

func (b *Local) renderPlan(plan *plans.Plan, schemas *terraform.Schemas) {

counts := map[plans.Action]int{}
for _, change := range plan.Changes.Resources {
counts[change.Action]++
Expand Down
Loading