| 1 | package alert |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/CenturyLinkCloud/clc-sdk/api" |
| 7 | ) |
| 8 | |
| 9 | func New(client api.HTTP) *Service { |
| 10 | return &Service{ |
| 11 | client: client, |
| 12 | config: client.Config(), |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | type Service struct { |
| 17 | client api.HTTP |
| 18 | config *api.Config |
| 19 | } |
| 20 | |
| 21 | func (s *Service) Get(id string) (*Alert, error) { |
| 22 | url := fmt.Sprintf("%s/alertPolicies/%s/%s", s.config.BaseURL, s.config.Alias, id) |
| 23 | resp := &Alert{} |
| 24 | err := s.client.Get(url, resp) |
| 25 | return resp, err |
| 26 | } |
| 27 | |
| 28 | func (s *Service) GetAll() (*Alerts, error) { |
| 29 | url := fmt.Sprintf("%s/alertPolicies/%s", s.config.BaseURL, s.config.Alias) |
| 30 | resp := &Alerts{} |
| 31 | err := s.client.Get(url, resp) |
| 32 | return resp, err |
| 33 | } |
| 34 | |
| 35 | func (s *Service) Create(alert Alert) (*Alert, error) { |
| 36 | url := fmt.Sprintf("%s/alertPolicies/%s", s.config.BaseURL, s.config.Alias) |
| 37 | resp := &Alert{} |
| 38 | err := s.client.Post(url, alert, resp) |
| 39 | return resp, err |
| 40 | } |
| 41 | |
| 42 | func (s *Service) Update(id string, alert Alert) (*Alert, error) { |
| 43 | url := fmt.Sprintf("%s/alertPolicies/%s/%s", s.config.BaseURL, s.config.Alias, id) |
| 44 | resp := &Alert{} |
| 45 | err := s.client.Put(url, alert, resp) |
| 46 | return resp, err |
| 47 | } |
| 48 | |
| 49 | func (s *Service) Delete(id string) error { |
| 50 | url := fmt.Sprintf("%s/alertPolicies/%s/%s", s.config.BaseURL, s.config.Alias, id) |
| 51 | return s.client.Delete(url, nil) |
| 52 | } |
| 53 | |
| 54 | type Alerts struct { |
| 55 | Items []Alert `json:"items"` |
| 56 | Links api.Links `json:"links"` |
| 57 | } |
| 58 | |
| 59 | type Alert struct { |
| 60 | ID string `json:"id,omitempty"` |
| 61 | Name string `json:"name,omitempty"` |
| 62 | Actions []Action `json:"actions,omitempty"` |
| 63 | Triggers []Trigger `json:"triggers,omitempty"` |
| 64 | Links api.Links `json:"links,omitempty"` |
| 65 | } |
| 66 | |
| 67 | type Action struct { |
| 68 | Action string `json:"action"` |
| 69 | Setting Setting `json:"settings"` |
| 70 | } |
| 71 | |
| 72 | type Setting struct { |
| 73 | Recipients []string `json:"recipients"` |
| 74 | } |
| 75 | |
| 76 | type Trigger struct { |
| 77 | Metric string `json:"metric"` |
| 78 | Duration string `json:"duration"` |
| 79 | Threshold float64 `json:"threshold"` |
| 80 | } |