|
| 1 | +package consulClient |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/hashicorp/consul/api" |
| 9 | + "github.com/netplugin.orig/core" |
| 10 | + |
| 11 | + log "github.com/Sirupsen/logrus" |
| 12 | + "github.com/contiv/objdb" |
| 13 | +) |
| 14 | + |
| 15 | +type ConsulPlugin struct { |
| 16 | + client *api.Client // consul client |
| 17 | + consulConfig api.Config |
| 18 | + |
| 19 | + mutex *sync.Mutex |
| 20 | +} |
| 21 | + |
| 22 | +var consulPlugin = &ConsulPlugin{mutex: new(sync.Mutex)} |
| 23 | + |
| 24 | +// InitPlugin Register the plugin |
| 25 | +func InitPlugin() { |
| 26 | + objdb.RegisterPlugin("consul", consulPlugin) |
| 27 | +} |
| 28 | + |
| 29 | +// Init initializes the consul client |
| 30 | +func (self *ConsulPlugin) Init(machines []string) error { |
| 31 | + // default consul config |
| 32 | + self.consulConfig = api.Config{Address: "127.0.0.1:8500"} |
| 33 | + |
| 34 | + // Init consul client |
| 35 | + client, err := api.NewClient(&self.consulConfig) |
| 36 | + if err != nil { |
| 37 | + log.Fatalf("Error initializing consul client") |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + self.client = client |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func processKey(inKey string) string { |
| 47 | + //consul doesn't accepts keys starting with a '/', so trim the leading slash |
| 48 | + return strings.TrimPrefix(inKey, "/") |
| 49 | +} |
| 50 | + |
| 51 | +func (self *ConsulPlugin) GetObj(key string, retVal interface{}) error { |
| 52 | + key = processKey("/contiv.io/obj/" + processKey(key)) |
| 53 | + |
| 54 | + resp, _, err := self.client.KV().Get(key, nil) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + // Consul returns success and a nil kv when a key is not found, |
| 59 | + // translate it to 'Key not found' error |
| 60 | + if resp == nil { |
| 61 | + return core.Errorf("Key not found") |
| 62 | + } |
| 63 | + |
| 64 | + // Parse JSON response |
| 65 | + if err := json.Unmarshal([]byte(resp.Value), retVal); err != nil { |
| 66 | + log.Errorf("Error parsing object %s, Err %v", resp.Value, err) |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// ListDir returns a list of keys in a directory |
| 74 | +func (self *ConsulPlugin) ListDir(key string) ([]string, error) { |
| 75 | + key = processKey("/contiv.io/obj/" + processKey(key)) |
| 76 | + |
| 77 | + kvs, _, err := self.client.KV().List(key, nil) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + // Consul returns success and a nil kv when a key is not found, |
| 82 | + // translate it to 'Key not found' error |
| 83 | + if kvs == nil { |
| 84 | + return nil, core.Errorf("Key not found") |
| 85 | + } |
| 86 | + |
| 87 | + var keys []string |
| 88 | + for _, kv := range kvs { |
| 89 | + keys = append(keys, kv.Key) |
| 90 | + } |
| 91 | + |
| 92 | + return keys, nil |
| 93 | +} |
| 94 | + |
| 95 | +// SetObj writes an object |
| 96 | +func (self *ConsulPlugin) SetObj(key string, value interface{}) error { |
| 97 | + key = processKey("/contiv.io/obj/" + processKey(key)) |
| 98 | + |
| 99 | + // JSON format the object |
| 100 | + jsonVal, err := json.Marshal(value) |
| 101 | + if err != nil { |
| 102 | + log.Errorf("Json conversion error. Err %v", err) |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + _, err = self.client.KV().Put(&api.KVPair{Key: key, Value: jsonVal}, nil) |
| 107 | + |
| 108 | + return err |
| 109 | +} |
| 110 | + |
| 111 | +// DelObj deletes an object |
| 112 | +func (self *ConsulPlugin) DelObj(key string) error { |
| 113 | + key = processKey("/contiv.io/obj/" + processKey(key)) |
| 114 | + _, err := self.client.KV().Delete(key, nil) |
| 115 | + return err |
| 116 | +} |
| 117 | + |
| 118 | +func (self *ConsulPlugin) GetLocalAddr() (string, error) { |
| 119 | + return "", nil |
| 120 | +} |
| 121 | +func (self *ConsulPlugin) NewLock(name string, myId string, ttl uint64) (objdb.LockInterface, error) { |
| 122 | + return nil, nil |
| 123 | +} |
| 124 | +func (self *ConsulPlugin) RegisterService(serviceInfo objdb.ServiceInfo) error { |
| 125 | + return nil |
| 126 | +} |
| 127 | +func (self *ConsulPlugin) GetService(name string) ([]objdb.ServiceInfo, error) { |
| 128 | + return nil, nil |
| 129 | +} |
| 130 | +func (self *ConsulPlugin) WatchService(name string, eventCh chan objdb.WatchServiceEvent, stopCh chan bool) error { |
| 131 | + return nil |
| 132 | +} |
| 133 | +func (self *ConsulPlugin) DeregisterService(serviceInfo objdb.ServiceInfo) error { |
| 134 | + return nil |
| 135 | +} |
0 commit comments