44 "fmt"
55 "log"
66 "reflect"
7+ "regexp"
78 "sort"
89 "strings"
910 "testing"
@@ -17,6 +18,7 @@ import (
1718)
1819
1920// Creating basic DNS alert
21+ // NOTE: You would need the `manage_zones` permission to create this alert type. Otherwise this test will fail!
2022func TestAccAlert_basic (t * testing.T ) {
2123 var (
2224 alert = alerting.Alert {}
@@ -45,6 +47,46 @@ func TestAccAlert_basic(t *testing.T) {
4547 })
4648}
4749
50+ func TestAccAlert_RecordUsage (t * testing.T ) {
51+ var (
52+ alert = alerting.Alert {}
53+ alertName = fmt .Sprintf ("terraform-test-alert-%s" , acctest .RandStringFromCharSet (15 , acctest .CharSetAlphaNum ))
54+ )
55+
56+ resource .Test (t , resource.TestCase {
57+ PreCheck : func () { testAccPreCheck (t ) },
58+ Providers : testAccProviders ,
59+ CheckDestroy : testAccCheckAlertDestroy ,
60+ Steps : []resource.TestStep {
61+ {Config : testRecordUsageAlert (alertName , 10 ),
62+ Check : resource .ComposeTestCheckFunc (
63+ testAccCheckAlertExists ("ns1_alert.it" , & alert ),
64+ testAccCheckAlertName (& alert , alertName ),
65+ testAccCheckAlertType (& alert , "account" ),
66+ testAccCheckAlertSubtype (& alert , "record_usage" ),
67+ testAccCheckAlertData (& alert , `{"alert_at_percent":10}` ),
68+ )},
69+ {Config : testRecordUsageAlert (alertName , 101 ),
70+ ExpectError : regexp .MustCompile ("Please correct the following field: data.alert_at_percent" ),
71+ ExpectNonEmptyPlan : true ,
72+ },
73+ {Config : testRecordUsageAlert (alertName , 50 ),
74+ Check : resource .ComposeTestCheckFunc (
75+ testAccCheckAlertExists ("ns1_alert.it" , & alert ),
76+ testAccCheckAlertName (& alert , alertName ),
77+ testAccCheckAlertType (& alert , "account" ),
78+ testAccCheckAlertSubtype (& alert , "record_usage" ),
79+ testAccCheckAlertData (& alert , `{"alert_at_percent":50}` ),
80+ )},
81+ {
82+ ResourceName : "ns1_alert.it" ,
83+ ImportState : true ,
84+ ImportStateVerify : true ,
85+ },
86+ },
87+ })
88+ }
89+
4890func TestAccAlert_links (t * testing.T ) {
4991 var (
5092 alert = alerting.Alert {}
@@ -184,6 +226,18 @@ func TestAccAlert_ManualDelete(t *testing.T) {
184226 })
185227}
186228
229+ func testRecordUsageAlert (alertName string , threshold int ) string {
230+ return fmt .Sprintf (`resource "ns1_alert" "it" {
231+ name = "%s"
232+ type = "account"
233+ subtype = "record_usage"
234+ notification_lists = []
235+ data {
236+ alert_at_percent = %d
237+ }
238+ }` , alertName , threshold )
239+ }
240+
187241func testAccAlertBasic (alertName string ) string {
188242 return fmt .Sprintf (`resource "ns1_alert" "it" {
189243 name = "%s"
@@ -306,6 +360,16 @@ func testAccCheckAlertSubtype(alert *alerting.Alert, expected string) resource.T
306360 }
307361}
308362
363+ func testAccCheckAlertData (alert * alerting.Alert , expected string ) resource.TestCheckFunc {
364+ return func (s * terraform.State ) error {
365+ data := string (alert .Data )
366+ if data != expected {
367+ return fmt .Errorf ("alert.Data: got: %s want: %s" , data , expected )
368+ }
369+ return nil
370+ }
371+ }
372+
309373func testAccCheckAlertZoneNames (alert * alerting.Alert , expected []string ) resource.TestCheckFunc {
310374 return func (s * terraform.State ) error {
311375 actualSorted := alert .ZoneNames
0 commit comments