diff --git a/ns1/resource_notifylist.go b/ns1/resource_notifylist.go index e4f17111..e493db27 100644 --- a/ns1/resource_notifylist.go +++ b/ns1/resource_notifylist.go @@ -18,7 +18,7 @@ func notifyListResource() *schema.Resource { Required: true, }, "notifications": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -66,39 +66,39 @@ func notifyListToResourceData(d *schema.ResourceData, nl *monitor.NotifyList) er func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) error { nl.ID = d.Id() - if rawNotifications := d.Get("notifications").([]interface{}); len(rawNotifications) > 0 { - ns := make([]*monitor.Notification, len(rawNotifications)) - for i, notificationRaw := range rawNotifications { + if rawNotifications := d.Get("notifications").(*schema.Set); rawNotifications.Len() > 0 { + ns := make([]*monitor.Notification, 0, rawNotifications.Len()) + for _, notificationRaw := range rawNotifications.List() { ni := notificationRaw.(map[string]interface{}) config := ni["config"].(map[string]interface{}) - if config != nil { + if len(config) > 0 { switch ni["type"].(string) { case "email": email := config["email"] if email != nil { - ns[i] = monitor.NewEmailNotification(email.(string)) + ns = append(ns, monitor.NewEmailNotification(email.(string))) } else { return fmt.Errorf("wrong config for email expected email field into config") } case "datafeed": sourceId := config["sourceid"] if sourceId != nil { - ns[i] = monitor.NewFeedNotification(sourceId.(string)) + ns = append(ns, monitor.NewFeedNotification(sourceId.(string))) } else { return fmt.Errorf("wrong config for datafeed expected sourceid field into config") } case "webhook": url := config["url"] if url != nil { - ns[i] = monitor.NewWebNotification(url.(string), nil) + ns = append(ns, monitor.NewWebNotification(url.(string), nil)) } else { return fmt.Errorf("wrong config for webhook expected url field into config") } case "pagerduty": serviceKey := config["service_key"] if serviceKey != nil { - ns[i] = monitor.NewPagerDutyNotification(serviceKey.(string)) + ns = append(ns, monitor.NewPagerDutyNotification(serviceKey.(string))) } else { return fmt.Errorf("wrong config for pagerduty expected serviceKey field into config") } @@ -107,7 +107,7 @@ func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) er username := config["username"] channel := config["channel"] if url != nil && username != nil && channel != nil { - ns[i] = monitor.NewSlackNotification(url.(string), username.(string), channel.(string)) + ns = append(ns, monitor.NewSlackNotification(url.(string), username.(string), channel.(string))) } else { return fmt.Errorf("wrong config for slack expected url, username and channel fields into config") } diff --git a/ns1/resource_notifylist_test.go b/ns1/resource_notifylist_test.go index f4f1687a..feb15b9e 100644 --- a/ns1/resource_notifylist_test.go +++ b/ns1/resource_notifylist_test.go @@ -72,19 +72,19 @@ func TestAccNotifyList_multiple(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckNotifyListExists("ns1_notifylist.test_multiple", &nl), testAccCheckNotifyListName(&nl, "terraform test multiple"), + testAccCheckNotifyTypeOrder(&nl, "pagerduty", "webhook"), + ), + }, + // Despite the change in order the object is the same: we want to make sure + // that it's not actually modified and the order stays the same + { + Config: testAccNotifyListMultipleDifferentOrder, + Check: resource.ComposeTestCheckFunc( + testAccCheckNotifyListExists("ns1_notifylist.test_multiple", &nl), + testAccCheckNotifyListName(&nl, "terraform test multiple different order"), + testAccCheckNotifyTypeOrder(&nl, "pagerduty", "webhook"), ), }, - // This fails because the schema.TypeList is ordered. We want to switch - // this to schema.TypeSet but cannot due to SDK issue #652 / #895, fix for - // which is waiting for review, see - // https://github.com/hashicorp/terraform-plugin-sdk/pull/1042 - // { - // Config: testAccNotifyListMultipleDifferentOrder , - // Check: resource.ComposeTestCheckFunc( - // testAccCheckNotifyListExists("ns1_notifylist.test_multiple2", &nl), - // testAccCheckNotifyListName(&nl, "terraform test multiple2"), - // ), - // }, { ResourceName: "ns1_notifylist.test_multiple", ImportState: true, @@ -157,27 +157,6 @@ func TestAccNotifyList_ManualDelete(t *testing.T) { }) } -func testAccCheckNotifyListState(key, value string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources["ns1_notifylist.test"] - if !ok { - return fmt.Errorf("not found: %s", "ns1_notifylist.test") - } - - if rs.Primary.ID == "" { - return fmt.Errorf("no ID is set") - } - - p := rs.Primary - if p.Attributes[key] != value { - return fmt.Errorf( - "%s != %s (actual: %s)", key, value, p.Attributes[key]) - } - - return nil - } -} - func testAccCheckNotifyListExists(n string, nl *monitor.NotifyList) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -236,6 +215,21 @@ func testAccCheckNotifyListName(nl *monitor.NotifyList, expected string) resourc } } +func testAccCheckNotifyTypeOrder(nl *monitor.NotifyList, type1, type2 string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if len(nl.Notifications) < 2 { + return fmt.Errorf("nl.Name: got: %#v want: 2 elements", nl.Notifications) + } + if nl.Notifications[0].Type != type1 { + return fmt.Errorf("nl.Name: got: %#v want: %#v", nl.Notifications[0].Type, type1) + } + if nl.Notifications[1].Type != type2 { + return fmt.Errorf("nl.Name: got: %#v want: %#v", nl.Notifications[1].Type, type2) + } + return nil + } +} + // Simulate a manual deletion of a notify list. func testAccManualDeleteNotifyList(nl *monitor.NotifyList) func() { return func() { @@ -315,8 +309,8 @@ resource "ns1_notifylist" "test_multiple" { ` const testAccNotifyListMultipleDifferentOrder = ` -resource "ns1_notifylist" "test_multiple2" { - name = "terraform test multiple2" +resource "ns1_notifylist" "test_multiple" { + name = "terraform test multiple different order" notifications { type = "webhook" config = {