github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/apps_delete.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/iron-io/functions/api"
     9  	"github.com/iron-io/functions/api/models"
    10  	"github.com/iron-io/runner/common"
    11  )
    12  
    13  func (s *Server) handleAppDelete(c *gin.Context) {
    14  	ctx := c.MustGet("ctx").(context.Context)
    15  	log := common.Logger(ctx)
    16  
    17  	app := &models.App{Name: c.MustGet(api.AppName).(string)}
    18  
    19  	routes, err := s.Datastore.GetRoutesByApp(ctx, app.Name, &models.RouteFilter{})
    20  	if err != nil {
    21  		log.WithError(err).Error(models.ErrAppsRemoving)
    22  		c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
    23  		return
    24  	}
    25  	//TODO allow this? #528
    26  	if len(routes) > 0 {
    27  		log.WithError(err).Debug(models.ErrDeleteAppsWithRoutes)
    28  		c.JSON(http.StatusBadRequest, simpleError(models.ErrDeleteAppsWithRoutes))
    29  		return
    30  	}
    31  
    32  	err = s.FireBeforeAppDelete(ctx, app)
    33  	if err != nil {
    34  		log.WithError(err).Error(models.ErrAppsRemoving)
    35  		c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
    36  		return
    37  	}
    38  
    39  	err = s.Datastore.RemoveApp(ctx, app.Name)
    40  	if err != nil {
    41  		handleErrorResponse(c, err)
    42  		return
    43  	}
    44  
    45  	err = s.FireAfterAppDelete(ctx, app)
    46  	if err != nil {
    47  		log.WithError(err).Error(models.ErrAppsRemoving)
    48  		c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
    49  		return
    50  	}
    51  
    52  	c.JSON(http.StatusOK, gin.H{"message": "App deleted"})
    53  }