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

Alerting: Workaround for bad handling of empty strings in contact points API #656

Merged
merged 1 commit into from
Sep 14, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "grafana_contact_point" "default_settings" {
name = "Default Settings"

slack {
token = "xoxb-token"
recipient = "#channel"
}
}
13 changes: 12 additions & 1 deletion grafana/resource_alerting_contact_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,25 @@ func unpackContactPoints(data *schema.ResourceData) []gapi.ContactPoint {
for _, n := range notifiers {
if points, ok := data.GetOk(n.meta().field); ok {
for _, p := range points.([]interface{}) {
result = append(result, n.unpack(p, name))
result = append(result, unpackPointConfig(n, p, name))
}
}
}

return result
}

func unpackPointConfig(n notifier, data interface{}, name string) gapi.ContactPoint {
pt := n.unpack(data, name)
// Treat settings like `omitempty`. Workaround for versions affected by https://github.com/grafana/grafana/issues/55139
for k, v := range pt.Settings {
if v == "" {
delete(pt.Settings, k)
}
}
return pt
}

func packContactPoints(ps []gapi.ContactPoint, data *schema.ResourceData) error {
pointsPerNotifier := map[notifier][]interface{}{}
for _, p := range ps {
Expand Down
29 changes: 29 additions & 0 deletions grafana/resource_alerting_contact_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ func TestAccContactPoint_notifiers(t *testing.T) {
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "wecom.0.title", "title"),
),
},
// Test blank fields in settings should be omitted.
{
Config: testAccExample(t, "resources/grafana_contact_point/_acc_default_settings.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.default_settings", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.default_settings", "slack.#", "1"),
resource.TestCheckNoResourceAttr("grafana_contact_point.default_settings", "slack.endpoint_url"),
func(s *terraform.State) error {
rname := "grafana_contact_point.default_settings"
rs, ok := s.RootModule().Resources[rname]
if !ok {
return fmt.Errorf("resource not found: %s, resources: %#v", rname, s.RootModule().Resources)
}
uid := rs.Primary.ID

client := testAccProvider.Meta().(*client).gapi
pt, err := client.ContactPoint(uid)
if err != nil {
return fmt.Errorf("error getting resource: %w", err)
}

if val, ok := pt.Settings["endpointUrl"]; ok {
return fmt.Errorf("endpointUrl was still present in the settings when it should have been omitted. value: %#v", val)
}

return nil
},
),
},
},
})
}
Expand Down