Files
fn-serverless/api/server/apps_delete.go
Travis Reeder 1c8d12b09e Cleanup main (#470)
* main: clean up

* server: replace magical constants and use them for app name tracking
2017-01-03 14:59:26 -08:00

59 lines
1.6 KiB
Go

package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/runner/common"
)
func (s *Server) handleAppDelete(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := common.Logger(ctx)
app := &models.App{Name: c.MustGet(api.AppName).(string)}
routes, err := s.Datastore.GetRoutesByApp(ctx, app.Name, &models.RouteFilter{})
if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
if len(routes) > 0 {
log.WithError(err).Debug(models.ErrDeleteAppsWithRoutes)
c.JSON(http.StatusBadRequest, simpleError(models.ErrDeleteAppsWithRoutes))
return
}
err = s.FireBeforeAppDelete(ctx, app)
if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
err = s.Datastore.RemoveApp(ctx, app.Name)
if err == models.ErrAppsNotFound {
log.WithError(err).Debug(models.ErrAppsRemoving)
c.JSON(http.StatusNotFound, simpleError(err))
return
} else if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
err = s.FireAfterAppDelete(ctx, app)
if err != nil {
log.WithError(err).Error(models.ErrAppsRemoving)
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
return
}
c.JSON(http.StatusOK, gin.H{"message": "App deleted"})
}