diff options
author | Ritu Sood <Ritu.Sood@intel.com> | 2020-04-23 21:40:06 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-04-23 21:40:06 +0000 |
commit | 334322f5995394dfa6a948101064634eaaf5f0b5 (patch) | |
tree | e30d1625633de0e2ffb68fe195be75a2873a5edf /src/orchestrator/pkg/module | |
parent | b54eda4919487b700554a27cb46b6e6b465ad180 (diff) | |
parent | 0b59486c82a9786c85438f6352636b19b83e1021 (diff) |
Merge "Controller API support"
Diffstat (limited to 'src/orchestrator/pkg/module')
-rw-r--r-- | src/orchestrator/pkg/module/controller.go | 75 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/controller_test.go | 39 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/module.go | 41 |
3 files changed, 129 insertions, 26 deletions
diff --git a/src/orchestrator/pkg/module/controller.go b/src/orchestrator/pkg/module/controller.go index 35d6f892..976b8988 100644 --- a/src/orchestrator/pkg/module/controller.go +++ b/src/orchestrator/pkg/module/controller.go @@ -27,13 +27,24 @@ import ( // Controller contains the parameters needed for Controllers // It implements the interface for managing the Controllers type Controller struct { - Name string `json:"name"` - - Host string `json:"host"` + Metadata Metadata `json:"metadata"` + Spec ControllerSpec `json:"spec"` +} - Port int64 `json:"port"` +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"` @@ -52,8 +63,9 @@ func (mk ControllerKey) String() string { // ControllerManager is an interface exposes the Controller functionality type ControllerManager interface { - CreateController(ms Controller) (Controller, error) + CreateController(ms Controller, mayExist bool) (Controller, error) GetController(name string) (Controller, error) + GetControllers() ([]Controller, error) DeleteController(name string) error } @@ -74,20 +86,20 @@ func NewControllerClient() *ControllerClient { } // CreateController a new collection based on the Controller -func (mc *ControllerClient) CreateController(m Controller) (Controller, error) { +func (mc *ControllerClient) CreateController(m Controller, mayExist bool) (Controller, error) { //Construct the composite key to select the entry key := ControllerKey{ - ControllerName: m.Name, + ControllerName: m.Metadata.Name, } //Check if this Controller already exists - _, err := mc.GetController(m.Name) - if err == nil { + _, err := mc.GetController(m.Metadata.Name) + if err == nil && !mayExist { return Controller{}, pkgerrors.New("Controller already exists") } - err = db.DBconn.Create(mc.collectionName, key, mc.tagMeta, m) + err = db.DBconn.Insert(mc.collectionName, key, nil, mc.tagMeta, m) if err != nil { return Controller{}, pkgerrors.Wrap(err, "Creating DB Entry") } @@ -102,15 +114,14 @@ func (mc *ControllerClient) GetController(name string) (Controller, error) { key := ControllerKey{ ControllerName: name, } - value, err := db.DBconn.Read(mc.collectionName, key, mc.tagMeta) + value, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) if err != nil { return Controller{}, pkgerrors.Wrap(err, "Get Controller") } - //value is a byte array if value != nil { microserv := Controller{} - err = db.DBconn.Unmarshal(value, µserv) + err = db.DBconn.Unmarshal(value[0], µserv) if err != nil { return Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") } @@ -120,6 +131,42 @@ func (mc *ControllerClient) GetController(name string) (Controller, error) { 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") + } + + // run healthcheck + /* + err = mc.HealthCheck(microserv.Name) + if err != nil { + log.Warn("HealthCheck Failed", log.Fields{ + "Controller": microserv.Name, + }) + } + */ + resp = append(resp, microserv) + } + + return resp, nil +} + // DeleteController the Controller from database func (mc *ControllerClient) DeleteController(name string) error { @@ -127,7 +174,7 @@ func (mc *ControllerClient) DeleteController(name string) error { key := ControllerKey{ ControllerName: name, } - err := db.DBconn.Delete(name, key, mc.tagMeta) + err := db.DBconn.Remove(mc.collectionName, key) if err != nil { return pkgerrors.Wrap(err, "Delete Controller Entry;") } diff --git a/src/orchestrator/pkg/module/controller_test.go b/src/orchestrator/pkg/module/controller_test.go index 2e783c13..a13f43a5 100644 --- a/src/orchestrator/pkg/module/controller_test.go +++ b/src/orchestrator/pkg/module/controller_test.go @@ -37,14 +37,22 @@ func TestCreateController(t *testing.T) { { label: "Create Controller", inp: Controller{ - Name: "testController", - Host: "132.156.0.10", - Port: 8080, + Metadata: Metadata{ + Name: "testController", + }, + Spec: ControllerSpec{ + Host: "132.156.0.10", + Port: 8080, + }, }, expected: Controller{ - Name: "testController", - Host: "132.156.0.10", - Port: 8080, + Metadata: Metadata{ + Name: "testController", + }, + Spec: ControllerSpec{ + Host: "132.156.0.10", + Port: 8080, + }, }, expectedError: "", mockdb: &db.MockDB{}, @@ -62,7 +70,7 @@ func TestCreateController(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewControllerClient() - got, err := impl.CreateController(testCase.inp) + got, err := impl.CreateController(testCase.inp, false) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create returned an unexpected error %s", err) @@ -94,18 +102,25 @@ func TestGetController(t *testing.T) { label: "Get Controller", name: "testController", expected: Controller{ - Name: "testController", - Host: "132.156.0.10", - Port: 8080, + 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( - "{\"name\":\"testController\"," + + "{\"metadata\":{" + + "\"name\":\"testController\"" + + "}," + + "\"spec\":{" + "\"host\":\"132.156.0.10\"," + - "\"port\":8080}"), + "\"port\": 8080 }}"), }, }, }, diff --git a/src/orchestrator/pkg/module/module.go b/src/orchestrator/pkg/module/module.go index e05b8753..463a55b3 100644 --- a/src/orchestrator/pkg/module/module.go +++ b/src/orchestrator/pkg/module/module.go @@ -16,6 +16,11 @@ package module +import ( + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" + pkgerrors "github.com/pkg/errors" +) + // Client for using the services in the orchestrator type Client struct { Project *ProjectClient @@ -49,3 +54,39 @@ 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 +} |