/
setup.go
97 lines (90 loc) · 2.25 KB
/
setup.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
package main
import (
"context"
"fmt"
"log"
"github.com/vissong/TCOP-Policy-CMD/tcop/entity"
)
// PolicyResult 策略初始化之后的结果
type PolicyResult struct {
Name string
ID string
URL string
}
const (
urlFormat = "https://console.cloud.tencent.com/monitor/alarm/policy/detail/%s"
)
func SetupAlarmPolicy(ctx context.Context, t *TCOP, config *Config) error {
var result []PolicyResult
for _, policy := range config.Policies {
var _policyID string
log.Printf("处理告警策略: %s \n", policy.Name)
existPolicies, err := t.SearchAlarmPolicyByName(ctx, policy.Name, true)
if err != nil {
continue
}
// 创建逻辑
if existPolicies.TotalCount == 0 {
p, err := t.CreateAlarmPolicy(
ctx, &CreateAlarmParams{
Name: policy.Name,
Remark: policy.Remark,
Namespace: policy.Namespace,
ConditionTemplateId: int64(policy.ConditionID),
NoticeIDs: policy.NoticeIDs,
Tags: policy.Tags,
},
)
if err != nil {
continue
}
log.Printf("policy create success, policy id is %s", p.PolicyId)
if err := t.BindResourcesByTag(ctx, p.PolicyId, convertTags(config.ResourceTags)); err != nil {
continue
}
_policyID = p.PolicyId
} else {
// 更新逻辑,只更新搜到的第一个策略
if err := t.BindResourcesByTag(
ctx, existPolicies.Policies[0].PolicyId,
convertTags(config.ResourceTags),
); err != nil {
continue
}
_policyID = existPolicies.Policies[0].PolicyId
}
log.Printf(
"bind resoureces to policy %s by tag success, tags is %+v",
_policyID,
config.ResourceTags,
)
result = append(
result, PolicyResult{
Name: policy.Name,
ID: _policyID,
URL: fmt.Sprintf(urlFormat, _policyID),
},
)
}
log.Printf("Policy List Is:")
for _, policyResult := range result {
log.Println("------")
log.Printf("Name: %s", policyResult.Name)
log.Printf("LINK: %s", policyResult.URL)
}
return nil
}
func convertTags(inputTags []ResourceTag) []entity.Tag {
var result []entity.Tag
for _, inputTag := range inputTags {
for _, value := range inputTag.Values {
result = append(
result, entity.Tag{
Key: inputTag.Key,
Value: value,
},
)
}
}
return result
}