github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/consul/data_source_consul_catalog_nodes.go (about) 1 package consul 2 3 import ( 4 "fmt" 5 6 consulapi "github.com/hashicorp/consul/api" 7 "github.com/hashicorp/errwrap" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 const ( 12 catalogNodesElem = "nodes" 13 catalogNodesDatacenter = "datacenter" 14 catalogNodesQueryOpts = "query_options" 15 16 catalogNodesNodeID = "id" 17 catalogNodesNodeAddress = "address" 18 catalogNodesNodeMeta = "meta" 19 catalogNodesNodeName = "name" 20 catalogNodesNodeTaggedAddresses = "tagged_addresses" 21 22 catalogNodesNodeIDs = "node_ids" 23 catalogNodesNodeNames = "node_names" 24 25 catalogNodesAPITaggedLAN = "lan" 26 catalogNodesAPITaggedWAN = "wan" 27 catalogNodesSchemaTaggedLAN = "lan" 28 catalogNodesSchemaTaggedWAN = "wan" 29 ) 30 31 func dataSourceConsulCatalogNodes() *schema.Resource { 32 return &schema.Resource{ 33 Read: dataSourceConsulCatalogNodesRead, 34 Schema: map[string]*schema.Schema{ 35 // Filters 36 catalogNodesQueryOpts: schemaQueryOpts, 37 38 // Out parameters 39 catalogNodesDatacenter: &schema.Schema{ 40 Computed: true, 41 Type: schema.TypeString, 42 }, 43 catalogNodesNodeIDs: &schema.Schema{ 44 Computed: true, 45 Type: schema.TypeList, 46 Elem: &schema.Schema{Type: schema.TypeString}, 47 }, 48 catalogNodesNodeNames: &schema.Schema{ 49 Computed: true, 50 Type: schema.TypeList, 51 Elem: &schema.Schema{Type: schema.TypeString}, 52 }, 53 catalogNodesElem: &schema.Schema{ 54 Computed: true, 55 Type: schema.TypeList, 56 Elem: &schema.Resource{ 57 Schema: map[string]*schema.Schema{ 58 catalogNodesNodeID: &schema.Schema{ 59 Type: schema.TypeString, 60 Computed: true, 61 }, 62 catalogNodesNodeName: &schema.Schema{ 63 Type: schema.TypeString, 64 Computed: true, 65 }, 66 catalogNodesNodeAddress: &schema.Schema{ 67 Type: schema.TypeString, 68 Computed: true, 69 }, 70 catalogNodesNodeMeta: &schema.Schema{ 71 Type: schema.TypeMap, 72 Computed: true, 73 }, 74 catalogNodesNodeTaggedAddresses: &schema.Schema{ 75 Type: schema.TypeMap, 76 Computed: true, 77 Elem: &schema.Resource{ 78 Schema: map[string]*schema.Schema{ 79 catalogNodesSchemaTaggedLAN: &schema.Schema{ 80 Type: schema.TypeString, 81 Computed: true, 82 }, 83 catalogNodesSchemaTaggedWAN: &schema.Schema{ 84 Type: schema.TypeString, 85 Computed: true, 86 }, 87 }, 88 }, 89 }, 90 }, 91 }, 92 }, 93 }, 94 } 95 } 96 97 func dataSourceConsulCatalogNodesRead(d *schema.ResourceData, meta interface{}) error { 98 client := meta.(*consulapi.Client) 99 100 // Parse out data source filters to populate Consul's query options 101 queryOpts, err := getQueryOpts(d, client) 102 if err != nil { 103 return errwrap.Wrapf("unable to get query options for fetching catalog nodes: {{err}}", err) 104 } 105 106 nodes, meta, err := client.Catalog().Nodes(queryOpts) 107 if err != nil { 108 return err 109 } 110 111 l := make([]interface{}, 0, len(nodes)) 112 113 nodeNames := make([]interface{}, 0, len(nodes)) 114 nodeIDs := make([]interface{}, 0, len(nodes)) 115 116 for _, node := range nodes { 117 const defaultNodeAttrs = 4 118 m := make(map[string]interface{}, defaultNodeAttrs) 119 id := node.ID 120 if id == "" { 121 id = node.Node 122 } 123 124 nodeIDs = append(nodeIDs, id) 125 nodeNames = append(nodeNames, node.Node) 126 127 m[catalogNodesNodeAddress] = node.Address 128 m[catalogNodesNodeID] = id 129 m[catalogNodesNodeName] = node.Node 130 m[catalogNodesNodeMeta] = node.Meta 131 m[catalogNodesNodeTaggedAddresses] = node.TaggedAddresses 132 133 l = append(l, m) 134 } 135 136 const idKeyFmt = "catalog-nodes-%s" 137 d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter)) 138 139 d.Set(catalogNodesDatacenter, queryOpts.Datacenter) 140 if err := d.Set(catalogNodesNodeIDs, nodeIDs); err != nil { 141 return errwrap.Wrapf("Unable to store node IDs: {{err}}", err) 142 } 143 144 if err := d.Set(catalogNodesNodeNames, nodeNames); err != nil { 145 return errwrap.Wrapf("Unable to store node names: {{err}}", err) 146 } 147 148 if err := d.Set(catalogNodesElem, l); err != nil { 149 return errwrap.Wrapf("Unable to store nodes: {{err}}", err) 150 } 151 152 return nil 153 }