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

Oncall Integration: Fix removing templates #1201

Merged
Merged
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit convoluted. I'm approving because I eventually understood it, but it took me a short while.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained it!

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