Skip to content

Commit

Permalink
Change how to fall back from remote to local backend
Browse files Browse the repository at this point in the history
In order to support free organizations, we need a way to load the `remote` backend and then, depending on the used offering/plan, enable or disable remote operations.

In other words, we should be able to dynamically fall back to the `local` backend if needed, after first configuring the `remote` backend.

To make this works we need to change the way this was done previously when the env var `TF_FORCE_LOCAL_BACKEND` was set. The clear difference of course being that the env var would be available on startup, while the used offering/plan is only known after being able to connect to TFE.
  • Loading branch information
Sander van Harmelen committed Nov 15, 2018
1 parent 812e7f6 commit 0c259fb
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 179 deletions.
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)
}
}
}
4 changes: 2 additions & 2 deletions backend/local/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ func (b *Local) DeleteWorkspace(name string) error {
}

func (b *Local) StateMgr(name string) (statemgr.Full, error) {
statePath, stateOutPath, backupPath := b.StatePaths(name)

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

statePath, stateOutPath, backupPath := b.StatePaths(name)

s := statemgr.NewFilesystemBetweenPaths(statePath, stateOutPath)
if backupPath != "" {
s.SetBackupPath(backupPath)
Expand Down
Loading

0 comments on commit 0c259fb

Please sign in to comment.