forked from hashicorp/terraform-provider-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add consul_autopilot_health data source
- Loading branch information
Rémi Lapeyre
committed
Jan 9, 2019
1 parent
53c97f4
commit 3c28e99
Showing
5 changed files
with
311 additions
and
7 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,152 @@ | ||
package consul | ||
|
||
import ( | ||
"fmt" | ||
|
||
consulapi "github.com/hashicorp/consul/api" | ||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
const ( | ||
autopilotHealthDatacenter = "datacenter" | ||
autopilotHealthHealthy = "healthy" | ||
autopilotHealthFailureTolerance = "failure_tolerance" | ||
autopilotHealthServers = "servers" | ||
autopilotHealthServerID = "id" | ||
autopilotHealthServerName = "name" | ||
autopilotHealthServerAddress = "address" | ||
autopilotHealthServerSerfStatus = "serf_status" | ||
autopilotHealthServerVersion = "version" | ||
autopilotHealthServerLeader = "leader" | ||
autopilotHealthServerLastContact = "last_contact" | ||
autopilotHealthServerLastTerm = "last_term" | ||
autopilotHealthServerLastIndex = "last_index" | ||
autopilotHealthServerHealthy = "healthy" | ||
autopilotHealthServerVoter = "voter" | ||
autopilotHealthServerStableSince = "stable_since" | ||
) | ||
|
||
func dataSourceConsulAutopilotHealth() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceConsulAutopilotHealthRead, | ||
Schema: map[string]*schema.Schema{ | ||
// Filters | ||
autopilotHealthDatacenter: &schema.Schema{ | ||
Optional: true, | ||
Type: schema.TypeString, | ||
}, | ||
|
||
// Out parameters | ||
autopilotHealthHealthy: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeBool, | ||
}, | ||
autopilotHealthFailureTolerance: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeInt, | ||
}, | ||
autopilotHealthServers: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeList, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
autopilotHealthServerID: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
autopilotHealthServerName: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
autopilotHealthServerAddress: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
autopilotHealthServerSerfStatus: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
autopilotHealthServerVersion: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
autopilotHealthServerLeader: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeBool, | ||
}, | ||
autopilotHealthServerLastContact: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
autopilotHealthServerLastTerm: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeInt, | ||
}, | ||
autopilotHealthServerLastIndex: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeInt, | ||
}, | ||
autopilotHealthServerHealthy: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeBool, | ||
}, | ||
autopilotHealthServerVoter: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeBool, | ||
}, | ||
autopilotHealthServerStableSince: &schema.Schema{ | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceConsulAutopilotHealthRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*consulapi.Client) | ||
operator := client.Operator() | ||
|
||
queryOpts, err := getQueryOpts(d, client) | ||
if datacenter, ok := d.GetOk(autopilotHealthDatacenter); ok { | ||
queryOpts.Datacenter = datacenter.(string) | ||
} | ||
|
||
health, err := operator.AutopilotServerHealth(queryOpts) | ||
if err != nil { | ||
return err | ||
} | ||
const idKeyFmt = "autopilot-health-%s" | ||
d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter)) | ||
|
||
d.Set("healthy", health.Healthy) | ||
d.Set("failure_tolerance", health.FailureTolerance) | ||
|
||
serversHealth := make([]interface{}, 0, len(health.Servers)) | ||
for _, server := range health.Servers { | ||
h := make(map[string]interface{}, 12) | ||
|
||
h[autopilotHealthServerID] = server.ID | ||
h[autopilotHealthServerName] = server.Name | ||
h[autopilotHealthServerAddress] = server.Address | ||
h[autopilotHealthServerSerfStatus] = server.SerfStatus | ||
h[autopilotHealthServerVersion] = server.Version | ||
h[autopilotHealthServerLeader] = server.Leader | ||
h[autopilotHealthServerLastContact] = server.LastContact.String() | ||
h[autopilotHealthServerLastTerm] = server.LastTerm | ||
h[autopilotHealthServerLastIndex] = server.LastIndex | ||
h[autopilotHealthServerHealthy] = server.Healthy | ||
h[autopilotHealthServerVoter] = server.Voter | ||
h[autopilotHealthServerStableSince] = server.StableSince.String() | ||
|
||
serversHealth = append(serversHealth, h) | ||
} | ||
|
||
if err := d.Set("servers", serversHealth); err != nil { | ||
return errwrap.Wrapf("Unable to store servers health: {{err}}", err) | ||
} | ||
return nil | ||
} |
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,90 @@ | ||
package consul | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataConsulAutopilotHealth_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataAutopilotHealth, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "healthy", "true"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "failure_tolerance", "0"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.#", "1"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.id", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.name", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.address", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.serf_status", "alive"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.version", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.leader", "true"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.last_contact", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.last_term", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.last_index", "<any>"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.healthy", "true"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.voter", "true"), | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.stable_since", "<any>"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataConsulAutopilotHealth_config(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataAutopilotHealthDatacenter, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.#", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataConsulAutopilotHealth_wrongDatacenter(t *testing.T) { | ||
re, err := regexp.Compile("No path to datacenter") | ||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataAutopilotHealthWrongDatacenter, | ||
ExpectError: re, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccDataAutopilotHealth = ` | ||
data "consul_autopilot_health" "read" {} | ||
output "health" { | ||
value = "${data.consul_autopilot_health.read.healthy}" | ||
} | ||
` | ||
|
||
const testAccDataAutopilotHealthDatacenter = ` | ||
data "consul_autopilot_health" "read" { | ||
datacenter = "dc1" | ||
} | ||
` | ||
|
||
const testAccDataAutopilotHealthWrongDatacenter = ` | ||
data "consul_autopilot_health" "read" { | ||
datacenter = "wrong_datacenter" | ||
} | ||
` |
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
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,57 @@ | ||
--- | ||
layout: "consul" | ||
page_title: "Consul: consul_autopilot_health" | ||
sidebar_current: "docs-consul-data-source-autopilot-health" | ||
description: |- | ||
Provides health information of the autopilot. | ||
--- | ||
|
||
# consul_autopilot_health | ||
|
||
The `consul_autopilot_health` data source returns | ||
[autopilot health information](https://www.consul.io/api/operator/autopilot.html#read-health) | ||
about the current Consul cluster. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "consul_autopilot_health" "read" {} | ||
output "health" { | ||
value = "${data.consul_autopilot_health.read.healthy}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `datacenter` - (Optional) The datacenter to use. This overrides the | ||
datacenter in the provider setup and the agent's default datacenter. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `healthy` - Whether all the servers in the cluster are currently healthy | ||
* `failure_tolerance` - The number of redundant healthy servers that could fail | ||
without causing an outage | ||
* `servers` - A list of server health information. See below for details on the | ||
available information. | ||
|
||
### Server health information | ||
* `id` - The Raft ID of the server | ||
* `name` - The node name of the server | ||
* `address` - The address of the server | ||
* `serf_status` - The status of the SerfHealth check of the server | ||
* `version` - The Consul version of the server | ||
* `leader` - Whether the server is currently leader | ||
* `last_contact` - The time elapsed since the server's last contact with | ||
the leader | ||
* `last_term` - The server's last known Raft leader term | ||
* `last_index` - The index of the server's last committed Raft log entry | ||
* `healthy` - Whether the server is healthy according to the current Autopilot | ||
configuration | ||
* `voter` - Whether the server is a voting member of the Raft cluster | ||
* `stable_since` - The time this server has been in its current ``Healthy`` | ||
state |