Move the query options schema into its own set and nested collection of attributes.

This is being done in advance of supporting the services data source.
This commit is contained in:
Sean Chittenden 2017-02-13 14:09:59 -08:00
parent 0d049026be
commit 974415d5cf
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16
4 changed files with 113 additions and 96 deletions

View File

@ -9,13 +9,7 @@ import (
)
const (
allowStale = "allow_stale"
nodeMeta = "node_meta"
nodesAttr = "nodes"
requireConsistent = "require_consistent"
token = "token"
waitIndex = "wait_index"
waitTime = "wait_time"
queryOptNodesAttr = "nodes"
nodeID = "id"
nodeAddress = "address"
@ -23,6 +17,8 @@ const (
nodeName = "name"
nodeTaggedAddresses = "tagged_addresses"
queryOpts = "query_opts"
apiTaggedLAN = "lan"
apiTaggedWAN = "wan"
schemaTaggedLAN = "lan"
@ -33,12 +29,8 @@ func dataSourceConsulCatalogNodes() *schema.Resource {
return &schema.Resource{
Read: dataSourceConsulCatalogNodesRead,
Schema: map[string]*schema.Schema{
allowStale: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeBool,
},
nodesAttr: &schema.Schema{
queryOpts: schemaQueryOpts,
queryOptNodesAttr: &schema.Schema{
Computed: true,
Type: schema.TypeList,
Elem: &schema.Resource{
@ -80,32 +72,6 @@ func dataSourceConsulCatalogNodes() *schema.Resource {
},
},
},
requireConsistent: &schema.Schema{
Optional: true,
Default: false,
Type: schema.TypeBool,
},
token: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeString,
},
waitIndex: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeInt,
ValidateFunc: makeValidationFunc(waitIndex, []interface{}{
validateIntMin(0),
}),
},
waitTime: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeString,
ValidateFunc: makeValidationFunc(waitTime, []interface{}{
validateDurationMin("0ns"),
}),
},
},
}
}
@ -166,7 +132,7 @@ func dataSourceConsulCatalogNodesRead(d *schema.ResourceData, meta interface{})
d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))
d.Set("datacenter", queryOpts.Datacenter)
if err := d.Set(nodesAttr, l); err != nil {
if err := d.Set(queryOptNodesAttr, l); err != nil {
return errwrap.Wrapf("Unable to store nodes: {{err}}", err)
}

View File

@ -26,10 +26,12 @@ func TestAccDataConsulCatalogNodes_basic(t *testing.T) {
const testAccDataConsulCatalogNodesConfig = `
data "consul_catalog_nodes" "read" {
allow_stale = true
require_consistent = false
token = ""
wait_index = 0
wait_time = "1m"
query_options {
allow_stale = true
require_consistent = false
token = ""
wait_index = 0
wait_time = "1m"
}
}
`

View File

@ -0,0 +1,100 @@
package consul
import (
"time"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/schema"
)
const (
queryOptAllowStale = "allow_stale"
queryOptNodeMeta = "node_meta"
queryOptRequireConsistent = "require_consistent"
queryOptToken = "token"
queryOptWaitIndex = "wait_index"
queryOptWaitTime = "wait_time"
)
var schemaQueryOpts = &schema.Schema{
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
queryOptAllowStale: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeBool,
},
queryOptRequireConsistent: &schema.Schema{
Optional: true,
Default: false,
Type: schema.TypeBool,
},
queryOptToken: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeString,
},
queryOptWaitIndex: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeInt,
ValidateFunc: makeValidationFunc(queryOptWaitIndex, []interface{}{
validateIntMin(0),
}),
},
queryOptWaitTime: &schema.Schema{
Optional: true,
Default: true,
Type: schema.TypeString,
ValidateFunc: makeValidationFunc(queryOptWaitTime, []interface{}{
validateDurationMin("0ns"),
}),
},
},
},
}
func getQueryOpts(d *schema.ResourceData, client *consulapi.Client) (*consulapi.QueryOptions, error) {
dc, err := getDC(d, client)
if err != nil {
return nil, err
}
queryOpts := &consulapi.QueryOptions{
Datacenter: dc,
}
if v, ok := d.GetOk(queryOptAllowStale); ok {
queryOpts.AllowStale = v.(bool)
}
if v, ok := d.GetOk(queryOptRequireConsistent); ok {
queryOpts.RequireConsistent = v.(bool)
}
if v, ok := d.GetOk(queryOptNodeMeta); ok {
m := v.(map[string]interface{})
nodeMetaMap := make(map[string]string, len(queryOptNodeMeta))
for s, t := range m {
nodeMetaMap[s] = t.(string)
}
queryOpts.NodeMeta = nodeMetaMap
}
if v, ok := d.GetOk(queryOptToken); ok {
queryOpts.Token = v.(string)
}
if v, ok := d.GetOk(queryOptWaitIndex); ok {
queryOpts.WaitIndex = uint64(v.(int))
}
if v, ok := d.GetOk(queryOptWaitTime); ok {
d, _ := time.ParseDuration(v.(string))
queryOpts.WaitTime = d
}
return queryOpts, nil
}

View File

@ -1,51 +0,0 @@
package consul
import (
"time"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/schema"
)
func getQueryOpts(d *schema.ResourceData, client *consulapi.Client) (*consulapi.QueryOptions, error) {
dc, err := getDC(d, client)
if err != nil {
return nil, err
}
queryOpts := &consulapi.QueryOptions{
Datacenter: dc,
}
if v, ok := d.GetOk(allowStale); ok {
queryOpts.AllowStale = v.(bool)
}
if v, ok := d.GetOk(requireConsistent); ok {
queryOpts.RequireConsistent = v.(bool)
}
if v, ok := d.GetOk(nodeMeta); ok {
m := v.(map[string]interface{})
nodeMetaMap := make(map[string]string, len(nodeMeta))
for s, t := range m {
nodeMetaMap[s] = t.(string)
}
queryOpts.NodeMeta = nodeMetaMap
}
if v, ok := d.GetOk(token); ok {
queryOpts.Token = v.(string)
}
if v, ok := d.GetOk(waitIndex); ok {
queryOpts.WaitIndex = uint64(v.(int))
}
if v, ok := d.GetOk(waitTime); ok {
d, _ := time.ParseDuration(v.(string))
queryOpts.WaitTime = d
}
return queryOpts, nil
}