Skip to content

Commit bfb55c9

Browse files
author
weilong.pwl
committed
add alert and resource api
1 parent e084cf3 commit bfb55c9

10 files changed

+1186
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

client_alert.go

+123-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,38 @@ const (
3030
NotificationTypeMessageCenter = "MessageCenter"
3131
)
3232

33+
const (
34+
CountConditionKey = "__count__"
35+
)
36+
37+
type Severity int
38+
39+
const (
40+
Report Severity = 2
41+
Low Severity = 4
42+
Medium Severity = 6
43+
High Severity = 8
44+
Critical Severity = 10
45+
)
46+
47+
const (
48+
JoinTypeCross = "cross_join"
49+
JoinTypeInner = "inner_join"
50+
JoinTypeLeft = "left_join"
51+
JoinTypeRight = "right_join"
52+
JoinTypeFull = "full_join"
53+
JoinTypeLeftExclude = "left_exclude"
54+
JoinTypeRightExclude = "right_exclude"
55+
JoinTypeConcat = "concat"
56+
JoinTypeNo = "no_join"
57+
)
58+
59+
const (
60+
GroupTypeNoGroup = "no_group"
61+
GroupTypeLabelsAuto = "labels_auto"
62+
GroupTypeCustom = "custom"
63+
)
64+
3365
const (
3466
ScheduleTypeFixedRate = "FixedRate"
3567
ScheduleTypeHourly = "Hourly"
@@ -40,6 +72,63 @@ const (
4072
ScheduleTypeResident = "Resident"
4173
)
4274

75+
const (
76+
StoreTypeLog = "log"
77+
StoreTypeMetric = "metric"
78+
StoreTypeMeta = "meta"
79+
)
80+
81+
// SeverityConfiguration severity config by group
82+
type SeverityConfiguration struct {
83+
Severity Severity `json:"severity"`
84+
EvalCondition ConditionConfiguration `json:"evalCondition"`
85+
}
86+
87+
type ConditionConfiguration struct {
88+
Condition string `json:"condition"`
89+
CountCondition string `json:"countCondition"`
90+
}
91+
92+
type JoinConfiguration struct {
93+
Type string `json:"type"`
94+
Condition string `json:"condition"`
95+
}
96+
97+
type GroupConfiguration struct {
98+
Type string `json:"type"`
99+
Fields []string `json:"fields"`
100+
}
101+
102+
type Tag struct {
103+
Key string `json:"key"`
104+
Value string `json:"value"`
105+
}
106+
107+
type Token struct {
108+
Name string `json:"name"`
109+
DisplayName string `json:"display_name"`
110+
Required bool `json:"required"`
111+
Type string `json:"type"`
112+
Default string `json:"default"`
113+
Hide bool `json:"hide"`
114+
}
115+
116+
type TemplateConfiguration struct {
117+
Id string `json:"id"`
118+
Type string `json:"type"`
119+
Version string `json:"version"`
120+
Lang string `json:"lang"`
121+
Tokens map[string]string `json:"tokens"`
122+
Annotations map[string]string `json:"annotations"`
123+
}
124+
125+
type PolicyConfiguration struct {
126+
UseDefault bool `json:"useDefault"`
127+
RepeatInterval string `json:"repeatInterval"`
128+
AlertPolicyId string `json:"alertPolicyId"`
129+
ActionPolicyId string `json:"actionPolicyId"`
130+
}
131+
43132
type Alert struct {
44133
Name string `json:"name"`
45134
DisplayName string `json:"displayName"`
@@ -66,23 +155,20 @@ func (alert *Alert) MarshalJSON() ([]byte, error) {
66155
return json.Marshal(body)
67156
}
68157

69-
type AlertConfiguration struct {
70-
Condition string `json:"condition"`
71-
Dashboard string `json:"dashboard"`
72-
QueryList []*AlertQuery `json:"queryList"`
73-
MuteUntil int64 `json:"muteUntil"`
74-
NotificationList []*Notification `json:"notificationList"`
75-
NotifyThreshold int32 `json:"notifyThreshold"`
76-
Throttling string `json:"throttling"`
77-
}
78-
79158
type AlertQuery struct {
80159
ChartTitle string `json:"chartTitle"`
81160
LogStore string `json:"logStore"`
82161
Query string `json:"query"`
83162
TimeSpanType string `json:"timeSpanType"`
84163
Start string `json:"start"`
85164
End string `json:"end"`
165+
166+
StoreType string `json:"storeType"`
167+
Project string `json:"project"`
168+
Store string `json:"store"`
169+
Region string `json:"region"`
170+
RoleArn string `json:"roleArn"`
171+
DashboardId string `json:"dashboardId"`
86172
}
87173

88174
type Notification struct {
@@ -104,6 +190,33 @@ type Schedule struct {
104190
Hour int32 `json:"hour"`
105191
}
106192

193+
type AlertConfiguration struct {
194+
Condition string `json:"condition"`
195+
MuteUntil int64 `json:"muteUntil"`
196+
NotificationList []*Notification `json:"notificationList"`
197+
NotifyThreshold int32 `json:"notifyThreshold"`
198+
Throttling string `json:"throttling"`
199+
200+
Version string `json:"version"`
201+
Type string `json:"type"`
202+
TemplateConfiguration *TemplateConfiguration `json:"templateConfiguration"`
203+
204+
Dashboard string `json:"dashboard"`
205+
Threshold int `json:"threshold"`
206+
NoDataFire bool `json:"noDataFire"`
207+
NoDataSeverity Severity `json:"noDataSeverity"`
208+
SendResolved bool `json:"sendResolved"`
209+
QueryList []*AlertQuery `json:"queryList"`
210+
Annotations []*Tag `json:"annotations"`
211+
Labels []*Tag `json:"labels"`
212+
SeverityConfigurations []*SeverityConfiguration `json:"severityConfigurations"`
213+
214+
JoinConfigurations []*JoinConfiguration `json:"joinConfigurations"`
215+
GroupConfiguration GroupConfiguration `json:"groupConfiguration"`
216+
217+
PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
218+
}
219+
107220
func (c *Client) CreateSavedSearch(project string, savedSearch *SavedSearch) error {
108221
body, err := json.Marshal(savedSearch)
109222
if err != nil {

client_alert_resource.go

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package sls
2+
3+
import "encoding/json"
4+
5+
const (
6+
ResourceNameAlertPolicy = "sls.alert.alert_policy"
7+
ResourceNameActionPolicy = "sls.alert.action_policy"
8+
ResourceNameUser = "sls.common.user"
9+
ResourceNameUserGroup = "sls.common.user_group"
10+
ResourceNameContentTemplate = "sls.alert.content_template"
11+
ResourceNameGlobalConfig = "sls.alert.global_config"
12+
ResourceNameWebhookIntegration = "sls.alert.webhook_application"
13+
)
14+
15+
type (
16+
// Notified users.
17+
ResourceUser struct {
18+
UserId string `json:"user_id"`
19+
UserName string `json:"user_name"`
20+
Enabled bool `json:"enabled"`
21+
CountryCode string `json:"country_code"`
22+
Phone string `json:"phone"`
23+
Email []string `json:"email"`
24+
SmsEnabled bool `json:"sms_enabled"`
25+
VoiceEnabled bool `json:"voice_enabled"`
26+
}
27+
28+
// ResourceUserGroup is a collection of users.
29+
ResourceUserGroup struct {
30+
Id string `json:"user_group_id"`
31+
Name string `json:"user_group_name"`
32+
Enabled bool `json:"enabled"`
33+
Members []string `json:"members"`
34+
}
35+
36+
// ResourceAlertPolicy defines how alerts should be grouped, inhibited and silenced.
37+
ResourceAlertPolicy struct {
38+
PolicyId string `json:"policy_id"`
39+
PolicyName string `json:"policy_name"`
40+
Parent string `json:"parent_id"`
41+
IsDefault bool `json:"is_default"`
42+
GroupPolicy string `json:"group_script"`
43+
InhibitPolicy string `json:"inhibit_script"`
44+
SilencePolicy string `json:"silence_script"`
45+
}
46+
47+
// ResourceActionPolicy defines how to send alert notifications.
48+
ResourceActionPolicy struct {
49+
ActionPolicyId string `json:"action_policy_id"`
50+
ActionPolicyName string `json:"action_policy_name"`
51+
IsDefault bool `json:"is_default"`
52+
PrimaryPolicyScript string `json:"primary_policy_script"`
53+
SecondaryPolicyScript string `json:"secondary_policy_script"`
54+
EscalationStartEnabled bool `json:"escalation_start_enabled"`
55+
EscalationStartTimeout string `json:"escalation_start_timeout"`
56+
EscalationInprogressEnabled bool `json:"escalation_inprogress_enabled"`
57+
EscalationInprogressTimeout string `json:"escalation_inprogress_timeout"`
58+
EscalationEnabled bool `json:"escalation_enabled"`
59+
EscalationTimeout string `json:"escalation_timeout"`
60+
Labels map[string]string `json:"labels"`
61+
}
62+
63+
// ContentTemplate
64+
ResourceTemplate struct {
65+
Content string `json:"content"`
66+
Locale string `json:"locale"`
67+
Title string `json:"title"`
68+
Subject string `json:"subject"`
69+
SendType string `json:"send_type"`
70+
Limit int `json:"limit"`
71+
}
72+
73+
ResourceTemplates struct {
74+
Sms ResourceTemplate `json:"sms"`
75+
Voice ResourceTemplate `json:"voice"`
76+
Email ResourceTemplate `json:"email"`
77+
Dingtalk ResourceTemplate `json:"dingtalk"`
78+
Webhook ResourceTemplate `json:"webhook"`
79+
MessageCenter ResourceTemplate `json:"message_center"`
80+
Wechat ResourceTemplate `json:"wechat"`
81+
Lark ResourceTemplate `json:"lark"`
82+
Slack ResourceTemplate `json:"slack"`
83+
}
84+
ResourceContentTemplate struct {
85+
TemplateId string `json:"template_id"`
86+
TemplateName string `json:"template_name"`
87+
IsDefault bool `json:"is_default"`
88+
Templates ResourceTemplates `json:"templates"`
89+
}
90+
91+
// WebhookIntegration is a wrap of webhook notification config.
92+
ResourceWebhookHeader struct {
93+
Key string `json:"key"`
94+
Value string `json:"value"`
95+
}
96+
WebhookIntegration struct {
97+
Id string `json:"id"`
98+
Name string `json:"name"`
99+
Method string `json:"method"`
100+
Url string `json:"url"`
101+
Type string `json:"type"`
102+
Headers []*ResourceWebhookHeader `json:"headers"`
103+
}
104+
105+
// GlobalConfig is the global configuration for alerts.
106+
GlobalConfig struct {
107+
ConfigId string `json:"config_id"`
108+
ConfigName string `json:"config_name"`
109+
ConfigDetail struct {
110+
AlertCenterLog struct {
111+
Region string `json:"region"`
112+
} `json:"alert_center_log"`
113+
} `json:"config_detail"`
114+
}
115+
)
116+
117+
func JsonMarshal(v interface{}) string {
118+
vBytes, _ := json.Marshal(v)
119+
return string(vBytes)
120+
}

0 commit comments

Comments
 (0)