-
Notifications
You must be signed in to change notification settings - Fork 191
/
opsgenie_contact_client.go
142 lines (113 loc) · 4.24 KB
/
opsgenie_contact_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package client
import (
"errors"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/contact"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/logging"
)
const (
contactURL = "/v1/json/user/contact"
)
// OpsGenieContactClient is the data type to make Contact API requests.
type OpsGenieContactClient struct {
OpsGenieClient
}
// SetOpsGenieClient sets the embedded OpsGenieClient type of the OpsGenieContactClient.
func (cli *OpsGenieContactClient) SetOpsGenieClient(ogCli OpsGenieClient) {
cli.OpsGenieClient = ogCli
}
// Create method creates a contact at OpsGenie.
func (cli *OpsGenieContactClient) Create(req contact.CreateContactRequest) (*contact.CreateContactResponse, error) {
req.APIKey = cli.apiKey
resp, err := cli.sendRequest(cli.buildPostRequest(contactURL, req))
if resp == nil {
return nil, err
}
defer resp.Body.Close()
var createContactResp contact.CreateContactResponse
if err = resp.Body.FromJsonTo(&createContactResp); err != nil {
message := "Server response can not be parsed, " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return &createContactResp, nil
}
// Delete method deletes a contact at OpsGenie.
func (cli *OpsGenieContactClient) Delete(req contact.DeleteContactRequest) (*contact.DeleteContactResponse, error) {
req.APIKey = cli.apiKey
resp, err := cli.sendRequest(cli.buildDeleteRequest(contactURL, req))
if resp == nil {
return nil, err
}
defer resp.Body.Close()
var deleteContactResp contact.DeleteContactResponse
if err = resp.Body.FromJsonTo(&deleteContactResp); err != nil {
message := "Server response can not be parsed, " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return &deleteContactResp, nil
}
// Disable method disables a contact at OpsGenie.
func (cli *OpsGenieContactClient) Disable(req contact.DisableContactRequest) (*contact.DisableContactResponse, error) {
req.APIKey = cli.apiKey
resp, err := cli.sendRequest(cli.buildPostRequest(contactURL+"/disable", req))
if resp == nil {
return nil, err
}
defer resp.Body.Close()
var disableContactResp contact.DisableContactResponse
if err = resp.Body.FromJsonTo(&disableContactResp); err != nil {
message := "Server response can not be parsed, " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return &disableContactResp, nil
}
// Enable method enables a contact at OpsGenie.
func (cli *OpsGenieContactClient) Enable(req contact.EnableContactRequest) (*contact.EnableContactResponse, error) {
req.APIKey = cli.apiKey
resp, err := cli.sendRequest(cli.buildPostRequest(contactURL+"/enable", req))
if resp == nil {
return nil, err
}
defer resp.Body.Close()
var enableContactResp contact.EnableContactResponse
if err = resp.Body.FromJsonTo(&enableContactResp); err != nil {
message := "Server response can not be parsed, " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return &enableContactResp, nil
}
// Get method retrieves specified contact details from OpsGenie.
func (cli *OpsGenieContactClient) Get(req contact.GetContactRequest) (*contact.GetContactResponse, error) {
req.APIKey = cli.apiKey
resp, err := cli.sendRequest(cli.buildGetRequest(contactURL, req))
if resp == nil {
return nil, err
}
defer resp.Body.Close()
var getContactResp contact.GetContactResponse
if err = resp.Body.FromJsonTo(&getContactResp); err != nil {
message := "Server response can not be parsed, " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return &getContactResp, nil
}
// Update method updates a contact at OpsGenie.
func (cli *OpsGenieContactClient) Update(req contact.UpdateContactRequest) (*contact.UpdateContactResponse, error) {
req.APIKey = cli.apiKey
resp, err := cli.sendRequest(cli.buildPostRequest(contactURL, req))
if resp == nil {
return nil, err
}
defer resp.Body.Close()
var updateContactResp contact.UpdateContactResponse
if err = resp.Body.FromJsonTo(&updateContactResp); err != nil {
message := "Server response can not be parsed, " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return &updateContactResp, nil
}