-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new datasource
consul_datacenters
(#293)
Closes #290
- Loading branch information
1 parent
b56b18d
commit 5baf0b9
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |