Skip to content

Commit

Permalink
Add new datasource consul_datacenters (#293)
Browse files Browse the repository at this point in the history
Closes #290
  • Loading branch information
Seth-Karlo authored Jan 31, 2022
1 parent b56b18d commit 5baf0b9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
34 changes: 34 additions & 0 deletions consul/data_source_consul_datacenters.go
Original file line number Diff line number Diff line change
@@ -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()
}
27 changes: 27 additions & 0 deletions consul/data_source_consul_datacenters_test.go
Original file line number Diff line number Diff line change
@@ -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" {}
`
1 change: 1 addition & 0 deletions consul/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions website/docs/d/datacenters.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 5baf0b9

Please sign in to comment.