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"
|
|
|
|
|
2019-10-11 11:34:26 +02:00
|
|
|
"github.com/hashicorp/terraform-svchost/disco"
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2018-10-31 16:45:03 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-02-22 20:17:06 +01:00
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
backendLocal "github.com/hashicorp/terraform/internal/backend/local"
|
|
|
|
backendRemote "github.com/hashicorp/terraform/internal/backend/remote"
|
|
|
|
backendArtifactory "github.com/hashicorp/terraform/internal/backend/remote-state/artifactory"
|
|
|
|
backendAzure "github.com/hashicorp/terraform/internal/backend/remote-state/azure"
|
|
|
|
backendConsul "github.com/hashicorp/terraform/internal/backend/remote-state/consul"
|
|
|
|
backendCos "github.com/hashicorp/terraform/internal/backend/remote-state/cos"
|
|
|
|
backendEtcdv2 "github.com/hashicorp/terraform/internal/backend/remote-state/etcdv2"
|
|
|
|
backendEtcdv3 "github.com/hashicorp/terraform/internal/backend/remote-state/etcdv3"
|
|
|
|
backendGCS "github.com/hashicorp/terraform/internal/backend/remote-state/gcs"
|
|
|
|
backendHTTP "github.com/hashicorp/terraform/internal/backend/remote-state/http"
|
|
|
|
backendInmem "github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
|
|
|
|
backendKubernetes "github.com/hashicorp/terraform/internal/backend/remote-state/kubernetes"
|
|
|
|
backendManta "github.com/hashicorp/terraform/internal/backend/remote-state/manta"
|
|
|
|
backendOSS "github.com/hashicorp/terraform/internal/backend/remote-state/oss"
|
|
|
|
backendPg "github.com/hashicorp/terraform/internal/backend/remote-state/pg"
|
|
|
|
backendS3 "github.com/hashicorp/terraform/internal/backend/remote-state/s3"
|
|
|
|
backendSwift "github.com/hashicorp/terraform/internal/backend/remote-state/swift"
|
2021-08-12 21:30:24 +02:00
|
|
|
backendCloud "github.com/hashicorp/terraform/internal/cloud"
|
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-10-31 16:45:03 +01:00
|
|
|
var backends map[string]backend.InitFn
|
2017-02-22 20:17:06 +01:00
|
|
|
var backendsLock sync.Mutex
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// Init initializes the backends map with all our hardcoded backends.
|
2018-10-17 04:48:28 +02:00
|
|
|
func Init(services *disco.Disco) {
|
2018-10-31 16:45:03 +01:00
|
|
|
backendsLock.Lock()
|
|
|
|
defer backendsLock.Unlock()
|
|
|
|
|
|
|
|
backends = map[string]backend.InitFn{
|
2018-11-15 20:26:46 +01:00
|
|
|
"local": func() backend.Backend { return backendLocal.New() },
|
|
|
|
"remote": func() backend.Backend { return backendRemote.New(services) },
|
2018-10-31 16:45:03 +01:00
|
|
|
|
|
|
|
// Remote State backends.
|
|
|
|
"artifactory": func() backend.Backend { return backendArtifactory.New() },
|
2018-03-20 22:17:14 +01:00
|
|
|
"azurerm": func() backend.Backend { return backendAzure.New() },
|
2018-10-31 16:45:03 +01:00
|
|
|
"consul": func() backend.Backend { return backendConsul.New() },
|
2020-02-13 17:37:11 +01:00
|
|
|
"cos": func() backend.Backend { return backendCos.New() },
|
2018-10-31 16:45:03 +01:00
|
|
|
"etcd": func() backend.Backend { return backendEtcdv2.New() },
|
|
|
|
"etcdv3": func() backend.Backend { return backendEtcdv3.New() },
|
2018-03-20 22:17:14 +01:00
|
|
|
"gcs": func() backend.Backend { return backendGCS.New() },
|
2018-10-31 16:45:03 +01:00
|
|
|
"http": func() backend.Backend { return backendHTTP.New() },
|
|
|
|
"inmem": func() backend.Backend { return backendInmem.New() },
|
2020-04-13 22:17:17 +02:00
|
|
|
"kubernetes": func() backend.Backend { return backendKubernetes.New() },
|
2018-03-20 22:17:14 +01:00
|
|
|
"manta": func() backend.Backend { return backendManta.New() },
|
2019-03-07 11:31:36 +01:00
|
|
|
"oss": func() backend.Backend { return backendOSS.New() },
|
2018-10-13 02:00:22 +02:00
|
|
|
"pg": func() backend.Backend { return backendPg.New() },
|
2018-10-31 16:45:03 +01:00
|
|
|
"s3": func() backend.Backend { return backendS3.New() },
|
|
|
|
"swift": func() backend.Backend { return backendSwift.New() },
|
2018-03-17 00:30:07 +01:00
|
|
|
|
2021-08-12 21:30:24 +02:00
|
|
|
// Terraform Cloud 'backend'
|
|
|
|
// This is an implementation detail only, used for the cloud package
|
|
|
|
"cloud": func() backend.Backend { return backendCloud.New(services) },
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// Deprecated backends.
|
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-10-31 16:45:03 +01:00
|
|
|
func Backend(name string) backend.InitFn {
|
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-10-31 16:45:03 +01:00
|
|
|
func Set(name string, f backend.InitFn) {
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-26 00:37:20 +01:00
|
|
|
// PrepareConfig delegates to the wrapped backend to validate its config
|
2018-03-17 00:30:07 +01:00
|
|
|
// and then appends shim's deprecation warning.
|
2019-02-26 00:37:20 +01:00
|
|
|
func (b deprecatedBackendShim) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
|
|
|
|
newObj, diags := b.Backend.PrepareConfig(obj)
|
|
|
|
return newObj, 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
|
|
|
}
|
|
|
|
}
|