Skip to content

Commit

Permalink
Fix the way datacenter, token and namespace are inherited from the pr…
Browse files Browse the repository at this point in the history
…ovider configuration (#259)

This also adds test with multiple datacenters, we were not testing this
configuration until now. The tests are not run by default to not make
local development more complicated but are run in CI both with Consul
Community Edition and Consul Enterprise.

Closes #8
  • Loading branch information
remilapeyre authored May 12, 2021
1 parent 51364f6 commit 584ad89
Show file tree
Hide file tree
Showing 70 changed files with 1,062 additions and 974 deletions.
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ script:
docker.mirror.hashicorp.services/hashicorp/consul-enterprise:latest consul agent -dev -config-file consul_test.hcl -client=0.0.0.0
- make testacc TESTARGS="-count=1"
- docker stop consul-test
- docker run --rm
-d
--name consul-test-dc2
-v $PWD/consul_test_dc2.hcl:/consul_test_dc2.hcl:ro
--net host
docker.mirror.hashicorp.services/consul:latest consul agent -dev -config-file consul_test_dc2.hcl
- docker run --rm
-d
--name consul-test
-v $PWD/consul_test.hcl:/consul_test.hcl:ro
--net host
docker.mirror.hashicorp.services/consul:latest consul agent -dev -config-file consul_test.hcl
- TEST_REMOTE_DATACENTER=1 make testacc TESTARGS="-count=1"
- docker stop consul-test consul-test-dc2
- docker run --rm
-d
--name consul-test-dc2
-v $PWD/consul_test_dc2.hcl:/consul_test_dc2.hcl:ro
--net host
docker.mirror.hashicorp.services/hashicorp/consul-enterprise:latest consul agent -dev -config-file consul_test_dc2.hcl
- docker run --rm
-d
--name consul-test
-v $PWD/consul_test.hcl:/consul_test.hcl:ro
--net host
docker.mirror.hashicorp.services/hashicorp/consul-enterprise:latest consul agent -dev -config-file consul_test.hcl
- TEST_REMOTE_DATACENTER=1 make testacc TESTARGS="-count=1"
- docker stop consul-test consul-test-dc2
- make vet

branches:
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ locally and then run it in development mode:
```
$ consul agent -dev
```

It is also possible to run additional tests to test the provider with multiple
datacenters:

```sh
$ consul agent -dev -config-file ./consul_test_dc2.hcl &
$ consul agent -dev -config-file ./consul_test.hcl &
$ export CONSUL_HTTP_ADDR=localhost:8500
$ export CONSUL_HTTP_TOKEN=master-token
$ TEST_REMOTE_DATACENTER=1 make testacc
```
11 changes: 1 addition & 10 deletions consul/data_source_consul_acl_auth_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"

consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -86,16 +85,8 @@ func dataSourceConsulACLAuthMethod() *schema.Resource {
}

func dataSourceConsulACLAuthMethodRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
client, qOpts, _ := getClient(d, meta)
name := d.Get("name").(string)
dc, err := getDC(d, client, meta)
if err != nil {
return fmt.Errorf("Failed to get DC: %v", err)
}
qOpts := &consulapi.QueryOptions{
Datacenter: dc,
Namespace: getNamespace(d, meta),
}

authMethod, _, err := client.ACL().AuthMethodRead(name, qOpts)
if err != nil {
Expand Down
10 changes: 1 addition & 9 deletions consul/data_source_consul_acl_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ func dataSourceConsulACLPolicy() *schema.Resource {
}

func dataSourceConsulACLPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
client, qOpts, _ := getClient(d, meta)
name := d.Get("name").(string)
dc, err := getDC(d, client, meta)
if err != nil {
return fmt.Errorf("Failed to get DC: %v", err)
}
qOpts := &consulapi.QueryOptions{
Datacenter: dc,
Namespace: getNamespace(d, meta),
}

var policyEntry *consulapi.ACLPolicyListEntry
policyEntries, _, err := client.ACL().PolicyList(qOpts)
Expand Down
11 changes: 1 addition & 10 deletions consul/data_source_consul_acl_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consul
import (
"fmt"

consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -69,16 +68,8 @@ func dataSourceConsulACLRole() *schema.Resource {
}

func datasourceConsulACLRoleRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
client, qOpts, _ := getClient(d, meta)
name := d.Get("name").(string)
dc, err := getDC(d, client, meta)
if err != nil {
return fmt.Errorf("Failed to get DC: %v", err)
}
qOpts := &consulapi.QueryOptions{
Datacenter: dc,
Namespace: getNamespace(d, meta),
}

role, _, err := client.ACL().RoleReadByName(name, qOpts)
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions consul/data_source_consul_acl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consul
import (
"fmt"

consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -56,11 +55,9 @@ func dataSourceConsulACLToken() *schema.Resource {
}

func dataSourceConsulACLTokenRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
client, qOpts, _ := getClient(d, meta)
accessorID := d.Get("accessor_id").(string)
qOpts := &consulapi.QueryOptions{
Namespace: getNamespace(d, meta),
}

aclToken, _, err := client.ACL().TokenRead(accessorID, qOpts)
if err != nil {
return err
Expand Down
7 changes: 2 additions & 5 deletions consul/data_source_consul_acl_token_secret_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consul
import (
"fmt"

consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/encryption"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand Down Expand Up @@ -47,11 +46,9 @@ func dataSourceConsulACLTokenSecretID() *schema.Resource {
}

func dataSourceConsulACLTokenSecretIDRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
client, qOpts, _ := getClient(d, meta)
accessorID := d.Get("accessor_id").(string)
qOpts := &consulapi.QueryOptions{
Namespace: getNamespace(d, meta),
}

aclToken, _, err := client.ACL().TokenRead(accessorID, qOpts)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion consul/data_source_consul_agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func dataSourceConsulAgentConfig() *schema.Resource {
}

func dataSourceConsulAgentConfigRead(d *schema.ResourceData, meta interface{}) error {
agentSelf, err := getClient(meta).Agent().Self()
client, _, _ := getClient(d, meta)
agentSelf, err := client.Agent().Self()
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion consul/data_source_consul_agent_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ func dataSourceConsulAgentSelf() *schema.Resource {
}

func dataSourceConsulAgentSelfRead(d *schema.ResourceData, meta interface{}) error {
info, err := getClient(meta).Agent().Self()
client, _, _ := getClient(d, meta)
info, err := client.Agent().Self()
if err != nil {
return err
}
Expand Down
47 changes: 20 additions & 27 deletions consul/data_source_consul_autopilot_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,70 @@ func dataSourceConsulAutopilotHealth() *schema.Resource {
Read: dataSourceConsulAutopilotHealthRead,
Schema: map[string]*schema.Schema{
// Filters
"datacenter": &schema.Schema{
"datacenter": {
Optional: true,
Type: schema.TypeString,
},

// Out parameters
"healthy": &schema.Schema{
"healthy": {
Computed: true,
Type: schema.TypeBool,
},
"failure_tolerance": &schema.Schema{
"failure_tolerance": {
Computed: true,
Type: schema.TypeInt,
},
"servers": &schema.Schema{
"servers": {
Computed: true,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
"id": {
Computed: true,
Type: schema.TypeString,
},
"name": &schema.Schema{
"name": {
Computed: true,
Type: schema.TypeString,
},
"address": &schema.Schema{
"address": {
Computed: true,
Type: schema.TypeString,
},
"serf_status": &schema.Schema{
"serf_status": {
Computed: true,
Type: schema.TypeString,
},
"version": &schema.Schema{
"version": {
Computed: true,
Type: schema.TypeString,
},
"leader": &schema.Schema{
"leader": {
Computed: true,
Type: schema.TypeBool,
},
"last_contact": &schema.Schema{
"last_contact": {
Computed: true,
Type: schema.TypeString,
},
"last_term": &schema.Schema{
"last_term": {
Computed: true,
Type: schema.TypeInt,
},
"last_index": &schema.Schema{
"last_index": {
Computed: true,
Type: schema.TypeInt,
},
"healthy": &schema.Schema{
"healthy": {
Computed: true,
Type: schema.TypeBool,
},
"voter": &schema.Schema{
"voter": {
Computed: true,
Type: schema.TypeBool,
},
"stable_since": &schema.Schema{
"stable_since": {
Computed: true,
Type: schema.TypeString,
},
Expand All @@ -87,23 +87,16 @@ func dataSourceConsulAutopilotHealth() *schema.Resource {
}

func dataSourceConsulAutopilotHealthRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
client, qOpts, _ := getClient(d, meta)
operator := client.Operator()
getQueryOpts(qOpts, d, meta)

queryOpts, err := getQueryOpts(d, client, meta)
if err != nil {
return err
}
if datacenter, ok := d.GetOk("datacenter"); ok {
queryOpts.Datacenter = datacenter.(string)
}

health, err := operator.AutopilotServerHealth(queryOpts)
health, err := operator.AutopilotServerHealth(qOpts)
if err != nil {
return err
}
const idKeyFmt = "autopilot-health-%s"
d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))
d.SetId(fmt.Sprintf(idKeyFmt, qOpts.Datacenter))

d.Set("healthy", health.Healthy)
d.Set("failure_tolerance", health.FailureTolerance)
Expand Down
14 changes: 2 additions & 12 deletions consul/data_source_consul_key_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,7 @@ func dataSourceConsulKeyPrefix() *schema.Resource {
}

func dataSourceConsulKeyPrefixRead(d *schema.ResourceData, meta interface{}) error {
client := getClient(meta)
namespace := getNamespace(d, meta)

kv := client.KV()
token := d.Get("token").(string)
dc, err := getDC(d, client, meta)
if err != nil {
return err
}

keyClient := newKeyClient(kv, dc, token, namespace)
keyClient := newKeyClient(d, meta)

pathPrefix := d.Get("path_prefix").(string)

Expand Down Expand Up @@ -126,7 +116,7 @@ func dataSourceConsulKeyPrefixRead(d *schema.ResourceData, meta interface{}) err

// Store the datacenter on this resource, which can be helpful for reference
// in case it was read from the provider
d.Set("datacenter", dc)
d.Set("datacenter", keyClient.qOpts.Datacenter)
d.Set("path_prefix", pathPrefix)

d.SetId("-")
Expand Down
36 changes: 36 additions & 0 deletions consul/data_source_consul_key_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ func TestAccDataConsulKeyPrefix_namespaceEE(t *testing.T) {
})
}

func TestAccDataConsulKeyPrefix_datacenter(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccRemoteDatacenterPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testAccDataConsulKeyPrefixConfigDatacenter,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulKeyPrefixAttribute("data.consul_key_prefix.dc1", "subkeys.%", "0"),
testAccCheckConsulKeyPrefixAttribute("data.consul_key_prefix.dc2", "subkeys.%", "1"),
),
},
},
})
}

func testAccCheckConsulKeyPrefixAttribute(n, attr, val string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rn, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -146,3 +162,23 @@ data "consul_key_prefix" "read" {
namespace = "test-key-prefix"
}
`

const testAccDataConsulKeyPrefixConfigDatacenter = `
resource "consul_key_prefix" "dc2" {
datacenter = "dc2"
path_prefix = "test/"
subkeys = {
"dc" = "dc2"
}
}
data "consul_key_prefix" "dc1" {
path_prefix = consul_key_prefix.dc2.path_prefix
}
data "consul_key_prefix" "dc2" {
datacenter = "dc2"
path_prefix = consul_key_prefix.dc2.path_prefix
}
`
Loading

0 comments on commit 584ad89

Please sign in to comment.