Move connectionhandler.go into api package 78/90478/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Tue, 25 Jun 2019 18:42:31 +0000 (11:42 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Wed, 26 Jun 2019 17:38:47 +0000 (10:38 -0700)
Move connectionhandler.go into api package
This brings it inline with the other API handlers

Issue-ID: MULTICLOUD-666
Change-Id: Ia3b832159f537c6d8d8ecc93b56bc9b085649f7e
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
src/k8splugin/api/api.go
src/k8splugin/api/brokerhandler_test.go
src/k8splugin/api/connectionhandler.go [moved from src/k8splugin/internal/connection/connectionhandler.go with 87% similarity]
src/k8splugin/api/defhandler_test.go
src/k8splugin/api/healthcheckhandler_test.go
src/k8splugin/api/instancehandler_test.go
src/k8splugin/api/profilehandler_test.go
src/k8splugin/cmd/main.go

index 636b80b..353972a 100644 (file)
@@ -26,6 +26,7 @@ func NewRouter(defClient rb.DefinitionManager,
        profileClient rb.ProfileManager,
        instClient app.InstanceManager,
        configClient app.ConfigManager,
+       connectionClient connection.ConnectionManager,
        templateClient rb.ConfigTemplateManager) *mux.Router {
 
        router := mux.NewRouter()
@@ -51,11 +52,13 @@ func NewRouter(defClient rb.DefinitionManager,
        router.HandleFunc("/{cloud-owner}/{cloud-region}/infra_workload/{instID}", brokerHandler.deleteHandler).Methods("DELETE")
 
        //Setup the connectivity api handler here
-       connectionClient := connection.NewConnectionClient()
-       connectionHandler := connection.ConnectionHandler{Client: connectionClient}
-       instRouter.HandleFunc("/connectivity-info", connectionHandler.CreateHandler).Methods("POST")
-       instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.GetHandler).Methods("GET")
-       instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.DeleteHandler).Methods("DELETE")
+       if connectionClient == nil {
+               connectionClient = connection.NewConnectionClient()
+       }
+       connectionHandler := connectionHandler{client: connectionClient}
+       instRouter.HandleFunc("/connectivity-info", connectionHandler.createHandler).Methods("POST")
+       instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.getHandler).Methods("GET")
+       instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.deleteHandler).Methods("DELETE")
 
        //Setup resource bundle definition routes
        if defClient == nil {
index f93ce5f..319c64e 100644 (file)
@@ -154,7 +154,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                body, _ := ioutil.ReadAll(resp.Body)
@@ -238,7 +238,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                t.Fatalf("Request method returned: %v and it was expected: %v",
@@ -334,7 +334,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                t.Fatalf("Request method returned: %v and it was expected: %v",
@@ -396,7 +396,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package connection
+package api
 
 import (
        "bytes"
@@ -24,15 +24,17 @@ import (
        "io/ioutil"
        "net/http"
 
+       "github.com/onap/multicloud-k8s/src/k8splugin/internal/connection"
+
        "github.com/gorilla/mux"
 )
 
-// ConnectionHandler is used to store backend implementations objects
+// connectionHandler is used to store backend implementations objects
 // Also simplifies mocking for unit testing purposes
-type ConnectionHandler struct {
+type connectionHandler struct {
        // Interface that implements Connectivity operations
        // We will set this variable with a mock interface for testing
-       Client ConnectionManager
+       client connection.ConnectionManager
 }
 
 // CreateHandler handles creation of the connectivity entry in the database
@@ -40,8 +42,8 @@ type ConnectionHandler struct {
 // curl -i -F "metadata={\"cloud-region\":\"kud\",\"cloud-owner\":\"me\"};type=application/json" \
 //         -F file=@/home/user/.kube/config \
 //         -X POST http://localhost:8081/v1/connectivity-info
-func (h ConnectionHandler) CreateHandler(w http.ResponseWriter, r *http.Request) {
-       var v Connection
+func (h connectionHandler) createHandler(w http.ResponseWriter, r *http.Request) {
+       var v connection.Connection
 
        // Implemenation using multipart form
        // Review and enable/remove at a later date
@@ -93,7 +95,7 @@ func (h ConnectionHandler) CreateHandler(w http.ResponseWriter, r *http.Request)
 
        v.Kubeconfig = base64.StdEncoding.EncodeToString(content)
 
-       ret, err := h.Client.Create(v)
+       ret, err := h.client.Create(v)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
@@ -110,11 +112,11 @@ func (h ConnectionHandler) CreateHandler(w http.ResponseWriter, r *http.Request)
 
 // getHandler handles GET operations on a particular name
 // Returns a  Connectivity instance
-func (h ConnectionHandler) GetHandler(w http.ResponseWriter, r *http.Request) {
+func (h connectionHandler) getHandler(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        name := vars["connname"]
 
-       ret, err := h.Client.Get(name)
+       ret, err := h.client.Get(name)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
@@ -130,11 +132,11 @@ func (h ConnectionHandler) GetHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 // deleteHandler handles DELETE operations on a particular record
-func (h ConnectionHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) {
+func (h connectionHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        name := vars["connname"]
 
-       err := h.Client.Delete(name)
+       err := h.client.Delete(name)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
index 912f96d..077d070 100644 (file)
@@ -20,13 +20,14 @@ import (
        "bytes"
        "encoding/json"
        "io"
-       "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
        "net/http"
        "net/http/httptest"
        "reflect"
        "sort"
        "testing"
 
+       "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
+
        pkgerrors "github.com/pkg/errors"
 )
 
@@ -138,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))
+                       resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -207,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))
+                       resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -287,7 +288,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))
+                       resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -338,7 +339,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))
+                       resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -395,7 +396,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))
+                       resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
index d96bb3c..ab9322f 100644 (file)
@@ -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))
+               resp := executeRequest(request, NewRouter(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))
+               resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil))
 
                //Check returned code
                if resp.StatusCode != http.StatusInternalServerError {
index 3fc0202..35cef53 100644 (file)
@@ -175,7 +175,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                body, _ := ioutil.ReadAll(resp.Body)
@@ -276,7 +276,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                t.Fatalf("Request method returned: %v and it was expected: %v",
@@ -323,7 +323,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))
+                       resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
 
                        if testCase.expectedCode != resp.StatusCode {
                                t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode)
index eb65827..e81fb26 100644 (file)
@@ -20,12 +20,13 @@ import (
        "bytes"
        "encoding/json"
        "io"
-       "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
        "net/http"
        "net/http/httptest"
        "reflect"
        "testing"
 
+       "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
+
        pkgerrors "github.com/pkg/errors"
 )
 
@@ -117,7 +118,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))
+                       resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -188,7 +189,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))
+                       resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -236,7 +237,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))
+                       resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
@@ -289,7 +290,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))
+                       resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
 
                        //Check returned code
                        if resp.StatusCode != testCase.expectedCode {
index 8c887eb..8f9ffc5 100644 (file)
@@ -39,7 +39,7 @@ func main() {
 
        rand.Seed(time.Now().UnixNano())
 
-       httpRouter := api.NewRouter(nil, nil, nil, nil, nil)
+       httpRouter := api.NewRouter(nil, nil, nil, nil, nil, nil)
        loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter)
        log.Println("Starting Kubernetes Multicloud API")