Skip to content

Commit

Permalink
Add import support to consul_config_entry
Browse files Browse the repository at this point in the history
Related to hashicorp#316
  • Loading branch information
remilapeyre committed Aug 28, 2022
1 parent ae3681d commit d3efcac
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 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
34 changes: 33 additions & 1 deletion consul/resource_consul_config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ func resourceConsulConfigEntry() *schema.Resource {
Update: resourceConsulConfigEntryUpdate,
Read: resourceConsulConfigEntryRead,
Delete: resourceConsulConfigEntryDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
var kind, name, partition, namespace string
switch len(parts) {
case 2:
kind = parts[0]
name = parts[1]
case 4:
kind = parts[0]
name = parts[1]
partition = parts[2]
namespace = parts[3]
default:
return nil, fmt.Errorf(`expected path of the form "<kind>/<name>" or "<kind>/<name>/<partition>/<namespace>"`)
}

d.SetId(fmt.Sprintf("%s-%s", kind, name))
sw := newStateWriter(d)
sw.set("kind", kind)
sw.set("name", name)
sw.set("partition", partition)
sw.set("namespace", namespace)

err := sw.error()
if err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"kind": {
Expand Down Expand Up @@ -56,7 +88,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
24 changes: 20 additions & 4 deletions consul/resource_consul_config_entry_ce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func TestAccConsulConfigEntryCE_basic(t *testing.T) {
resource.TestCheckResourceAttr("consul_config_entry.service_intentions", "kind", "service-intentions"),
),
},
{
Config: testAccConsulConfigEntryCE_ServiceConfigL7Mixed,
ImportState: true,
ResourceName: "consul_config_entry.service_intentions",
ExpectError: regexp.MustCompile(`expected path of the form "<kind>/<name>" or "<kind>/<name>/<partition>/<namespace>"`),
},
{
Config: testAccConsulConfigEntryCE_ServiceConfigL7Mixed,
ImportState: true,
ResourceName: "consul_config_entry.service_intentions",
ImportStateId: "service-defaults/api",
},
},
})
}
Expand All @@ -135,8 +147,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 +642,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
12 changes: 12 additions & 0 deletions website/docs/r/config_entry.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,15 @@ The following attributes are exported:
* `namespace` - The namespace to create the config entry within.

* `config_json` - A map of configuration values.


## Import

`consul_config_entry` can be imported using the syntax `<kind>/<name>` if the
config entry is in the default partition and default namespace, or
`<kind>/<name>/<partition>/<namespace>` for config entries in a non-default
partition or namespace:

```
$ terraform import consul_config_entry.service_splitter 816a195f-6cb1-2e8d-92af-3011ae706318
```

0 comments on commit d3efcac

Please sign in to comment.