mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
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:
parent
0d049026be
commit
974415d5cf
@ -9,13 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
allowStale = "allow_stale"
|
queryOptNodesAttr = "nodes"
|
||||||
nodeMeta = "node_meta"
|
|
||||||
nodesAttr = "nodes"
|
|
||||||
requireConsistent = "require_consistent"
|
|
||||||
token = "token"
|
|
||||||
waitIndex = "wait_index"
|
|
||||||
waitTime = "wait_time"
|
|
||||||
|
|
||||||
nodeID = "id"
|
nodeID = "id"
|
||||||
nodeAddress = "address"
|
nodeAddress = "address"
|
||||||
@ -23,6 +17,8 @@ const (
|
|||||||
nodeName = "name"
|
nodeName = "name"
|
||||||
nodeTaggedAddresses = "tagged_addresses"
|
nodeTaggedAddresses = "tagged_addresses"
|
||||||
|
|
||||||
|
queryOpts = "query_opts"
|
||||||
|
|
||||||
apiTaggedLAN = "lan"
|
apiTaggedLAN = "lan"
|
||||||
apiTaggedWAN = "wan"
|
apiTaggedWAN = "wan"
|
||||||
schemaTaggedLAN = "lan"
|
schemaTaggedLAN = "lan"
|
||||||
@ -33,12 +29,8 @@ func dataSourceConsulCatalogNodes() *schema.Resource {
|
|||||||
return &schema.Resource{
|
return &schema.Resource{
|
||||||
Read: dataSourceConsulCatalogNodesRead,
|
Read: dataSourceConsulCatalogNodesRead,
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
allowStale: &schema.Schema{
|
queryOpts: schemaQueryOpts,
|
||||||
Optional: true,
|
queryOptNodesAttr: &schema.Schema{
|
||||||
Default: true,
|
|
||||||
Type: schema.TypeBool,
|
|
||||||
},
|
|
||||||
nodesAttr: &schema.Schema{
|
|
||||||
Computed: true,
|
Computed: true,
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeList,
|
||||||
Elem: &schema.Resource{
|
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.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))
|
||||||
|
|
||||||
d.Set("datacenter", 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)
|
return errwrap.Wrapf("Unable to store nodes: {{err}}", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,10 +26,12 @@ func TestAccDataConsulCatalogNodes_basic(t *testing.T) {
|
|||||||
|
|
||||||
const testAccDataConsulCatalogNodesConfig = `
|
const testAccDataConsulCatalogNodesConfig = `
|
||||||
data "consul_catalog_nodes" "read" {
|
data "consul_catalog_nodes" "read" {
|
||||||
allow_stale = true
|
query_options {
|
||||||
require_consistent = false
|
allow_stale = true
|
||||||
token = ""
|
require_consistent = false
|
||||||
wait_index = 0
|
token = ""
|
||||||
wait_time = "1m"
|
wait_index = 0
|
||||||
|
wait_time = "1m"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
100
builtin/providers/consul/query_options.go
Normal file
100
builtin/providers/consul/query_options.go
Normal 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
|
||||||
|
}
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user