-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add consul_config_entry datasource (#318)
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
1 parent
b381e34
commit 5bf225e
Showing
3 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters