From acddbe2e281ebf80d31e7a03646ccfa0a9d6b25b Mon Sep 17 00:00:00 2001 From: sanjaymekhale Date: Fri, 25 Oct 2019 06:45:06 +0000 Subject: Implementing suggested changes Renamed package from logs to logutils Issue-ID: MULTICLOUD-577 Change-Id: I05e2acbfbb5dd79bd26df73e0ad64c2068b9f6e5 Signed-off-by: sanjaymekhale --- src/k8splugin/api/instancehandler.go | 18 ++++++++++++++++++ src/k8splugin/go.mod | 1 + src/k8splugin/internal/logutils/logger.go | 15 +++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/k8splugin/internal/logutils/logger.go (limited to 'src') diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go index ab98e4be..1dcbcda9 100644 --- a/src/k8splugin/api/instancehandler.go +++ b/src/k8splugin/api/instancehandler.go @@ -23,6 +23,7 @@ import ( "github.com/gorilla/mux" pkgerrors "github.com/pkg/errors" + log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils" ) // Used to store the backend implementation objects @@ -36,14 +37,18 @@ func (i instanceHandler) validateBody(body interface{}) error { switch b := body.(type) { case app.InstanceRequest: if b.CloudRegion == "" { + log.WithFields("CreateVnfRequest bad request", "CloudRegion", "Invalid/Missing CloudRegion in POST request") werr := pkgerrors.Wrap(errors.New("Invalid/Missing CloudRegion in POST request"), "CreateVnfRequest bad request") return werr } if b.RBName == "" || b.RBVersion == "" { + log.WithFields("CreateVnfRequest bad request", "RBName", "Invalid/Missing resource bundle parameters in POST request") + log.WithFields("CreateVnfRequest bad request", "RBVersion", "Invalid/Missing resource bundle parameters in POST request") werr := pkgerrors.Wrap(errors.New("Invalid/Missing resource bundle parameters in POST request"), "CreateVnfRequest bad request") return werr } if b.ProfileName == "" { + log.WithFields("CreateVnfRequest bad request", "ProfileName", "Invalid/Missing profile name in POST request") werr := pkgerrors.Wrap(errors.New("Invalid/Missing profile name in POST request"), "CreateVnfRequest bad request") return werr } @@ -57,9 +62,11 @@ func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) { err := json.NewDecoder(r.Body).Decode(&resource) switch { case err == io.EOF: + log.WithFields("http.StatusBadRequest", "Error", "Body empty") http.Error(w, "Body empty", http.StatusBadRequest) return case err != nil: + log.WithFields("http.StatusUnprocessableEntity", "Error", "http.StatusUnprocessableEntity") http.Error(w, err.Error(), http.StatusUnprocessableEntity) return } @@ -67,12 +74,14 @@ func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) { // Check body for expected parameters err = i.validateBody(resource) if err != nil { + log.WithFields("StatusUnprocessableEntity", "Error", "http.StatusUnprocessableEntity") http.Error(w, err.Error(), http.StatusUnprocessableEntity) return } resp, err := i.client.Create(resource) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -81,6 +90,7 @@ func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) err = json.NewEncoder(w).Encode(resp) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -93,6 +103,7 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) { resp, err := i.client.Get(id) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -101,6 +112,7 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(resp) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -113,6 +125,7 @@ func (i instanceHandler) statusHandler(w http.ResponseWriter, r *http.Request) { resp, err := i.client.Status(id) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -121,6 +134,7 @@ func (i instanceHandler) statusHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(resp) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -137,6 +151,7 @@ func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) { resp, err := i.client.List(rbName, rbVersion, ProfileName) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -145,6 +160,7 @@ func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(resp) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -157,6 +173,7 @@ func (i instanceHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { err := i.client.Delete(id) if err != nil { + log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -164,3 +181,4 @@ func (i instanceHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) } + diff --git a/src/k8splugin/go.mod b/src/k8splugin/go.mod index aab4cd2f..f924828d 100644 --- a/src/k8splugin/go.mod +++ b/src/k8splugin/go.mod @@ -63,6 +63,7 @@ require ( github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.8.1 github.com/rubenv/sql-migrate v0.0.0-20190902133344-8926f37f0bc1 // indirect + github.com/sirupsen/logrus v1.4.2 github.com/soheilhy/cmux v0.1.4 // indirect github.com/technosophos/moniker v0.0.0-20180509230615-a5dbd03a2245 // indirect github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51 // indirect diff --git a/src/k8splugin/internal/logutils/logger.go b/src/k8splugin/internal/logutils/logger.go new file mode 100644 index 00000000..7df23474 --- /dev/null +++ b/src/k8splugin/internal/logutils/logger.go @@ -0,0 +1,15 @@ +package logutils + +import ( + log "github.com/sirupsen/logrus" +) + +func init() { + // Log as JSON instead of the default ASCII formatter. + log.SetFormatter(&log.JSONFormatter{}) +} + +func WithFields(msg string, fkey string, fvalue string) { + log.WithFields(log.Fields{fkey: fvalue}).Error(msg) +} + -- cgit 1.2.3-korg