-
Notifications
You must be signed in to change notification settings - Fork 59
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. #244
Open
hhellyer
wants to merge
1
commit into
v2
Choose a base branch
from
PENG-5494/add_alerts_api_support
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("test@test.com") | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this called in the example somewhere?