github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/consul/data_source_consul_keys.go (about) 1 package consul 2 3 import ( 4 consulapi "github.com/hashicorp/consul/api" 5 "github.com/hashicorp/terraform/helper/schema" 6 ) 7 8 func dataSourceConsulKeys() *schema.Resource { 9 return &schema.Resource{ 10 Read: dataSourceConsulKeysRead, 11 12 Schema: map[string]*schema.Schema{ 13 "datacenter": &schema.Schema{ 14 Type: schema.TypeString, 15 Optional: true, 16 Computed: true, 17 ForceNew: true, 18 }, 19 20 "token": &schema.Schema{ 21 Type: schema.TypeString, 22 Optional: true, 23 }, 24 25 "key": &schema.Schema{ 26 Type: schema.TypeSet, 27 Optional: true, 28 Elem: &schema.Resource{ 29 Schema: map[string]*schema.Schema{ 30 "name": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 }, 34 35 "path": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 }, 39 40 "default": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 }, 44 }, 45 }, 46 }, 47 48 "var": &schema.Schema{ 49 Type: schema.TypeMap, 50 Computed: true, 51 }, 52 }, 53 } 54 } 55 56 func dataSourceConsulKeysRead(d *schema.ResourceData, meta interface{}) error { 57 client := meta.(*consulapi.Client) 58 kv := client.KV() 59 token := d.Get("token").(string) 60 dc, err := getDC(d, client) 61 if err != nil { 62 return err 63 } 64 65 keyClient := newKeyClient(kv, dc, token) 66 67 vars := make(map[string]string) 68 69 keys := d.Get("key").(*schema.Set).List() 70 for _, raw := range keys { 71 key, path, sub, err := parseKey(raw) 72 if err != nil { 73 return err 74 } 75 76 value, err := keyClient.Get(path) 77 if err != nil { 78 return err 79 } 80 81 value = attributeValue(sub, value) 82 vars[key] = value 83 } 84 85 if err := d.Set("var", vars); err != nil { 86 return err 87 } 88 89 // Store the datacenter on this resource, which can be helpful for reference 90 // in case it was read from the provider 91 d.Set("datacenter", dc) 92 93 d.SetId("-") 94 95 return nil 96 }