mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* functions: application updates no longer accept name in the body AppUpdate was initially conceived as an upsert endpoint for apps. It turns out that it created an inconsistency regarding updates: updates with names divergent with URL would not actually change application's name. This commit atempts to address the issue by returning an HTTP error when trying to update an application name. In swagger.yml, application names are already `readOnly:true`. Thus there is no change from expected behavior. Fixes #380 * functions: use specific error value for name change
65 lines
1.5 KiB
Go
65 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 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 = c.Param("app")
|
|
|
|
err = Api.FireAfterAppUpdate(ctx, wapp.App)
|
|
if err != nil {
|
|
log.WithError(err).Errorln(models.ErrAppsUpdate)
|
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
|
return
|
|
}
|
|
|
|
app, err := Api.Datastore.UpdateApp(ctx, wapp.App)
|
|
if err != nil {
|
|
log.WithError(err).Debug(models.ErrAppsUpdate)
|
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsUpdate))
|
|
return
|
|
}
|
|
|
|
err = Api.FireAfterAppUpdate(ctx, wapp.App)
|
|
if err != nil {
|
|
log.WithError(err).Errorln(models.ErrAppsUpdate)
|
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
|
return
|
|
}
|
|
|
|
wapp.App = app
|
|
|
|
// Nothing to update right now in apps
|
|
c.JSON(http.StatusOK, appResponse{"App successfully updated", wapp.App})
|
|
}
|