Skip to content

Commit

Permalink
Add consul_config_entry datasource (#318)
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 Dec 13, 2022
1 parent b381e34 commit 5bf225e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
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"
}
`
3 changes: 2 additions & 1 deletion consul/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func Provider() terraform.ResourceProvider {
"consul_network_segments": dataSourceConsulNetworkSegments(),
"consul_network_area_members": dataSourceConsulNetworkAreaMembers(),
"consul_datacenters": dataSourceConsulDatacenters(),
"consul_config_entry": dataSourceConsulConfigEntry(),
"consul_peering": dataSourceConsulPeering(),
"consul_peerings": dataSourceConsulPeerings(),

Expand Down Expand Up @@ -308,7 +309,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 5bf225e

Please sign in to comment.