Skip to content

Commit

Permalink
Add consul_config_entry datasource
Browse files Browse the repository at this point in the history
I'm not updating the docs in this pull request as I will switch the
whole provider to use terraform-plugin-docs in
#309. I will
update the documentation after merging this PR.

Closes #317
  • Loading branch information
remilapeyre committed Aug 28, 2022
1 parent 0c14667 commit d7ca8dd
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
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 +29,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
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
74 changes: 74 additions & 0 deletions consul/data_source_consul_config_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package consul

import (
"fmt"

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

func dataSourceConsulConfigEntry() *schema.Resource {
return &schema.Resource{
Read: dataSourceConsulConfigEntryRead,

Schema: map[string]*schema.Schema{
"kind": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The kind of config entry to read.",
},

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the config entry to read.",
},

"partition": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The partition the config entry is associated with.",
},

"namespace": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The namespace the config entry is associated with.",
},

"config_json": {
Type: schema.TypeString,
Computed: true,
Description: "The configuration of the config entry.",
},
},
}
}

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

kind := d.Get("kind").(string)
name := d.Get("name").(string)

configEntry, _, err := client.ConfigEntries().Get(kind, name, qOpts)
if err != nil {
return fmt.Errorf("failed to read config entry %s/%s: %w", kind, name, err)
}

// Config Entries are too complex to write as maps for now so we save their JSON representation
data, err := configEntryToMap(configEntry)
if err != nil {
return err
}

d.SetId(fmt.Sprintf("%s/%s", kind, name))

sw := newStateWriter(d)
sw.setJson("config_json", data)

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

import (
"regexp"
"testing"

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

func TestAccDataConsulConfigEntry_basic(t *testing.T) {
providers, _ := startTestServer(t)

resource.Test(t, resource.TestCase{
Providers: providers,
Steps: []resource.TestStep{
{
Config: testAccDataConsulConfigEntryMissing,
ExpectError: regexp.MustCompile(`failed to read config entry service-defaults/foo: Unexpected response code: 404`),
},
{
Config: testAccDataConsulConfigEntry,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.consul_config_entry.read", "config_json", "{\"Expose\":{},\"MeshGateway\":{},\"Protocol\":\"https\",\"TransparentProxy\":{}}"),
resource.TestCheckResourceAttr("data.consul_config_entry.read", "id", "service-defaults/foo"),
resource.TestCheckResourceAttr("data.consul_config_entry.read", "kind", "service-defaults"),
resource.TestCheckResourceAttr("data.consul_config_entry.read", "name", "foo"),
),
},
},
})
}

const testAccDataConsulConfigEntry = `
resource "consul_config_entry" "test" {
name = "foo"
kind = "service-defaults"
config_json = jsonencode({
MeshGateway = {}
Protocol = "https"
TransparentProxy = {}
})
}
data "consul_config_entry" "read" {
name = consul_config_entry.test.name
kind = consul_config_entry.test.kind
}
`

const testAccDataConsulConfigEntryMissing = `
data "consul_config_entry" "read" {
name = "foo"
kind = "service-defaults"
}
`
2 changes: 1 addition & 1 deletion consul/resource_consul_config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func fixQOptsForConfigEntry(name, kind string, qOpts *consulapi.QueryOptions) {
// exported-services config entries are weird in that their name correspond
// to the partition they are created in, see
// https://www.consul.io/docs/connect/config-entries/exported-services#configuration-parameters
if kind == "exported-services" {
if kind == "exported-services" && name != "default" {
qOpts.Partition = name
}
}
Expand Down
12 changes: 8 additions & 4 deletions consul/resource_consul_config_entry_ce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ func TestAccConsulConfigEntryCE_ServicesExported(t *testing.T) {
Providers: providers,
Steps: []resource.TestStep{
{
Config: TestAccConsulConfigEntryCE_exportedServicesCE,
ExpectError: regexp.MustCompile(`Config entry kind "exported-services" requires Consul Enterprise`),
Config: TestAccConsulConfigEntryCE_exportedServicesCE,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("consul_config_entry.exported_services", "config_json", "{\"Services\":[{\"Consumers\":[{\"Partition\":\"default\"}],\"Name\":\"test\"}]}"),
resource.TestCheckResourceAttr("consul_config_entry.exported_services", "id", "exported-services-default"),
resource.TestCheckResourceAttr("consul_config_entry.exported_services", "kind", "exported-services"),
resource.TestCheckResourceAttr("consul_config_entry.exported_services", "name", "default"),
),
},
},
})
Expand Down Expand Up @@ -625,13 +630,12 @@ const TestAccConsulConfigEntryCE_mesh = `

const TestAccConsulConfigEntryCE_exportedServicesCE = `
resource "consul_config_entry" "exported_services" {
name = "test"
name = "default"
kind = "exported-services"
config_json = jsonencode({
Services = [{
Name = "test"
Namespace = "default"
Consumers = [{
Partition = "default"
}]
Expand Down
3 changes: 2 additions & 1 deletion consul/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func Provider() terraform.ResourceProvider {
"consul_network_segments": dataSourceConsulNetworkSegments(),
"consul_network_area_members": dataSourceConsulNetworkAreaMembers(),
"consul_datacenters": dataSourceConsulDatacenters(),
"consul_config_entry": dataSourceConsulConfigEntry(),

// Aliases to limit the impact of rename of catalog
// datasources
Expand Down Expand Up @@ -294,7 +295,7 @@ func (sw *stateWriter) setJson(key string, value interface{}) {
if err != nil {
sw.errors = append(
sw.errors,
fmt.Sprintf("failed to marshal '%s': %v", key, err),
fmt.Sprintf(" - failed to marshal '%s': %v", key, err),
)
return
}
Expand Down

0 comments on commit d7ca8dd

Please sign in to comment.