Skip to content

Commit

Permalink
Fix spurious diff with hidden_queries (#222)
Browse files Browse the repository at this point in the history
* fix spurious diff

* version

* test name

* comment

* angryfix
  • Loading branch information
MisterSquishy authored May 3, 2024
1 parent e23e851 commit 6c548e1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1.95.0
1.95.1

1 change: 1 addition & 0 deletions lightstep/resource_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func getQueriesFromUnifiedConditionResourceData(
"query_name": q.Name,
"query_string": q.QueryString,
}
setHiddenQueriesFromResourceData(qs, q)
queries = append(queries, qs)
}
return queries, nil
Expand Down
47 changes: 47 additions & 0 deletions lightstep/resource_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,3 +1265,50 @@ resource "lightstep_alert" "test" {
},
})
}

func TestAlertWithHiddenQueries(t *testing.T) {
var condition client.UnifiedCondition

conditionConfig := `
resource "lightstep_alert" "test" {
name = "hidden queries spurious diff"
project_name = "` + testProject + `"
expression {
operand = "below"
thresholds {
critical = "1"
}
}
query {
display = "line"
hidden = false
// previously, this would result in a spurious diff
hidden_queries = {
b = true
}
query_name = "a"
query_string = "metric cpu.utilization | delta 5m | group_by[], sum"
}
}
`

resourceName := "lightstep_alert.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccMetricConditionDestroy,
Steps: []resource.TestStep{
{
Config: conditionConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckLightstepAlertExists(resourceName, &condition),
resource.TestCheckResourceAttr(resourceName, "name", "hidden queries spurious diff"),
resource.TestCheckResourceAttr(resourceName, "query.0.hidden_queries.%", "1"),
resource.TestCheckResourceAttr(resourceName, "query.0.hidden_queries.b", "true"),
),
},
},
})
}
14 changes: 1 addition & 13 deletions lightstep/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,7 @@ func getQueriesFromUnifiedDashboardResourceData(
"query_string": q.QueryString,
"dependency_map_options": getDependencyMapOptions(q.DependencyMapOptions),
}
if len(q.HiddenQueries) > 0 {
// Note due to Terraform's issues with TypeMap having TypeBool elements, we
// need to use boolean strings
hq := make(map[string]interface{}, len(q.HiddenQueries)+1)
for k, v := range q.HiddenQueries {
// Don't include the top-level query in the TF resource data
if k == q.Name {
continue
}
hq[k] = fmt.Sprintf("%t", v)
}
qs["hidden_queries"] = hq
}
setHiddenQueriesFromResourceData(qs, q)

queries = append(queries, qs)
}
Expand Down
24 changes: 23 additions & 1 deletion lightstep/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package lightstep

import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/lightstep/terraform-provider-lightstep/client"
)

func mergeSchemas(arr ...map[string]*schema.Schema) map[string]*schema.Schema {
dst := make(map[string]*schema.Schema)
Expand All @@ -21,3 +25,21 @@ func convertNestedMapToSchemaSet(opts map[string]interface{}) *schema.Set {
}
return schema.NewSet(f, []interface{}{opts})
}

// Note due to Terraform's issues with TypeMap having TypeBool elements, we
// need to use boolean strings
func setHiddenQueriesFromResourceData(qs map[string]interface{}, query client.MetricQueryWithAttributes) {
if len(query.HiddenQueries) == 0 {
// nothing to do
return
}
hq := make(map[string]interface{}, len(query.HiddenQueries))
for k, v := range query.HiddenQueries {
// Don't include the top-level query in the TF resource data
if k == query.Name {
continue
}
hq[k] = fmt.Sprintf("%t", v)
}
qs["hidden_queries"] = hq
}

0 comments on commit 6c548e1

Please sign in to comment.