Skip to content

Commit

Permalink
Workaround and test by treating settings as omitempty (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweav authored Sep 14, 2022
1 parent f9cd123 commit ca46f0d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
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

0 comments on commit ca46f0d

Please sign in to comment.