Skip to content

Commit

Permalink
Merge branch 'ns1-terraform:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fformica authored May 31, 2024
2 parents 93e4f95 + 33170e8 commit caaf957
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ns1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
clientVersion = "2.2.1"
clientVersion = "2.2.2"
providerUserAgent = "tf-ns1" + "/" + clientVersion
defaultRetryMax = 3
)
Expand Down
20 changes: 10 additions & 10 deletions ns1/resource_notifylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
62 changes: 28 additions & 34 deletions ns1/resource_notifylist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/record.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ The following arguments are supported:
[Answers](#answers-1) are documented below.
* `filters` - (Optional) One or more NS1 filters for the record(order matters).
[Filters](#filters-1) are documented below.
* `tags` - map of tags in the form of `"key" = "value"` where both key and value are strings

#### Answers

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/zone.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ The following arguments are supported:
workflow for creating zones with the NS record in terraform state. See
above for an example. Note that this option only has an effect when a zone is
being created.
* `tags` - map of tags in the form of `"key" = "value"` where both key and value are strings
* `tsig` - [TSIG](#TSIG-2) is documented below

#### Secondaries
Expand Down

0 comments on commit caaf957

Please sign in to comment.