Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Consul Peering #309

Merged
merged 8 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 3 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ on:
- master
jobs:
test:
env:
GO111MODULE: on
GOFLAGS: -mod=vendor
strategy:
fail-fast: false
matrix:
go-version: [1.16.x, 1.17.x]
go-version: [1.18.x, 1.19.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -29,8 +26,8 @@ jobs:
run: docker run -v $PWD:/src docker.mirror.hashicorp.services/bflad/tfproviderlint -S006 -S022 -S023 ./...
- name: Run OSS acceptance tests
run: |
curl -LO https://releases.hashicorp.com/consul/1.11.4/consul_1.11.4_linux_amd64.zip
sudo unzip consul_1.11.4_linux_amd64.zip consul -d /usr/local/bin
curl -LO https://releases.hashicorp.com/consul/1.13.1/consul_1.13.1_linux_amd64.zip
sudo unzip consul_1.13.1_linux_amd64.zip consul -d /usr/local/bin
SKIP_REMOTE_DATACENTER_TESTS=1 make testacc TESTARGS="-count=1"
- name: Run go vet
run: make vet
23 changes: 23 additions & 0 deletions consul/data_source_consul_acl_auth_method_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package consul

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccDataACLAuthMethod_basic(t *testing.T) {
Expand Down Expand Up @@ -86,6 +88,27 @@ func TestAccDataACLAuthMethod_namespaceEE(t *testing.T) {
})
}

func testAccCheckDataSourceValue(n, attr, val string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rn, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Resource not found")
}
out, found := rn.Primary.Attributes[attr]
switch {
case !found:
return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
case val == "<all>":
// Value found, don't care what the payload is (including the zero value)
case val != "<any>" && out != val:
return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
case val == "<any>" && out == "":
return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
}
return nil
}
}

const testAccDataSourceACLAuthMethodConfigNotFound = `
data "consul_acl_auth_method" "test" {
name = "not-found"
Expand Down
94 changes: 0 additions & 94 deletions consul/data_source_consul_agent_self_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions consul/data_source_consul_autopilot_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ func TestAccDataConsulAutopilotHealth_basic(t *testing.T) {
resource.TestStep{
Config: testAccDataAutopilotHealth,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "healthy", "true"),
resource.TestCheckResourceAttrSet("data.consul_autopilot_health.read", "healthy"),
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"),
resource.TestCheckResourceAttrSet("data.consul_autopilot_health.read", "servers.0.leader"),
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"),
resource.TestCheckResourceAttrSet("data.consul_autopilot_health.read", "servers.0.healthy"),
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.voter", "true"),
testAccCheckDataSourceValue("data.consul_autopilot_health.read", "servers.0.stable_since", "<any>"),
),
Expand Down
108 changes: 108 additions & 0 deletions consul/data_source_consul_peering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package consul

import (
"context"
"fmt"

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

func dataSourceConsulPeering() *schema.Resource {
return &schema.Resource{
Read: dataSourceConsulPeeringRead,
Schema: map[string]*schema.Schema{
// In
"peer_name": {
Type: schema.TypeString,
Required: true,
},

"partition": {
Type: schema.TypeString,
Optional: true,
},

// Out
"deleted_at": {
Type: schema.TypeString,
Computed: true,
},
"meta": {
Type: schema.TypeMap,
Computed: true,

Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"peer_id": {
Type: schema.TypeString,
Computed: true,
},
"peer_ca_pems": {
Type: schema.TypeList,
Computed: true,

Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"peer_server_name": {
Type: schema.TypeString,
Computed: true,
},
"peer_server_addresses": {
Type: schema.TypeList,
Computed: true,

Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"imported_service_count": {
Type: schema.TypeInt,
Computed: true,
},
"exported_service_count": {
Type: schema.TypeInt,
Computed: true,
},
},
remilapeyre marked this conversation as resolved.
Show resolved Hide resolved
}
}

func dataSourceConsulPeeringRead(d *schema.ResourceData, meta interface{}) error {
client, qOpts, _ := getClient(d, meta)
name := d.Get("peer_name").(string)

peer, _, err := client.Peerings().Read(context.Background(), name, qOpts)
if err != nil {
return fmt.Errorf("failed to get peer information: %w", err)
}
if peer == nil {
return fmt.Errorf("no peer name %#v found", name)
}
d.SetId(peer.ID)

var deletedAt string
if peer.DeletedAt != nil {
deletedAt = peer.DeletedAt.String()
}

sw := newStateWriter(d)
sw.set("deleted_at", deletedAt)
sw.set("meta", peer.Meta)
sw.set("state", peer.State)
sw.set("peer_id", peer.PeerID)
sw.set("peer_ca_pems", peer.PeerCAPems)
sw.set("peer_server_name", peer.PeerServerName)
sw.set("peer_server_addresses", peer.PeerServerAddresses)
sw.set("imported_service_count", peer.ImportedServiceCount)
sw.set("exported_service_count", peer.ExportedServiceCount)

return sw.error()
}
72 changes: 72 additions & 0 deletions consul/data_source_consul_peering_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package consul

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataConsulPeering_basic(t *testing.T) {
providers, _ := startRemoteDatacenterTestServer(t)

resource.Test(t, resource.TestCase{
Providers: providers,
Steps: []resource.TestStep{
{
Config: testAccDataConsulPeeringMissing,
ExpectError: regexp.MustCompile(`no peer name "hello" found`),
},
{
Config: testAccDataConsulPeeringBasic,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.consul_peering.basic", "deleted_at", ""),
resource.TestCheckResourceAttr("data.consul_peering.basic", "exported_service_count", "0"),
resource.TestCheckResourceAttrSet("data.consul_peering.basic", "id"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "imported_service_count", "0"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "meta.%", "1"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "meta.foo", "bar"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "peer_ca_pems.#", "0"),
resource.TestCheckResourceAttrSet("data.consul_peering.basic", "peer_id"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "peer_name", "test"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "peer_server_addresses.#", "1"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "peer_server_addresses.0", "127.0.0.1:8508"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "peer_server_name", "server.dc2.consul"),
resource.TestCheckResourceAttr("data.consul_peering.basic", "state", "ESTABLISHING"),
),
},
},
})
}

const testAccDataConsulPeeringMissing = `
data "consul_peering" "basic" {
peer_name = "hello"
}
`

const testAccDataConsulPeeringBasic = `
remilapeyre marked this conversation as resolved.
Show resolved Hide resolved
provider "consul" {}

provider "consulremote" {
address = "http://localhost:8501"
}

resource "consul_peering_token" "basic" {
provider = consulremote
peer_name = "hello-world"
}

resource "consul_peering" "basic" {
peer_name = "test"
peering_token = consul_peering_token.basic.peering_token

meta = {
foo = "bar"
}
}

data "consul_peering" "basic" {
peer_name = consul_peering.basic.peer_name
}
`
Loading