woodpecker/api/node.go

81 lines
1.4 KiB
Go
Raw Normal View History

package api
2015-09-30 01:21:17 +00:00
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/context"
"github.com/drone/drone/store"
2015-09-30 01:21:17 +00:00
)
func GetNodes(c *gin.Context) {
nodes, err := store.GetNodeList(c)
2015-09-30 01:21:17 +00:00
if err != nil {
c.String(400, err.Error())
2015-09-30 01:21:17 +00:00
} else {
c.JSON(200, nodes)
2015-09-30 01:21:17 +00:00
}
}
func GetNode(c *gin.Context) {
}
func PostNode(c *gin.Context) {
engine := context.Engine(c)
in := struct {
Addr string `json:"address"`
Arch string `json:"architecture"`
Cert string `json:"cert"`
Key string `json:"key"`
CA string `json:"ca"`
}{}
err := c.Bind(&in)
2015-09-30 01:21:17 +00:00
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
node := &model.Node{}
node.Addr = in.Addr
node.Cert = in.Cert
node.Key = in.Key
node.CA = in.CA
2015-09-30 01:21:17 +00:00
node.Arch = "linux_amd64"
err = engine.Allocate(node)
2015-09-30 01:21:17 +00:00
if err != nil {
c.String(http.StatusBadRequest, err.Error())
2015-09-30 01:21:17 +00:00
return
}
err = store.CreateNode(c, node)
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithStatus(http.StatusInternalServerError)
return
2015-09-30 01:21:17 +00:00
}
c.IndentedJSON(http.StatusOK, node)
2015-09-30 01:21:17 +00:00
}
func DeleteNode(c *gin.Context) {
engine := context.Engine(c)
id, _ := strconv.Atoi(c.Param("node"))
node, err := store.GetNode(c, int64(id))
2015-09-30 01:21:17 +00:00
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
err = store.DeleteNode(c, node)
2015-09-30 01:21:17 +00:00
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
engine.Deallocate(node)
}