github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/backend/remote-state/consul/backend.go (about) 1 package consul 2 3 import ( 4 "context" 5 "strings" 6 7 consulapi "github.com/hashicorp/consul/api" 8 "github.com/hashicorp/terraform/backend" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 // New creates a new backend for Consul remote state. 13 func New() backend.Backend { 14 s := &schema.Backend{ 15 Schema: map[string]*schema.Schema{ 16 "path": &schema.Schema{ 17 Type: schema.TypeString, 18 Required: true, 19 Description: "Path to store state in Consul", 20 }, 21 22 "access_token": &schema.Schema{ 23 Type: schema.TypeString, 24 Optional: true, 25 Description: "Access token for a Consul ACL", 26 Default: "", // To prevent input 27 }, 28 29 "address": &schema.Schema{ 30 Type: schema.TypeString, 31 Optional: true, 32 Description: "Address to the Consul Cluster", 33 Default: "", // To prevent input 34 }, 35 36 "scheme": &schema.Schema{ 37 Type: schema.TypeString, 38 Optional: true, 39 Description: "Scheme to communicate to Consul with", 40 Default: "", // To prevent input 41 }, 42 43 "datacenter": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 Description: "Datacenter to communicate with", 47 Default: "", // To prevent input 48 }, 49 50 "http_auth": &schema.Schema{ 51 Type: schema.TypeString, 52 Optional: true, 53 Description: "HTTP Auth in the format of 'username:password'", 54 Default: "", // To prevent input 55 }, 56 57 "gzip": &schema.Schema{ 58 Type: schema.TypeBool, 59 Optional: true, 60 Description: "Compress the state data using gzip", 61 Default: false, 62 }, 63 64 "lock": &schema.Schema{ 65 Type: schema.TypeBool, 66 Optional: true, 67 Description: "Lock state access", 68 Default: true, 69 }, 70 }, 71 } 72 73 result := &Backend{Backend: s} 74 result.Backend.ConfigureFunc = result.configure 75 return result 76 } 77 78 type Backend struct { 79 *schema.Backend 80 81 // The fields below are set from configure 82 configData *schema.ResourceData 83 lock bool 84 } 85 86 func (b *Backend) configure(ctx context.Context) error { 87 // Grab the resource data 88 b.configData = schema.FromContextBackendConfig(ctx) 89 90 // Store the lock information 91 b.lock = b.configData.Get("lock").(bool) 92 93 // Initialize a client to test config 94 _, err := b.clientRaw() 95 return err 96 } 97 98 func (b *Backend) clientRaw() (*consulapi.Client, error) { 99 data := b.configData 100 101 // Configure the client 102 config := consulapi.DefaultConfig() 103 if v, ok := data.GetOk("access_token"); ok && v.(string) != "" { 104 config.Token = v.(string) 105 } 106 if v, ok := data.GetOk("address"); ok && v.(string) != "" { 107 config.Address = v.(string) 108 } 109 if v, ok := data.GetOk("scheme"); ok && v.(string) != "" { 110 config.Scheme = v.(string) 111 } 112 if v, ok := data.GetOk("datacenter"); ok && v.(string) != "" { 113 config.Datacenter = v.(string) 114 } 115 if v, ok := data.GetOk("http_auth"); ok && v.(string) != "" { 116 auth := v.(string) 117 118 var username, password string 119 if strings.Contains(auth, ":") { 120 split := strings.SplitN(auth, ":", 2) 121 username = split[0] 122 password = split[1] 123 } else { 124 username = auth 125 } 126 127 config.HttpAuth = &consulapi.HttpBasicAuth{ 128 Username: username, 129 Password: password, 130 } 131 } 132 133 return consulapi.NewClient(config) 134 }