aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/DreamItGetIT/statuscake/contactGroups.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/DreamItGetIT/statuscake/contactGroups.go')
-rw-r--r--vendor/github.com/DreamItGetIT/statuscake/contactGroups.go149
1 files changed, 149 insertions, 0 deletions
diff --git a/vendor/github.com/DreamItGetIT/statuscake/contactGroups.go b/vendor/github.com/DreamItGetIT/statuscake/contactGroups.go
new file mode 100644
index 0000000..437fe37
--- /dev/null
+++ b/vendor/github.com/DreamItGetIT/statuscake/contactGroups.go
@@ -0,0 +1,149 @@
1package statuscake
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7 "strings"
8 "github.com/google/go-querystring/query"
9)
10
11//ContactGroup represent the data received by the API with GET
12type ContactGroup struct {
13 GroupName string `json:"GroupName" url:"GroupName,omitempty"`
14 Emails []string `json:"Emails"`
15 EmailsPut string `url:"Email,omitempty"`
16 Mobiles string `json:"Mobiles" url:"Mobile,omitempty"`
17 Boxcar string `json:"Boxcar" url:"Boxcar,omitempty"`
18 Pushover string `json:"Pushover" url:"Pushover,omitempty"`
19 ContactID int `json:"ContactID" url:"ContactID,omitempty"`
20 DesktopAlert string `json:"DesktopAlert" url:"DesktopAlert,omitempty"`
21 PingURL string `json:"PingURL" url:"PingURL,omitempty"`
22
23}
24
25type Response struct {
26 Success bool `json:"Success"`
27 Message string `json:"Message"`
28 InsertID int `json:"InsertID"`
29}
30
31//ContactGroups represent the actions done wit the API
32type ContactGroups interface {
33 All() ([]*ContactGroup, error)
34 Detail(int) (*ContactGroup, error)
35 Update(*ContactGroup) (*ContactGroup, error)
36 Delete(int) error
37 Create(*ContactGroup) (*ContactGroup, error)
38}
39
40func findContactGroup(responses []*ContactGroup, id int) (*ContactGroup, error) {
41 var response *ContactGroup
42 for _, elem := range responses {
43 if (*elem).ContactID == id {
44 return elem, nil
45 }
46 }
47 return response, fmt.Errorf("%d Not found", id)
48}
49
50type contactGroups struct{
51 client apiClient
52}
53
54//NewContactGroups return a new ssls
55func NewContactGroups(c apiClient) ContactGroups {
56 return &contactGroups{
57 client: c,
58 }
59}
60
61//All return a list of all the ContactGroup from the API
62func (tt *contactGroups) All() ([]*ContactGroup, error) {
63 rawResponse, err := tt.client.get("/ContactGroups", nil)
64 if err != nil {
65 return nil, fmt.Errorf("Error getting StatusCake contactGroups: %s", err.Error())
66 }
67 var getResponse []*ContactGroup
68 err = json.NewDecoder(rawResponse.Body).Decode(&getResponse)
69 if err != nil {
70 return nil, err
71 }
72 return getResponse, err
73}
74
75//Detail return the ContactGroup corresponding to the id
76func (tt *contactGroups) Detail(id int) (*ContactGroup, error) {
77 responses, err := tt.All()
78 if err != nil {
79 return nil, err
80 }
81 myContactGroup, errF := findContactGroup(responses, id)
82 if errF != nil {
83 return nil, errF
84 }
85 return myContactGroup, nil
86}
87
88//Update update the API with cg and create one if cg.ContactID=0 then return the corresponding ContactGroup
89func (tt *contactGroups) Update(cg *ContactGroup) (*ContactGroup, error) {
90
91 if(cg.ContactID == 0){
92 return tt.Create(cg)
93 }
94 cg.EmailsPut=strings.Join(cg.Emails,",")
95 var v url.Values
96
97 v, _ = query.Values(*cg)
98
99 rawResponse, err := tt.client.put("/ContactGroups/Update", v)
100 if err != nil {
101 return nil, fmt.Errorf("Error creating StatusCake ContactGroup: %s", err.Error())
102 }
103
104 var response Response
105 err = json.NewDecoder(rawResponse.Body).Decode(&response)
106 if err != nil {
107 return nil, err
108 }
109
110 if !response.Success {
111 return nil, fmt.Errorf("%s", response.Message)
112 }
113
114 return cg, nil
115}
116
117//Delete delete the ContactGroup which ID is id
118func (tt *contactGroups) Delete(id int) error {
119 _, err := tt.client.delete("/ContactGroups/Update", url.Values{"ContactID": {fmt.Sprint(id)}})
120 return err
121}
122
123//CreatePartial create the ContactGroup whith the data in cg and return the ContactGroup created
124func (tt *contactGroups) Create(cg *ContactGroup) (*ContactGroup, error) {
125 cg.ContactID=0
126 cg.EmailsPut=strings.Join(cg.Emails,",")
127 var v url.Values
128 v, _ = query.Values(*cg)
129
130 rawResponse, err := tt.client.put("/ContactGroups/Update", v)
131 if err != nil {
132 return nil, fmt.Errorf("Error creating StatusCake ContactGroup: %s", err.Error())
133 }
134
135 var response Response
136 err = json.NewDecoder(rawResponse.Body).Decode(&response)
137 if err != nil {
138 return nil, err
139 }
140
141 if !response.Success {
142 return nil, fmt.Errorf("%s", response.Message)
143 }
144
145 cg.ContactID = response.InsertID
146
147 return cg,nil
148}
149