From bd3e69e7a26aaa00d3367f1eeb78e91e63ebd404 Mon Sep 17 00:00:00 2001 From: Eric Multanen Date: Tue, 12 May 2020 14:28:33 -0700 Subject: Move controller module into separate package Move the controller part of the module package into a separate controller package. Issue-ID: MULTICLOUD-1029 Signed-off-by: Eric Multanen Change-Id: Id07c1012aeff8a72458b37d42671495510cade75 --- src/orchestrator/api/api.go | 3 +- src/orchestrator/api/controllerhandler.go | 17 +- src/orchestrator/api/controllerhandler_test.go | 51 +++--- src/orchestrator/cmd/main.go | 4 +- src/orchestrator/pkg/module/controller.go | 193 -------------------- .../pkg/module/controller/controller.go | 194 ++++++++++++++++++++ .../pkg/module/controller/controller_test.go | 197 +++++++++++++++++++++ src/orchestrator/pkg/module/controller_test.go | 196 -------------------- src/orchestrator/pkg/module/module.go | 43 +---- src/orchestrator/pkg/module/types/types.go | 58 ++++++ 10 files changed, 491 insertions(+), 465 deletions(-) delete mode 100644 src/orchestrator/pkg/module/controller.go create mode 100644 src/orchestrator/pkg/module/controller/controller.go create mode 100644 src/orchestrator/pkg/module/controller/controller_test.go delete mode 100644 src/orchestrator/pkg/module/controller_test.go create mode 100644 src/orchestrator/pkg/module/types/types.go (limited to 'src') diff --git a/src/orchestrator/api/api.go b/src/orchestrator/api/api.go index 8b4b91a1..5703226f 100644 --- a/src/orchestrator/api/api.go +++ b/src/orchestrator/api/api.go @@ -19,6 +19,7 @@ package api import ( "github.com/gorilla/mux" moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + controller "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/controller" ) var moduleClient *moduleLib.Client @@ -27,7 +28,7 @@ var moduleClient *moduleLib.Client func NewRouter(projectClient moduleLib.ProjectManager, compositeAppClient moduleLib.CompositeAppManager, appClient moduleLib.AppManager, - ControllerClient moduleLib.ControllerManager, + ControllerClient controller.ControllerManager, genericPlacementIntentClient moduleLib.GenericPlacementIntentManager, appIntentClient moduleLib.AppIntentManager, deploymentIntentGrpClient moduleLib.DeploymentIntentGroupManager, diff --git a/src/orchestrator/api/controllerhandler.go b/src/orchestrator/api/controllerhandler.go index 1dad2bf8..5df691f3 100644 --- a/src/orchestrator/api/controllerhandler.go +++ b/src/orchestrator/api/controllerhandler.go @@ -23,7 +23,8 @@ import ( "github.com/gorilla/mux" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" - moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + controller "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/controller" + mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" pkgerrors "github.com/pkg/errors" ) @@ -32,13 +33,13 @@ import ( type controllerHandler struct { // Interface that implements controller operations // We will set this variable with a mock interface for testing - client moduleLib.ControllerManager + client controller.ControllerManager } // Check for valid format of input parameters -func validateControllerInputs(c moduleLib.Controller) error { +func validateControllerInputs(c controller.Controller) error { // validate metadata - err := moduleLib.IsValidMetadata(c.Metadata) + err := mtypes.IsValidMetadata(c.Metadata) if err != nil { return pkgerrors.Wrap(err, "Invalid controller metadata") } @@ -54,7 +55,7 @@ func validateControllerInputs(c moduleLib.Controller) error { } found := false - for _, val := range moduleLib.CONTROLLER_TYPES { + for _, val := range controller.CONTROLLER_TYPES { if c.Spec.Type == val { found = true break @@ -64,7 +65,7 @@ func validateControllerInputs(c moduleLib.Controller) error { return pkgerrors.Errorf("Invalid controller type: %v", c.Spec.Type) } - errs = validation.IsValidNumber(c.Spec.Priority, moduleLib.MinControllerPriority, moduleLib.MaxControllerPriority) + errs = validation.IsValidNumber(c.Spec.Priority, controller.MinControllerPriority, controller.MaxControllerPriority) if len(errs) > 0 { return pkgerrors.Errorf("Invalid controller priority = [%v], errors: %v", c.Spec.Priority, errs) } @@ -74,7 +75,7 @@ func validateControllerInputs(c moduleLib.Controller) error { // Create handles creation of the controller entry in the database func (h controllerHandler) createHandler(w http.ResponseWriter, r *http.Request) { - var m moduleLib.Controller + var m controller.Controller err := json.NewDecoder(r.Body).Decode(&m) switch { @@ -109,7 +110,7 @@ func (h controllerHandler) createHandler(w http.ResponseWriter, r *http.Request) // Put handles creation or update of the controller entry in the database func (h controllerHandler) putHandler(w http.ResponseWriter, r *http.Request) { - var m moduleLib.Controller + var m controller.Controller vars := mux.Vars(r) name := vars["controller-name"] diff --git a/src/orchestrator/api/controllerhandler_test.go b/src/orchestrator/api/controllerhandler_test.go index b88fc1e7..6cbb2a79 100644 --- a/src/orchestrator/api/controllerhandler_test.go +++ b/src/orchestrator/api/controllerhandler_test.go @@ -25,7 +25,8 @@ import ( "reflect" "testing" - moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/controller" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" pkgerrors "github.com/pkg/errors" ) @@ -36,29 +37,29 @@ import ( type mockControllerManager struct { // Items and err will be used to customize each test // via a localized instantiation of mockControllerManager - Items []moduleLib.Controller + Items []controller.Controller Err error } -func (m *mockControllerManager) CreateController(inp moduleLib.Controller, mayExist bool) (moduleLib.Controller, error) { +func (m *mockControllerManager) CreateController(inp controller.Controller, mayExist bool) (controller.Controller, error) { if m.Err != nil { - return moduleLib.Controller{}, m.Err + return controller.Controller{}, m.Err } return m.Items[0], nil } -func (m *mockControllerManager) GetController(name string) (moduleLib.Controller, error) { +func (m *mockControllerManager) GetController(name string) (controller.Controller, error) { if m.Err != nil { - return moduleLib.Controller{}, m.Err + return controller.Controller{}, m.Err } return m.Items[0], nil } -func (m *mockControllerManager) GetControllers() ([]moduleLib.Controller, error) { +func (m *mockControllerManager) GetControllers() ([]controller.Controller, error) { if m.Err != nil { - return []moduleLib.Controller{}, m.Err + return []controller.Controller{}, m.Err } return m.Items, nil @@ -76,7 +77,7 @@ func TestControllerCreateHandler(t *testing.T) { testCases := []struct { label string reader io.Reader - expected moduleLib.Controller + expected controller.Controller expectedCode int controllerClient *mockControllerManager }{ @@ -96,23 +97,23 @@ func TestControllerCreateHandler(t *testing.T) { "ip-address":"10.188.234.1", "port":8080 } }`)), - expected: moduleLib.Controller{ - Metadata: moduleLib.Metadata{ + expected: controller.Controller{ + Metadata: types.Metadata{ Name: "testController", }, - Spec: moduleLib.ControllerSpec{ + Spec: controller.ControllerSpec{ Host: "10.188.234.1", Port: 8080, }, }, controllerClient: &mockControllerManager{ //Items that will be returned by the mocked Client - Items: []moduleLib.Controller{ + Items: []controller.Controller{ { - Metadata: moduleLib.Metadata{ + Metadata: types.Metadata{ Name: "testController", }, - Spec: moduleLib.ControllerSpec{ + Spec: controller.ControllerSpec{ Host: "10.188.234.1", Port: 8080, }, @@ -142,7 +143,7 @@ func TestControllerCreateHandler(t *testing.T) { //Check returned body only if statusCreated if resp.StatusCode == http.StatusCreated { - got := moduleLib.Controller{} + got := controller.Controller{} json.NewDecoder(resp.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { @@ -158,7 +159,7 @@ func TestControllerGetHandler(t *testing.T) { testCases := []struct { label string - expected moduleLib.Controller + expected controller.Controller name, version string expectedCode int controllerClient *mockControllerManager @@ -166,23 +167,23 @@ func TestControllerGetHandler(t *testing.T) { { label: "Get Controller", expectedCode: http.StatusOK, - expected: moduleLib.Controller{ - Metadata: moduleLib.Metadata{ + expected: controller.Controller{ + Metadata: types.Metadata{ Name: "testController", }, - Spec: moduleLib.ControllerSpec{ + Spec: controller.ControllerSpec{ Host: "10.188.234.1", Port: 8080, }, }, name: "testController", controllerClient: &mockControllerManager{ - Items: []moduleLib.Controller{ + Items: []controller.Controller{ { - Metadata: moduleLib.Metadata{ + Metadata: types.Metadata{ Name: "testController", }, - Spec: moduleLib.ControllerSpec{ + Spec: controller.ControllerSpec{ Host: "10.188.234.1", Port: 8080, }, @@ -195,7 +196,7 @@ func TestControllerGetHandler(t *testing.T) { expectedCode: http.StatusInternalServerError, name: "nonexistingController", controllerClient: &mockControllerManager{ - Items: []moduleLib.Controller{}, + Items: []controller.Controller{}, Err: pkgerrors.New("Internal Error"), }, }, @@ -213,7 +214,7 @@ func TestControllerGetHandler(t *testing.T) { //Check returned body only if statusOK if resp.StatusCode == http.StatusOK { - got := moduleLib.Controller{} + got := controller.Controller{} json.NewDecoder(resp.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { diff --git a/src/orchestrator/cmd/main.go b/src/orchestrator/cmd/main.go index 7f258bbf..b5792b9b 100644 --- a/src/orchestrator/cmd/main.go +++ b/src/orchestrator/cmd/main.go @@ -29,7 +29,7 @@ import ( contextDb "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/rpc" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/controller" ) func main() { @@ -58,7 +58,7 @@ func main() { Addr: ":" + config.GetConfiguration().ServicePort, } - module.NewControllerClient().InitControllers() + controller.NewControllerClient().InitControllers() connectionsClose := make(chan struct{}) go func() { diff --git a/src/orchestrator/pkg/module/controller.go b/src/orchestrator/pkg/module/controller.go deleted file mode 100644 index 6827af87..00000000 --- a/src/orchestrator/pkg/module/controller.go +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright 2020 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 module - -import ( - "encoding/json" - - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" - rpc "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/rpc" - pkgerrors "github.com/pkg/errors" -) - -// Controller contains the parameters needed for Controllers -// It implements the interface for managing the Controllers -type Controller struct { - Metadata Metadata `json:"metadata"` - Spec ControllerSpec `json:"spec"` -} - -type ControllerSpec struct { - Host string `json:"host"` - Port int `json:"port"` - Type string `json:"type"` - Priority int `json:"priority"` -} - -const MinControllerPriority = 1 -const MaxControllerPriority = 1000000 -const CONTROLLER_TYPE_ACTION string = "action" -const CONTROLLER_TYPE_PLACEMENT string = "placement" - -var CONTROLLER_TYPES = [...]string{CONTROLLER_TYPE_ACTION, CONTROLLER_TYPE_PLACEMENT} - -// ControllerKey is the key structure that is used in the database -type ControllerKey struct { - ControllerName string `json:"controller-name"` -} - -// We will use json marshalling to convert to string to -// preserve the underlying structure. -func (mk ControllerKey) String() string { - out, err := json.Marshal(mk) - if err != nil { - return "" - } - - return string(out) -} - -// ControllerManager is an interface exposes the Controller functionality -type ControllerManager interface { - CreateController(ms Controller, mayExist bool) (Controller, error) - GetController(name string) (Controller, error) - GetControllers() ([]Controller, error) - InitControllers() - DeleteController(name string) error -} - -// ControllerClient implements the Manager -// It will also be used to maintain some localized state -type ControllerClient struct { - collectionName string - tagMeta string -} - -// NewControllerClient returns an instance of the ControllerClient -// which implements the Manager -func NewControllerClient() *ControllerClient { - return &ControllerClient{ - collectionName: "controller", - tagMeta: "controllermetadata", - } -} - -// CreateController a new collection based on the Controller -func (mc *ControllerClient) CreateController(m Controller, mayExist bool) (Controller, error) { - - //Construct the composite key to select the entry - key := ControllerKey{ - ControllerName: m.Metadata.Name, - } - - //Check if this Controller already exists - _, err := mc.GetController(m.Metadata.Name) - if err == nil && !mayExist { - return Controller{}, pkgerrors.New("Controller already exists") - } - - err = db.DBconn.Insert(mc.collectionName, key, nil, mc.tagMeta, m) - if err != nil { - return Controller{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - // send message to create/update the rpc connection - rpc.UpdateRpcConn(m.Metadata.Name, m.Spec.Host, m.Spec.Port) - - return m, nil -} - -// GetController returns the Controller for corresponding name -func (mc *ControllerClient) GetController(name string) (Controller, error) { - - //Construct the composite key to select the entry - key := ControllerKey{ - ControllerName: name, - } - value, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) - if err != nil { - return Controller{}, pkgerrors.Wrap(err, "Get Controller") - } - - if value != nil { - microserv := Controller{} - err = db.DBconn.Unmarshal(value[0], µserv) - if err != nil { - return Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") - } - return microserv, nil - } - - return Controller{}, pkgerrors.New("Error getting Controller") -} - -// GetControllers returns all the Controllers that are registered -func (mc *ControllerClient) GetControllers() ([]Controller, error) { - - //Construct the composite key to select the entry - key := ControllerKey{ - ControllerName: "", - } - - var resp []Controller - values, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) - if err != nil { - return []Controller{}, pkgerrors.Wrap(err, "Get Controller") - } - - for _, value := range values { - microserv := Controller{} - err = db.DBconn.Unmarshal(value, µserv) - if err != nil { - return []Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") - } - - resp = append(resp, microserv) - } - - return resp, nil -} - -// DeleteController the Controller from database -func (mc *ControllerClient) DeleteController(name string) error { - - //Construct the composite key to select the entry - key := ControllerKey{ - ControllerName: name, - } - err := db.DBconn.Remove(mc.collectionName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Controller Entry;") - } - - // send message to close rpc connection - rpc.RemoveRpcConn(name) - - return nil -} - -// InitControllers initializes connctions for controllers in the DB -func (mc *ControllerClient) InitControllers() { - vals, _ := mc.GetControllers() - for _, v := range vals { - log.Info("Initializing RPC connection for controller", log.Fields{ - "Controller": v.Metadata.Name, - }) - rpc.UpdateRpcConn(v.Metadata.Name, v.Spec.Host, v.Spec.Port) - } -} diff --git a/src/orchestrator/pkg/module/controller/controller.go b/src/orchestrator/pkg/module/controller/controller.go new file mode 100644 index 00000000..c4957413 --- /dev/null +++ b/src/orchestrator/pkg/module/controller/controller.go @@ -0,0 +1,194 @@ +/* + * Copyright 2020 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 controller + +import ( + "encoding/json" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" + rpc "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/rpc" + mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + pkgerrors "github.com/pkg/errors" +) + +// Controller contains the parameters needed for Controllers +// It implements the interface for managing the Controllers +type Controller struct { + Metadata mtypes.Metadata `json:"metadata"` + Spec ControllerSpec `json:"spec"` +} + +type ControllerSpec struct { + Host string `json:"host"` + Port int `json:"port"` + Type string `json:"type"` + Priority int `json:"priority"` +} + +const MinControllerPriority = 1 +const MaxControllerPriority = 1000000 +const CONTROLLER_TYPE_ACTION string = "action" +const CONTROLLER_TYPE_PLACEMENT string = "placement" + +var CONTROLLER_TYPES = [...]string{CONTROLLER_TYPE_ACTION, CONTROLLER_TYPE_PLACEMENT} + +// ControllerKey is the key structure that is used in the database +type ControllerKey struct { + ControllerName string `json:"controller-name"` +} + +// We will use json marshalling to convert to string to +// preserve the underlying structure. +func (mk ControllerKey) String() string { + out, err := json.Marshal(mk) + if err != nil { + return "" + } + + return string(out) +} + +// ControllerManager is an interface exposes the Controller functionality +type ControllerManager interface { + CreateController(ms Controller, mayExist bool) (Controller, error) + GetController(name string) (Controller, error) + GetControllers() ([]Controller, error) + InitControllers() + DeleteController(name string) error +} + +// ControllerClient implements the Manager +// It will also be used to maintain some localized state +type ControllerClient struct { + collectionName string + tagMeta string +} + +// NewControllerClient returns an instance of the ControllerClient +// which implements the Manager +func NewControllerClient() *ControllerClient { + return &ControllerClient{ + collectionName: "controller", + tagMeta: "controllermetadata", + } +} + +// CreateController a new collection based on the Controller +func (mc *ControllerClient) CreateController(m Controller, mayExist bool) (Controller, error) { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: m.Metadata.Name, + } + + //Check if this Controller already exists + _, err := mc.GetController(m.Metadata.Name) + if err == nil && !mayExist { + return Controller{}, pkgerrors.New("Controller already exists") + } + + err = db.DBconn.Insert(mc.collectionName, key, nil, mc.tagMeta, m) + if err != nil { + return Controller{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + // send message to create/update the rpc connection + rpc.UpdateRpcConn(m.Metadata.Name, m.Spec.Host, m.Spec.Port) + + return m, nil +} + +// GetController returns the Controller for corresponding name +func (mc *ControllerClient) GetController(name string) (Controller, error) { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: name, + } + value, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) + if err != nil { + return Controller{}, pkgerrors.Wrap(err, "Get Controller") + } + + if value != nil { + microserv := Controller{} + err = db.DBconn.Unmarshal(value[0], µserv) + if err != nil { + return Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") + } + return microserv, nil + } + + return Controller{}, pkgerrors.New("Error getting Controller") +} + +// GetControllers returns all the Controllers that are registered +func (mc *ControllerClient) GetControllers() ([]Controller, error) { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: "", + } + + var resp []Controller + values, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) + if err != nil { + return []Controller{}, pkgerrors.Wrap(err, "Get Controller") + } + + for _, value := range values { + microserv := Controller{} + err = db.DBconn.Unmarshal(value, µserv) + if err != nil { + return []Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") + } + + resp = append(resp, microserv) + } + + return resp, nil +} + +// DeleteController the Controller from database +func (mc *ControllerClient) DeleteController(name string) error { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: name, + } + err := db.DBconn.Remove(mc.collectionName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Controller Entry;") + } + + // send message to close rpc connection + rpc.RemoveRpcConn(name) + + return nil +} + +// InitControllers initializes connctions for controllers in the DB +func (mc *ControllerClient) InitControllers() { + vals, _ := mc.GetControllers() + for _, v := range vals { + log.Info("Initializing RPC connection for controller", log.Fields{ + "Controller": v.Metadata.Name, + }) + rpc.UpdateRpcConn(v.Metadata.Name, v.Spec.Host, v.Spec.Port) + } +} diff --git a/src/orchestrator/pkg/module/controller/controller_test.go b/src/orchestrator/pkg/module/controller/controller_test.go new file mode 100644 index 00000000..44e7a0ca --- /dev/null +++ b/src/orchestrator/pkg/module/controller/controller_test.go @@ -0,0 +1,197 @@ +/* + * Copyright 2020 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 controller + +import ( + "reflect" + "strings" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + + pkgerrors "github.com/pkg/errors" +) + +func TestCreateController(t *testing.T) { + testCases := []struct { + label string + inp Controller + expectedError string + mockdb *db.MockDB + expected Controller + }{ + { + label: "Create Controller", + inp: Controller{ + Metadata: types.Metadata{ + Name: "testController", + }, + Spec: ControllerSpec{ + Host: "132.156.0.10", + Port: 8080, + }, + }, + expected: Controller{ + Metadata: types.Metadata{ + Name: "testController", + }, + Spec: ControllerSpec{ + Host: "132.156.0.10", + Port: 8080, + }, + }, + expectedError: "", + mockdb: &db.MockDB{}, + }, + { + label: "Failed Create Controller", + expectedError: "Error Creating Controller", + mockdb: &db.MockDB{ + Err: pkgerrors.New("Error Creating Controller"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewControllerClient() + got, err := impl.CreateController(testCase.inp, false) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Create returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Create returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("Create returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestGetController(t *testing.T) { + + testCases := []struct { + label string + name string + expectedError string + mockdb *db.MockDB + inp string + expected Controller + }{ + { + label: "Get Controller", + name: "testController", + expected: Controller{ + Metadata: types.Metadata{ + Name: "testController", + }, + Spec: ControllerSpec{ + Host: "132.156.0.10", + Port: 8080, + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ControllerKey{ControllerName: "testController"}.String(): { + "controllermetadata": []byte( + "{\"metadata\":{" + + "\"name\":\"testController\"" + + "}," + + "\"spec\":{" + + "\"host\":\"132.156.0.10\"," + + "\"port\": 8080 }}"), + }, + }, + }, + }, + { + label: "Get Error", + expectedError: "DB Error", + mockdb: &db.MockDB{ + Err: pkgerrors.New("DB Error"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewControllerClient() + got, err := impl.GetController(testCase.name) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Get returned an unexpected error: %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Get returned an unexpected error: %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("Get returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestDeleteController(t *testing.T) { + + testCases := []struct { + label string + name string + expectedError string + mockdb *db.MockDB + }{ + { + label: "Delete Controller", + name: "testController", + mockdb: &db.MockDB{}, + }, + { + label: "Delete Error", + expectedError: "DB Error", + mockdb: &db.MockDB{ + Err: pkgerrors.New("DB Error"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewControllerClient() + err := impl.DeleteController(testCase.name) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Delete returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Delete returned an unexpected error %s", err) + } + } + }) + } +} diff --git a/src/orchestrator/pkg/module/controller_test.go b/src/orchestrator/pkg/module/controller_test.go deleted file mode 100644 index a13f43a5..00000000 --- a/src/orchestrator/pkg/module/controller_test.go +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright 2020 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 module - -import ( - "reflect" - "strings" - "testing" - - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - - pkgerrors "github.com/pkg/errors" -) - -func TestCreateController(t *testing.T) { - testCases := []struct { - label string - inp Controller - expectedError string - mockdb *db.MockDB - expected Controller - }{ - { - label: "Create Controller", - inp: Controller{ - Metadata: Metadata{ - Name: "testController", - }, - Spec: ControllerSpec{ - Host: "132.156.0.10", - Port: 8080, - }, - }, - expected: Controller{ - Metadata: Metadata{ - Name: "testController", - }, - Spec: ControllerSpec{ - Host: "132.156.0.10", - Port: 8080, - }, - }, - expectedError: "", - mockdb: &db.MockDB{}, - }, - { - label: "Failed Create Controller", - expectedError: "Error Creating Controller", - mockdb: &db.MockDB{ - Err: pkgerrors.New("Error Creating Controller"), - }, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - db.DBconn = testCase.mockdb - impl := NewControllerClient() - got, err := impl.CreateController(testCase.inp, false) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("Create returned an unexpected error %s", err) - } - if strings.Contains(err.Error(), testCase.expectedError) == false { - t.Fatalf("Create returned an unexpected error %s", err) - } - } else { - if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("Create returned unexpected body: got %v;"+ - " expected %v", got, testCase.expected) - } - } - }) - } -} - -func TestGetController(t *testing.T) { - - testCases := []struct { - label string - name string - expectedError string - mockdb *db.MockDB - inp string - expected Controller - }{ - { - label: "Get Controller", - name: "testController", - expected: Controller{ - Metadata: Metadata{ - Name: "testController", - }, - Spec: ControllerSpec{ - Host: "132.156.0.10", - Port: 8080, - }, - }, - expectedError: "", - mockdb: &db.MockDB{ - Items: map[string]map[string][]byte{ - ControllerKey{ControllerName: "testController"}.String(): { - "controllermetadata": []byte( - "{\"metadata\":{" + - "\"name\":\"testController\"" + - "}," + - "\"spec\":{" + - "\"host\":\"132.156.0.10\"," + - "\"port\": 8080 }}"), - }, - }, - }, - }, - { - label: "Get Error", - expectedError: "DB Error", - mockdb: &db.MockDB{ - Err: pkgerrors.New("DB Error"), - }, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - db.DBconn = testCase.mockdb - impl := NewControllerClient() - got, err := impl.GetController(testCase.name) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("Get returned an unexpected error: %s", err) - } - if strings.Contains(err.Error(), testCase.expectedError) == false { - t.Fatalf("Get returned an unexpected error: %s", err) - } - } else { - if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("Get returned unexpected body: got %v;"+ - " expected %v", got, testCase.expected) - } - } - }) - } -} - -func TestDeleteController(t *testing.T) { - - testCases := []struct { - label string - name string - expectedError string - mockdb *db.MockDB - }{ - { - label: "Delete Controller", - name: "testController", - mockdb: &db.MockDB{}, - }, - { - label: "Delete Error", - expectedError: "DB Error", - mockdb: &db.MockDB{ - Err: pkgerrors.New("DB Error"), - }, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - db.DBconn = testCase.mockdb - impl := NewControllerClient() - err := impl.DeleteController(testCase.name) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("Delete returned an unexpected error %s", err) - } - if strings.Contains(err.Error(), testCase.expectedError) == false { - t.Fatalf("Delete returned an unexpected error %s", err) - } - } - }) - } -} diff --git a/src/orchestrator/pkg/module/module.go b/src/orchestrator/pkg/module/module.go index 463a55b3..5ac704e6 100644 --- a/src/orchestrator/pkg/module/module.go +++ b/src/orchestrator/pkg/module/module.go @@ -17,8 +17,7 @@ package module import ( - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" - pkgerrors "github.com/pkg/errors" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/controller" ) // Client for using the services in the orchestrator @@ -26,7 +25,7 @@ type Client struct { Project *ProjectClient CompositeApp *CompositeAppClient App *AppClient - Controller *ControllerClient + Controller *controller.ControllerClient GenericPlacementIntent *GenericPlacementIntentClient AppIntent *AppIntentClient DeploymentIntentGroup *DeploymentIntentGroupClient @@ -43,7 +42,7 @@ func NewClient() *Client { c.Project = NewProjectClient() c.CompositeApp = NewCompositeAppClient() c.App = NewAppClient() - c.Controller = NewControllerClient() + c.Controller = controller.NewControllerClient() c.GenericPlacementIntent = NewGenericPlacementIntentClient() c.AppIntent = NewAppIntentClient() c.DeploymentIntentGroup = NewDeploymentIntentGroupClient() @@ -54,39 +53,3 @@ func NewClient() *Client { c.Instantiation = NewInstantiationClient() return c } - -// It implements the interface for managing the ClusterProviders -const MAX_DESCRIPTION_LEN int = 1024 -const MAX_USERDATA_LEN int = 4096 - -type Metadata struct { - Name string `json:"name" yaml:"name"` - Description string `json:"description" yaml:"-"` - UserData1 string `json:"userData1" yaml:"-"` - UserData2 string `json:"userData2" yaml:"-"` -} - -// Check for valid format Metadata -func IsValidMetadata(metadata Metadata) error { - errs := validation.IsValidName(metadata.Name) - if len(errs) > 0 { - return pkgerrors.Errorf("Invalid Metadata name=[%v], errors: %v", metadata.Name, errs) - } - - errs = validation.IsValidString(metadata.Description, 0, MAX_DESCRIPTION_LEN, validation.VALID_ANY_STR) - if len(errs) > 0 { - return pkgerrors.Errorf("Invalid Metadata description=[%v], errors: %v", metadata.Description, errs) - } - - errs = validation.IsValidString(metadata.UserData1, 0, MAX_DESCRIPTION_LEN, validation.VALID_ANY_STR) - if len(errs) > 0 { - return pkgerrors.Errorf("Invalid Metadata description=[%v], errors: %v", metadata.UserData1, errs) - } - - errs = validation.IsValidString(metadata.UserData2, 0, MAX_DESCRIPTION_LEN, validation.VALID_ANY_STR) - if len(errs) > 0 { - return pkgerrors.Errorf("Invalid Metadata description=[%v], errors: %v", metadata.UserData2, errs) - } - - return nil -} diff --git a/src/orchestrator/pkg/module/types/types.go b/src/orchestrator/pkg/module/types/types.go new file mode 100644 index 00000000..0159444d --- /dev/null +++ b/src/orchestrator/pkg/module/types/types.go @@ -0,0 +1,58 @@ +/* + * Copyright 2020 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 types + +import ( + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" + pkgerrors "github.com/pkg/errors" +) + +// It implements the interface for managing the ClusterProviders +const MAX_DESCRIPTION_LEN int = 1024 +const MAX_USERDATA_LEN int = 4096 + +type Metadata struct { + Name string `json:"name" yaml:"name"` + Description string `json:"description" yaml:"-"` + UserData1 string `json:"userData1" yaml:"-"` + UserData2 string `json:"userData2" yaml:"-"` +} + +// Check for valid format Metadata +func IsValidMetadata(metadata Metadata) error { + errs := validation.IsValidName(metadata.Name) + if len(errs) > 0 { + return pkgerrors.Errorf("Invalid Metadata name=[%v], errors: %v", metadata.Name, errs) + } + + errs = validation.IsValidString(metadata.Description, 0, MAX_DESCRIPTION_LEN, validation.VALID_ANY_STR) + if len(errs) > 0 { + return pkgerrors.Errorf("Invalid Metadata description=[%v], errors: %v", metadata.Description, errs) + } + + errs = validation.IsValidString(metadata.UserData1, 0, MAX_DESCRIPTION_LEN, validation.VALID_ANY_STR) + if len(errs) > 0 { + return pkgerrors.Errorf("Invalid Metadata description=[%v], errors: %v", metadata.UserData1, errs) + } + + errs = validation.IsValidString(metadata.UserData2, 0, MAX_DESCRIPTION_LEN, validation.VALID_ANY_STR) + if len(errs) > 0 { + return pkgerrors.Errorf("Invalid Metadata description=[%v], errors: %v", metadata.UserData2, errs) + } + + return nil +} -- cgit 1.2.3-korg