Dragonfly2

Форк
0
171 строка · 4.5 Кб
1
/*
2
 *     Copyright 2023 The Dragonfly Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package handlers
18

19
import (
20
	"net/http"
21

22
	"github.com/gin-gonic/gin"
23

24
	// nolint
25
	_ "d7y.io/dragonfly/v2/manager/models"
26
	"d7y.io/dragonfly/v2/manager/types"
27
)
28

29
// @Summary Create Cluster
30
// @Description Create by json config
31
// @Tags Cluster
32
// @Accept json
33
// @Produce json
34
// @Param Cluster body types.CreateClusterRequest true "Cluster"
35
// @Success 200 {object} types.CreateClusterResponse
36
// @Failure 400
37
// @Failure 404
38
// @Failure 500
39
// @Router /clusters [post]
40
func (h *Handlers) CreateCluster(ctx *gin.Context) {
41
	var json types.CreateClusterRequest
42
	if err := ctx.ShouldBindJSON(&json); err != nil {
43
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
44
		return
45
	}
46

47
	cluster, err := h.service.CreateCluster(ctx.Request.Context(), json)
48
	if err != nil {
49
		ctx.Error(err) // nolint: errcheck
50
		return
51
	}
52

53
	ctx.JSON(http.StatusOK, cluster)
54
}
55

56
// @Summary Destroy Cluster
57
// @Description Destroy by id
58
// @Tags Cluster
59
// @Accept json
60
// @Produce json
61
// @Param id path string true "id"
62
// @Success 200
63
// @Failure 400
64
// @Failure 404
65
// @Failure 500
66
// @Router /clusters/{id} [delete]
67
func (h *Handlers) DestroyCluster(ctx *gin.Context) {
68
	var params types.ClusterParams
69
	if err := ctx.ShouldBindUri(&params); err != nil {
70
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
71
		return
72
	}
73

74
	if err := h.service.DestroyCluster(ctx.Request.Context(), params.ID); err != nil {
75
		ctx.Error(err) // nolint: errcheck
76
		return
77
	}
78

79
	ctx.Status(http.StatusOK)
80
}
81

82
// @Summary Update Cluster
83
// @Description Update by json config
84
// @Tags Cluster
85
// @Accept json
86
// @Produce json
87
// @Param id path string true "id"
88
// @Param Cluster body types.UpdateClusterRequest true "Cluster"
89
// @Success 200 {object} types.UpdateClusterResponse
90
// @Failure 400
91
// @Failure 404
92
// @Failure 500
93
// @Router /clusters/{id} [patch]
94
func (h *Handlers) UpdateCluster(ctx *gin.Context) {
95
	var params types.ClusterParams
96
	if err := ctx.ShouldBindUri(&params); err != nil {
97
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
98
		return
99
	}
100

101
	var json types.UpdateClusterRequest
102
	if err := ctx.ShouldBindJSON(&json); err != nil {
103
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
104
		return
105
	}
106

107
	cluster, err := h.service.UpdateCluster(ctx.Request.Context(), params.ID, json)
108
	if err != nil {
109
		ctx.Error(err) // nolint: errcheck
110
		return
111
	}
112

113
	ctx.JSON(http.StatusOK, cluster)
114
}
115

116
// @Summary Get Cluster
117
// @Description Get Cluster by id
118
// @Tags Cluster
119
// @Accept json
120
// @Produce json
121
// @Param id path string true "id"
122
// @Success 200 {object} types.GetClusterResponse
123
// @Failure 400
124
// @Failure 404
125
// @Failure 500
126
// @Router /clusters/{id} [get]
127
func (h *Handlers) GetCluster(ctx *gin.Context) {
128
	var params types.ClusterParams
129
	if err := ctx.ShouldBindUri(&params); err != nil {
130
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
131
		return
132
	}
133

134
	cluster, err := h.service.GetCluster(ctx.Request.Context(), params.ID)
135
	if err != nil {
136
		ctx.Error(err) // nolint: errcheck
137
		return
138
	}
139

140
	ctx.JSON(http.StatusOK, cluster)
141
}
142

143
// @Summary Get Clusters
144
// @Description Get Clusters
145
// @Tags Cluster
146
// @Accept json
147
// @Produce json
148
// @Param page query int true "current page" default(0)
149
// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
150
// @Success 200 {object} []types.GetClusterResponse
151
// @Failure 400
152
// @Failure 404
153
// @Failure 500
154
// @Router /clusters [get]
155
func (h *Handlers) GetClusters(ctx *gin.Context) {
156
	var query types.GetClustersQuery
157
	if err := ctx.ShouldBindQuery(&query); err != nil {
158
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
159
		return
160
	}
161

162
	h.setPaginationDefault(&query.Page, &query.PerPage)
163
	clusters, count, err := h.service.GetClusters(ctx.Request.Context(), query)
164
	if err != nil {
165
		ctx.Error(err) // nolint: errcheck
166
		return
167
	}
168

169
	h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
170
	ctx.JSON(http.StatusOK, clusters)
171
}
172

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.