Files
fn-serverless/api/server/routes_create.go
Pedro Nasser 32de7d5361 Improvements on API error, swagger and status code (#428)
* improvements on API error, swagger and status code

* missing validation

* removing typo

* fix if-within-if

* fix handle app delete
2016-12-13 19:18:52 -02:00

104 lines
2.9 KiB
Go

package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/api/runner/task"
"github.com/iron-io/runner/common"
)
func (s *Server) handleRouteCreate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := common.Logger(ctx)
var wroute models.RouteWrapper
err := c.BindJSON(&wroute)
if err != nil {
log.WithError(err).Debug(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
return
}
if wroute.Route == nil {
log.WithError(err).Debug(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesMissingNew))
return
}
wroute.Route.AppName = ctx.Value("appName").(string)
if err := wroute.Validate(); err != nil {
log.WithError(err).Debug(models.ErrRoutesCreate)
c.JSON(http.StatusBadRequest, simpleError(err))
return
}
if wroute.Route.Image == "" {
log.WithError(models.ErrRoutesValidationMissingImage).Debug(models.ErrRoutesCreate)
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesValidationMissingImage))
return
}
err = s.Runner.EnsureImageExists(ctx, &task.Config{
Image: wroute.Route.Image,
})
if err != nil {
c.JSON(http.StatusBadRequest, simpleError(models.ErrUsableImage))
return
}
app, err := s.Datastore.GetApp(ctx, wroute.Route.AppName)
if err != nil && err != models.ErrAppsNotFound {
log.WithError(err).Error(models.ErrAppsGet)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
return
} else if app == nil {
// Create a new application and add the route to that new application
newapp := &models.App{Name: wroute.Route.AppName}
if err := newapp.Validate(); err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, simpleError(err))
return
}
err = s.FireBeforeAppCreate(ctx, newapp)
if err != nil {
log.WithError(err).Error(models.ErrAppsCreate)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
_, err = s.Datastore.InsertApp(ctx, newapp)
if err != nil {
log.WithError(err).Error(models.ErrRoutesCreate)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
err = s.FireAfterAppCreate(ctx, newapp)
if err != nil {
log.WithError(err).Error(models.ErrRoutesCreate)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
}
route, err := s.Datastore.InsertRoute(ctx, wroute.Route)
if err == models.ErrRoutesAlreadyExists {
log.WithError(err).Debug(models.ErrRoutesCreate)
c.JSON(http.StatusConflict, simpleError(models.ErrRoutesAlreadyExists))
return
} else if err != nil {
log.WithError(err).Error(models.ErrRoutesCreate)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
c.JSON(http.StatusOK, routeResponse{"Route successfully created", route})
}