/
node.go
135 lines (118 loc) · 3.79 KB
/
node.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package router
import (
"net/http"
"time"
"github.com/cloud-barista/cb-ladybug/src/core/common"
"github.com/cloud-barista/cb-ladybug/src/core/model"
"github.com/cloud-barista/cb-ladybug/src/rest-api/service"
"github.com/cloud-barista/cb-ladybug/src/utils/app"
"github.com/labstack/echo/v4"
)
// ListNode
// @Tags Node
// @Summary List Node
// @Description List Node
// @ID ListNode
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param cluster path string true "cluster"
// @Success 200 {object} model.NodeList
// @Router /ns/{namespace}/clusters/{cluster}/nodes [get]
func ListNode(c echo.Context) error {
if err := app.Validate(c, []string{"cluster"}); err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
nodeList, err := service.ListNode(c.Param("namespace"), c.Param("cluster"))
if err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
return app.Send(c, http.StatusOK, nodeList)
}
// GetNode
// @Tags Node
// @Summary Get Node
// @Description Get Node
// @ID GetNode
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param cluster path string true "cluster"
// @Param node path string true "node"
// @Success 200 {object} model.Node
// @Router /ns/{namespace}/clusters/{cluster}/nodes/{node} [get]
func GetNode(c echo.Context) error {
if err := app.Validate(c, []string{"cluster", "node"}); err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
node, err := service.GetNode(c.Param("namespace"), c.Param("cluster"), c.Param("node"))
if err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
return app.Send(c, http.StatusOK, node)
}
// AddNode
// @Tags Node
// @Summary Add Node
// @Description Add Node
// @ID AddNode
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param cluster path string true "cluster"
// @Param json body model.NodeReq true "Reuest json"
// @Success 200 {object} model.Node
// @Router /ns/{namespace}/clusters/{cluster}/nodes [post]
func AddNode(c echo.Context) error {
start := time.Now()
if err := app.Validate(c, []string{"cluster"}); err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
nodeReq := &model.NodeReq{}
if err := c.Bind(nodeReq); err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
err := app.NodeReqValidate(c, *nodeReq)
if err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
node, err := service.AddNode(c.Param("namespace"), c.Param("cluster"), nodeReq)
if err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
duration := time.Since(start)
common.CBLog.Info(" duration := ", duration)
return app.Send(c, http.StatusOK, node)
}
// RemoveNode
// @Tags Node
// @Summary Remove Node
// @Description Remove Node
// @ID RemoveNode
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param cluster path string true "cluster"
// @Param node path string true "node"
// @Success 200 {object} model.Status
// @Router /ns/{namespace}/clusters/{cluster}/nodes/{node} [delete]
func RemoveNode(c echo.Context) error {
if err := app.Validate(c, []string{"cluster", "node"}); err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
status, err := service.RemoveNode(c.Param("namespace"), c.Param("cluster"), c.Param("node"))
if err != nil {
common.CBLog.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
return app.Send(c, http.StatusOK, status)
}