This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
criteria.go
126 lines (100 loc) · 2.34 KB
/
criteria.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package skynet
type CriteriaMatcher interface {
Matches(s ServiceInfo) bool
}
type Criteria struct {
Hosts []string
Regions []string
Instances []string
Services []ServiceCriteria
Registered *bool
}
type ServiceCriteria struct {
Name string
Version string
}
func (sc *ServiceCriteria) String() string {
if sc.Version == "" {
return sc.Name
}
return sc.Name + ":" + sc.Version
}
func (sc *ServiceCriteria) Matches(name, version string) bool {
if sc.Name != "" && sc.Name != name {
return false
}
if sc.Version != "" && sc.Version != version {
return false
}
return true
}
func (c *Criteria) Matches(s ServiceInfo) bool {
if c.Instances != nil && len(c.Instances) > 0 && !exists(c.Instances, s.UUID) {
return false
}
if c.Registered != nil && s.Registered != *c.Registered {
return false
}
// If no hosts were provided we assume any hosts match
if c.Hosts != nil && len(c.Hosts) > 0 && !exists(c.Hosts, s.ServiceAddr.IPAddress) {
return false
}
// If no regions were provided we assume any regions match
if c.Regions != nil && len(c.Regions) > 0 && !exists(c.Regions, s.Region) {
return false
}
// Check for service match
if c.Services != nil && len(c.Services) > 0 {
match := false
for _, sc := range c.Services {
if sc.Matches(s.Name, s.Version) {
match = true
break
}
}
if !match {
return false
}
}
return true
}
func (c *Criteria) AddInstance(uuid string) {
if !exists(c.Instances, uuid) {
c.Instances = append(c.Instances, uuid)
}
}
func (c *Criteria) AddHost(host string) {
if !exists(c.Hosts, host) {
c.Hosts = append(c.Hosts, host)
}
}
func (c *Criteria) AddRegion(region string) {
if !exists(c.Regions, region) {
c.Regions = append(c.Regions, region)
}
}
func (c *Criteria) AddService(service ServiceCriteria) {
for _, s := range c.Services {
if s.Name == service.Name && s.Version == service.Version {
return
}
}
c.Services = append(c.Services, service)
}
// Returns a copy of this criteria
func (c *Criteria) Clone() *Criteria {
criteria := new(Criteria)
copy(c.Hosts, criteria.Hosts)
copy(c.Regions, criteria.Regions)
copy(c.Instances, criteria.Instances)
copy(c.Services, criteria.Services)
return criteria
}
func exists(haystack []string, needle string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}