-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
114 lines (90 loc) · 2.43 KB
/
redis.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
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/jhunt/vcaptive"
"github.com/starkandwayne/shield-cf-demo/internal/rand"
)
const RedisVerificationKey = "redis-verification-key"
const RedisDataKey = "dat"
type RedisSystem struct {
redis redis.Conn
}
func (s *RedisSystem) Configure(services vcaptive.Services) (bool, error) {
svc, found := services.Tagged("redis")
if !found {
return false, nil
}
host, ok := svc.GetString("host")
if !ok {
return true, fmt.Errorf("VCAP_SERVICES: '%s' service has no 'host' credential", svc.Label)
}
port, ok := svc.GetUint("port")
if !ok {
return true, fmt.Errorf("VCAP_SERVICES: '%s' service has no 'port' credential", svc.Label)
}
password, ok := svc.GetString("password")
if !ok {
return true, fmt.Errorf("VCAP_SERVICES: '%s' service has no 'password' credential", svc.Label)
}
conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", host, port), redis.DialPassword(password))
if err != nil {
return true, err
}
s.redis = conn
return true, nil
}
func (s *RedisSystem) Setup() error {
if exists, _ := redis.Bool(s.redis.Do("EXISTS", RedisVerificationKey)); exists {
return nil
}
if _, err := s.redis.Do("SET", RedisVerificationKey, rand.VerificationKey()); err != nil {
return err
}
for i := 0; i < rand.Bound(2048, 128); i++ {
if _, err := s.redis.Do("RPUSH", RedisDataKey, fmt.Sprintf("%x", i)); err != nil {
return err
}
}
return nil
}
func (s *RedisSystem) Teardown() error {
if _, err := s.redis.Do("DEL", RedisDataKey); err != nil {
return err
}
if _, err := s.redis.Do("DEL", RedisVerificationKey); err != nil {
return err
}
return nil
}
func (s *RedisSystem) Summarize() Data {
dat := Data{
System: "Redis",
Summary: " *no data found*\n",
Verification: "UNKNOWN",
OK: false,
}
if exists, _ := redis.Bool(s.redis.Do("EXISTS", RedisVerificationKey)); !exists {
return dat
}
n, err := redis.Int(s.redis.Do("LLEN", RedisDataKey))
if err != nil {
dat.Summary = fmt.Sprintf("ERROR: *%s*\n", err)
return dat
}
vfy, err := redis.String(s.redis.Do("GET", RedisVerificationKey))
if err != nil {
dat.Summary = fmt.Sprintf("ERROR: *%s*\n", err)
return dat
}
dat.Verification = vfy
dat.Summary = fmt.Sprintf("Stored Keys: *%d*\n", n)
dat.OK = true
return dat
}
func init() {
if Systems == nil {
Systems = make(map[string]System)
}
Systems["redis"] = &RedisSystem{}
}