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"
|
2018-03-17 00:30:07 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2017-02-22 20:17:06 +01:00
|
|
|
|
2018-03-17 00:30:07 +01:00
|
|
|
backendatlas "github.com/hashicorp/terraform/backend/atlas"
|
|
|
|
backendlocal "github.com/hashicorp/terraform/backend/local"
|
2018-03-20 22:17:14 +01:00
|
|
|
backendartifactory "github.com/hashicorp/terraform/backend/remote-state/artifactory"
|
2017-03-30 16:33:54 +02:00
|
|
|
backendAzure "github.com/hashicorp/terraform/backend/remote-state/azure"
|
2018-03-17 00:30:07 +01:00
|
|
|
backendconsul "github.com/hashicorp/terraform/backend/remote-state/consul"
|
|
|
|
backendetcdv2 "github.com/hashicorp/terraform/backend/remote-state/etcdv2"
|
|
|
|
backendetcdv3 "github.com/hashicorp/terraform/backend/remote-state/etcdv3"
|
2017-09-08 16:11:41 +02:00
|
|
|
backendGCS "github.com/hashicorp/terraform/backend/remote-state/gcs"
|
2018-03-17 02:53:21 +01:00
|
|
|
backendhttp "github.com/hashicorp/terraform/backend/remote-state/http"
|
2018-03-17 00:30:07 +01:00
|
|
|
backendinmem "github.com/hashicorp/terraform/backend/remote-state/inmem"
|
2017-10-09 13:46:54 +02:00
|
|
|
backendManta "github.com/hashicorp/terraform/backend/remote-state/manta"
|
2017-03-21 18:43:31 +01:00
|
|
|
backendS3 "github.com/hashicorp/terraform/backend/remote-state/s3"
|
2017-04-22 22:42:37 +02:00
|
|
|
backendSwift "github.com/hashicorp/terraform/backend/remote-state/swift"
|
2018-03-17 00:30:07 +01:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
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.
|
2018-03-17 00:30:07 +01:00
|
|
|
var backends map[string]func() backend.Backend
|
2017-02-22 20:17:06 +01:00
|
|
|
var backendsLock sync.Mutex
|
|
|
|
|
2018-03-17 00:30:07 +01:00
|
|
|
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{
|
2018-03-20 22:17:14 +01:00
|
|
|
"artifactory": func() backend.Backend { return backendartifactory.New() },
|
|
|
|
"atlas": func() backend.Backend { return &backendatlas.Backend{} },
|
|
|
|
"http": func() backend.Backend { return backendhttp.New() },
|
|
|
|
"local": func() backend.Backend { return &backendlocal.Local{} },
|
|
|
|
"consul": func() backend.Backend { return backendconsul.New() },
|
|
|
|
"inmem": func() backend.Backend { return backendinmem.New() },
|
|
|
|
"swift": func() backend.Backend { return backendSwift.New() },
|
|
|
|
"s3": func() backend.Backend { return backendS3.New() },
|
|
|
|
"azurerm": func() backend.Backend { return backendAzure.New() },
|
|
|
|
"etcd": func() backend.Backend { return backendetcdv2.New() },
|
|
|
|
"etcdv3": func() backend.Backend { return backendetcdv3.New() },
|
|
|
|
"gcs": func() backend.Backend { return backendGCS.New() },
|
|
|
|
"manta": func() backend.Backend { return backendManta.New() },
|
2018-03-17 00:30:07 +01:00
|
|
|
|
|
|
|
"azure": func() backend.Backend {
|
|
|
|
return deprecateBackend(
|
|
|
|
backendAzure.New(),
|
|
|
|
`Warning: "azure" name is deprecated, please use "azurerm"`,
|
|
|
|
)
|
2018-07-04 17:24:49 +02:00
|
|
|
},
|
2017-02-22 20:17:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backend returns the initialization factory for the given backend, or
|
|
|
|
// nil if none exists.
|
2018-03-17 00:30:07 +01:00
|
|
|
func Backend(name string) func() backend.Backend {
|
2017-02-22 20:17:06 +01:00
|
|
|
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.
|
2018-03-17 00:30:07 +01:00
|
|
|
func Set(name string, f func() backend.Backend) {
|
2017-02-22 20:37:56 +01:00
|
|
|
backendsLock.Lock()
|
|
|
|
defer backendsLock.Unlock()
|
|
|
|
|
|
|
|
if f == nil {
|
|
|
|
delete(backends, name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backends[name] = f
|
|
|
|
}
|
2017-08-17 17:39:33 +02:00
|
|
|
|
|
|
|
// deprecatedBackendShim is used to wrap a backend and inject a deprecation
|
|
|
|
// warning into the Validate method.
|
|
|
|
type deprecatedBackendShim struct {
|
|
|
|
backend.Backend
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:30:07 +01:00
|
|
|
// ValidateConfig delegates to the wrapped backend to validate its config
|
|
|
|
// and then appends shim's deprecation warning.
|
|
|
|
func (b deprecatedBackendShim) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
|
|
|
|
diags := b.Backend.ValidateConfig(obj)
|
|
|
|
return diags.Append(tfdiags.SimpleWarning(b.Message))
|
2017-08-17 17:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeprecateBackend can be used to wrap a backend to retrun a deprecation
|
|
|
|
// warning during validation.
|
2018-03-17 00:30:07 +01:00
|
|
|
func deprecateBackend(b backend.Backend, message string) backend.Backend {
|
2017-08-17 17:39:33 +02:00
|
|
|
// Since a Backend wrapped by deprecatedBackendShim can no longer be
|
|
|
|
// asserted as an Enhanced or Local backend, disallow those types here
|
|
|
|
// entirely. If something other than a basic backend.Backend needs to be
|
|
|
|
// deprecated, we can add that functionality to schema.Backend or the
|
|
|
|
// backend itself.
|
|
|
|
if _, ok := b.(backend.Enhanced); ok {
|
|
|
|
panic("cannot use DeprecateBackend on an Enhanced Backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.(backend.Local); ok {
|
|
|
|
panic("cannot use DeprecateBackend on a Local Backend")
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:30:07 +01:00
|
|
|
return deprecatedBackendShim{
|
|
|
|
Backend: b,
|
|
|
|
Message: message,
|
2017-08-17 17:39:33 +02:00
|
|
|
}
|
|
|
|
}
|