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

Add support for the v1beta1 alerts api. #335

Merged
merged 3 commits into from
Dec 17, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.5.0 (December 13, 2024)
ENHANCEMENTS
* Adds support for alert endpoints

## 2.4.5 (October 17, 2024)
BUGFIX
* `ns1-go` client version bump to allow wiping out record regions
Expand Down
31 changes: 31 additions & 0 deletions examples/alert.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "ns1_alert" "email_on_zone_transfer_failure" {
name = "Zone transfer failed"
type = "zone"
subtype = "transfer_failed"
notification_lists = [ns1_notifylist.email_list.id]
zone_names = [ns1_zone.alert_example_one.zone, ns1_zone.alert_example_two.zone]
}

# Notification list
resource "ns1_notifylist" "email_list" {
name = "email list"
notifications {
type = "email"
config = {
email = "[email protected]"
}
}
}

# Secondary zones
resource "ns1_zone" "alert_example_one" {
zone = "alert1.example"
primary = "192.0.2.1"
additional_primaries = ["192.0.2.2"]
}

resource "ns1_zone" "alert_example_two" {
zone = "alert2.example"
primary = "192.0.2.1"
additional_primaries = ["192.0.2.2"]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/stretchr/testify v1.8.1
gopkg.in/ns1/ns1-go.v2 v2.12.2
gopkg.in/ns1/ns1-go.v2 v2.13.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ns1/ns1-go.v2 v2.12.2 h1:SPM5BTTMJ1zVBhMMiiPFdF7l6Y3fq5o7bKM7jDqsUfM=
gopkg.in/ns1/ns1-go.v2 v2.12.2/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
gopkg.in/ns1/ns1-go.v2 v2.13.0 h1:I5NNqI9Bi1SGK92TVkOvLTwux5LNrix/99H2datVh48=
gopkg.in/ns1/ns1-go.v2 v2.13.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
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.4.5"
clientVersion = "2.5.0"
providerUserAgent = "tf-ns1" + "/" + clientVersion
defaultRetryMax = 3
)
Expand Down
11 changes: 11 additions & 0 deletions ns1/examples/alert.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "ns1_alert" "example" {
#required
name = "Example Alert"
type = "zone"
subtype = "transfer_failed"

#optional
notification_lists = []
zone_names = []
record_ids = []
}
1 change: 1 addition & 0 deletions ns1/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Provider() *schema.Provider {
"ns1_dataset": datasetResource(),
"ns1_redirect": redirectConfigResource(),
"ns1_redirect_certificate": redirectCertificateResource(),
"ns1_alert": alertResource(),
},
ConfigureFunc: ns1Configure,
}
Expand Down
218 changes: 218 additions & 0 deletions ns1/resource_alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package ns1

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
"gopkg.in/ns1/ns1-go.v2/rest/model/alerting"
)

func alertResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
// Required
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
// ValidateFunc: validatePath,
// DiffSuppressFunc: caseSensitivityDiffSuppress,
},
"subtype": {
Type: schema.TypeString,
Required: true,
// ValidateFunc: validateURL,
// DiffSuppressFunc: caseSensitivityDiffSuppress,
},
// Read-only
"id": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeInt,
Computed: true,
},
"updated_at": {
Type: schema.TypeInt,
Computed: true,
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
},
// Optional
"notification_lists": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"zone_names": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
ConflictsWith: []string{"record_ids"},
},
"record_ids": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
ConflictsWith: []string{"zone_names"},
},
},
Create: AlertConfigCreate,
Read: AlertConfigRead,
Update: AlertConfigUpdate,
Delete: AlertConfigDelete,
Importer: &schema.ResourceImporter{},
}
}

func alertToResourceData(d *schema.ResourceData, alert *alerting.Alert) error {
d.SetId(*alert.ID)
d.Set("name", alert.Name)
d.Set("type", alert.Type)
d.Set("subtype", alert.Subtype)
d.Set("created_at", alert.CreatedAt)
d.Set("updated_at", alert.UpdatedAt)
d.Set("created_by", alert.CreatedBy)
d.Set("updated_by", alert.UpdatedBy)
d.Set("notification_lists", alert.NotifierListIds)
d.Set("zone_names", alert.ZoneNames)
d.Set("record_ids", alert.RecordIds)
return nil
}

func strPtr(str string) *string {
return &str
}

func int64Ptr(n int) *int64 {
n64 := int64(n)
return &n64
}

func resourceDataToAlert(d *schema.ResourceData) (*alerting.Alert, error) {
alert := alerting.Alert{
ID: strPtr(d.Id()),
}
if v, ok := d.GetOk("name"); ok {
alert.Name = strPtr(v.(string))
}
if v, ok := d.GetOk("type"); ok {
alert.Type = strPtr(v.(string))
}
if v, ok := d.GetOk("subtype"); ok {
alert.Subtype = strPtr(v.(string))
}
if v, ok := d.GetOk("created_at"); ok {
alert.CreatedAt = int64Ptr(v.(int))
}
if v, ok := d.GetOk("updated_at"); ok {
alert.UpdatedAt = int64Ptr(v.(int))
}
if v, ok := d.GetOk("created_by"); ok {
alert.CreatedBy = strPtr(v.(string))
}
if v, ok := d.GetOk("updated_by"); ok {
alert.UpdatedBy = strPtr(v.(string))
}
if v, ok := d.GetOk("notification_lists"); ok {
listIds := v.(*schema.Set)
alert.NotifierListIds = make([]string, 0, listIds.Len())
for _, id := range listIds.List() {
alert.NotifierListIds = append(alert.NotifierListIds, id.(string))
}
} else {
alert.NotifierListIds = []string{}
}
if v, ok := d.GetOk("zone_names"); ok {
zoneNames := v.(*schema.Set)
alert.ZoneNames = make([]string, 0, zoneNames.Len())
for _, zone := range zoneNames.List() {
alert.ZoneNames = append(alert.ZoneNames, zone.(string))
}
} else {
alert.ZoneNames = []string{}
}
if v, ok := d.GetOk("record_ids"); ok {
recordIds := v.(*schema.Set)
alert.RecordIds = make([]string, 0, recordIds.Len())
for _, id := range recordIds.List() {
alert.RecordIds = append(alert.RecordIds, id.(string))
}
} else {
alert.RecordIds = []string{}
}
return &alert, nil
}

func AlertConfigCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

var alert *alerting.Alert = nil
alert, err := resourceDataToAlert(d)
if err != nil {
return err
}

if resp, err := client.Alerts.Create(alert); err != nil {
return ConvertToNs1Error(resp, err)
}

return alertToResourceData(d, alert)
}

func AlertConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

alert, resp, err := client.Alerts.Get(d.Id())
if err != nil {
if err == ns1.ErrAlertMissing {
log.Printf("[DEBUG] NS1 alert (%s) not found", d.Id())
d.SetId("")
return nil
}

return ConvertToNs1Error(resp, err)
}

return alertToResourceData(d, alert)
}

func AlertConfigUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

alert, err := resourceDataToAlert(d)
if err != nil {
return err
}

if resp, err := client.Alerts.Update(alert); err != nil {
return ConvertToNs1Error(resp, err)
}

return alertToResourceData(d, alert)
}

func AlertConfigDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

resp, err := client.Alerts.Delete(d.Id())
d.SetId("")
return ConvertToNs1Error(resp, err)
}
Loading
Loading