diff options
author | Eric Multanen <eric.w.multanen@intel.com> | 2020-04-16 10:44:06 -0700 |
---|---|---|
committer | Eric Multanen <eric.w.multanen@intel.com> | 2020-04-22 23:30:24 -0700 |
commit | 0b59486c82a9786c85438f6352636b19b83e1021 (patch) | |
tree | 06af2aa3929357f9cc4e328cd09c7fce554cbf43 /src/orchestrator/api/controllerhandler.go | |
parent | 9e086eb494441de0967b84d0f04d3b4dc7e753c2 (diff) |
Controller API support
Add controller API support as baseline for adding
gRPC framework.
Issue-ID: MULTICLOUD-1019
Signed-off-by: Eric Multanen <eric.w.multanen@intel.com>
Change-Id: Ifd522a0eefbb8e54be45cc62003d3809283c9bfe
Diffstat (limited to 'src/orchestrator/api/controllerhandler.go')
-rw-r--r-- | src/orchestrator/api/controllerhandler.go | 106 |
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 { |