Skip to content

Commit

Permalink
Oncall Integration: Fix removing templates (#1201)
Browse files Browse the repository at this point in the history
* Oncall Integration: Fix removing templates
Closes #1199
This was broken in #1185
I added a test so that it doesn't occur again

* Explain string print

* Correct mispelling
  • Loading branch information
julienduchesne authored Dec 6, 2023
1 parent 7932966 commit dcc2c5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/resources/oncall/resource_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func ResourceIntegration() *schema.Resource {
MaxItems: 1,
Description: "Jinja2 templates for Alert payload. An empty templates block will be ignored.",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old != "" && new == "" || old == "" && new != "" {
return false
}

oldTemplate, newTemplate := d.GetChange("templates")

getTemplatesOrEmpty := func(template interface{}) map[string]interface{} {
Expand All @@ -218,7 +222,10 @@ func ResourceIntegration() *schema.Resource {
return false
}
for k, v := range oldTemplateMap {
if newTemplateMap[k] != v {
// Convert everything to string to be able to compare across types.
// We're only interested in the actual value here,
// and Terraform will implicitly convert a string to a number, and vice versa.
if fmt.Sprintf("%v", newTemplateMap[k]) != fmt.Sprintf("%v", v) {
return false
}
}
Expand Down
14 changes: 14 additions & 0 deletions internal/resources/oncall/resource_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAccOnCallIntegration_basic(t *testing.T) {
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "name", rName),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "type", rType),
resource.TestCheckResourceAttrSet("grafana_oncall_integration.test-acc-integration", "link"),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "templates.#", "0"),
),
},
{
Expand All @@ -38,6 +39,7 @@ func TestAccOnCallIntegration_basic(t *testing.T) {
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "name", rName),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "type", rType),
resource.TestCheckResourceAttrSet("grafana_oncall_integration.test-acc-integration", "link"),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "templates.#", "0"),
),
},
{
Expand All @@ -49,9 +51,21 @@ func TestAccOnCallIntegration_basic(t *testing.T) {
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "name", rName),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "type", rType),
resource.TestCheckResourceAttrSet("grafana_oncall_integration.test-acc-integration", "link"),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "templates.#", "1"),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "templates.0.grouping_key", "test"),
),
},
// Remove templates
{
Config: testAccOnCallIntegrationConfig(rName, rType, ``),
Check: resource.ComposeTestCheckFunc(
testAccCheckOnCallIntegrationResourceExists("grafana_oncall_integration.test-acc-integration"),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "name", rName),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "type", rType),
resource.TestCheckResourceAttrSet("grafana_oncall_integration.test-acc-integration", "link"),
resource.TestCheckResourceAttr("grafana_oncall_integration.test-acc-integration", "templates.#", "0"),
),
},
},
})
}
Expand Down

0 comments on commit dcc2c5e

Please sign in to comment.