diff options
Diffstat (limited to 'src/k8splugin/api')
-rw-r--r-- | src/k8splugin/api/api.go | 20 | ||||
-rw-r--r-- | src/k8splugin/api/brokerhandler_test.go | 8 | ||||
-rw-r--r-- | src/k8splugin/api/confighandler.go | 1 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler_test.go | 12 | ||||
-rw-r--r-- | src/k8splugin/api/healthcheckhandler_test.go | 4 | ||||
-rw-r--r-- | src/k8splugin/api/instancehandler.go | 10 | ||||
-rw-r--r-- | src/k8splugin/api/instancehandler_test.go | 22 | ||||
-rw-r--r-- | src/k8splugin/api/profilehandler_test.go | 10 | ||||
-rw-r--r-- | src/k8splugin/api/queryhandler.go | 79 |
9 files changed, 124 insertions, 42 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 1c7c5eaf..33eaefca 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -1,6 +1,8 @@ /* Copyright 2018 Intel Corporation. Copyright © 2021 Samsung Electronics +Copyright © 2021 Orange + 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 @@ -27,6 +29,7 @@ import ( func NewRouter(defClient rb.DefinitionManager, profileClient rb.ProfileManager, instClient app.InstanceManager, + queryClient app.QueryManager, configClient app.ConfigManager, connectionClient connection.ConnectionManager, templateClient rb.ConfigTemplateManager, @@ -56,8 +59,21 @@ func NewRouter(defClient rb.DefinitionManager, "Name", "{Name}", "Labels", "{Labels}").Methods("GET") instRouter.HandleFunc("/instance/{instID}", instHandler.deleteHandler).Methods("DELETE") - // (TODO): Fix update method - // instRouter.HandleFunc("/{vnfInstanceId}", UpdateHandler).Methods("PUT") + + // Query handler routes + if queryClient == nil { + queryClient = app.NewQueryClient() + } + queryHandler := queryHandler{client: queryClient} + queryRouter := router.PathPrefix("/v1").Subrouter() + queryRouter.HandleFunc("/query", queryHandler.queryHandler).Methods("GET") + queryRouter.HandleFunc("/query", queryHandler.queryHandler). + Queries("Namespace", "{Namespace}", + "CloudRegion", "{CloudRegion}", + "ApiVersion", "{ApiVersion}", + "Kind", "{Kind}", + "Name", "{Name}", + "Labels", "{Labels}").Methods("GET") //Setup the broker handler here //Use the base router without any path prefixes diff --git a/src/k8splugin/api/brokerhandler_test.go b/src/k8splugin/api/brokerhandler_test.go index 97c8a393..767cae1e 100644 --- a/src/k8splugin/api/brokerhandler_test.go +++ b/src/k8splugin/api/brokerhandler_test.go @@ -313,7 +313,7 @@ func TestBrokerCreateHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/cloudowner/cloudregion/infra_workload", testCase.input) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) defer resp.Body.Close() if testCase.expectedCode != resp.StatusCode { @@ -409,7 +409,7 @@ func TestBrokerGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -489,7 +489,7 @@ func TestBrokerFindHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload?name="+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -551,7 +551,7 @@ func TestBrokerDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode) diff --git a/src/k8splugin/api/confighandler.go b/src/k8splugin/api/confighandler.go index 4ce30799..c2236378 100644 --- a/src/k8splugin/api/confighandler.go +++ b/src/k8splugin/api/confighandler.go @@ -1,6 +1,7 @@ /* * Copyright 2018 Intel Corporation, Inc * Copyright © 2021 Samsung Electronics + * Copyright © 2021 Orange * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go index bb2f9dc4..b626b6f3 100644 --- a/src/k8splugin/api/defhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -139,7 +139,7 @@ func TestRBDefCreateHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition", testCase.reader) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -208,7 +208,7 @@ func TestRBDefListVersionsHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/testresourcebundle", nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -288,7 +288,7 @@ func TestRBDefListAllHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition", nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -368,7 +368,7 @@ func TestRBDefGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.name+"/"+testCase.version, nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -419,7 +419,7 @@ func TestRBDefDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/v1/rb/definition/"+testCase.name+"/"+testCase.version, nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -476,7 +476,7 @@ func TestRBDefUploadHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition/"+testCase.name+"/"+testCase.version+"/content", testCase.body) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { diff --git a/src/k8splugin/api/healthcheckhandler_test.go b/src/k8splugin/api/healthcheckhandler_test.go index 293ddf91..3a03d902 100644 --- a/src/k8splugin/api/healthcheckhandler_test.go +++ b/src/k8splugin/api/healthcheckhandler_test.go @@ -35,7 +35,7 @@ func TestHealthCheckHandler(t *testing.T) { Err: nil, } request := httptest.NewRequest("GET", "/v1/healthcheck", nil) - resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != http.StatusOK { @@ -48,7 +48,7 @@ func TestHealthCheckHandler(t *testing.T) { Err: pkgerrors.New("Runtime Error in DB"), } request := httptest.NewRequest("GET", "/v1/healthcheck", nil) - resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != http.StatusInternalServerError { diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go index b56a8e12..3baa8065 100644 --- a/src/k8splugin/api/instancehandler.go +++ b/src/k8splugin/api/instancehandler.go @@ -1,6 +1,8 @@ /* Copyright 2018 Intel Corporation. Copyright © 2021 Samsung Electronics +Copyright © 2021 Orange + 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 @@ -181,15 +183,11 @@ func (i instanceHandler) queryHandler(w http.ResponseWriter, r *http.Request) { name := r.FormValue("Name") labels := r.FormValue("Labels") if apiVersion == "" { - http.Error(w, "Missing apiVersion mandatory parameter", http.StatusBadRequest) + http.Error(w, "Missing ApiVersion mandatory parameter", http.StatusBadRequest) return } if kind == "" { - http.Error(w, "Missing kind mandatory parameter", http.StatusBadRequest) - return - } - if name == "" && labels == "" { - http.Error(w, "Name or Labels parameter must be provided", http.StatusBadRequest) + http.Error(w, "Missing Kind mandatory parameter", http.StatusBadRequest) return } resp, err := i.client.Query(id, apiVersion, kind, name, labels) diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index e05bd2d7..faec1329 100644 --- a/src/k8splugin/api/instancehandler_test.go +++ b/src/k8splugin/api/instancehandler_test.go @@ -205,7 +205,7 @@ func TestInstanceCreateHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/instance", testCase.input) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { body, _ := ioutil.ReadAll(resp.Body) @@ -306,7 +306,7 @@ func TestInstanceGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/instance/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -441,7 +441,7 @@ func TestInstanceListHandler(t *testing.T) { } request.URL.RawQuery = q.Encode() } - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -500,7 +500,7 @@ func TestDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/v1/instance/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode) @@ -539,18 +539,6 @@ func TestInstanceQueryHandler(t *testing.T) { }, }, { - label: "Missing name or label mandatory parameters", - id: "HaKpys8e", - input: map[string]string{ - "ApiVersion": "v1", - "Kind": "Pod", - }, - expectedCode: http.StatusBadRequest, - instClient: &mockInstanceClient{ - err: pkgerrors.New("Name or Labels parameter must be provided"), - }, - }, - { label: "Query instance by name", id: "HaKpys8e", input: map[string]string{ @@ -746,7 +734,7 @@ func TestInstanceQueryHandler(t *testing.T) { } url := "/v1/instance/" + testCase.id + "/query?" + params.Encode() request := httptest.NewRequest("GET", url, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { body, _ := ioutil.ReadAll(resp.Body) diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index 6897e01d..32d0061f 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -127,7 +127,7 @@ func TestRBProfileCreateHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile", testCase.reader) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -207,7 +207,7 @@ func TestRBProfileGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -288,7 +288,7 @@ func TestRBProfileListHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.def+"/"+testCase.version+"/profile", nil) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -347,7 +347,7 @@ func TestRBProfileDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -400,7 +400,7 @@ func TestRBProfileUploadHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname+"/content", testCase.body) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { diff --git a/src/k8splugin/api/queryhandler.go b/src/k8splugin/api/queryhandler.go new file mode 100644 index 00000000..9c11954c --- /dev/null +++ b/src/k8splugin/api/queryhandler.go @@ -0,0 +1,79 @@ +/* +Copyright 2018 Intel Corporation. +Copyright © 2021 Samsung Electronics +Copyright © 2021 Orange + +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" + "net/http" + + "github.com/onap/multicloud-k8s/src/k8splugin/internal/app" + log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils" +) + +// Used to store the backend implementation objects +// Also simplifies the mocking needed for unit testing +type queryHandler struct { + // Interface that implements the Instance operations + client app.QueryManager +} + +// queryHandler retrieves information about specified resources for instance +func (i queryHandler) queryHandler(w http.ResponseWriter, r *http.Request) { + namespace := r.FormValue("Namespace") + cloudRegion := r.FormValue("CloudRegion") + apiVersion := r.FormValue("ApiVersion") + kind := r.FormValue("Kind") + name := r.FormValue("Name") + labels := r.FormValue("Labels") + if cloudRegion == "" { + http.Error(w, "Missing CloudRegion mandatory parameter", http.StatusBadRequest) + return + } + if apiVersion == "" { + http.Error(w, "Missing ApiVersion mandatory parameter", http.StatusBadRequest) + return + } + if kind == "" { + http.Error(w, "Missing Kind mandatory parameter", http.StatusBadRequest) + return + } + // instance id is irrelevant here + resp, err := i.client.Query(namespace, cloudRegion, apiVersion, kind, name, labels, "query") + if err != nil { + log.Error("Error getting Query results", log.Fields{ + "error": err, + "cloudRegion": cloudRegion, + "namespace": namespace, + "apiVersion": apiVersion, + "kind": kind, + "name": name, + "labels": labels, + }) + http.Error(w, err.Error(), http.StatusInternalServerError) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(resp) + if err != nil { + log.Error("Error Marshaling Response", log.Fields{ + "error": err, + "response": resp, + }) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} |