From 5baf0b9235a126f5721e827c75c93517d41e016c Mon Sep 17 00:00:00 2001 From: Andy Repton Date: Mon, 31 Jan 2022 23:13:08 +0100 Subject: [PATCH] Add new datasource `consul_datacenters` (#293) Closes https://github.com/hashicorp/terraform-provider-consul/issues/290 --- consul/data_source_consul_datacenters.go | 34 ++++++++++++ consul/data_source_consul_datacenters_test.go | 27 ++++++++++ consul/resource_provider.go | 1 + website/docs/d/datacenters.html.markdown | 52 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 consul/data_source_consul_datacenters.go create mode 100644 consul/data_source_consul_datacenters_test.go create mode 100644 website/docs/d/datacenters.html.markdown diff --git a/consul/data_source_consul_datacenters.go b/consul/data_source_consul_datacenters.go new file mode 100644 index 00000000..b33234ca --- /dev/null +++ b/consul/data_source_consul_datacenters.go @@ -0,0 +1,34 @@ +package consul + +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataSourceConsulDatacenters() *schema.Resource { + return &schema.Resource{ + Read: dataSourceConsulDatacentersRead, + Schema: map[string]*schema.Schema{ + // Out parameters + "datacenters": { + Computed: true, + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceConsulDatacentersRead(d *schema.ResourceData, meta interface{}) error { + client, _, _ := getClient(d, meta) + + datacenters, err := client.Catalog().Datacenters() + if err != nil { + return err + } + + d.SetId("-") + sw := newStateWriter(d) + sw.set("datacenters", datacenters) + + return sw.error() +} diff --git a/consul/data_source_consul_datacenters_test.go b/consul/data_source_consul_datacenters_test.go new file mode 100644 index 00000000..b543c8e2 --- /dev/null +++ b/consul/data_source_consul_datacenters_test.go @@ -0,0 +1,27 @@ +package consul + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccDataConsulDatacenters_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataConsulDatacentersConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckDataSourceValue("data.consul_datacenters.read", "datacenters.#", "1"), + testAccCheckDataSourceValue("data.consul_datacenters.read", "datacenters.0", "dc1"), + ), + }, + }, + }) +} + +const testAccDataConsulDatacentersConfig = ` +data "consul_datacenters" "read" {} +` diff --git a/consul/resource_provider.go b/consul/resource_provider.go index b0de1016..61c2ce52 100644 --- a/consul/resource_provider.go +++ b/consul/resource_provider.go @@ -150,6 +150,7 @@ func Provider() terraform.ResourceProvider { "consul_acl_token_secret_id": dataSourceConsulACLTokenSecretID(), "consul_network_segments": dataSourceConsulNetworkSegments(), "consul_network_area_members": dataSourceConsulNetworkAreaMembers(), + "consul_datacenters": dataSourceConsulDatacenters(), // Aliases to limit the impact of rename of catalog // datasources diff --git a/website/docs/d/datacenters.html.markdown b/website/docs/d/datacenters.html.markdown new file mode 100644 index 00000000..c974db9c --- /dev/null +++ b/website/docs/d/datacenters.html.markdown @@ -0,0 +1,52 @@ +--- +layout: "consul" +page_title: "Consul: consul_datacenters" +sidebar_current: "docs-consul-data-source-datacenters" +description: |- + Provides the list of all known datacenters. +--- + +# consul_datacenters + +The `consul_datacenters` data source returns the list of all knwown Consul +datacenters. + +## Example Usage + +```hcl +data "consul_datacenters" "all" {} + +# Register a prepared query in each of the datacenters +resource "consul_prepared_query" "myapp-query" { + for_each = toset(data.consul_datacenters.all.datacenters) + + name = "myquery" + datacenter = each.key + 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" + } +} +``` + +## Argument Reference + +This data source has no arguments. + +## Attributes Reference + +The following attributes are exported: + +* `datacenters` - The list of datacenters known. The datacenters will be sorted + in ascending order based on the estimated median round trip time from the server + to the servers in that datacenter.