Skip to content
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 node_meta attr to consul_catalog_entry #51

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions consul/resource_consul_catalog_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func resourceConsulCatalogEntry() *schema.Resource {
ForceNew: true,
},

"node_meta": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},

"service": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -140,6 +145,16 @@ func resourceConsulCatalogEntryCreate(d *schema.ResourceData, meta interface{})
address := d.Get("address").(string)
node := d.Get("node").(string)

var nodeMeta map[string]string
if v, ok := d.GetOk("node_meta"); ok {
nodeMeta = make(map[string]string, len(v.(map[string]interface{})))
for k, v := range v.(map[string]interface{}) {
if val, ok := v.(string); ok {
nodeMeta[k] = val
}
}
}

var serviceIDs []string
if service, ok := d.GetOk("service"); ok {
serviceList := service.(*schema.Set).List()
Expand All @@ -165,6 +180,7 @@ func resourceConsulCatalogEntryCreate(d *schema.ResourceData, meta interface{})
Address: address,
Datacenter: dc,
Node: node,
NodeMeta: nodeMeta,
Service: &consulapi.AgentService{
Address: serviceData["address"].(string),
ID: serviceID,
Expand Down
98 changes: 98 additions & 0 deletions consul/resource_consul_catalog_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,69 @@ func TestAccConsulCatalogEntry_basic(t *testing.T) {
})
}

func TestAccConsulCatalogEntry_nodeMeta(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {},
Providers: testAccProviders,
CheckDestroy: testAccCheckConsulCatalogEntryDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccConsulCatalogEntryConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulCatalogEntryExists(),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "address", "127.0.0.1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node", "bastion"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.#", "1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.address", "www.google.com"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.id", "google1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.name", "google"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.port", "80"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.#", "2"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.2154398732", "tag0"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.4151227546", "tag1"),
),
},
resource.TestStep{
Config: testAccConsulCatalogEntryConfig_wMeta,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulCatalogEntryExists(),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "address", "127.0.0.1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node", "bastion"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node_meta.%", "2"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node_meta.foo", "bar"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node_meta.baz", "bam"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.#", "1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.address", "www.google.com"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.id", "google1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.name", "google"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.port", "80"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.#", "2"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.2154398732", "tag0"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.4151227546", "tag1"),
),
},
resource.TestStep{
Config: testAccConsulCatalogEntryConfig_wMetaUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulCatalogEntryExists(),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "address", "127.0.0.1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node", "bastion"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node_meta.foo", "fam"),
resource.TestCheckNoResourceAttr("consul_catalog_entry.app", "node_meta.baz"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.#", "1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.address", "www.google.com"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.id", "google1"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.name", "google"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.port", "80"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.#", "2"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.2154398732", "tag0"),
testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.4151227546", "tag1"),
),
},
},
})
}

func TestAccConsulCatalogEntry_extremove(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {},
Expand Down Expand Up @@ -152,3 +215,38 @@ resource "consul_catalog_entry" "app" {
}
}
`

const testAccConsulCatalogEntryConfig_wMeta = `
resource "consul_catalog_entry" "app" {
address = "127.0.0.1"
node = "bastion"
node_meta = {
foo = "bar"
baz = "bam"
}
service = {
address = "www.google.com"
id = "google1"
name = "google"
port = 80
tags = ["tag0", "tag1"]
}
}
`

const testAccConsulCatalogEntryConfig_wMetaUpdated = `
resource "consul_catalog_entry" "app" {
address = "127.0.0.1"
node = "bastion"
node_meta = {
foo = "fam"
}
service = {
address = "www.google.com"
id = "google1"
name = "google"
port = 80
tags = ["tag0", "tag1"]
}
}
`
7 changes: 7 additions & 0 deletions website/docs/r/catalog_entry.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ resource "consul_catalog_entry" "app" {
address = "192.168.10.10"
node = "foobar"

node_meta = {
foo = "bar"
}

service = {
address = "127.0.0.1"
id = "redis1"
Expand All @@ -38,6 +42,9 @@ The following arguments are supported:
* `node` - (Required) The name of the node being added to, or
referenced in the catalog.

* `node_meta` - (Optional) A map of key/value metadata pairs to assign
to the entry.

* `service` - (Optional) A service to optionally associated with
the node. Supported values are documented below.

Expand Down