diff --git a/consul/resource_consul_prepared_query.go b/consul/resource_consul_prepared_query.go index a3d2950b..52072b71 100644 --- a/consul/resource_consul_prepared_query.go +++ b/consul/resource_consul_prepared_query.go @@ -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.", }, }, }, @@ -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, }) } @@ -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 } diff --git a/consul/resource_consul_prepared_query_test.go b/consul/resource_consul_prepared_query_test.go index 2c4c33f9..63235cfc 100644 --- a/consul/resource_consul_prepared_query_test.go +++ b/consul/resource_consul_prepared_query_test.go @@ -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"), + ), + }, }, }) } @@ -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" diff --git a/docs/resources/prepared_query.md b/docs/resources/prepared_query.md index 9f108e9a..5cf222a2 100644 --- a/docs/resources/prepared_query.md +++ b/docs/resources/prepared_query.md @@ -1,22 +1,21 @@ --- -layout: "consul" -page_title: "Consul: consul_prepared_query" -sidebar_current: "docs-consul-resource-prepared-query" +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "consul_prepared_query Resource - terraform-provider-consul" +subcategory: "" description: |- - Allows Terraform to manage a Consul prepared query + 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. --- -# consul_prepared_query +# consul_prepared_query (Resource) 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. +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. ## Example Usage -```hcl +```terraform # Creates a prepared query myquery.query.consul that finds the nearest # healthy myapp.service.consul instance that has the active tag and not # the standby tag. @@ -71,94 +70,74 @@ resource "consul_prepared_query" "service-near-self" { } ``` -## Argument Reference + +## Schema -The following arguments are supported: +### Required -* `datacenter` - (Optional) The datacenter to use. This overrides the - agent's default datacenter and the datacenter in the provider setup. +- `name` (String) 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. +- `service` (String) The name of the service to query -* `token` - (Optional) The ACL token to use when saving the prepared query. - This overrides the token that the agent provides by default. +### Optional -* `stored_token` - (Optional) The ACL token to store with the prepared - query. This token will be used by default whenever the query is executed. +- `connect` (Boolean) 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. +- `datacenter` (String) The datacenter to use. This overrides the agent's default datacenter and the datacenter in the provider setup. +- `dns` (Block List, Max: 1) Settings for controlling the DNS response details. (see [below for nested schema](#nestedblock--dns)) +- `failover` (Block List, Max: 1) Options for controlling behavior when no healthy nodes are available in the local DC. (see [below for nested schema](#nestedblock--failover)) +- `ignore_check_ids` (List of String) 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. +- `near` (String) 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. +- `node_meta` (Map of String) 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. +- `only_passing` (Boolean) When `true`, the prepared query will only return nodes with passing health checks in the result. +- `service_meta` (Map of String) 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. +- `session` (String) 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. +- `stored_token` (String) The ACL token to store with the prepared query. This token will be used by default whenever the query is executed. +- `tags` (Set of String) 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. +- `template` (Block List, Max: 1) Query templating options. This is used to make a single prepared query respond to many different requests (see [below for nested schema](#nestedblock--template)) +- `token` (String, Sensitive, Deprecated) The ACL token to use when saving the prepared query. This overrides the token that the agent provides by default. -* `name` - (Required) 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. +### Read-Only -* `service` - (Required) The name of the service to query. +- `id` (String) The ID of this resource. -* `session` - (Optional) 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. + +### Nested Schema for `dns` -* `tags` - (Optional) 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. +Optional: -* `only_passing` - (Optional) When `true`, the prepared query will only - return nodes with passing health checks in the result. +- `ttl` (String) The TTL to send when returning DNS results. -* `connect` - (Optional) 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. -* `near` - (Optional) 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. + +### Nested Schema for `failover` -* `ignore_check_ids` - (Optional) 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. +Optional: -* `node_meta` - (Optional) 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. +- `datacenters` (List of String) Remote datacenters to return results from. +- `nearest_n` (Number) Return results from this many datacenters, sorted in ascending order of estimated RTT. +- `targets` (Block List) 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`. (see [below for nested schema](#nestedblock--failover--targets)) -* `service_meta` - (Optional) 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. + +### Nested Schema for `failover.targets` +Optional: -* `failover` - (Optional) Options for controlling behavior when no healthy - nodes are available in the local DC. +- `datacenter` (String) Specifies a WAN federated datacenter to forward the query to. +- `peer` (String) Specifies a cluster peer to use for failover. - * `nearest_n` - (Optional) Return results from this many datacenters, - sorted in ascending order of estimated RTT. - * `datacenters` - (Optional) Remote datacenters to return results from. -* `dns` - (Optional) Settings for controlling the DNS response details. + +### Nested Schema for `template` - * `ttl` - (Optional) The TTL to send when returning DNS results. +Required: -* `template` - (Optional) Query templating options. This is used to make a - single prepared query respond to many different requests. - - * `type` - (Required) The type of template matching to perform. Currently - only `name_prefix_match` is supported. - - * `regexp` - (Required) The regular expression to match with. When using - `name_prefix_match`, this regex is applied against the query name. - -## Attributes Reference - -The following attributes are exported: - -* `id` - The ID of the prepared query, generated by Consul. +- `regexp` (String) The regular expression to match with. When using `name_prefix_match`, this regex is applied against the query name. +- `type` (String) The type of template matching to perform. Currently only `name_prefix_match` is supported. ## Import -`consul_prepared_query` can be imported with the query's ID in the Consul HTTP API. +Import is supported using the following syntax: -``` -$ terraform import consul_prepared_query.my_service 71ecfb82-717a-4258-b4b6-2fb75144d856 +```shell +terraform import consul_prepared_query.my_service 71ecfb82-717a-4258-b4b6-2fb75144d856 ``` diff --git a/examples/data-sources/consul_peering/data-source.tf b/examples/data-sources/consul_peering/data-source.tf index 8d3a3bb2..6451ced4 100644 --- a/examples/data-sources/consul_peering/data-source.tf +++ b/examples/data-sources/consul_peering/data-source.tf @@ -1,6 +1,3 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - data "consul_peering" "basic" { peer_name = "peered-cluster" } diff --git a/examples/data-sources/consul_peerings/data-source.tf b/examples/data-sources/consul_peerings/data-source.tf index b36fb33b..646c357d 100644 --- a/examples/data-sources/consul_peerings/data-source.tf +++ b/examples/data-sources/consul_peerings/data-source.tf @@ -1,4 +1 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - data "consul_peerings" "peers" {} diff --git a/examples/kv/main.tf b/examples/kv/main.tf index d849a6d9..515ebde4 100644 --- a/examples/kv/main.tf +++ b/examples/kv/main.tf @@ -1,6 +1,3 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - # Setup the Consul provisioner to use the demo cluster provider "consul" { address = "demo.consul.io:80" diff --git a/examples/kv/variables.tf b/examples/kv/variables.tf index 0caa092b..2cb9be01 100644 --- a/examples/kv/variables.tf +++ b/examples/kv/variables.tf @@ -1,6 +1,3 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - variable "aws_region" { description = "The AWS region to create resources in." default = "us-east-1" diff --git a/examples/resources/consul_peering/resource.tf b/examples/resources/consul_peering/resource.tf index 3e5dd14e..ceef5437 100644 --- a/examples/resources/consul_peering/resource.tf +++ b/examples/resources/consul_peering/resource.tf @@ -1,6 +1,3 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - # Create a peering between the EU and US Consul clusters provider "consul" { diff --git a/examples/resources/consul_peering_token/resource.tf b/examples/resources/consul_peering_token/resource.tf index 6c68e994..7730205b 100644 --- a/examples/resources/consul_peering_token/resource.tf +++ b/examples/resources/consul_peering_token/resource.tf @@ -1,6 +1,3 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - resource "consul_peering_token" "token" { peer_name = "eu-cluster" } diff --git a/examples/resources/consul_prepared_query/import.sh b/examples/resources/consul_prepared_query/import.sh new file mode 100644 index 00000000..fd19dc40 --- /dev/null +++ b/examples/resources/consul_prepared_query/import.sh @@ -0,0 +1 @@ +terraform import consul_prepared_query.my_service 71ecfb82-717a-4258-b4b6-2fb75144d856 diff --git a/examples/resources/consul_prepared_query/resource.tf b/examples/resources/consul_prepared_query/resource.tf new file mode 100644 index 00000000..bf4bdbd5 --- /dev/null +++ b/examples/resources/consul_prepared_query/resource.tf @@ -0,0 +1,52 @@ +# Creates a prepared query myquery.query.consul that finds the nearest +# healthy myapp.service.consul instance that has the active tag and not +# the standby tag. +resource "consul_prepared_query" "myapp-query" { + name = "myquery" + datacenter = "us-central1" + token = "abcd" + stored_token = "wxyz" + only_passing = true + near = "_agent" + + service = "myapp" + tags = ["active", "!standby"] + + failover { + nearest_n = 3 + datacenters = ["us-west1", "us-east-2", "asia-east1"] + } + + dns { + ttl = "30s" + } +} + +# Creates a Prepared Query Template that matches *-near-self.query.consul +# and finds the nearest service that matches the glob character (e.g. +# foo-near-self.query.consul will find the nearest healthy foo.service.consul). +resource "consul_prepared_query" "service-near-self" { + datacenter = "nyc1" + token = "abcd" + stored_token = "wxyz" + name = "" + only_passing = true + connect = true + near = "_agent" + + template { + type = "name_prefix_match" + regexp = "^(.*)-near-self$" + } + + service = "$${match(1)}" + + failover { + nearest_n = 3 + datacenters = ["dc2", "dc3", "dc4"] + } + + dns { + ttl = "5m" + } +} diff --git a/templates/resources/prepared_query.md b/templates/resources/prepared_query.md deleted file mode 100644 index 9f108e9a..00000000 --- a/templates/resources/prepared_query.md +++ /dev/null @@ -1,164 +0,0 @@ ---- -layout: "consul" -page_title: "Consul: consul_prepared_query" -sidebar_current: "docs-consul-resource-prepared-query" -description: |- - Allows Terraform to manage a Consul prepared query ---- - -# consul_prepared_query - -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. - -## Example Usage - -```hcl -# Creates a prepared query myquery.query.consul that finds the nearest -# healthy myapp.service.consul instance that has the active tag and not -# the standby tag. -resource "consul_prepared_query" "myapp-query" { - name = "myquery" - datacenter = "us-central1" - token = "abcd" - stored_token = "wxyz" - only_passing = true - near = "_agent" - - service = "myapp" - tags = ["active", "!standby"] - - failover { - nearest_n = 3 - datacenters = ["us-west1", "us-east-2", "asia-east1"] - } - - dns { - ttl = "30s" - } -} - -# Creates a Prepared Query Template that matches *-near-self.query.consul -# and finds the nearest service that matches the glob character (e.g. -# foo-near-self.query.consul will find the nearest healthy foo.service.consul). -resource "consul_prepared_query" "service-near-self" { - datacenter = "nyc1" - token = "abcd" - stored_token = "wxyz" - name = "" - only_passing = true - connect = true - near = "_agent" - - template { - type = "name_prefix_match" - regexp = "^(.*)-near-self$" - } - - service = "$${match(1)}" - - failover { - nearest_n = 3 - datacenters = ["dc2", "dc3", "dc4"] - } - - dns { - ttl = "5m" - } -} -``` - -## Argument Reference - -The following arguments are supported: - -* `datacenter` - (Optional) The datacenter to use. This overrides the - agent's default datacenter and the datacenter in the provider setup. - -* `token` - (Optional) The ACL token to use when saving the prepared query. - This overrides the token that the agent provides by default. - -* `stored_token` - (Optional) The ACL token to store with the prepared - query. This token will be used by default whenever the query is executed. - -* `name` - (Required) 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. - -* `service` - (Required) The name of the service to query. - -* `session` - (Optional) 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. - -* `tags` - (Optional) 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. - -* `only_passing` - (Optional) When `true`, the prepared query will only - return nodes with passing health checks in the result. - -* `connect` - (Optional) 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. - -* `near` - (Optional) 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. - -* `ignore_check_ids` - (Optional) 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` - (Optional) 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` - (Optional) 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` - (Optional) Options for controlling behavior when no healthy - nodes are available in the local DC. - - * `nearest_n` - (Optional) Return results from this many datacenters, - sorted in ascending order of estimated RTT. - - * `datacenters` - (Optional) Remote datacenters to return results from. - -* `dns` - (Optional) Settings for controlling the DNS response details. - - * `ttl` - (Optional) The TTL to send when returning DNS results. - -* `template` - (Optional) Query templating options. This is used to make a - single prepared query respond to many different requests. - - * `type` - (Required) The type of template matching to perform. Currently - only `name_prefix_match` is supported. - - * `regexp` - (Required) The regular expression to match with. When using - `name_prefix_match`, this regex is applied against the query name. - -## Attributes Reference - -The following attributes are exported: - -* `id` - The ID of the prepared query, generated by Consul. - -## Import - -`consul_prepared_query` can be imported with the query's ID in the Consul HTTP API. - -``` -$ terraform import consul_prepared_query.my_service 71ecfb82-717a-4258-b4b6-2fb75144d856 -```