-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.go
123 lines (107 loc) · 3.42 KB
/
cluster.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
package router
import (
"net/http"
"time"
"github.com/cloud-barista/poc-cicd-ladybug/src/core/model"
"github.com/cloud-barista/poc-cicd-ladybug/src/core/service"
"github.com/cloud-barista/poc-cicd-ladybug/src/utils/app"
"github.com/labstack/echo/v4"
logger "github.com/sirupsen/logrus"
)
// ListCluster
// @Tags Cluster
// @Summary List Cluster
// @Description List Cluster
// @ID ListCluster
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Success 200 {object} model.ClusterList
// @Router /ns/{namespace}/clusters [get]
func ListCluster(c echo.Context) error {
clusterList, err := service.ListCluster(c.Param("namespace"))
if err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
return app.Send(c, http.StatusOK, clusterList)
}
// GetCluster
// @Tags Cluster
// @Summary Get Cluster
// @Description Get Cluster
// @ID GetCluster
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param cluster path string true "cluster"
// @Success 200 {object} model.Cluster
// @Router /ns/{namespace}/clusters/{cluster} [get]
func GetCluster(c echo.Context) error {
if err := app.Validate(c, []string{"namespace", "cluster"}); err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
cluster, err := service.GetCluster(c.Param("namespace"), c.Param("cluster"))
if err != nil {
logger.Infof("not found a cluster (namespace=%s, cluster=%s, cause=%s)", c.Param("namespace"), c.Param("cluster"), err)
return app.SendMessage(c, http.StatusNotFound, err.Error())
}
return app.Send(c, http.StatusOK, cluster)
}
// CreateCluster
// @Tags Cluster
// @Summary Create Cluster
// @Description Create Cluster
// @ID CreateCluster
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param json body model.ClusterReq true "Reuest json"
// @Success 200 {object} model.Cluster
// @Router /ns/{namespace}/clusters [post]
func CreateCluster(c echo.Context) error {
start := time.Now()
clusterReq := &model.ClusterReq{}
if err := c.Bind(clusterReq); err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
app.ClusterReqDef(*clusterReq)
err := app.ClusterReqValidate(*clusterReq)
if err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
cluster, err := service.CreateCluster(c.Param("namespace"), clusterReq)
if err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
duration := time.Since(start)
logger.Info("duration := ", duration)
return app.Send(c, http.StatusOK, cluster)
}
// DeleteCluster
// @Tags Cluster
// @Summary Delete a cluster
// @Description Delete a cluster
// @ID DeleteCluster
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param cluster path string true "cluster"
// @Success 200 {object} model.Status
// @Router /ns/{namespace}/clusters/{cluster} [delete]
func DeleteCluster(c echo.Context) error {
if err := app.Validate(c, []string{"namespace", "cluster"}); err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
status, err := service.DeleteCluster(c.Param("namespace"), c.Param("cluster"))
if err != nil {
logger.Error(err)
return app.SendMessage(c, http.StatusBadRequest, err.Error())
}
return app.Send(c, http.StatusOK, status)
}