Skip to content

Commit 02f31b8

Browse files
committed
Add support for the v1beta1 alerts api.
- Add new resource types for alerts. - Add testcases. - Add examples.
1 parent b0e7170 commit 02f31b8

File tree

7 files changed

+655
-1
lines changed

7 files changed

+655
-1
lines changed

examples/alert.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
resource "ns1_alert" "email_on_zone_transfer_failure" {
2+
name = "Zone transfer failed"
3+
type = "zone"
4+
subtype = "transfer_failed"
5+
notification_lists = [ns1_notifylist.email_list.id]
6+
zone_names = [ns1_zone.alert_example_one.zone, ns1_zone.alert_example_two.zone]
7+
}
8+
9+
# Nofitication list
10+
resource "ns1_notifylist" "email_list" {
11+
name = "email list"
12+
notifications {
13+
type = "email"
14+
config = {
15+
16+
}
17+
}
18+
}
19+
20+
# Secondary zones
21+
resource "ns1_zone" "alert_example_one" {
22+
zone = "alert1.example"
23+
primary = "192.0.2.1"
24+
additional_primaries = ["192.0.2.2"]
25+
}
26+
27+
resource "ns1_zone" "alert_example_two" {
28+
zone = "alert2.example"
29+
primary = "192.0.2.1"
30+
additional_primaries = ["192.0.2.2"]
31+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/hashicorp/go-retryablehttp v0.7.7
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
99
github.com/stretchr/testify v1.8.1
10-
gopkg.in/ns1/ns1-go.v2 v2.12.2
10+
gopkg.in/ns1/ns1-go.v2 v2.12.3-0.20241028132723-c522965c035f
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
253253
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
254254
gopkg.in/ns1/ns1-go.v2 v2.12.2 h1:SPM5BTTMJ1zVBhMMiiPFdF7l6Y3fq5o7bKM7jDqsUfM=
255255
gopkg.in/ns1/ns1-go.v2 v2.12.2/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
256+
gopkg.in/ns1/ns1-go.v2 v2.12.3-0.20241028132723-c522965c035f h1:C+s7m/FyQZDi7VvkKPbhPD+6ZUZk/ua+1gt5J4BPtBY=
257+
gopkg.in/ns1/ns1-go.v2 v2.12.3-0.20241028132723-c522965c035f/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
256258
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
257259
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
258260
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

ns1/examples/alert.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "ns1_alert" "example" {
2+
#required
3+
name = "Example Alert"
4+
type = "zone"
5+
subtype = "transfer_failed"
6+
7+
#optional
8+
notification_lists = []
9+
zone_names = []
10+
record_ids = []
11+
}

ns1/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func Provider() *schema.Provider {
7474
"ns1_dataset": datasetResource(),
7575
"ns1_redirect": redirectConfigResource(),
7676
"ns1_redirect_certificate": redirectCertificateResource(),
77+
"ns1_alert": alertResource(),
7778
},
7879
ConfigureFunc: ns1Configure,
7980
}

ns1/resource_alert.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package ns1
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
8+
"gopkg.in/ns1/ns1-go.v2/rest/model/alerting"
9+
)
10+
11+
func alertResource() *schema.Resource {
12+
return &schema.Resource{
13+
Schema: map[string]*schema.Schema{
14+
// Required
15+
"name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"type": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
// ValidateFunc: validatePath,
23+
// DiffSuppressFunc: caseSensitivityDiffSuppress,
24+
},
25+
"subtype": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
// ValidateFunc: validateURL,
29+
// DiffSuppressFunc: caseSensitivityDiffSuppress,
30+
},
31+
// Read-only
32+
"id": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"created_at": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
},
40+
"updated_at": {
41+
Type: schema.TypeInt,
42+
Computed: true,
43+
},
44+
"created_by": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"updated_by": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
// Optional
53+
"notification_lists": {
54+
Type: schema.TypeSet,
55+
Optional: true,
56+
Elem: &schema.Schema{
57+
Type: schema.TypeString,
58+
},
59+
},
60+
"zone_names": {
61+
Type: schema.TypeSet,
62+
Optional: true,
63+
Elem: &schema.Schema{
64+
Type: schema.TypeString,
65+
},
66+
ConflictsWith: []string{"record_ids"},
67+
},
68+
"record_ids": {
69+
Type: schema.TypeSet,
70+
Optional: true,
71+
Elem: &schema.Schema{
72+
Type: schema.TypeString,
73+
},
74+
ConflictsWith: []string{"zone_names"},
75+
},
76+
},
77+
Create: AlertConfigCreate,
78+
Read: AlertConfigRead,
79+
Update: AlertConfigUpdate,
80+
Delete: AlertConfigDelete,
81+
Importer: &schema.ResourceImporter{},
82+
}
83+
}
84+
85+
func alertToResourceData(d *schema.ResourceData, alert *alerting.Alert) error {
86+
d.SetId(*alert.ID)
87+
d.Set("name", alert.Name)
88+
d.Set("type", alert.Type)
89+
d.Set("subtype", alert.Subtype)
90+
d.Set("created_at", alert.CreatedAt)
91+
d.Set("updated_at", alert.UpdatedAt)
92+
d.Set("created_by", alert.CreatedBy)
93+
d.Set("updated_by", alert.UpdatedBy)
94+
d.Set("notification_lists", alert.NotifierListIds)
95+
d.Set("zone_names", alert.ZoneNames)
96+
d.Set("record_ids", alert.RecordIds)
97+
return nil
98+
}
99+
100+
func strPtr(str string) *string {
101+
return &str
102+
}
103+
104+
func int64Ptr(n int) *int64 {
105+
n64 := int64(n)
106+
return &n64
107+
}
108+
109+
func resourceDataToAlert(d *schema.ResourceData) (*alerting.Alert, error) {
110+
alert := alerting.Alert{
111+
ID: strPtr(d.Id()),
112+
}
113+
if v, ok := d.GetOk("name"); ok {
114+
alert.Name = strPtr(v.(string))
115+
}
116+
if v, ok := d.GetOk("type"); ok {
117+
alert.Type = strPtr(v.(string))
118+
}
119+
if v, ok := d.GetOk("subtype"); ok {
120+
alert.Subtype = strPtr(v.(string))
121+
}
122+
if v, ok := d.GetOk("created_at"); ok {
123+
alert.CreatedAt = int64Ptr(v.(int))
124+
}
125+
if v, ok := d.GetOk("updated_at"); ok {
126+
alert.UpdatedAt = int64Ptr(v.(int))
127+
}
128+
if v, ok := d.GetOk("created_by"); ok {
129+
alert.CreatedBy = strPtr(v.(string))
130+
}
131+
if v, ok := d.GetOk("updated_by"); ok {
132+
alert.UpdatedBy = strPtr(v.(string))
133+
}
134+
if v, ok := d.GetOk("notification_lists"); ok {
135+
listIds := v.(*schema.Set)
136+
alert.NotifierListIds = make([]string, 0, listIds.Len())
137+
for _, id := range listIds.List() {
138+
alert.NotifierListIds = append(alert.NotifierListIds, id.(string))
139+
}
140+
} else {
141+
alert.NotifierListIds = []string{}
142+
}
143+
if v, ok := d.GetOk("zone_names"); ok {
144+
zoneNames := v.(*schema.Set)
145+
alert.ZoneNames = make([]string, 0, zoneNames.Len())
146+
for _, zone := range zoneNames.List() {
147+
alert.ZoneNames = append(alert.ZoneNames, zone.(string))
148+
}
149+
} else {
150+
alert.ZoneNames = []string{}
151+
}
152+
if v, ok := d.GetOk("record_ids"); ok {
153+
recordIds := v.(*schema.Set)
154+
alert.RecordIds = make([]string, 0, recordIds.Len())
155+
for _, id := range recordIds.List() {
156+
alert.RecordIds = append(alert.RecordIds, id.(string))
157+
}
158+
} else {
159+
alert.RecordIds = []string{}
160+
}
161+
return &alert, nil
162+
}
163+
164+
func AlertConfigCreate(d *schema.ResourceData, meta interface{}) error {
165+
client := meta.(*ns1.Client)
166+
167+
var alert *alerting.Alert = nil
168+
alert, err := resourceDataToAlert(d)
169+
if err != nil {
170+
return err
171+
}
172+
173+
if resp, err := client.Alerts.Create(alert); err != nil {
174+
return ConvertToNs1Error(resp, err)
175+
}
176+
177+
return alertToResourceData(d, alert)
178+
}
179+
180+
func AlertConfigRead(d *schema.ResourceData, meta interface{}) error {
181+
client := meta.(*ns1.Client)
182+
183+
alert, resp, err := client.Alerts.Get(d.Id())
184+
if err != nil {
185+
if err == ns1.ErrAlertMissing {
186+
log.Printf("[DEBUG] NS1 alert (%s) not found", d.Id())
187+
d.SetId("")
188+
return nil
189+
}
190+
191+
return ConvertToNs1Error(resp, err)
192+
}
193+
194+
return alertToResourceData(d, alert)
195+
}
196+
197+
func AlertConfigUpdate(d *schema.ResourceData, meta interface{}) error {
198+
client := meta.(*ns1.Client)
199+
200+
alert, err := resourceDataToAlert(d)
201+
if err != nil {
202+
return err
203+
}
204+
205+
if resp, err := client.Alerts.Update(alert); err != nil {
206+
return ConvertToNs1Error(resp, err)
207+
}
208+
209+
return alertToResourceData(d, alert)
210+
}
211+
212+
func AlertConfigDelete(d *schema.ResourceData, meta interface{}) error {
213+
client := meta.(*ns1.Client)
214+
215+
resp, err := client.Alerts.Delete(d.Id())
216+
d.SetId("")
217+
return ConvertToNs1Error(resp, err)
218+
}

0 commit comments

Comments
 (0)