This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
forked from cloudflare/unsee
/
silences.go
88 lines (76 loc) · 2.26 KB
/
silences.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
// Package v05 package implements support for interacting with Alertmanager 0.5
// Collected data will be mapped to unsee internal schema defined the
// unsee/models package
// This file defines Alertmanager alerts mapping
package v05
import (
"encoding/json"
"errors"
"io"
"time"
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/uri"
)
type silence struct {
ID string `json:"id"`
Matchers []struct {
Name string `json:"name"`
Value string `json:"value"`
IsRegex bool `json:"isRegex"`
} `json:"matchers"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
CreatedAt time.Time `json:"createdAt"`
CreatedBy string `json:"createdBy"`
Comment string `json:"comment"`
}
type silenceAPISchema struct {
Status string `json:"status"`
Data []silence `json:"data"`
Error string `json:"error"`
}
// SilenceMapper implements Alertmanager 0.4 API schema
type SilenceMapper struct {
mapper.SilenceMapper
}
// AbsoluteURL for silences API endpoint this mapper supports
func (m SilenceMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/silences")
}
// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m SilenceMapper) QueryArgs() string {
return ""
}
// IsSupported returns true if given version string is supported
func (m SilenceMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.5.0")
return versionRange(semver.MustParse(version))
}
// Decode Alertmanager API response body and return unsee model instances
func (m SilenceMapper) Decode(source io.ReadCloser) ([]models.Silence, error) {
silences := []models.Silence{}
resp := silenceAPISchema{}
defer source.Close()
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return silences, err
}
if resp.Status != "success" {
return silences, errors.New(resp.Error)
}
for _, s := range resp.Data {
us := models.Silence{
ID: s.ID,
Matchers: s.Matchers,
StartsAt: s.StartsAt,
EndsAt: s.EndsAt,
CreatedAt: s.CreatedAt,
CreatedBy: s.CreatedBy,
Comment: s.Comment,
}
silences = append(silences, us)
}
return silences, nil
}