-
Notifications
You must be signed in to change notification settings - Fork 112
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 consul_config_entry datasource #318
Merged
remilapeyre
merged 3 commits into
hashicorp:master
from
remilapeyre:remilapeyre/issue317
Dec 13, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't id collide with other config entries with the same kind/name in other partitions/namespaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id
in Terraform resources or datasources is often unique but does not need to, when objects don't have a specific ID it can happen that they have the same of another.For example Consul has this at https://github.com/hashicorp/terraform-provider-consul/blob/master/consul/resource_consul_keys.go#L188-L191 where all
consul_keys
resources will have the sameid
no matter the keys being set and another occurence of this in the Vault provider is at https://github.com/hashicorp/terraform-provider-vault/blob/main/vault/resource_generic_endpoint.go#L111 where multiplevault_generic_endpoint
resources can have the sameid
despite being in different namespace.Here because config entries do not have IDs and the user won't use it it does not matter what placeholder we use.