|
| 1 | +package sdk |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2016-2020 The Grafana SDK authors |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | +) |
| 23 | + |
| 24 | +// GetAllAlertNotifications gets all alert notification channels. |
| 25 | +// Reflects GET /api/alert-notifications API call. |
| 26 | +func (c *Client) GetAllAlertNotifications() ([]AlertNotification, error) { |
| 27 | + var ( |
| 28 | + raw []byte |
| 29 | + an []AlertNotification |
| 30 | + code int |
| 31 | + err error |
| 32 | + ) |
| 33 | + if raw, code, err = c.get("api/alert-notifications", nil); err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + if code != 200 { |
| 37 | + return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 38 | + } |
| 39 | + err = json.Unmarshal(raw, &an) |
| 40 | + return an, err |
| 41 | +} |
| 42 | + |
| 43 | +// GetAlertNotificationUID gets the alert notification channel which has the specified uid. |
| 44 | +// Reflects GET /api/alert-notifications/uid/:uid API call. |
| 45 | +func (c *Client) GetAlertNotificationUID(uid string) (AlertNotification, error) { |
| 46 | + var ( |
| 47 | + raw []byte |
| 48 | + an AlertNotification |
| 49 | + code int |
| 50 | + err error |
| 51 | + ) |
| 52 | + if raw, code, err = c.get(fmt.Sprintf("api/alert-notifications/uid/%s", uid), nil); err != nil { |
| 53 | + return an, err |
| 54 | + } |
| 55 | + if code != 200 { |
| 56 | + return an, fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 57 | + } |
| 58 | + err = json.Unmarshal(raw, &an) |
| 59 | + return an, err |
| 60 | +} |
| 61 | + |
| 62 | +// GetAlertNotificationID gets the alert notification channel which has the specified id. |
| 63 | +// Reflects GET /api/alert-notifications/:id API call. |
| 64 | +func (c *Client) GetAlertNotificationID(id uint) (AlertNotification, error) { |
| 65 | + var ( |
| 66 | + raw []byte |
| 67 | + an AlertNotification |
| 68 | + code int |
| 69 | + err error |
| 70 | + ) |
| 71 | + if raw, code, err = c.get(fmt.Sprintf("api/alert-notifications/%d", id), nil); err != nil { |
| 72 | + return an, err |
| 73 | + } |
| 74 | + if code != 200 { |
| 75 | + return an, fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 76 | + } |
| 77 | + err = json.Unmarshal(raw, &an) |
| 78 | + return an, err |
| 79 | +} |
| 80 | + |
| 81 | +// CreateAlertNotification creates a new alert notification channel. |
| 82 | +// Reflects POST /api/alert-notifications API call. |
| 83 | +func (c *Client) CreateAlertNotification(an AlertNotification) (int64, error) { |
| 84 | + var ( |
| 85 | + raw []byte |
| 86 | + code int |
| 87 | + err error |
| 88 | + ) |
| 89 | + if raw, err = json.Marshal(an); err != nil { |
| 90 | + return -1, err |
| 91 | + } |
| 92 | + if raw, code, err = c.post(fmt.Sprintf("api/alert-notifications"), nil, raw); err != nil { |
| 93 | + return -1, err |
| 94 | + } |
| 95 | + if code != 200 { |
| 96 | + return -1, fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 97 | + } |
| 98 | + result := struct { |
| 99 | + ID int64 `json:"id"` |
| 100 | + }{} |
| 101 | + err = json.Unmarshal(raw, &result) |
| 102 | + return result.ID, err |
| 103 | +} |
| 104 | + |
| 105 | +// UpdateAlertNotificationUID updates the specified alert notification channel. |
| 106 | +// Reflects PUT /api/alert-notifications/uid/:uid API call. |
| 107 | +func (c *Client) UpdateAlertNotificationUID(an AlertNotification, uid string) error { |
| 108 | + var ( |
| 109 | + raw []byte |
| 110 | + code int |
| 111 | + err error |
| 112 | + ) |
| 113 | + if raw, err = json.Marshal(an); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + if raw, code, err = c.put(fmt.Sprintf("api/alert-notifications/uid/%s", uid), nil, raw); err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + if code != 200 { |
| 120 | + return fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +// UpdateAlertNotificationID updates the specified alert notification channel. |
| 126 | +// Reflects PUT /api/alert-notifications/:id API call. |
| 127 | +func (c *Client) UpdateAlertNotificationID(an AlertNotification, id uint) error { |
| 128 | + var ( |
| 129 | + raw []byte |
| 130 | + code int |
| 131 | + err error |
| 132 | + ) |
| 133 | + if raw, err = json.Marshal(an); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + if raw, code, err = c.put(fmt.Sprintf("api/alert-notifications/%d", id), nil, raw); err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + if code != 200 { |
| 140 | + return fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +// DeleteAlertNotificationUID deletes the specified alert notification channel. |
| 146 | +// Reflects DELETE /api/alert-notifications/uid/:uid API call. |
| 147 | +func (c *Client) DeleteAlertNotificationUID(uid string) error { |
| 148 | + var ( |
| 149 | + raw []byte |
| 150 | + code int |
| 151 | + err error |
| 152 | + ) |
| 153 | + if raw, code, err = c.delete(fmt.Sprintf("api/alert-notifications/uid/%s", uid)); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + if code != 200 { |
| 157 | + return fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 158 | + } |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +// DeleteAlertNotificationID deletes the specified alert notification channel. |
| 163 | +// Reflects DELETE /api/alert-notifications/:id API call. |
| 164 | +func (c *Client) DeleteAlertNotificationID(id uint) error { |
| 165 | + var ( |
| 166 | + raw []byte |
| 167 | + code int |
| 168 | + err error |
| 169 | + ) |
| 170 | + if raw, code, err = c.delete(fmt.Sprintf("api/alert-notifications/%d", id)); err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + if code != 200 { |
| 174 | + return fmt.Errorf("HTTP error %d: returns %s", code, raw) |
| 175 | + } |
| 176 | + return nil |
| 177 | +} |
0 commit comments