forked from koding/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kites.go
59 lines (48 loc) · 1.31 KB
/
kites.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
package kontrol
import (
"math/rand"
"strings"
"github.com/hashicorp/go-version"
"github.com/koding/kite/protocol"
)
// Kites is a helpe type to work with a set of kites
type Kites []*protocol.KiteWithToken
// Attach attaches the given token to each kite. It replaces any previous
// token.
func (k Kites) Attach(token string) {
for _, kite := range k {
kite.Token = token
}
}
// Shuffle shuffles the order of the kites. This is usefull if you want send
// back a randomized list of kites.
func (k Kites) Shuffle() {
shuffled := make(Kites, len(k))
for i, v := range rand.Perm(len(k)) {
shuffled[v] = k[i]
}
k = shuffled
}
// Filter filters out kites with the given constraints
func (k Kites) Filter(constraint version.Constraints, keyRest string) {
filtered := make(Kites, 0)
for _, kite := range k {
if isValid(&kite.Kite, constraint, keyRest) {
filtered = append(filtered, kite)
}
}
k = filtered
}
func isValid(k *protocol.Kite, c version.Constraints, keyRest string) bool {
// Check the version constraint.
v, _ := version.NewVersion(k.Version)
if !c.Check(v) {
return false
}
// Check the fields after version field.
kiteKeyAfterVersion := "/" + strings.TrimRight(k.Region+"/"+k.Hostname+"/"+k.ID, "/")
if !strings.HasPrefix(kiteKeyAfterVersion, keyRest) {
return false
}
return true
}