2017-02-22 20:17:06 +01:00
|
|
|
// Package init contains the list of backends that can be initialized and
|
|
|
|
// basic helper functions for initializing those backends.
|
|
|
|
package init
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
|
2017-02-27 22:37:09 +01:00
|
|
|
backendatlas "github.com/hashicorp/terraform/backend/atlas"
|
2017-02-22 20:17:06 +01:00
|
|
|
backendlegacy "github.com/hashicorp/terraform/backend/legacy"
|
|
|
|
backendlocal "github.com/hashicorp/terraform/backend/local"
|
|
|
|
backendconsul "github.com/hashicorp/terraform/backend/remote-state/consul"
|
|
|
|
backendinmem "github.com/hashicorp/terraform/backend/remote-state/inmem"
|
2017-03-21 18:43:31 +01:00
|
|
|
backendS3 "github.com/hashicorp/terraform/backend/remote-state/s3"
|
2017-02-22 20:17:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// backends is the list of available backends. This is a global variable
|
|
|
|
// because backends are currently hardcoded into Terraform and can't be
|
|
|
|
// modified without recompilation.
|
|
|
|
//
|
|
|
|
// To read an available backend, use the Backend function. This ensures
|
|
|
|
// safe concurrent read access to the list of built-in backends.
|
|
|
|
//
|
|
|
|
// Backends are hardcoded into Terraform because the API for backends uses
|
|
|
|
// complex structures and supporting that over the plugin system is currently
|
|
|
|
// prohibitively difficult. For those wanting to implement a custom backend,
|
|
|
|
// they can do so with recompilation.
|
|
|
|
var backends map[string]func() backend.Backend
|
|
|
|
var backendsLock sync.Mutex
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Our hardcoded backends. We don't need to acquire a lock here
|
|
|
|
// since init() code is serial and can't spawn goroutines.
|
|
|
|
backends = map[string]func() backend.Backend{
|
2017-02-27 22:37:09 +01:00
|
|
|
"atlas": func() backend.Backend { return &backendatlas.Backend{} },
|
2017-02-22 20:17:06 +01:00
|
|
|
"local": func() backend.Backend { return &backendlocal.Local{} },
|
|
|
|
"consul": func() backend.Backend { return backendconsul.New() },
|
|
|
|
"inmem": func() backend.Backend { return backendinmem.New() },
|
2017-03-21 18:43:31 +01:00
|
|
|
"s3": func() backend.Backend { return backendS3.New() },
|
2017-02-22 20:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the legacy remote backends that haven't yet been convertd to
|
|
|
|
// the new backend API.
|
|
|
|
backendlegacy.Init(backends)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backend returns the initialization factory for the given backend, or
|
|
|
|
// nil if none exists.
|
|
|
|
func Backend(name string) func() backend.Backend {
|
|
|
|
backendsLock.Lock()
|
|
|
|
defer backendsLock.Unlock()
|
|
|
|
return backends[name]
|
|
|
|
}
|
|
|
|
|
2017-02-22 20:37:56 +01:00
|
|
|
// Set sets a new backend in the list of backends. If f is nil then the
|
|
|
|
// backend will be removed from the map. If this backend already exists
|
|
|
|
// then it will be overwritten.
|
|
|
|
//
|
|
|
|
// This method sets this backend globally and care should be taken to do
|
|
|
|
// this only before Terraform is executing to prevent odd behavior of backends
|
|
|
|
// changing mid-execution.
|
|
|
|
func Set(name string, f func() backend.Backend) {
|
|
|
|
backendsLock.Lock()
|
|
|
|
defer backendsLock.Unlock()
|
|
|
|
|
|
|
|
if f == nil {
|
|
|
|
delete(backends, name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backends[name] = f
|
|
|
|
}
|