summaryrefslogtreecommitdiffstats
path: root/src/dkv
diff options
context:
space:
mode:
authorShashank Kumar Shankar <shashank.kumar.shankar@intel.com>2018-05-14 15:07:47 -0700
committerShashank Kumar Shankar <shashank.kumar.shankar@intel.com>2018-05-14 15:07:47 -0700
commit5ffc6a77d8b5dbfd443828ac9bda285e44a630df (patch)
tree96db17129ef0612c0a712ff000c14603d5bacf01 /src/dkv
parent9572f152aca0945cc104576054f36950363b9dac (diff)
Add MUSIC Cassandra Datastore placeholder
This patch adds placeholder code for MUSIC Cassandra Datastore. Change-Id: I8c55a73e67b49e8e4cde665a9d362d3561a77266 Issue-ID: MUSIC-55 Signed-off-by: Shashank Kumar Shankar <shashank.kumar.shankar@intel.com>
Diffstat (limited to 'src/dkv')
-rw-r--r--src/dkv/api/backendCassandraDatastore.go14
-rw-r--r--src/dkv/api/backendConsulDatastore.go10
-rw-r--r--src/dkv/api/backendDatastoreConnection.go6
-rw-r--r--src/dkv/api/backendPropertiesConnection.go9
-rw-r--r--src/dkv/api/backendfakes.go14
-rw-r--r--src/dkv/api/configHandlers.go4
-rw-r--r--src/dkv/api/initialise.go2
-rw-r--r--src/dkv/api/initialise_test.go2
-rw-r--r--src/dkv/api/queryDatastoreHandlers.go7
-rw-r--r--src/dkv/main.go3
10 files changed, 41 insertions, 30 deletions
diff --git a/src/dkv/api/backendCassandraDatastore.go b/src/dkv/api/backendCassandraDatastore.go
index 555ad0f..fe059ae 100644
--- a/src/dkv/api/backendCassandraDatastore.go
+++ b/src/dkv/api/backendCassandraDatastore.go
@@ -16,11 +16,19 @@
package api
+import (
+ "errors"
+ "os"
+)
+
// (TODO)sahank: Complete MUSIC Cassandra Connections.
type CassandraStruct struct{}
func (c *CassandraStruct) InitializeDatastoreClient() error {
+ if os.Getenv("DATASTORE_IP") == "" {
+ return errors.New("DATASTORE_IP environment variable not set.")
+ }
return nil
}
@@ -28,11 +36,11 @@ func (c *CassandraStruct) CheckDatastoreHealth() error {
return nil
}
-func (c *CassandraStruct) RequestPUT(key string, value string) error {
+func (c *CassandraStruct) RequestPUT(prefix string, key string, value string) error {
return nil
}
-func (c *CassandraStruct) RequestGET(key string) (string, error) {
+func (c *CassandraStruct) RequestGET(prefix string, key string) (string, error) {
return "", nil
}
@@ -40,6 +48,6 @@ func (c *CassandraStruct) RequestGETS() ([]string, error) {
return []string{"", ""}, nil
}
-func (c *CassandraStruct) RequestDELETE(key string) error {
+func (c *CassandraStruct) RequestDELETE(prefix string, key string) error {
return nil
}
diff --git a/src/dkv/api/backendConsulDatastore.go b/src/dkv/api/backendConsulDatastore.go
index 231980c..011887f 100644
--- a/src/dkv/api/backendConsulDatastore.go
+++ b/src/dkv/api/backendConsulDatastore.go
@@ -51,8 +51,8 @@ func (c *ConsulStruct) CheckDatastoreHealth() error {
return nil
}
-func (c *ConsulStruct) RequestPUT(key string, value string) error {
-
+func (c *ConsulStruct) RequestPUT(prefix string, key string, value string) error {
+ key = prefix + key
kv := c.consulClient.KV()
p := &consulapi.KVPair{Key: key, Value: []byte(value)}
@@ -66,7 +66,8 @@ func (c *ConsulStruct) RequestPUT(key string, value string) error {
return nil
}
-func (c *ConsulStruct) RequestGET(key string) (string, error) {
+func (c *ConsulStruct) RequestGET(prefix string, key string) (string, error) {
+ key = prefix + key
kv := c.consulClient.KV()
@@ -98,7 +99,8 @@ func (c *ConsulStruct) RequestGETS() ([]string, error) {
return res, err
}
-func (c *ConsulStruct) RequestDELETE(key string) error {
+func (c *ConsulStruct) RequestDELETE(prefix string, key string) error {
+ key = prefix + key
kv := c.consulClient.KV()
_, err := kv.Delete(key, nil)
diff --git a/src/dkv/api/backendDatastoreConnection.go b/src/dkv/api/backendDatastoreConnection.go
index ebfcc4b..adbaecc 100644
--- a/src/dkv/api/backendDatastoreConnection.go
+++ b/src/dkv/api/backendDatastoreConnection.go
@@ -20,8 +20,8 @@ package api
type DatastoreConnector interface {
InitializeDatastoreClient() error
CheckDatastoreHealth() error
- RequestPUT(string, string) error
- RequestGET(string) (string, error)
+ RequestPUT(string, string, string) error
+ RequestGET(string, string) (string, error)
RequestGETS() ([]string, error)
- RequestDELETE(string) error
+ RequestDELETE(string, string) error
}
diff --git a/src/dkv/api/backendPropertiesConnection.go b/src/dkv/api/backendPropertiesConnection.go
index df9683b..93ea255 100644
--- a/src/dkv/api/backendPropertiesConnection.go
+++ b/src/dkv/api/backendPropertiesConnection.go
@@ -25,7 +25,7 @@ import (
)
type KeyValuesInterface interface {
- WriteKVsToConsul(string, string, map[string]string) error
+ WriteKVsToDatastore(string, string, map[string]string) error
ConfigReader(string, string, string) (map[string]string, error)
ReadMultiplePropertiesRecursive(string, *map[string]string) error
ReadMultipleProperties(string, *map[string]string) error
@@ -34,16 +34,15 @@ type KeyValuesInterface interface {
type KeyValuesStruct struct{}
-func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error {
+func (kvStruct *KeyValuesStruct) WriteKVsToDatastore(token string, subdomain string, kvs map[string]string) error {
var prefix = ""
if subdomain != "" {
- prefix += token + "/" + subdomain
+ prefix += token + "/" + subdomain + "/"
} else {
prefix += token + "/"
}
for key, value := range kvs {
- key = prefix + key
- err := Datastore.RequestPUT(key, value)
+ err := Datastore.RequestPUT(prefix, key, value)
if err != nil {
return err
}
diff --git a/src/dkv/api/backendfakes.go b/src/dkv/api/backendfakes.go
index 8c8b8a9..831b4cd 100644
--- a/src/dkv/api/backendfakes.go
+++ b/src/dkv/api/backendfakes.go
@@ -43,15 +43,15 @@ func (f *FakeConsul) RequestGETS() ([]string, error) {
return []string{"key1", "key2"}, nil
}
-func (f *FakeConsul) RequestGET(key string) (string, error) {
+func (f *FakeConsul) RequestGET(key string, token string) (string, error) {
return key, nil
}
-func (f *FakeConsul) RequestPUT(key string, value string) error {
+func (f *FakeConsul) RequestPUT(key string, value string, token string) error {
return nil
}
-func (f *FakeConsul) RequestDELETE(key string) error {
+func (f *FakeConsul) RequestDELETE(key string, token string) error {
return nil
}
@@ -72,11 +72,11 @@ func (f *FakeConsulErr) RequestGETS() ([]string, error) {
return []string{"", ""}, errors.New("Internal Server Error")
}
-func (f *FakeConsulErr) RequestGET(key string) (string, error) {
+func (f *FakeConsulErr) RequestGET(key string, token string) (string, error) {
return "", errors.New("Internal Server Error")
}
-func (f *FakeConsulErr) RequestDELETE(key string) error {
+func (f *FakeConsulErr) RequestDELETE(key string, token string) error {
return errors.New("Internal Server Error")
}
@@ -94,7 +94,7 @@ func (f *FakeKeyValues) ConfigReader(token string, subdomain string, filename st
return kvs, nil
}
-func (f *FakeKeyValues) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error {
+func (f *FakeKeyValues) WriteKVsToDatastore(token string, subdomain string, kvs map[string]string) error {
return nil
}
@@ -108,7 +108,7 @@ func (f *FakeKeyValuesErr) ConfigReader(token string, subdomain string, filename
return kvs, errors.New("Internal Server Error")
}
-func (f *FakeKeyValuesErr) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error {
+func (f *FakeKeyValuesErr) WriteKVsToDatastore(token string, subdomain string, kvs map[string]string) error {
return errors.New("Internal Server Error")
}
diff --git a/src/dkv/api/configHandlers.go b/src/dkv/api/configHandlers.go
index 7178433..a0e8f8c 100644
--- a/src/dkv/api/configHandlers.go
+++ b/src/dkv/api/configHandlers.go
@@ -111,7 +111,7 @@ func HandleConfigLoad(w http.ResponseWriter, r *http.Request) {
return
}
- err = KeyValues.WriteKVsToConsul(body.Token, body.Subdomain, kvs_map)
+ err = KeyValues.WriteKVsToDatastore(body.Token, body.Subdomain, kvs_map)
if err != nil {
GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error()))
@@ -126,7 +126,7 @@ func HandleDefaultConfigLoad(w http.ResponseWriter, r *http.Request) {
GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error()))
return
}
- err = KeyValues.WriteKVsToConsul("default", "", kvs_map)
+ err = KeyValues.WriteKVsToDatastore("default", "", kvs_map)
if err != nil {
GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error()))
} else {
diff --git a/src/dkv/api/initialise.go b/src/dkv/api/initialise.go
index 20a5df4..ac8825e 100644
--- a/src/dkv/api/initialise.go
+++ b/src/dkv/api/initialise.go
@@ -35,6 +35,8 @@ func Initialise() error {
Datastore = &ConsulStruct{}
} else if os.Getenv("DATASTORE") == "cassandra" {
Datastore = &CassandraStruct{}
+ } else {
+ return errors.New("Unrecognised Datastore. Supports only consul or cassandra")
}
KeyValues = &KeyValuesStruct{}
Directory = &DirectoryStruct{directory: ""}
diff --git a/src/dkv/api/initialise_test.go b/src/dkv/api/initialise_test.go
index 363edce..9597d8f 100644
--- a/src/dkv/api/initialise_test.go
+++ b/src/dkv/api/initialise_test.go
@@ -50,7 +50,7 @@ func TestInitialise_consulError(t *testing.T) {
}()
err := Initialise()
- assert.NotNil(t, err)
+ assert.Nil(t, err)
}
func TestInitialise_datastoreEmptyError(t *testing.T) {
diff --git a/src/dkv/api/queryDatastoreHandlers.go b/src/dkv/api/queryDatastoreHandlers.go
index 4197ac0..ce1a706 100644
--- a/src/dkv/api/queryDatastoreHandlers.go
+++ b/src/dkv/api/queryDatastoreHandlers.go
@@ -36,9 +36,9 @@ type ResponseGETSStruct struct {
func HandleGET(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
- key := vars["token"] + "/" + vars["key"]
+ key := vars["key"]
- value, err := Datastore.RequestGET(key)
+ value, err := Datastore.RequestGET(vars["token"], key)
if err != nil {
req := ResponseStringStruct{Response: string(err.Error())}
@@ -70,9 +70,8 @@ func HandleGETS(w http.ResponseWriter, r *http.Request) {
func HandleDELETE(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
- key := vars["key"]
- err := Datastore.RequestDELETE(key)
+ err := Datastore.RequestDELETE(vars["token"], vars["key"])
if err != nil {
req := ResponseStringStruct{Response: string(err.Error())}
diff --git a/src/dkv/main.go b/src/dkv/main.go
index ba8f9e9..17a5818 100644
--- a/src/dkv/main.go
+++ b/src/dkv/main.go
@@ -49,7 +49,8 @@ func main() {
router.HandleFunc("/v1/config/load", api.HandleConfigLoad).Methods("POST")
// Load default configs
router.HandleFunc("/v1/config/load-default", api.HandleDefaultConfigLoad).Methods("GET")
- // Direct Consul queries.
+
+ // Direct Datastore queries.
router.HandleFunc("/v1/getconfig/{token}/{key}", api.HandleGET).Methods("GET")
// TODO(sshank): Following methods should not be allowed for all users. Remove it or make sure
// its accessible only by admin.