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

Fix consul_prepared_query handling of default values for failover, dns and template #121

Merged
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 2.6.0 (Unreleased)
## 2.5.1 (Unreleased)

BUG FIXES:

* The `consul_prepared_query` now handles default values correctly for the `failover`, `dns` and `template` blocks ([[#119](https://github.com/terraform-providers/terraform-provider-consul/issues/119)] and [[#121](https://github.com/terraform-providers/terraform-provider-consul/issues/121)])

## 2.5.0 (June 03, 2019)

NEW FEATURES:
Expand Down
24 changes: 21 additions & 3 deletions consul/resource_consul_prepared_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,34 @@ func resourceConsulPreparedQueryRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Failed to set 'tags': %v", err)
}

// Since failover and dns are implemented with an optionnal list instead of a
// sub-resource, writing those attributes to the state is more involved that
// it needs to.

failover := make([]map[string]interface{}, 0)
if pq.Service.Failover.NearestN > 0 {

// First we must find whether the user wrote a failover block
userWroteFailover := len(d.Get("failover").([]interface{})) != 0

// We must write a failover block if the user wrote one or if one of the values
// differ from the defaults
if userWroteFailover || pq.Service.Failover.NearestN > 0 || len(pq.Service.Failover.Datacenters) > 0 {
failover = append(failover, map[string]interface{}{
"nearest_n": pq.Service.Failover.NearestN,
"datacenters": pq.Service.Failover.Datacenters,
})
}

// We can finally set the failover attribute
if err = d.Set("failover", failover); err != nil {
return fmt.Errorf("Failed to set 'failover': %v", err)
}

dns := make([]map[string]interface{}, 0)
if pq.DNS.TTL != "" {

userWroteDNS := len(d.Get("dns").([]interface{})) != 0

if userWroteDNS || pq.DNS.TTL != "" {
dns = append(dns, map[string]interface{}{
"ttl": pq.DNS.TTL,
})
Expand All @@ -231,7 +246,10 @@ func resourceConsulPreparedQueryRead(d *schema.ResourceData, meta interface{}) e
}

template := make([]map[string]interface{}, 0)
if pq.Template.Type != "" {

userWroteTemplate := len(d.Get("template").([]interface{})) != 0

if userWroteTemplate || pq.Template.Type != "" {
template = append(template, map[string]interface{}{
"type": pq.Template.Type,
"regexp": pq.Template.Regexp,
Expand Down
84 changes: 84 additions & 0 deletions consul/resource_consul_prepared_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ func TestAccConsulPreparedQuery_import(t *testing.T) {
})
}

func TestAccConsulPreparedQuery_blocks(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccConsulPreparedQueryBlocks,
},
resource.TestStep{
Config: testAccConsulPreparedQueryBlocks2,
},
resource.TestStep{
Config: testAccConsulPreparedQueryBlocks3,
},
resource.TestStep{
Config: testAccConsulPreparedQueryBlocks4,
},
},
})
}

func checkPreparedQueryExists(s *terraform.State) bool {
rn, ok := s.RootModule().Resources["consul_prepared_query.foo"]
if !ok {
Expand Down Expand Up @@ -220,3 +241,66 @@ resource "consul_prepared_query" "foo" {
service = "memcached"
}
`

const testAccConsulPreparedQueryBlocks = `
resource "consul_prepared_query" "foo" {
name = "foo"
stored_token = "pq-token"
service = "redis"
tags = ["prod"]
near = "_agent"
only_passing = true

failover {
nearest_n = 0
datacenters = ["dc1", "dc2"]
}
}
`

const testAccConsulPreparedQueryBlocks2 = `
resource "consul_prepared_query" "foo" {
name = "foo"
stored_token = "pq-token"
service = "redis"
tags = ["prod"]
near = "_agent"
only_passing = true

failover {
nearest_n = 0
datacenters = []
}
}
`

const testAccConsulPreparedQueryBlocks3 = `
resource "consul_prepared_query" "foo" {
name = "foo"
stored_token = "pq-token"
service = "redis"
tags = ["prod"]
near = "_agent"
only_passing = true

dns {
ttl = ""
}
}
`

const testAccConsulPreparedQueryBlocks4 = `
resource "consul_prepared_query" "foo" {
name = "foo"
stored_token = "pq-token"
service = "redis"
tags = ["prod"]
near = "_agent"
only_passing = true

template {
type = ""
regexp = ""
}
}
`