github.com/ticketmaster/terraform@v0.10.0-beta2.0.20170711045249-a12daf5aba4f/backend/init/init.go (about) 1 // Package init contains the list of backends that can be initialized and 2 // basic helper functions for initializing those backends. 3 package init 4 5 import ( 6 "sync" 7 8 "github.com/hashicorp/terraform/backend" 9 10 backendatlas "github.com/hashicorp/terraform/backend/atlas" 11 backendlegacy "github.com/hashicorp/terraform/backend/legacy" 12 backendlocal "github.com/hashicorp/terraform/backend/local" 13 backendconsul "github.com/hashicorp/terraform/backend/remote-state/consul" 14 backendinmem "github.com/hashicorp/terraform/backend/remote-state/inmem" 15 backendS3 "github.com/hashicorp/terraform/backend/remote-state/s3" 16 backendSwift "github.com/hashicorp/terraform/backend/remote-state/swift" 17 ) 18 19 // backends is the list of available backends. This is a global variable 20 // because backends are currently hardcoded into Terraform and can't be 21 // modified without recompilation. 22 // 23 // To read an available backend, use the Backend function. This ensures 24 // safe concurrent read access to the list of built-in backends. 25 // 26 // Backends are hardcoded into Terraform because the API for backends uses 27 // complex structures and supporting that over the plugin system is currently 28 // prohibitively difficult. For those wanting to implement a custom backend, 29 // they can do so with recompilation. 30 var backends map[string]func() backend.Backend 31 var backendsLock sync.Mutex 32 33 func init() { 34 // Our hardcoded backends. We don't need to acquire a lock here 35 // since init() code is serial and can't spawn goroutines. 36 backends = map[string]func() backend.Backend{ 37 "atlas": func() backend.Backend { return &backendatlas.Backend{} }, 38 "local": func() backend.Backend { return &backendlocal.Local{} }, 39 "consul": func() backend.Backend { return backendconsul.New() }, 40 "inmem": func() backend.Backend { return backendinmem.New() }, 41 "swift": func() backend.Backend { return backendSwift.New() }, 42 "s3": func() backend.Backend { return backendS3.New() }, 43 } 44 45 // Add the legacy remote backends that haven't yet been convertd to 46 // the new backend API. 47 backendlegacy.Init(backends) 48 } 49 50 // Backend returns the initialization factory for the given backend, or 51 // nil if none exists. 52 func Backend(name string) func() backend.Backend { 53 backendsLock.Lock() 54 defer backendsLock.Unlock() 55 return backends[name] 56 } 57 58 // Set sets a new backend in the list of backends. If f is nil then the 59 // backend will be removed from the map. If this backend already exists 60 // then it will be overwritten. 61 // 62 // This method sets this backend globally and care should be taken to do 63 // this only before Terraform is executing to prevent odd behavior of backends 64 // changing mid-execution. 65 func Set(name string, f func() backend.Backend) { 66 backendsLock.Lock() 67 defer backendsLock.Unlock() 68 69 if f == nil { 70 delete(backends, name) 71 return 72 } 73 74 backends[name] = f 75 }