aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/k8splugin/api')
-rw-r--r--src/k8splugin/api/api.go11
-rw-r--r--src/k8splugin/api/confighandler.go25
-rw-r--r--src/k8splugin/api/configtemplatehandler.go77
-rw-r--r--src/k8splugin/api/defhandler.go52
-rw-r--r--src/k8splugin/api/profilehandler.go64
-rw-r--r--src/k8splugin/api/profilehandler_test.go2
-rw-r--r--src/k8splugin/api/queryhandler.go2
7 files changed, 218 insertions, 15 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go
index 94fb9b34..a3e53dc8 100644
--- a/src/k8splugin/api/api.go
+++ b/src/k8splugin/api/api.go
@@ -104,6 +104,7 @@ func NewRouter(defClient rb.DefinitionManager,
resRouter.HandleFunc("/definition/{rbname}", defHandler.listVersionsHandler).Methods("GET")
resRouter.HandleFunc("/definition", defHandler.listAllHandler).Methods("GET")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.getHandler).Methods("GET")
+ resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.updateHandler).Methods("PUT")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.deleteHandler).Methods("DELETE")
//Setup resource bundle profile routes
@@ -115,6 +116,7 @@ func NewRouter(defClient rb.DefinitionManager,
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.listHandler).Methods("GET")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}/content", profileHandler.uploadHandler).Methods("POST")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.getHandler).Methods("GET")
+ resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.updateHandler).Methods("PUT")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.deleteHandler).Methods("DELETE")
// Config Template
@@ -123,8 +125,10 @@ func NewRouter(defClient rb.DefinitionManager,
}
templateHandler := rbTemplateHandler{client: templateClient}
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template", templateHandler.createHandler).Methods("POST")
+ resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template", templateHandler.listHandler).Methods("GET")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}/content", templateHandler.uploadHandler).Methods("POST")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}", templateHandler.getHandler).Methods("GET")
+ resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}", templateHandler.updateHandler).Methods("PUT")
resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}", templateHandler.deleteHandler).Methods("DELETE")
// Config value
@@ -136,9 +140,10 @@ func NewRouter(defClient rb.DefinitionManager,
instRouter.HandleFunc("/instance/{instID}/config", configHandler.listHandler).Methods("GET")
instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.getHandler).Methods("GET")
instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.updateHandler).Methods("PUT")
- instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.deleteHandler).Methods("DELETE")
- instRouter.HandleFunc("/instance/{instID}/config/rollback", configHandler.rollbackHandler).Methods("POST")
- instRouter.HandleFunc("/instance/{instID}/config/tagit", configHandler.tagitHandler).Methods("POST")
+ instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.deleteAllHandler).Methods("DELETE")
+ instRouter.HandleFunc("/instance/{instID}/config/{cfgname}/delete", configHandler.deleteHandler).Methods("POST")
+ instRouter.HandleFunc("/instance/{instID}/config/{cfgname}/rollback", configHandler.rollbackHandler).Methods("POST")
+ instRouter.HandleFunc("/instance/{instID}/config/{cfgname}/tagit", configHandler.tagitHandler).Methods("POST")
// Instance Healthcheck API
if healthcheckClient == nil {
diff --git a/src/k8splugin/api/confighandler.go b/src/k8splugin/api/confighandler.go
index c2236378..a4f08131 100644
--- a/src/k8splugin/api/confighandler.go
+++ b/src/k8splugin/api/confighandler.go
@@ -117,6 +117,22 @@ func (h rbConfigHandler) listHandler(w http.ResponseWriter, r *http.Request) {
}
// deleteHandler handles DELETE operations on a config
+func (h rbConfigHandler) deleteAllHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ instanceID := vars["instID"]
+ cfgName := vars["cfgname"]
+
+ err := h.client.DeleteAll(instanceID, cfgName)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+}
+
+// deleteHandler handles delete operations on a config creating its delete version
func (h rbConfigHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
instanceID := vars["instID"]
@@ -176,6 +192,7 @@ func (h rbConfigHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
instanceID := vars["instID"]
+ cfgName := vars["cfgname"]
if r.Body == nil {
http.Error(w, "Empty body", http.StatusBadRequest)
@@ -183,12 +200,13 @@ func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request)
}
var p app.ConfigRollback
- err := json.NewDecoder(r.Body).Decode(&p)
+ err := json.NewDecoder(r.Body).Decode(&p.AnyOf)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
- err = h.client.Rollback(instanceID, p)
+ err = h.client.Rollback(instanceID, cfgName, p)
+ //err = h.client.Cleanup(instanceID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -200,6 +218,7 @@ func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request)
func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
instanceID := vars["instID"]
+ cfgName := vars["cfgname"]
if r.Body == nil {
http.Error(w, "Empty body", http.StatusBadRequest)
@@ -213,7 +232,7 @@ func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
return
}
- err = h.client.Tagit(instanceID, p)
+ err = h.client.Tagit(instanceID, cfgName, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
diff --git a/src/k8splugin/api/configtemplatehandler.go b/src/k8splugin/api/configtemplatehandler.go
index bd7c2db9..e8750fd8 100644
--- a/src/k8splugin/api/configtemplatehandler.go
+++ b/src/k8splugin/api/configtemplatehandler.go
@@ -20,9 +20,10 @@ import (
"encoding/json"
"io"
"io/ioutil"
- "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
"net/http"
+ "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
+
"github.com/gorilla/mux"
)
@@ -58,7 +59,7 @@ func (h rbTemplateHandler) createHandler(w http.ResponseWriter, r *http.Request)
return
}
- err = h.client.Create(rbName, rbVersion, p)
+ err = h.client.CreateOrUpdate(rbName, rbVersion, p, false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -122,6 +123,78 @@ func (h rbTemplateHandler) getHandler(w http.ResponseWriter, r *http.Request) {
}
}
+// createHandler handles creation of the template entry in the database
+func (h rbTemplateHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
+ var p rb.ConfigTemplate
+
+ vars := mux.Vars(r)
+ rbName := vars["rbname"]
+ rbVersion := vars["rbversion"]
+ templateName := vars["tname"]
+
+ 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
+ }
+
+ // Name is required.
+ if p.TemplateName == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.Get(rbName, rbVersion, templateName)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ if p.TemplateName != "" && p.TemplateName != ret.TemplateName {
+ http.Error(w, "Template name mismatch", http.StatusBadRequest)
+ return
+ }
+
+ err = h.client.CreateOrUpdate(rbName, rbVersion, p, 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(p)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// getHandler handles GET operations on a particular template
+func (h rbTemplateHandler) listHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ rbName := vars["rbname"]
+ rbVersion := vars["rbversion"]
+
+ ret, err := h.client.List(rbName, rbVersion)
+ 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 {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
// deleteHandler handles DELETE operations on a template
func (h rbTemplateHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go
index 480d4be5..3dea8ade 100644
--- a/src/k8splugin/api/defhandler.go
+++ b/src/k8splugin/api/defhandler.go
@@ -35,7 +35,7 @@ type rbDefinitionHandler struct {
client rb.DefinitionManager
}
-// createHandler handles creation of the definition entry in the database
+// createOrUpdateHandler handles creation of the definition entry in the database
func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Request) {
var v rb.Definition
@@ -48,20 +48,64 @@ func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Reques
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
+ h.createOrUpdateHandler(v, w, false)
+}
+
+// createOrUpdateHandler handles creation of the definition entry in the database
+func (h rbDefinitionHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ name := vars["rbname"]
+ version := vars["rbversion"]
+
+ var v rb.Definition
+ err := json.NewDecoder(r.Body).Decode(&v)
+ 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 v.RBVersion != "" && v.RBVersion != version {
+ http.Error(w, "RB version mismatch", http.StatusBadRequest)
+ return
+ }
+
+ if v.RBName != "" && v.RBName != name {
+ http.Error(w, "RB name mismatch", http.StatusBadRequest)
+ return
+ }
+
+ v.RBVersion = version
+ v.RBName = name
+
+ h.createOrUpdateHandler(v, w, true)
+}
+
+// createOrUpdateHandler handles creation of the definition entry in the database
+func (h rbDefinitionHandler) createOrUpdateHandler(v rb.Definition, w http.ResponseWriter, update bool) {
// Name is required.
if v.RBName == "" {
- http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ http.Error(w, "Missing name in request", http.StatusBadRequest)
return
}
// Version is required.
if v.RBVersion == "" {
- http.Error(w, "Missing version in POST request", http.StatusBadRequest)
+ http.Error(w, "Missing version in request", http.StatusBadRequest)
return
}
- ret, err := h.client.Create(v)
+ var ret rb.Definition
+ var err error
+ if update {
+ ret, err = h.client.Update(v)
+ } else {
+ ret, err = h.client.Create(v)
+ }
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
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) {
diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go
index 32d0061f..181b775b 100644
--- a/src/k8splugin/api/profilehandler_test.go
+++ b/src/k8splugin/api/profilehandler_test.go
@@ -42,7 +42,7 @@ type mockRBProfile struct {
Err error
}
-func (m *mockRBProfile) Create(inp rb.Profile) (rb.Profile, error) {
+func (m *mockRBProfile) CreateOrUpdate(inp rb.Profile, update bool) (rb.Profile, error) {
if m.Err != nil {
return rb.Profile{}, m.Err
}
diff --git a/src/k8splugin/api/queryhandler.go b/src/k8splugin/api/queryhandler.go
index 9c11954c..f5950cdf 100644
--- a/src/k8splugin/api/queryhandler.go
+++ b/src/k8splugin/api/queryhandler.go
@@ -52,7 +52,7 @@ func (i queryHandler) queryHandler(w http.ResponseWriter, r *http.Request) {
return
}
// instance id is irrelevant here
- resp, err := i.client.Query(namespace, cloudRegion, apiVersion, kind, name, labels, "query")
+ resp, err := i.client.Query(namespace, cloudRegion, apiVersion, kind, name, labels)
if err != nil {
log.Error("Error getting Query results", log.Fields{
"error": err,