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

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"path"
     7  
     8  	"github.com/gin-gonic/gin"
     9  	"github.com/iron-io/functions/api"
    10  	"github.com/iron-io/functions/api/models"
    11  	"github.com/iron-io/runner/common"
    12  )
    13  
    14  func (s *Server) handleRouteUpdate(c *gin.Context) {
    15  	ctx := c.MustGet("ctx").(context.Context)
    16  	log := common.Logger(ctx)
    17  
    18  	var wroute models.RouteWrapper
    19  
    20  	err := c.BindJSON(&wroute)
    21  	if err != nil {
    22  		log.WithError(err).Debug(models.ErrInvalidJSON)
    23  		c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
    24  		return
    25  	}
    26  
    27  	if wroute.Route == nil {
    28  		log.Debug(models.ErrRoutesMissingNew)
    29  		c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesMissingNew))
    30  		return
    31  	}
    32  
    33  	if wroute.Route.Path != "" {
    34  		log.Debug(models.ErrRoutesPathImmutable)
    35  		c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesPathImmutable))
    36  		return
    37  	}
    38  
    39  	wroute.Route.AppName = c.MustGet(api.AppName).(string)
    40  	wroute.Route.Path = path.Clean(c.MustGet(api.Path).(string))
    41  
    42  	if err := wroute.Validate(true); err != nil {
    43  		log.WithError(err).Debug(models.ErrRoutesUpdate)
    44  		c.JSON(http.StatusBadRequest, simpleError(err))
    45  		return
    46  	}
    47  
    48  	if wroute.Route.Image != "" {
    49  		// err = s.Runner.EnsureImageExists(ctx, &task.Config{
    50  		// 	Image: wroute.Route.Image,
    51  		// })
    52  		// if err != nil {
    53  		// 	log.WithError(err).Debug(models.ErrRoutesUpdate)
    54  		// 	c.JSON(http.StatusBadRequest, simpleError(models.ErrUsableImage))
    55  		// 	return
    56  		// }
    57  	}
    58  
    59  	route, err := s.Datastore.UpdateRoute(ctx, wroute.Route)
    60  	if err != nil {
    61  		handleErrorResponse(c, err)
    62  		return
    63  	}
    64  
    65  	s.cacherefresh(route)
    66  
    67  	c.JSON(http.StatusOK, routeResponse{"Route successfully updated", route})
    68  }