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