forked from JamesClonk/backman
-
Notifications
You must be signed in to change notification settings - Fork 18
/
state.go
52 lines (47 loc) · 1.23 KB
/
state.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
47
48
49
50
51
52
package api
import (
"fmt"
"net/http"
"net/url"
echo "github.com/labstack/echo/v4"
"github.com/swisscom/backman/log"
"github.com/swisscom/backman/state"
)
// swagger:route GET /api/v1/states state listStates
// Lists current/last operation state of all service types.
//
// produces:
// - application/json
//
// schemes: http, https
//
// responses:
// 200: states
func (h *Handler) ListStates(c echo.Context) error {
states := state.Tracker().List()
return c.JSON(http.StatusOK, states)
}
// swagger:route GET /api/v1/state/{service_type}/{service_name} state getState
// Returns current/last operation state of backup/restore.
//
// produces:
// - application/json
//
// schemes: http, https
//
// responses:
// 200: state
func (h *Handler) GetState(c echo.Context) error {
serviceType := c.Param("service_type")
serviceName, err := url.QueryUnescape(c.Param("service_name"))
if err != nil {
log.Errorf("%v", err)
return c.JSON(http.StatusBadRequest, fmt.Sprintf("invalid service name: %v", err))
}
service := h.Service.GetService(serviceType, serviceName)
state, found := state.Tracker().Get(service)
if !found {
return c.JSON(http.StatusNotFound, fmt.Errorf("service state not found"))
}
return c.JSON(http.StatusOK, state)
}