Skip to content

Commit

Permalink
Add support for the targets block in consul_prepared_query
Browse files Browse the repository at this point in the history
Also remove the copyright notices in the examples.

Closes hashicorp#332
  • Loading branch information
remilapeyre committed May 1, 2023
1 parent 9c5772f commit 6581c43
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 310 deletions.
170 changes: 116 additions & 54 deletions consul/resource_consul_prepared_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,125 +22,169 @@ func resourceConsulPreparedQuery() *schema.Resource {

SchemaVersion: 0,

Description: `Allows Terraform to manage a Consul prepared query.
Managing prepared queries is done using Consul's REST API. This resource is useful to provide a consistent and declarative way of managing prepared queries in your Consul cluster using Terraform.`,

Schema: map[string]*schema.Schema{
"datacenter": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The datacenter to use. This overrides the agent's default datacenter and the datacenter in the provider setup.",
},

"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "The name of the prepared query. Used to identify the prepared query during requests. Can be specified as an empty string to configure the query as a catch-all.",
},

"session": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "The name of the Consul session to tie this query's lifetime to. This is an advanced parameter that should not be used without a complete understanding of Consul sessions and the implications of their use (it is recommended to leave this blank in nearly all cases). If this parameter is omitted the query will not expire.",
},

"token": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Deprecated: tokenDeprecationMessage,
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Deprecated: tokenDeprecationMessage,
Description: "The ACL token to use when saving the prepared query. This overrides the token that the agent provides by default.",
},

"stored_token": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "The ACL token to store with the prepared query. This token will be used by default whenever the query is executed.",
},

"service": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "The name of the service to query",
},

"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of required and/or disallowed tags. If a tag is in this list it must be present. If the tag is preceded with a "!" then it is disallowed.`,
},

"near": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "Allows specifying the name of a node to sort results near using Consul's distance sorting and network coordinates. The magic `_agent` value can be used to always sort nearest the node servicing the request.",
},

"only_passing": {
Type: schema.TypeBool,
Optional: true,
Type: schema.TypeBool,
Optional: true,
Description: "When `true`, the prepared query will only return nodes with passing health checks in the result.",
},

"connect": {
Type: schema.TypeBool,
Optional: true,
Type: schema.TypeBool,
Optional: true,
Description: "When `true` the prepared query will return connect proxy services for a queried service. Conditions such as `tags` in the prepared query will be matched against the proxy service. Defaults to false.",
},

"ignore_check_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Specifies a list of check IDs that should be ignored when filtering unhealthy instances. This is mostly useful in an emergency or as a temporary measure when a health check is found to be unreliable. Being able to ignore it in centrally-defined queries can be simpler than de-registering the check as an interim solution until the check can be fixed.",
},

"node_meta": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Specifies a list of user-defined key/value pairs that will be used for filtering the query results to nodes with the given metadata values present.",
},

"service_meta": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Specifies a list of user-defined key/value pairs that will be used for filtering the query results to services with the given metadata values present.",
},

"failover": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Options for controlling behavior when no healthy nodes are available in the local DC.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nearest_n": {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeInt,
Optional: true,
Description: "Return results from this many datacenters, sorted in ascending order of estimated RTT.",
},
"datacenters": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Remote datacenters to return results from.",
},
"targets": {
Type: schema.TypeList,
Optional: true,
Description: "Specifies a sequential list of remote datacenters and cluster peers to failover to if there are no healthy service instances in the local datacenter. This option cannot be used with `nearest_n` or `datacenters`.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"peer": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies a cluster peer to use for failover.",
},
"datacenter": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies a WAN federated datacenter to forward the query to.",
},
},
},
},
},
},
},

"dns": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Settings for controlling the DNS response details.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ttl": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "The TTL to send when returning DNS results.",
},
},
},
},

"template": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Query templating options. This is used to make a single prepared query respond to many different requests",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "The type of template matching to perform. Currently only `name_prefix_match` is supported.",
},
"regexp": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "The regular expression to match with. When using `name_prefix_match`, this regex is applied against the query name.",
},
},
},
Expand Down Expand Up @@ -216,10 +260,18 @@ func resourceConsulPreparedQueryRead(d *schema.ResourceData, meta interface{}) e

// We must write a failover block if the user wrote one or if one of the values
// differ from the defaults
if userWroteFailover || pq.Service.Failover.NearestN > 0 || len(pq.Service.Failover.Datacenters) > 0 {
if userWroteFailover || pq.Service.Failover.NearestN > 0 || len(pq.Service.Failover.Datacenters) > 0 || len(pq.Service.Failover.Targets) > 0 {
targets := []interface{}{}
for _, target := range pq.Service.Failover.Targets {
targets = append(targets, map[string]interface{}{
"peer": target.Peer,
"datacenter": target.Datacenter,
})
}
failover = append(failover, map[string]interface{}{
"nearest_n": pq.Service.Failover.NearestN,
"datacenters": pq.Service.Failover.Datacenters,
"targets": targets,
})
}

Expand Down Expand Up @@ -310,6 +362,16 @@ func preparedQueryDefinitionFromResourceData(d *schema.ResourceData) *consulapi.
failover.Datacenters[i] = v.(string)
}

targets := d.Get("failover.0.targets").([]interface{})
failover.Targets = make([]consulapi.QueryFailoverTarget, len(targets))
for i, v := range targets {
target := v.(map[string]interface{})
failover.Targets[i] = consulapi.QueryFailoverTarget{
Peer: target["peer"].(string),
Datacenter: target["datacenter"].(string),
}
}

pq.Service.Failover = failover
}

Expand Down
34 changes: 34 additions & 0 deletions consul/resource_consul_prepared_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ func TestAccConsulPreparedQuery_blocks(t *testing.T) {
{
Config: testAccConsulPreparedQueryBlocks4,
},
{
Config: testAccConsulPreparedQueryBlocks5,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.#", "1"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.nearest_n", "0"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.datacenters.#", "0"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.targets.#", "2"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.targets.0.peer", "test2"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.targets.0.datacenter", "dc2"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.targets.1.peer", "test3"),
resource.TestCheckResourceAttr("consul_prepared_query.foo", "failover.0.targets.1.datacenter", "dc3"),
),
},
},
})
}
Expand Down Expand Up @@ -407,6 +420,27 @@ resource "consul_prepared_query" "foo" {
}
`

const testAccConsulPreparedQueryBlocks5 = `
resource "consul_prepared_query" "foo" {
name = "foo"
stored_token = "pq-token"
service = "redis"
failover {
targets {
peer = "test2"
datacenter = "dc2"
}
targets {
peer = "test3"
datacenter = "dc3"
}
}
}
`

const testAccConsulPreparedQueryDatacenter = `
resource "consul_prepared_query" "dc1" {
name = "dc1"
Expand Down
Loading

0 comments on commit 6581c43

Please sign in to comment.