monitor_notify.go raw
1 package rest
2
3 import (
4 "errors"
5 "fmt"
6 "net/http"
7
8 "gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
9 )
10
11 // NotificationsService handles 'monitoring/lists' endpoint.
12 type NotificationsService service
13
14 // List returns all configured notification lists.
15 //
16 // NS1 API docs: https://ns1.com/api/#lists-get
17 func (s *NotificationsService) List() ([]*monitor.NotifyList, *http.Response, error) {
18 req, err := s.client.NewRequest("GET", "lists", nil)
19 if err != nil {
20 return nil, nil, err
21 }
22
23 nl := []*monitor.NotifyList{}
24 resp, err := s.client.Do(req, &nl)
25 if err != nil {
26 return nil, resp, err
27 }
28
29 return nl, resp, nil
30 }
31
32 // Get returns the details and notifiers associated with a specific notification list.
33 //
34 // NS1 API docs: https://ns1.com/api/#lists-listid-get
35 func (s *NotificationsService) Get(listID string) (*monitor.NotifyList, *http.Response, error) {
36 path := fmt.Sprintf("%s/%s", "lists", listID)
37
38 req, err := s.client.NewRequest("GET", path, nil)
39 if err != nil {
40 return nil, nil, err
41 }
42
43 var nl monitor.NotifyList
44 resp, err := s.client.Do(req, &nl)
45 if err != nil {
46 switch err.(type) {
47 case *Error:
48 if resourceMissingMatch(err.(*Error).Message) {
49 return nil, resp, ErrListMissing
50 }
51 }
52 return nil, resp, err
53 }
54
55 return &nl, resp, nil
56 }
57
58 // Create takes a *NotifyList and creates a new notify list.
59 //
60 // NS1 API docs: https://ns1.com/api/#lists-put
61 func (s *NotificationsService) Create(nl *monitor.NotifyList) (*http.Response, error) {
62 req, err := s.client.NewRequest("PUT", "lists", &nl)
63 if err != nil {
64 return nil, err
65 }
66
67 // Update notify list fields with data from api(ensure consistent)
68 resp, err := s.client.Do(req, &nl)
69 if err != nil {
70 switch err.(type) {
71 case *Error:
72 if err.(*Error).Message == fmt.Sprintf("notification list with name \"%s\" exists", nl.Name) {
73 return resp, ErrListExists
74 }
75 }
76 return resp, err
77 }
78
79 return resp, nil
80 }
81
82 // Update adds or removes entries or otherwise update a notification list.
83 //
84 // NS1 API docs: https://ns1.com/api/#list-listid-post
85 func (s *NotificationsService) Update(nl *monitor.NotifyList) (*http.Response, error) {
86 path := fmt.Sprintf("%s/%s", "lists", nl.ID)
87
88 req, err := s.client.NewRequest("POST", path, &nl)
89 if err != nil {
90 return nil, err
91 }
92
93 // Update mon lists' fields with data from api(ensure consistent)
94 resp, err := s.client.Do(req, &nl)
95 if err != nil {
96 return resp, err
97 }
98
99 return resp, nil
100 }
101
102 // Delete immediately deletes an existing notification list.
103 //
104 // NS1 API docs: https://ns1.com/api/#lists-listid-delete
105 func (s *NotificationsService) Delete(listID string) (*http.Response, error) {
106 path := fmt.Sprintf("%s/%s", "lists", listID)
107
108 req, err := s.client.NewRequest("DELETE", path, nil)
109 if err != nil {
110 return nil, err
111 }
112
113 resp, err := s.client.Do(req, nil)
114 if err != nil {
115 return resp, err
116 }
117
118 return resp, nil
119 }
120
121 var (
122 // ErrListExists bundles PUT create error.
123 ErrListExists = errors.New("notify List already exists")
124 // ErrListMissing bundles GET/POST/DELETE error.
125 ErrListMissing = errors.New("notify List does not exist")
126 )
127