./dkv
}
-if [ "$CONSUL_IP" = "localhost" ]; then
- start_consul_server
- sleep 5
+if [ "$DATASTORE_IP" = "localhost" ]; then
+ if [ "$DATASTORE" = "consul" ]; then
+ start_consul_server
+ sleep 5
+ fi
fi
start_api_server
#!/bin/bash
-CONSUL_IP="localhost"
+DATASTORE="consul"
+DATASTORE_IP="localhost"
+
MOUNTPATH="/dkv_mount_path/configs/"
DEFAULT_CONFIGS=$(pwd)/../mountpath/default # TODO(sshank): Change this to think from Kubernetes Volumes perspective.
-docker run -e CONSUL_IP=$CONSUL_IP -e MOUNTPATH=$MOUNTPATH -it \
+docker run -e DATASTORE=$DATASTORE -e DATASTORE_IP=$DATASTORE_IP -e MOUNTPATH=$MOUNTPATH -it \
--name dkv \
-v $DEFAULT_CONFIGS:/dkv_mount_path/configs/default \
-p 8200:8200 -p 8080:8080 nexus3.onap.org:10003/onap/music/distributed-kv-store
+++ /dev/null
-#!/bin/bash
-
-function create_mountpath {
- cp -r mountpath/ /configs
-}
-
-create_mountpath
--- /dev/null
+/*
+ * Copyright 2018 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+// (TODO)sahank: Complete MUSIC Cassandra Connections.
+
+type CassandraStruct struct{}
+
+func (c *CassandraStruct) InitializeDatastoreClient() error {
+ return nil
+}
+
+func (c *CassandraStruct) CheckDatastoreHealth() error {
+ return nil
+}
+
+func (c *CassandraStruct) RequestPUT(key string, value string) error {
+ return nil
+}
+
+func (c *CassandraStruct) RequestGET(key string) (string, error) {
+ return "", nil
+}
+
+func (c *CassandraStruct) RequestGETS() ([]string, error) {
+ return []string{"", ""}, nil
+}
+
+func (c *CassandraStruct) RequestDELETE(key string) error {
+ return nil
+}
"os"
)
-// Interface to have all signature methods.
-type ConsulRequester interface {
- InitializeConsulClient() error
- CheckConsulHealth() error
- RequestPUT(string, string) error
- RequestGET(string) (string, error)
- RequestGETS() ([]string, error)
- RequestDELETE(string) error
-}
-
type ConsulStruct struct {
consulClient *consulapi.Client
}
-/*
-This var is an interface used to initialize ConsulStruct when the who API is brought up. This is done this way so
-that a fake Consul can be created which satisfies the interface and we can use that fake Consul in unit testing.
-*/
-var Consul ConsulRequester
-
-/*
-The following functions seems like they are not used. But since they are following the ConsulRequest interface,
-they can be visible to any Struct which is initiated using the ConsulRequest. This is done for this project in
-the initialise.go file where we are creating a ConsulStruct and assigning it to Consul var which is declared
-above.
-*/
-func (c *ConsulStruct) InitializeConsulClient() error {
- if os.Getenv("CONSUL_IP") == "" {
- return errors.New("CONSUL_IP environment variable not set.")
+func (c *ConsulStruct) InitializeDatastoreClient() error {
+ if os.Getenv("DATASTORE_IP") == "" {
+ return errors.New("DATASTORE_IP environment variable not set.")
}
config := consulapi.DefaultConfig()
- config.Address = os.Getenv("CONSUL_IP") + ":8500"
+ config.Address = os.Getenv("DATASTORE_IP") + ":8500"
client, err := consulapi.NewClient(config)
if err != nil {
return nil
}
-func (c *ConsulStruct) CheckConsulHealth() error {
+func (c *ConsulStruct) CheckDatastoreHealth() error {
kv := c.consulClient.KV()
_, _, err := kv.Get("test", nil)
if err != nil {
- return errors.New("[ERROR] Cannot talk to Consul. Check if it is running/reachable.")
+ return errors.New("[ERROR] Cannot talk to Datastore. Check if it is running/reachable.")
}
return nil
}
--- /dev/null
+/*
+ * Copyright 2018 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+// Interface to have Data Store signature methods.
+type DatastoreConnector interface {
+ InitializeDatastoreClient() error
+ CheckDatastoreHealth() error
+ RequestPUT(string, string) error
+ RequestGET(string) (string, error)
+ RequestGETS() ([]string, error)
+ RequestDELETE(string) error
+}
var MOUNTPATH = ""
-var Directory DirectoryOperationer
-
func (d *DirectoryStruct) CreateService(body CreateRegisterServiceBody) (string, error) {
// Having same name is prohibited?
type KeyValuesStruct struct{}
-var KeyValues KeyValuesInterface
-
func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error {
var prefix = ""
if subdomain != "" {
}
for key, value := range kvs {
key = prefix + key
- err := Consul.RequestPUT(key, value)
+ err := Datastore.RequestPUT(key, value)
if err != nil {
return err
}
ConsulStruct
}
+func (f *FakeConsul) InitializeDatastoreClient() error {
+ return nil
+}
+
+func (f *FakeConsul) CheckDatastoreHealth() error {
+ return nil
+}
+
func (f *FakeConsul) RequestGETS() ([]string, error) {
return []string{"key1", "key2"}, nil
}
ConsulStruct
}
+func (f *FakeConsulErr) InitializeDatastoreClient() error {
+ return errors.New("Internal Server Error")
+}
+
+func (f *FakeConsulErr) CheckDatastoreHealth() error {
+ return errors.New("Internal Server Error")
+}
+
func (f *FakeConsulErr) RequestGETS() ([]string, error) {
return []string{"", ""}, errors.New("Internal Server Error")
}
}
func TestHandleConfigPOST(t *testing.T) {
- oldConsul := Consul
+ oldDatastore := Datastore
oldKeyValues := KeyValues
- Consul = &FakeConsul{}
+ Datastore = &FakeConsul{}
KeyValues = &FakeKeyValues{}
defer func() {
- Consul = oldConsul
+ Datastore = oldDatastore
KeyValues = oldKeyValues
}()
}
func TestHandleConfigPOST_only_token(t *testing.T) {
- oldConsul := Consul
+ oldDatastore := Datastore
oldKeyValues := KeyValues
- Consul = &FakeConsul{}
+ Datastore = &FakeConsul{}
KeyValues = &FakeKeyValues{}
defer func() {
- Consul = oldConsul
+ Datastore = oldDatastore
KeyValues = oldKeyValues
}()
}
func TestHandleConfigPOST_no_body(t *testing.T) {
- oldConsul := Consul
+ oldDatastore := Datastore
oldKeyValues := KeyValues
- Consul = &FakeConsul{}
+ Datastore = &FakeConsul{}
KeyValues = &FakeKeyValues{}
defer func() {
- Consul = oldConsul
+ Datastore = oldDatastore
KeyValues = oldKeyValues
}()
}
func TestHandleConfigPOST_ConsulError(t *testing.T) {
- oldConsul := Consul
+ oldDatastore := Datastore
oldKeyValues := KeyValues
- Consul = &FakeConsulErr{}
+ Datastore = &FakeConsulErr{}
KeyValues = &FakeKeyValuesErr{}
defer func() {
- Consul = oldConsul
+ Datastore = oldDatastore
KeyValues = oldKeyValues
}()
}
func TestHandleDefaultConfigLoad(t *testing.T) {
- oldConsul := Consul
+ oldDatastore := Datastore
oldKeyValues := KeyValues
- Consul = &FakeConsul{}
+ Datastore = &FakeConsul{}
KeyValues = &FakeKeyValues{}
defer func() {
- Consul = oldConsul
+ Datastore = oldDatastore
KeyValues = oldKeyValues
}()
}
func TestHandleDefaultConfigLoad_err(t *testing.T) {
- oldConsul := Consul
+ oldDatastore := Datastore
oldKeyValues := KeyValues
- Consul = &FakeConsul{}
+ Datastore = &FakeConsul{}
KeyValues = &FakeKeyValuesErr{}
defer func() {
- Consul = oldConsul
+ Datastore = oldDatastore
KeyValues = oldKeyValues
}()
package api
-import "os"
+import (
+ "errors"
+ "os"
+)
+
+var (
+ Datastore DatastoreConnector
+ KeyValues KeyValuesInterface
+ Directory DirectoryOperationer
+)
func Initialise() error {
- Consul = &ConsulStruct{}
+ if os.Getenv("DATASTORE") == "" {
+ return errors.New("DATASTORE environment variable not set.")
+ }
+ if os.Getenv("DATASTORE") == "consul" {
+ Datastore = &ConsulStruct{}
+ } else if os.Getenv("DATASTORE") == "cassandra" {
+ Datastore = &CassandraStruct{}
+ }
KeyValues = &KeyValuesStruct{}
Directory = &DirectoryStruct{directory: ""}
- err := Consul.InitializeConsulClient()
+ err := Datastore.InitializeDatastoreClient()
if err != nil {
return err
}
- err = Consul.CheckConsulHealth()
+ err = Datastore.CheckDatastoreHealth()
if err != nil {
return err
}
"testing"
)
-func TestInitialise_errorIP(t *testing.T) {
- consul_ip := os.Getenv("CONSUL_IP")
- os.Unsetenv("CONSUL_IP")
- defer os.Setenv("CONSUL_IP", consul_ip)
+func TestInitialise_cassandra(t *testing.T) {
+ oldDatastore_ip := os.Getenv("DATASTORE_IP")
+ oldDatastore_type := os.Getenv("DATASTORE")
+
+ os.Setenv("DATASTORE_IP", "localhost")
+ os.Setenv("DATASTORE", "cassandra")
+
+ defer func() {
+ os.Setenv("DATASTORE_IP", oldDatastore_ip)
+ os.Setenv("DATASTORE", oldDatastore_type)
+ }()
+
+ err := Initialise()
+ assert.Nil(t, err)
+}
+func TestInitialise_consulError(t *testing.T) {
+ oldDatastore_ip := os.Getenv("DATASTORE_IP")
+ oldDatastore_type := os.Getenv("DATASTORE")
+
+ os.Setenv("DATASTORE_IP", "localhost")
+ os.Setenv("DATASTORE", "consul")
+
+ defer func() {
+ os.Setenv("DATASTORE_IP", oldDatastore_ip)
+ os.Setenv("DATASTORE", oldDatastore_type)
+ }()
err := Initialise()
assert.NotNil(t, err)
}
-func TestInitialise_errorConsul(t *testing.T) {
- // This is done this way cause the Consul interface with Fake Struct will get
- // overridden with real Struct during runtime.
- os.Setenv("CONSUL_IP", "test")
- defer os.Unsetenv("CONSUL_IP")
+func TestInitialise_datastoreEmptyError(t *testing.T) {
+ datastore := os.Getenv("DATASTORE")
+ os.Unsetenv("DATASTORE")
+ defer os.Setenv("DATASTORE", datastore)
err := Initialise()
assert.NotNil(t, err)
vars := mux.Vars(r)
key := vars["token"] + "/" + vars["key"]
- value, err := Consul.RequestGET(key)
+ value, err := Datastore.RequestGET(key)
if err != nil {
req := ResponseStringStruct{Response: string(err.Error())}
func HandleGETS(w http.ResponseWriter, r *http.Request) {
- values, err := Consul.RequestGETS()
+ values, err := Datastore.RequestGETS()
if err != nil {
req := ResponseStringStruct{Response: string(err.Error())}
vars := mux.Vars(r)
key := vars["key"]
- err := Consul.RequestDELETE(key)
+ err := Datastore.RequestDELETE(key)
if err != nil {
req := ResponseStringStruct{Response: string(err.Error())}
}
func TestHandleGETS(t *testing.T) {
- oldConsul := Consul
- Consul = &FakeConsul{}
- defer func() { Consul = oldConsul }()
+ oldDataStore := Datastore
+ Datastore = &FakeConsul{}
+ defer func() { Datastore = oldDataStore }()
request, _ := http.NewRequest("GET", "/v1/getconfigs", nil)
response := httptest.NewRecorder()
}
func TestHandleGETS_err(t *testing.T) {
- oldConsul := Consul
- Consul = &FakeConsulErr{}
- defer func() { Consul = oldConsul }()
+ oldDataStore := Datastore
+ Datastore = &FakeConsulErr{}
+ defer func() { Datastore = oldDataStore }()
request, _ := http.NewRequest("GET", "/v1/getconfigs", nil)
response := httptest.NewRecorder()
}
func TestHandleGET(t *testing.T) {
- oldConsul := Consul
- Consul = &FakeConsul{}
- defer func() { Consul = oldConsul }()
+ oldDataStore := Datastore
+ Datastore = &FakeConsul{}
+ defer func() { Datastore = oldDataStore }()
request, _ := http.NewRequest("GET", "/v1/getconfig/key1", nil)
response := httptest.NewRecorder()
}
func TestHandleGET_err(t *testing.T) {
- oldConsul := Consul
- Consul = &FakeConsulErr{}
- defer func() { Consul = oldConsul }()
+ oldDataStore := Datastore
+ Datastore = &FakeConsulErr{}
+ defer func() { Datastore = oldDataStore }()
request, _ := http.NewRequest("GET", "/v1/getconfig/key1", nil)
response := httptest.NewRecorder()
}
func TestHandleDELETE(t *testing.T) {
- oldConsul := Consul
- Consul = &FakeConsul{}
- defer func() { Consul = oldConsul }()
+ oldDataStore := Datastore
+ Datastore = &FakeConsul{}
+ defer func() { Datastore = oldDataStore }()
request, _ := http.NewRequest("DELETE", "/v1/deleteconfig/key1", nil)
response := httptest.NewRecorder()
}
func TestHandleDELETE_err(t *testing.T) {
- oldConsul := Consul
- Consul = &FakeConsulErr{}
- defer func() { Consul = oldConsul }()
+ oldDataStore := Datastore
+ Datastore = &FakeConsulErr{}
+ defer func() { Datastore = oldDataStore }()
request, _ := http.NewRequest("DELETE", "/v1/deleteconfig/key1", nil)
response := httptest.NewRecorder()