diff options
Diffstat (limited to 'src/dkv/api')
-rw-r--r-- | src/dkv/api/consulConnection.go | 113 | ||||
-rw-r--r-- | src/dkv/api/consulConnection_test.go | 19 | ||||
-rw-r--r-- | src/dkv/api/endpointViews.go | 108 | ||||
-rw-r--r-- | src/dkv/api/endpointViews_test.go | 58 | ||||
-rw-r--r-- | src/dkv/api/propertiesReader.go | 73 | ||||
-rw-r--r-- | src/dkv/api/propertiesReader_test.go | 19 | ||||
-rw-r--r-- | src/dkv/api/utils.go | 101 | ||||
-rw-r--r-- | src/dkv/api/utils_test.go | 19 |
8 files changed, 510 insertions, 0 deletions
diff --git a/src/dkv/api/consulConnection.go b/src/dkv/api/consulConnection.go new file mode 100644 index 0000000..b8074e2 --- /dev/null +++ b/src/dkv/api/consulConnection.go @@ -0,0 +1,113 @@ +/* + * 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 + +import ( + "errors" + "fmt" + "github.com/hashicorp/consul/api" + "os" +) + +func (kvStruct *KeyValue) WriteKVsToConsul() error { + for key, value := range kvStruct.kv { + if os.Getenv("CONSUL_IP") == "" { + return errors.New("CONSUL_IP environment variable not set.") + } + err := requestPUT(os.Getenv("CONSUL_IP"), key, value) + if err != nil { + return err + } + fmt.Println("key:", key, "value", value) + } + fmt.Println("Wrote KVs to Consul") + return nil +} + +func GetKVFromConsul(key string) (string, error) { + if os.Getenv("CONSUL_IP") == "" { + return "", errors.New("CONSUL_IP environment variable not set.") + } + resp, err := requestGET(os.Getenv("CONSUL_IP"), key) + return resp, err +} + +func GetKVsFromConsul() ([]string, error) { + if os.Getenv("CONSUL_IP") == "" { + return []string{""}, errors.New("CONSUL_IP environment variable not set.") + } + resp, err := requestGETS(os.Getenv("CONSUL_IP")) + return resp, err +} + +func requestPUT(url string, key string, value string) error { + config := api.DefaultConfig() + config.Address = url + ":8500" + client, err := api.NewClient(config) + + if err != nil { + return err + } + + kv := client.KV() + + p := &api.KVPair{Key: key, Value: []byte(value)} + _, err = kv.Put(p, nil) + if err != nil { + return err + } + + return nil +} + +func requestGET(url string, key string) (string, error) { + config := api.DefaultConfig() + config.Address = url + ":8500" + client, err := api.NewClient(config) + + kv := client.KV() + + pair, _, err := kv.Get(key, nil) + + if pair == nil { + return string("No value found for key."), err + } + return string(pair.Value), err + +} + +func requestGETS(url string) ([]string, error) { + config := api.DefaultConfig() + config.Address = url + ":8500" + client, err := api.NewClient(config) + + kv := client.KV() + + pairs, _, err := kv.List("", nil) + + if len(pairs) == 0 { + return []string{"No keys found."}, err + } + + var res []string + + for _, keypair := range pairs { + res = append(res, keypair.Key) + } + + return res, err +} diff --git a/src/dkv/api/consulConnection_test.go b/src/dkv/api/consulConnection_test.go new file mode 100644 index 0000000..342542a --- /dev/null +++ b/src/dkv/api/consulConnection_test.go @@ -0,0 +1,19 @@ +/* + * 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(sshank) diff --git a/src/dkv/api/endpointViews.go b/src/dkv/api/endpointViews.go new file mode 100644 index 0000000..3c47ee5 --- /dev/null +++ b/src/dkv/api/endpointViews.go @@ -0,0 +1,108 @@ +/* + * 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 + +import ( + "encoding/json" + "github.com/gorilla/mux" + "net/http" +) + +var getkvs = GetKVsFromConsul + +func HandlePOST(w http.ResponseWriter, r *http.Request) { + + var body LoadStruct + + decoder := json.NewDecoder(r.Body) + err := decoder.Decode(&body) + + if err != nil { + req := ResponseStringStruct{Response: "Empty body."} + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(&req) + return + } + + err = ValidateBody(body) + + if err != nil { + req := ResponseStringStruct{Response: string(err.Error())} + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(req) + return + } + + err = KVStruct.ReadConfigs(body) + + if err != nil { + req := ResponseStringStruct{Response: string(err.Error())} + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(req) + return + } + + err = KVStruct.WriteKVsToConsul() + + if err != nil { + req := ResponseStringStruct{Response: string(err.Error())} + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(req) + } else { + req := ResponseStringStruct{Response: "Configuration read and default Key Values loaded to Consul"} + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(&req) + } +} + +func HandleGET(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + key := vars["key"] + + value, err := GetKVFromConsul(key) + + if err != nil { + req := ResponseStringStruct{Response: string(err.Error())} + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(req) + } else { + req := ResponseGETStruct{Response: map[string]string{key: value}} + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(req) + } +} + +func HandleGETS(w http.ResponseWriter, r *http.Request) { + + values, err := getkvs() + + if err != nil { + req := ResponseStringStruct{Response: string(err.Error())} + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(req) + } else { + req := ResponseGETSStruct{Response: values} + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(req) + } +} diff --git a/src/dkv/api/endpointViews_test.go b/src/dkv/api/endpointViews_test.go new file mode 100644 index 0000000..f603af4 --- /dev/null +++ b/src/dkv/api/endpointViews_test.go @@ -0,0 +1,58 @@ +/* + * 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 + +import ( + //"encoding/json" + "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" +) + +func Router() *mux.Router { + router := mux.NewRouter() + router.HandleFunc("/getconfigs", HandleGETS).Methods("GET") + router.HandleFunc("/loadconfigs", HandlePOST).Methods("POST") + return router +} + +func TestHandlePOST(t *testing.T) { + // TODO(sshank) + assert.Equal(t, 0, 0, "Not passed.") +} + +func TestHandleGET(t *testing.T) { + // TODO(sshank) + assert.Equal(t, 0, 0, "Not passed.") +} + +func TestHandleGETS(t *testing.T) { + getkvOld := getkvs + defer func() { getkvs = getkvOld }() + + getkvs = func() ([]string, error) { + return nil, nil + } + + request, _ := http.NewRequest("GET", "/getconfigs", nil) + response := httptest.NewRecorder() + Router().ServeHTTP(response, request) + + assert.Equal(t, 200, response.Code, "OK response is expected") +} diff --git a/src/dkv/api/propertiesReader.go b/src/dkv/api/propertiesReader.go new file mode 100644 index 0000000..018dabe --- /dev/null +++ b/src/dkv/api/propertiesReader.go @@ -0,0 +1,73 @@ +/* + * 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 + +import ( + "errors" + "github.com/magiconair/properties" + "io/ioutil" + "path" + "runtime" +) + +func PropertiesFilesToKV(directory string) (map[string]string, error) { + if directory == "default" { + kvs := make(map[string]string) + + _, filename, _, ok := runtime.Caller(0) + + if !ok { + return nil, errors.New("No caller") + } + + configDir := path.Dir(filename) + "/../configurations/" + err := ReadMultipleProperties(configDir, kvs) + if err != nil { + return nil, err + } + return kvs, nil + } else { + // Add case if directory is not there. + kvs := make(map[string]string) + directory += "/" + err := ReadMultipleProperties(directory, kvs) + if err != nil { + return nil, err + } + return kvs, nil + } +} + +func ReadProperty(path string, kvs map[string]string) { + p := properties.MustLoadFile(path, properties.UTF8) + for _, key := range p.Keys() { + kvs[key] = p.MustGet(key) + } +} + +func ReadMultipleProperties(path string, kvs map[string]string) error { + files, err := ioutil.ReadDir(path) + if err != nil { + return err + } + + for _, f := range files { + ReadProperty(path+f.Name(), kvs) + } + + return nil +} diff --git a/src/dkv/api/propertiesReader_test.go b/src/dkv/api/propertiesReader_test.go new file mode 100644 index 0000000..342542a --- /dev/null +++ b/src/dkv/api/propertiesReader_test.go @@ -0,0 +1,19 @@ +/* + * 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(sshank) diff --git a/src/dkv/api/utils.go b/src/dkv/api/utils.go new file mode 100644 index 0000000..8b87848 --- /dev/null +++ b/src/dkv/api/utils.go @@ -0,0 +1,101 @@ +/* + * 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 + +import ( + "errors" + "sync" +) + +type KeyValue struct { + sync.RWMutex + kv map[string]string +} + +type ResponseStringStruct struct { + Response string `json:"response"` +} + +type ResponseGETStruct struct { + Response map[string]string `json:"response"` +} + +type ResponseGETSStruct struct { + Response []string `json:"response"` +} + +type LoadStruct struct { + Type *TypeStruct `json:"type"` +} + +type TypeStruct struct { + FilePath string `json:"file_path"` +} + +var KVStruct = &KeyValue{kv: make(map[string]string)} + +func (kvStruct *KeyValue) ReadConfigs(body LoadStruct) error { + if body.Type.FilePath == "default" { + err := kvStruct.FileReader("default") + if err != nil { + return err + } + return nil + } else { + err := kvStruct.FileReader(body.Type.FilePath) + if err != nil { + return err + } + return nil + } +} + +func (kvStruct *KeyValue) FileReader(directory string) error { + defer kvStruct.Unlock() + + kvStruct.Lock() + + if directory == "default" { + propertiesValues, err := PropertiesFilesToKV("default") + if err != nil { + return err + } + for key, value := range propertiesValues { + kvStruct.kv[key] = value + } + return nil + } else { + propertiesValues, err := PropertiesFilesToKV(directory) + if err != nil { + return err + } + for key, value := range propertiesValues { + kvStruct.kv[key] = value + } + return nil + } +} + +func ValidateBody(body LoadStruct) error { + if body.Type == nil { + return errors.New("Type not set. Recheck POST data.") + } else if body.Type.FilePath == "" { + return errors.New("file_path not set") + } else { + return nil + } +} diff --git a/src/dkv/api/utils_test.go b/src/dkv/api/utils_test.go new file mode 100644 index 0000000..342542a --- /dev/null +++ b/src/dkv/api/utils_test.go @@ -0,0 +1,19 @@ +/* + * 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(sshank) |