aboutsummaryrefslogtreecommitdiffstats
path: root/src/orchestrator/api/controllerhandler.go
diff options
context:
space:
mode:
authorRitu Sood <Ritu.Sood@intel.com>2020-04-23 21:40:06 +0000
committerGerrit Code Review <gerrit@onap.org>2020-04-23 21:40:06 +0000
commit334322f5995394dfa6a948101064634eaaf5f0b5 (patch)
treee30d1625633de0e2ffb68fe195be75a2873a5edf /src/orchestrator/api/controllerhandler.go
parentb54eda4919487b700554a27cb46b6e6b465ad180 (diff)
parent0b59486c82a9786c85438f6352636b19b83e1021 (diff)
Merge "Controller API support"
Diffstat (limited to 'src/orchestrator/api/controllerhandler.go')
-rw-r--r--src/orchestrator/api/controllerhandler.go106
1 files changed, 100 insertions, 6 deletions
diff --git a/src/orchestrator/api/controllerhandler.go b/src/orchestrator/api/controllerhandler.go
index 4f98c023..1dad2bf8 100644
--- a/src/orchestrator/api/controllerhandler.go
+++ b/src/orchestrator/api/controllerhandler.go
@@ -22,7 +22,9 @@ import (
"net/http"
"github.com/gorilla/mux"
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation"
moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
+ pkgerrors "github.com/pkg/errors"
)
// Used to store backend implementations objects
@@ -33,6 +35,43 @@ type controllerHandler struct {
client moduleLib.ControllerManager
}
+// Check for valid format of input parameters
+func validateControllerInputs(c moduleLib.Controller) error {
+ // validate metadata
+ err := moduleLib.IsValidMetadata(c.Metadata)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Invalid controller metadata")
+ }
+
+ errs := validation.IsValidName(c.Spec.Host)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid host name for controller %v, errors: %v", c.Spec.Host, errs)
+ }
+
+ errs = validation.IsValidNumber(c.Spec.Port, 0, 65535)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid controller port [%v], errors: %v", c.Spec.Port, errs)
+ }
+
+ found := false
+ for _, val := range moduleLib.CONTROLLER_TYPES {
+ if c.Spec.Type == val {
+ found = true
+ break
+ }
+ }
+ if !found {
+ return pkgerrors.Errorf("Invalid controller type: %v", c.Spec.Type)
+ }
+
+ errs = validation.IsValidNumber(c.Spec.Priority, moduleLib.MinControllerPriority, moduleLib.MaxControllerPriority)
+ if len(errs) > 0 {
+ return pkgerrors.Errorf("Invalid controller priority = [%v], errors: %v", c.Spec.Priority, errs)
+ }
+
+ return nil
+}
+
// Create handles creation of the controller entry in the database
func (h controllerHandler) createHandler(w http.ResponseWriter, r *http.Request) {
var m moduleLib.Controller
@@ -48,12 +87,12 @@ func (h controllerHandler) createHandler(w http.ResponseWriter, r *http.Request)
}
// Name is required.
- if m.Name == "" {
+ if m.Metadata.Name == "" {
http.Error(w, "Missing name in POST request", http.StatusBadRequest)
return
}
- ret, err := h.client.CreateController(m)
+ ret, err := h.client.CreateController(m, false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -68,19 +107,74 @@ func (h controllerHandler) createHandler(w http.ResponseWriter, r *http.Request)
}
}
-// Get handles GET operations on a particular controller Name
-// Returns a controller
-func (h controllerHandler) getHandler(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
vars := mux.Vars(r)
name := vars["controller-name"]
- ret, err := h.client.GetController(name)
+ err := json.NewDecoder(r.Body).Decode(&m)
+ switch {
+ case err == io.EOF:
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ case err != nil:
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ // Name is required.
+ if m.Metadata.Name == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ // name in URL should match name in body
+ if m.Metadata.Name != name {
+ http.Error(w, "Mismatched name in PUT request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateController(m, true)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// Get handles GET operations on a particular controller Name
+// Returns a controller
+func (h controllerHandler) getHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ name := vars["controller-name"]
+ var ret interface{}
+ var err error
+
+ // handle the get all controllers case
+ if len(name) == 0 {
+ ret, err = h.client.GetControllers()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else {
+
+ ret, err = h.client.GetController(name)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ }
+
+ w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(ret)
if err != nil {