/
agents.go
46 lines (41 loc) · 1.25 KB
/
agents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package api
import (
"encoding/json"
"github.com/Peripli/service-manager/pkg/agents"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/web"
"net/http"
)
type AgentsController struct {
agentsConfig *agents.Settings
}
func NewAgentsController(agentsConfig *agents.Settings) *AgentsController {
return &AgentsController{
agentsConfig: agentsConfig,
}
}
func (c *AgentsController) GetSupportedVersions(req *web.Request) (resp *web.Response, err error) {
if c.agentsConfig != nil && len(c.agentsConfig.Versions) > 0 {
var supportedVersions = make(map[string][]string)
if err := json.Unmarshal([]byte(c.agentsConfig.Versions), &supportedVersions); err != nil {
return nil, &util.HTTPError{
ErrorType: http.StatusText(http.StatusInternalServerError),
Description: "failed to retrieve agents supported versions",
StatusCode: http.StatusInternalServerError,
}
}
return util.NewJSONResponse(http.StatusOK, supportedVersions)
}
return util.NewJSONResponse(http.StatusOK, map[string][]string{})
}
func (c *AgentsController) Routes() []web.Route {
return []web.Route{
{
Endpoint: web.Endpoint{
Method: http.MethodGet,
Path: web.AgentsURL,
},
Handler: c.GetSupportedVersions,
},
}
}