diff options
author | Lukasz Rajewski <lukasz.rajewski@orange.com> | 2021-10-15 12:11:44 +0200 |
---|---|---|
committer | Lukasz Rajewski <lukasz.rajewski@orange.com> | 2021-10-15 12:12:44 +0200 |
commit | b2f51225bc78212682fd087a4ef8a67c51a94188 (patch) | |
tree | 6984ad7ca8a6af18658957d4fa6be9d029d11111 /src/k8splugin/api/profilehandler.go | |
parent | 7e265aa8286a5e77ddc63fe2f9e20c776792e2f1 (diff) |
Expose Update Handlers
Expose Update Handlers for Definition, Profile and Config Tmpl
Issue-ID: MULTICLOUD-1410
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@orange.com>
Change-Id: Ibe6fe05458f2af28f3e1ca14a54492a4bae19362
Diffstat (limited to 'src/k8splugin/api/profilehandler.go')
-rw-r--r-- | src/k8splugin/api/profilehandler.go | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/k8splugin/api/profilehandler.go b/src/k8splugin/api/profilehandler.go index acd23060..1babc4ae 100644 --- a/src/k8splugin/api/profilehandler.go +++ b/src/k8splugin/api/profilehandler.go @@ -56,7 +56,7 @@ func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) return } - ret, err := h.client.Create(p) + ret, err := h.client.CreateOrUpdate(p, false) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -127,6 +127,68 @@ func (h rbProfileHandler) getHandler(w http.ResponseWriter, r *http.Request) { } } +// updateHandler updates Profile Key in the database +// Returns an rb.Profile +func (h rbProfileHandler) updateHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + rbName := vars["rbname"] + rbVersion := vars["rbversion"] + prName := vars["prname"] + + ret, err := h.client.Get(rbName, rbVersion, prName) + if err != nil { + // Separate "Not found" from generic DB errors + if strings.Contains(err.Error(), "Error finding") { + http.Error(w, err.Error(), http.StatusNotFound) + return + } else { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + var p rb.Profile + + err = json.NewDecoder(r.Body).Decode(&p) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + if p.ProfileName != "" && p.ProfileName != ret.ProfileName { + http.Error(w, "Profile name mismatch", http.StatusBadRequest) + return + } + + if p.RBVersion != "" && p.RBVersion != ret.RBVersion { + http.Error(w, "RB version mismatch", http.StatusBadRequest) + return + } + + if p.RBName != "" && p.RBName != ret.RBName { + http.Error(w, "RB name mismatch", http.StatusBadRequest) + return + } + + p.ProfileName = ret.ProfileName + p.RBVersion = ret.RBVersion + p.RBName = ret.RBName + + ret, err = h.client.CreateOrUpdate(p, true) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // getHandler gets all profiles of a Resource Bundle Key in the database // Returns a list of rb.Profile func (h rbProfileHandler) listHandler(w http.ResponseWriter, r *http.Request) { |