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