-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the v1beta1 alerts api.
- Add code to call api. - Add testcases. - Add example.
- Loading branch information
Showing
9 changed files
with
1,125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
## 2.13.0 (Oct 28th, 2024) | ||
|
||
FEATURES: | ||
|
||
* Adds support for alerts | ||
|
||
## 2.12.2 (October 17th, 2024) | ||
|
||
BUG FIXES: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package mockns1 | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"gopkg.in/ns1/ns1-go.v2/rest/model/alerting" | ||
) | ||
|
||
// Should be identical to rest.alertListResponse | ||
type mockAlertListResponse struct { | ||
Limit *int64 `json:"limit,omitempty"` | ||
Next *string `json:"next,omitempty"` | ||
Results []*alerting.Alert `json:"results"` | ||
TotalResults *int64 `json:"total_results,omitempty"` | ||
} | ||
|
||
const alertPath = "../alerting/v1beta1/alerts" | ||
|
||
// AddAlertListTestCase sets up a test case for the api.Client.Alert.List() | ||
// function | ||
func (s *Service) AddAlertListTestCase( | ||
params string, requestHeaders, responseHeaders http.Header, | ||
response []*alerting.Alert, | ||
) error { | ||
length := int64(len(response)) | ||
next := "" | ||
if length > 0 { | ||
next = *response[length-1].Name | ||
} | ||
listResponse := &mockAlertListResponse{ | ||
|
||
Next: &next, | ||
Results: response, | ||
Limit: &length, | ||
TotalResults: &length, | ||
} | ||
uri := alertPath | ||
if params != "" { | ||
uri = fmt.Sprintf("%s?%s", uri, params) | ||
} | ||
return s.AddTestCase( | ||
http.MethodGet, uri, http.StatusOK, requestHeaders, | ||
responseHeaders, "", listResponse, | ||
) | ||
} | ||
|
||
// AddAlertGetTestCase sets up a test case for the api.Client.Alerts.Get() | ||
// function | ||
func (s *Service) AddAlertGetTestCase( | ||
id string, | ||
requestHeaders, responseHeaders http.Header, | ||
response *alerting.Alert, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodGet, fmt.Sprintf("%s/%s", alertPath, id), http.StatusOK, requestHeaders, | ||
responseHeaders, "", response, | ||
) | ||
} | ||
|
||
// AddAlertCreateTestCase sets up a test case for the api.Client.Alerts.Update() | ||
// function | ||
func (s *Service) AddAlertCreateTestCase( | ||
requestHeaders, responseHeaders http.Header, | ||
request alerting.Alert, | ||
response alerting.Alert, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodPost, alertPath, http.StatusOK, requestHeaders, | ||
responseHeaders, request, response, | ||
) | ||
} | ||
|
||
// AddAlertUpdateTestCase sets up a test case for the api.Client.Alerts.Update() | ||
// function | ||
func (s *Service) AddAlertUpdateTestCase( | ||
requestHeaders, responseHeaders http.Header, | ||
request alerting.Alert, | ||
response alerting.Alert, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodPatch, fmt.Sprintf("%s/%s", alertPath, *request.ID), http.StatusOK, requestHeaders, | ||
responseHeaders, request, response, | ||
) | ||
} | ||
|
||
// AddAlertReplaceTestCase sets up a test case for the api.Client.Alerts.Update() | ||
// function | ||
func (s *Service) AddAlertReplaceTestCase( | ||
requestHeaders, responseHeaders http.Header, | ||
request alerting.Alert, | ||
response alerting.Alert, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodPut, fmt.Sprintf("%s/%s", alertPath, *request.ID), http.StatusOK, requestHeaders, | ||
responseHeaders, request, response, | ||
) | ||
} | ||
|
||
// AddAlertDeleteTestCase sets up a test case for the api.Client.Alerts.Delete() | ||
// function | ||
func (s *Service) AddAlertDeleteTestCase( | ||
id string, | ||
requestHeaders, responseHeaders http.Header, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodDelete, fmt.Sprintf("%s/%s", alertPath, id), http.StatusNoContent, requestHeaders, | ||
responseHeaders, "", nil, | ||
) | ||
} | ||
|
||
// AddAlertTestPostTestCase sets up a test case for the api.Client.Alerts.Test() | ||
// function | ||
func (s *Service) AddAlertTestPostTestCase( | ||
id string, | ||
requestHeaders, responseHeaders http.Header, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodPost, fmt.Sprintf("%s/%s/test", alertPath, id), http.StatusNoContent, requestHeaders, | ||
responseHeaders, "", nil, | ||
) | ||
} | ||
|
||
// AddAlertFailTestCase sets up a test case for the api.Client.Alerts.*() | ||
// functions that fails. | ||
func (s *Service) AddAlertFailTestCase( | ||
method string, id string, returnStatus int, | ||
requestHeaders, responseHeaders http.Header, | ||
responseBody string, | ||
) error { | ||
path := alertPath | ||
if id != "" { | ||
path = fmt.Sprintf("%s/%s", alertPath, id) | ||
} | ||
return s.AddTestCase( | ||
method, path, returnStatus, | ||
nil, nil, "", responseBody) | ||
} | ||
|
||
func (s *Service) AddAlertFailTestCaseWithReqBody( | ||
method string, id string, returnStatus int, | ||
requestHeaders, responseHeaders http.Header, | ||
requestBody interface{}, | ||
responseBody string, | ||
) error { | ||
path := alertPath | ||
if id != "" { | ||
path = fmt.Sprintf("%s/%s", alertPath, id) | ||
} | ||
return s.AddTestCase( | ||
method, path, returnStatus, | ||
nil, nil, requestBody, responseBody) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
api "gopkg.in/ns1/ns1-go.v2/rest" | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/alerting" | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/dns" | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/monitor" | ||
) | ||
|
||
var client *api.Client | ||
|
||
// Helper that initializes rest api client from environment variable. | ||
func init() { | ||
k := os.Getenv("NS1_APIKEY") | ||
if k == "" { | ||
fmt.Println("NS1_APIKEY environment variable is not set, giving up") | ||
} | ||
|
||
httpClient := &http.Client{Timeout: time.Second * 10} | ||
// Adds logging to each http request. | ||
doer := api.Decorate(httpClient, api.Logging(log.New(os.Stdout, "", log.LstdFlags))) | ||
client = api.NewClient(doer, api.SetAPIKey(k)) | ||
} | ||
|
||
func prettyPrint(header string, v interface{}) { | ||
fmt.Println(header) | ||
fmt.Printf("%#v \n", v) | ||
b, _ := json.MarshalIndent(v, "", " ") | ||
fmt.Println(string(b)) | ||
} | ||
|
||
func main() { | ||
alerts, _, err := client.Alerts.List() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, a := range alerts { | ||
prettyPrint("alert:", a) | ||
} | ||
|
||
webhook := monitor.NewWebNotification("test.com/test", map[string]string{}) | ||
webhookList := monitor.NewNotifyList("my webhook list", webhook) | ||
_, err = client.Notifications.Create(webhookList) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
prettyPrint("Webhook NotifyList:", webhookList) | ||
|
||
email := monitor.NewEmailNotification("[email protected]") | ||
emailList := monitor.NewNotifyList("my email list", email) | ||
_, err = client.Notifications.Create(emailList) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
prettyPrint("Email NotifyList:", emailList) | ||
|
||
// Construct/Create a zone. | ||
domain := "myalerttest.com" | ||
|
||
z := dns.NewZone(domain) | ||
z.NxTTL = 3600 | ||
_, err = client.Zones.Create(z) | ||
if err != nil { | ||
// Ignore if zone already exists | ||
if err != api.ErrZoneExists { | ||
log.Fatal(err) | ||
} else { | ||
log.Println("Zone already exists, continuing...") | ||
} | ||
} | ||
|
||
prettyPrint("Zone:", z) | ||
fmt.Printf("Creating alert...\n") | ||
alert := alerting.NewZoneAlert("myalerttest.com - transfer failed", "transfer_failed", []string{webhookList.ID}, []string{domain}) | ||
_, err = client.Alerts.Create(alert) | ||
if err != nil { | ||
if err == api.ErrAlertExists { | ||
// This is fatal as we need the id returned on create. | ||
log.Println("Alert already exists.") | ||
} | ||
log.Fatal(err) | ||
|
||
} | ||
alertID := *alert.ID | ||
|
||
// Pass the id and the field(s) to change on Update. | ||
updatedName := "myalerttest.com - updated" | ||
alertUpdate := &alerting.Alert{ | ||
ID: &alertID, | ||
Name: &updatedName, | ||
NotifierListIds: []string{webhookList.ID, emailList.ID}, | ||
} | ||
_, err = client.Alerts.Update(alertUpdate) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
prettyPrint("Updated Alert:", alertUpdate) | ||
|
||
// To pass the whole alert object on Replace, retrieve it by ID it first. | ||
alertToReplace, _, err := client.Alerts.Get(alertID) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Replace values in retrieved struct with new values. | ||
// e.g. Change name and clear list. | ||
replacedName := "myalerttest.com - replaced" | ||
alertToReplace.Name = &replacedName | ||
alertToReplace.NotifierListIds = []string{} | ||
|
||
// Pass the whole alert object | ||
_, err = client.Alerts.Replace(alertToReplace) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
prettyPrint("Replaced Alert:", alertToReplace) | ||
|
||
// Delete the alert. | ||
_, err = client.Alerts.Delete(*alertToReplace.ID) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.