-
Notifications
You must be signed in to change notification settings - Fork 375
/
monitor_orgs_alert_create.go
112 lines (106 loc) · 3.06 KB
/
monitor_orgs_alert_create.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package monitor
import (
"fmt"
"strconv"
"strings"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/openapi/api/apis"
"github.com/erda-project/erda/modules/openapi/api/spec"
)
var MONITOR_ORGS_ALERT_CREATE = apis.ApiSpec{
Path: "/api/orgCenter/alerts",
BackendPath: "/api/orgs/alerts",
Host: "monitor.marathon.l4lb.thisdcos.directory:7096",
Scheme: "http",
Method: "POST",
CheckLogin: true,
CheckToken: true,
Doc: "summary: 创建企业告警",
Audit: auditCreateOrgAlert(apistructs.CreateOrgAlert),
}
func auditCreateOrgAlert(tmp apistructs.TemplateName) func(ctx *spec.AuditContext) error {
return func(ctx *spec.AuditContext) error {
var requestBody struct {
Name string `json:"name"`
}
if err := ctx.BindRequestData(&requestBody); err != nil {
return err
}
org, err := ctx.Bundle.GetOrg(ctx.OrgID)
if err != nil {
return err
}
return ctx.CreateAudit(&apistructs.Audit{
ScopeType: apistructs.OrgScope,
ScopeID: uint64(ctx.OrgID),
TemplateName: tmp,
Context: map[string]interface{}{
"alertName": requestBody.Name,
"orgName": org.Name,
},
})
}
}
func auditOperateOrgAlert(tmp apistructs.TemplateName, act string) func(ctx *spec.AuditContext) error {
return func(ctx *spec.AuditContext) error {
action := act
if action == "" {
enable := strings.ToLower(ctx.Request.URL.Query().Get("enable"))
if enable == "true" {
action = "enabled"
} else if enable == "false" {
action = "disabled"
}
}
org, err := ctx.Bundle.GetOrg(ctx.OrgID)
if err != nil {
return err
}
if org == nil {
return nil
}
id, err := strconv.ParseInt(ctx.UrlParams["id"], 10, 64)
if err != nil {
return err
}
name := ctx.UrlParams["id"]
if tmp == apistructs.DeleteOrgAlert {
var respBody struct {
apistructs.Header
Data map[string]interface{} `json:"data"`
}
err := ctx.BindResponseData(&respBody)
if err == nil && respBody.Data != nil && respBody.Data["name"] != nil {
name = fmt.Sprint(respBody.Data["name"])
}
} else {
alert, err := ctx.Bundle.GetMonitorAlertByID(id)
if err == nil && alert != nil {
name = alert.Name
}
}
return ctx.CreateAudit(&apistructs.Audit{
ScopeType: apistructs.OrgScope,
ScopeID: uint64(ctx.OrgID),
TemplateName: tmp,
Context: map[string]interface{}{
"alertID": id,
"alertName": name,
"orgName": org.Name,
"action": action,
},
})
}
}