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// PutAuthRequired returns true because all requests need authentication
30func (r *BeeResource) PutAuthRequired() bool {
31	return false
32}
33
34// PutDoc returns the description of this API endpoint
35func (r *BeeResource) PutDoc() string {
36	return "update an existing bee"
37}
38
39// PutParams returns the parameters supported by this API endpoint
40func (r *BeeResource) PutParams() []*restful.Parameter {
41	return nil
42}
43
44// Put processes an incoming PUT (update) request
45func (r *BeeResource) Put(context smolder.APIContext, data interface{}, request *restful.Request, response *restful.Response) {
46	resp := BeeResponse{}
47	resp.Init(context)
48
49	pps := data.(*BeePostStruct)
50	id := request.PathParameter("bee-id")
51	bee := bees.GetBee(id)
52	if bee == nil {
53		r.NotFound(request, response)
54		return
55	}
56
57	(*bee).SetDescription(pps.Bee.Description)
58	(*bee).ReloadOptions(pps.Bee.Options)
59
60	if pps.Bee.Active {
61		bees.RestartBee(bee)
62	} else {
63		(*bee).Stop()
64	}
65
66	resp.AddBee(bee)
67	resp.Send(response)
68}
69