Build related cleanup. Entrypoint to read config from correct place.
[ric-plt/appmgr.git] / cmd / appmgr / api.go
index 2e7ddc7..7acb7c7 100755 (executable)
@@ -26,11 +26,16 @@ import (
        "github.com/spf13/viper"
        "log"
        "net/http"
+       "time"
 )
 
 // API functions
 
-func (m *XappManager) Initialize(h Helmer) {
+func (m *XappManager) Initialize(h Helmer, cm ConfigMapper) {
+       m.cm = cm
+       m.helm = h
+       m.helm.SetCM(cm)
+
        m.router = mux.NewRouter().StrictSlash(true)
 
        resources := []Resource{
@@ -38,6 +43,7 @@ func (m *XappManager) Initialize(h Helmer) {
                {"GET", "/ric/v1/health/ready", m.getHealthStatus},
 
                {"GET", "/ric/v1/xapps", m.getAllXapps},
+               {"GET", "/ric/v1/xapps/search", m.searchAllXapps},
                {"GET", "/ric/v1/xapps/{name}", m.getXappByName},
                {"GET", "/ric/v1/xapps/{name}/instances/{id}", m.getXappInstanceByName},
                {"POST", "/ric/v1/xapps", m.deployXapp},
@@ -51,11 +57,13 @@ func (m *XappManager) Initialize(h Helmer) {
 
                {"GET", "/ric/v1/config", m.getConfig},
                {"POST", "/ric/v1/config", m.createConfig},
+               {"PUT", "/ric/v1/config", m.updateConfig},
                {"DELETE", "/ric/v1/config", m.deleteConfig},
+               {"DELETE", "/ric/v1/config/{name}", m.deleteSingleConfig},
        }
 
        for _, resource := range resources {
-               handler := Logger(resource.HandlerFunc)
+               handler := LogRestRequests(resource.HandlerFunc)
                //handler = m.serviceChecker(handler)
                m.router.Methods(resource.Method).Path(resource.Url).Handler(handler)
        }
@@ -67,7 +75,6 @@ func (m *XappManager) finalize(h Helmer) {
        m.sd = SubscriptionDispatcher{}
        m.sd.Initialize()
 
-       m.helm = h
        m.helm.Initialize()
 
        m.notifyClients()
@@ -135,7 +142,7 @@ func (m *XappManager) getXappInstanceByName(w http.ResponseWriter, r *http.Reque
                        return
                }
        }
-       mdclog(MdclogErr, "Xapp instance not found - url="+r.URL.RequestURI())
+       Logger.Error("Xapp instance not found - url=%s", r.URL.RequestURI())
 
        respondWithError(w, http.StatusNotFound, "Xapp instance not found")
 }
@@ -150,16 +157,20 @@ func (m *XappManager) getAllXapps(w http.ResponseWriter, r *http.Request) {
        respondWithJSON(w, http.StatusOK, xapps)
 }
 
+func (m *XappManager) searchAllXapps(w http.ResponseWriter, r *http.Request) {
+       respondWithJSON(w, http.StatusOK, m.helm.SearchAll())
+}
+
 func (m *XappManager) deployXapp(w http.ResponseWriter, r *http.Request) {
        if r.Body == nil {
-               mdclog(MdclogErr, "No xapp data found in request body - url="+r.URL.RequestURI())
+               Logger.Error("No xapp data found in request body - url=%s", r.URL.RequestURI())
                respondWithError(w, http.StatusMethodNotAllowed, "No xapp data!")
                return
        }
 
-       var cm ConfigMetadata
+       var cm XappDeploy
        if err := json.NewDecoder(r.Body).Decode(&cm); err != nil {
-               mdclog(MdclogErr, "Invalid xapp data in request body - url="+r.URL.RequestURI())
+               Logger.Error("Invalid xapp data in request body - url=%s", r.URL.RequestURI())
                respondWithError(w, http.StatusMethodNotAllowed, "Invalid xapp data!")
                return
        }
@@ -171,6 +182,13 @@ func (m *XappManager) deployXapp(w http.ResponseWriter, r *http.Request) {
                return
        }
 
+       for i := 0; i < 3; i++ {
+               if xapp, err = m.helm.Status(xapp.Name); xapp.Instances != nil {
+                       break
+               }
+               time.Sleep(time.Duration(5) * time.Second)
+       }
+
        respondWithJSON(w, http.StatusCreated, xapp)
 
        m.sd.Publish(xapp, EventType("created"))
@@ -203,7 +221,7 @@ func (m *XappManager) getSubscription(w http.ResponseWriter, r *http.Request) {
                if s, ok := m.sd.Get(id); ok {
                        respondWithJSON(w, http.StatusOK, s)
                } else {
-                       mdclog(MdclogErr, "Subscription not found - url="+r.URL.RequestURI())
+                       Logger.Error("Subscription not found - url=%s", r.URL.RequestURI())
                        respondWithError(w, http.StatusNotFound, "Subscription not found")
                }
        }
@@ -214,7 +232,7 @@ func (m *XappManager) deleteSubscription(w http.ResponseWriter, r *http.Request)
                if _, ok := m.sd.Delete(id); ok {
                        respondWithJSON(w, http.StatusNoContent, nil)
                } else {
-                       mdclog(MdclogErr, "Subscription not found - url="+r.URL.RequestURI())
+                       Logger.Error("Subscription not found - url=%s", r.URL.RequestURI())
                        respondWithError(w, http.StatusNotFound, "Subscription not found")
                }
        }
@@ -223,7 +241,7 @@ func (m *XappManager) deleteSubscription(w http.ResponseWriter, r *http.Request)
 func (m *XappManager) addSubscription(w http.ResponseWriter, r *http.Request) {
        var req SubscriptionReq
        if r.Body == nil || json.NewDecoder(r.Body).Decode(&req) != nil {
-               mdclog(MdclogErr, "Invalid request payload - url="+r.URL.RequestURI())
+               Logger.Error("Invalid request payload - url=%s", r.URL.RequestURI())
                respondWithError(w, http.StatusMethodNotAllowed, "Invalid request payload")
                return
        }
@@ -236,7 +254,7 @@ func (m *XappManager) updateSubscription(w http.ResponseWriter, r *http.Request)
        if id, ok := getResourceId(r, w, "id"); ok == true {
                var req SubscriptionReq
                if r.Body == nil || json.NewDecoder(r.Body).Decode(&req) != nil {
-                       mdclog(MdclogErr, "Invalid request payload - url="+r.URL.RequestURI())
+                       Logger.Error("Invalid request payload - url=%s", r.URL.RequestURI())
                        respondWithError(w, http.StatusMethodNotAllowed, "Invalid request payload")
                        return
                }
@@ -245,7 +263,7 @@ func (m *XappManager) updateSubscription(w http.ResponseWriter, r *http.Request)
                if s, ok := m.sd.Update(id, req); ok {
                        respondWithJSON(w, http.StatusOK, s)
                } else {
-                       mdclog(MdclogErr, "Subscription not found - url="+r.URL.RequestURI())
+                       Logger.Error("Subscription not found - url=%s", r.URL.RequestURI())
                        respondWithError(w, http.StatusNotFound, "Subscription not found")
                }
        }
@@ -254,7 +272,7 @@ func (m *XappManager) updateSubscription(w http.ResponseWriter, r *http.Request)
 func (m *XappManager) notifyClients() {
        xapps, err := m.helm.StatusAll()
        if err != nil {
-               mdclog(MdclogInfo, "Couldn't fetch xapps status information"+err.Error())
+               Logger.Info("Couldn't fetch xapps status information: %v", err.Error())
                return
        }
 
@@ -262,7 +280,8 @@ func (m *XappManager) notifyClients() {
 }
 
 func (m *XappManager) getConfig(w http.ResponseWriter, r *http.Request) {
-       respondWithJSON(w, http.StatusOK, UploadConfig())
+       cfg := m.cm.UploadConfig()
+       respondWithJSON(w, http.StatusOK, cfg)
 }
 
 func (m *XappManager) createConfig(w http.ResponseWriter, r *http.Request) {
@@ -271,11 +290,42 @@ func (m *XappManager) createConfig(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       if err := CreateConfigMap(c); err != nil {
-               respondWithError(w, http.StatusInternalServerError, err.Error())
+       if errList, err := m.cm.CreateConfigMap(c); err != nil {
+               if err.Error() != "Validation failed!" {
+                       respondWithError(w, http.StatusInternalServerError, err.Error())
+               } else {
+                       respondWithJSON(w, http.StatusUnprocessableEntity, errList)
+               }
+               return
+       }
+       respondWithJSON(w, http.StatusCreated, c.Metadata)
+}
+
+func (m *XappManager) updateConfig(w http.ResponseWriter, r *http.Request) {
+       var c XAppConfig
+       if parseConfig(w, r, &c) != nil {
+               return
+       }
+
+       if errList, err := m.cm.UpdateConfigMap(c); err != nil {
+               if err.Error() != "Validation failed!" {
+                       respondWithError(w, http.StatusInternalServerError, err.Error())
+               } else {
+                       respondWithJSON(w, http.StatusInternalServerError, errList)
+               }
+               return
+       }
+       respondWithJSON(w, http.StatusOK, c.Metadata)
+}
+
+func (m *XappManager) deleteSingleConfig(w http.ResponseWriter, r *http.Request) {
+       xappName, ok := getResourceId(r, w, "name")
+       if ok != true {
                return
        }
-       respondWithJSON(w, http.StatusCreated, nil)
+
+       md := ConfigMetadata{Name: xappName, Namespace: m.cm.GetNamespace(""), ConfigName: xappName + "-appconfig"}
+       m.delConfig(w, XAppConfig{Metadata: md})
 }
 
 func (m *XappManager) deleteConfig(w http.ResponseWriter, r *http.Request) {
@@ -284,11 +334,15 @@ func (m *XappManager) deleteConfig(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       if _, err := DeleteConfigMap(c); err != nil {
+       m.delConfig(w, c)
+}
+
+func (m *XappManager) delConfig(w http.ResponseWriter, c XAppConfig) {
+       if _, err := m.cm.DeleteConfigMap(c); err != nil {
                respondWithError(w, http.StatusInternalServerError, err.Error())
                return
        }
-       respondWithJSON(w, http.StatusNotFound, nil)
+       respondWithJSON(w, http.StatusNoContent, nil)
 }
 
 // Helper functions
@@ -307,7 +361,7 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
 
 func getResourceId(r *http.Request, w http.ResponseWriter, pattern string) (id string, ok bool) {
        if id, ok = mux.Vars(r)[pattern]; ok != true {
-               mdclog(MdclogErr, "Couldn't resolve name/id from the request URL")
+               Logger.Error("Couldn't resolve name/id from the request URL")
                respondWithError(w, http.StatusMethodNotAllowed, "Couldn't resolve name/id from the request URL")
                return
        }
@@ -316,7 +370,7 @@ func getResourceId(r *http.Request, w http.ResponseWriter, pattern string) (id s
 
 func parseConfig(w http.ResponseWriter, r *http.Request, req *XAppConfig) error {
        if r.Body == nil || json.NewDecoder(r.Body).Decode(&req) != nil {
-               mdclog(MdclogErr, "Invalid request payload - url="+r.URL.RequestURI())
+               Logger.Error("Invalid request payload - url=%s", r.URL.RequestURI())
                respondWithError(w, http.StatusMethodNotAllowed, "Invalid request payload")
                return errors.New("Invalid payload")
        }