Files
fn-serverless/api/server/apps_update.go
Pedro Nasser 49a7712e6b API improvements (#410)
* api improvements, remove global Api object and reduce gin dependency

* requested changes
2016-12-09 15:24:35 -02:00

63 lines
1.5 KiB
Go

package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/runner/common"
)
func (s *Server) handleAppUpdate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := common.Logger(ctx)
wapp := models.AppWrapper{}
err := c.BindJSON(&wapp)
if err != nil {
log.WithError(err).Debug(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
return
}
if wapp.App == nil {
log.Debug(models.ErrAppsMissingNew)
c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsMissingNew))
return
}
if wapp.App.Name != "" {
log.Debug(models.ErrAppsNameImmutable)
c.JSON(http.StatusForbidden, simpleError(models.ErrAppsNameImmutable))
return
}
wapp.App.Name = ctx.Value("appName").(string)
err = s.FireAfterAppUpdate(ctx, wapp.App)
if err != nil {
log.WithError(err).Errorln(models.ErrAppsUpdate)
c.JSON(http.StatusInternalServerError, simpleError(err))
return
}
app, err := s.Datastore.UpdateApp(ctx, wapp.App)
if err != nil {
log.WithError(err).Debug(models.ErrAppsUpdate)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsUpdate))
return
}
err = s.FireAfterAppUpdate(ctx, wapp.App)
if err != nil {
log.WithError(err).Errorln(models.ErrAppsUpdate)
c.JSON(http.StatusInternalServerError, simpleError(err))
return
}
// Nothing to update right now in apps
c.JSON(http.StatusOK, appResponse{"App successfully updated", app})
}