forked from tencentyun/tsf-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
105 lines (96 loc) · 2.72 KB
/
rule.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
package router
import (
"strings"
"github.com/hisonsoft/tsf-go/pkg/meta"
"github.com/hisonsoft/tsf-go/pkg/sys/tag"
)
type RuleGroup struct {
RouteId string `yaml:"routeId"`
RouteName string `yaml:"routeName"`
RouteDesc string `yaml:"routeDesc"`
MicroserivceId string `yaml:"microserivceId"`
RuleList []Rule `yaml:"ruleList"`
NamespaceId string `yaml:"namespaceId"`
MicroserviceName string `yaml:"microserviceName"`
FallbackStatus bool `yaml:"fallbackStatus"`
}
type Rule struct {
RouteRuleId string `yaml:"routeRuleId"`
RouteId string `yaml:"routeId"`
TagList []TagRule `yaml:"tagList"`
DestList []Dest `yaml:"destList"`
}
type Dest struct {
DestId string `yaml:"destId"`
DestWeight int64 `yaml:"destWeight"`
DestItemList []DestItem `yaml:"destItemList"`
RouteRuleId string `yaml:"routeRuleId"`
}
type DestItem struct {
RouteDestItemId string `yaml:"routeDestItemId"`
RouteDestId string `yaml:"routeDestId"`
DestItemField string `yaml:"destItemField"`
DestItemValue string `yaml:"destItemValue"`
}
type TagRule struct {
TagID string `yaml:"tagID"`
TagType string `yaml:"tagType"`
TagField string `yaml:"tagField"`
TagOperator string `yaml:"tagOperator"`
TagValue string `yaml:"tagValue"`
RouteRuleId string `yaml:"routeRuleId"`
}
func (rule Rule) toCommonTagRule() tag.Rule {
tagRule := tag.Rule{
Expression: tag.AND,
}
for _, routeTag := range rule.TagList {
var t tag.Tag
field := routeTag.TagField
if routeTag.TagType != "U" {
switch field {
case "source.application.id":
field = meta.ApplicationID
case "source.group.id":
field = meta.GroupID
case "source.connection.ip":
field = meta.ConnnectionIP
case "source.application.version":
field = meta.ApplicationVersion
case "source.service.name":
field = meta.ServiceName
case "destination.interface":
field = "destination.interface"
case "request.http.method":
field = "request.http.method"
case "source.namespace.service.name":
values := strings.SplitN(routeTag.TagValue, "/", 2)
if len(values) != 2 {
continue
}
t.Field = meta.Namespace
t.Operator = routeTag.TagOperator
t.Type = tag.TypeSys
t.Value = values[0]
tagRule.Tags = append(tagRule.Tags, t)
t.Field = meta.ServiceName
t.Operator = routeTag.TagOperator
t.Type = tag.TypeSys
t.Value = values[1]
tagRule.Tags = append(tagRule.Tags, t)
continue
default:
}
}
t.Field = field
t.Operator = routeTag.TagOperator
if routeTag.TagType == "U" {
t.Type = tag.TypeUser
} else {
t.Type = tag.TypeSys
}
t.Value = routeTag.TagValue
tagRule.Tags = append(tagRule.Tags, t)
}
return tagRule
}