1/* 2 * Copyright (C) 2017 Christian Muehlhaeuser 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Affero General Public License as published 6 * by the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Affero General Public License for more details. 13 * 14 * You should have received a copy of the GNU Affero General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * 17 * Authors: 18 * Christian Muehlhaeuser <muesli@gmail.com> 19 */ 20 21package bees 22 23import ( 24 "github.com/emicklei/go-restful" 25 "github.com/muesli/beehive/bees" 26 "github.com/muesli/smolder" 27) 28 29// BeePostStruct holds all values of an incoming POST request 30type BeePostStruct struct { 31 Bee struct { 32 Name string `json:"name"` 33 Namespace string `json:"namespace"` 34 Description string `json:"description"` 35 Active bool `json:"active"` 36 Options bees.BeeOptions `json:"options"` 37 } `json:"bee"` 38} 39 40// PostAuthRequired returns true because all requests need authentication 41func (r *BeeResource) PostAuthRequired() bool { 42 return false 43} 44 45// PostDoc returns the description of this API endpoint 46func (r *BeeResource) PostDoc() string { 47 return "create a new bee" 48} 49 50// PostParams returns the parameters supported by this API endpoint 51func (r *BeeResource) PostParams() []*restful.Parameter { 52 return nil 53} 54 55// Post processes an incoming POST (create) request 56func (r *BeeResource) Post(context smolder.APIContext, data interface{}, request *restful.Request, response *restful.Response) { 57 resp := BeeResponse{} 58 resp.Init(context) 59 60 pps := data.(*BeePostStruct) 61 c, err := bees.NewBeeConfig(pps.Bee.Name, pps.Bee.Namespace, pps.Bee.Description, pps.Bee.Options) 62 if err != nil { 63 smolder.ErrorResponseHandler(request, response, err, smolder.NewErrorResponse( 64 422, // Go 1.7+: http.StatusUnprocessableEntity, 65 err, 66 "BeeResource POST")) 67 return 68 } 69 70 bee := bees.StartBee(c) 71 resp.AddBee(bee) 72 73 resp.Send(response) 74} 75