Dragonfly2
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
17package handlers
18
19import (
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]
40func (h *Handlers) CreateCluster(ctx *gin.Context) {
41var json types.CreateClusterRequest
42if err := ctx.ShouldBindJSON(&json); err != nil {
43ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
44return
45}
46
47cluster, err := h.service.CreateCluster(ctx.Request.Context(), json)
48if err != nil {
49ctx.Error(err) // nolint: errcheck
50return
51}
52
53ctx.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]
67func (h *Handlers) DestroyCluster(ctx *gin.Context) {
68var params types.ClusterParams
69if err := ctx.ShouldBindUri(¶ms); err != nil {
70ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
71return
72}
73
74if err := h.service.DestroyCluster(ctx.Request.Context(), params.ID); err != nil {
75ctx.Error(err) // nolint: errcheck
76return
77}
78
79ctx.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]
94func (h *Handlers) UpdateCluster(ctx *gin.Context) {
95var params types.ClusterParams
96if err := ctx.ShouldBindUri(¶ms); err != nil {
97ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
98return
99}
100
101var json types.UpdateClusterRequest
102if err := ctx.ShouldBindJSON(&json); err != nil {
103ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
104return
105}
106
107cluster, err := h.service.UpdateCluster(ctx.Request.Context(), params.ID, json)
108if err != nil {
109ctx.Error(err) // nolint: errcheck
110return
111}
112
113ctx.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]
127func (h *Handlers) GetCluster(ctx *gin.Context) {
128var params types.ClusterParams
129if err := ctx.ShouldBindUri(¶ms); err != nil {
130ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
131return
132}
133
134cluster, err := h.service.GetCluster(ctx.Request.Context(), params.ID)
135if err != nil {
136ctx.Error(err) // nolint: errcheck
137return
138}
139
140ctx.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]
155func (h *Handlers) GetClusters(ctx *gin.Context) {
156var query types.GetClustersQuery
157if err := ctx.ShouldBindQuery(&query); err != nil {
158ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
159return
160}
161
162h.setPaginationDefault(&query.Page, &query.PerPage)
163clusters, count, err := h.service.GetClusters(ctx.Request.Context(), query)
164if err != nil {
165ctx.Error(err) // nolint: errcheck
166return
167}
168
169h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
170ctx.JSON(http.StatusOK, clusters)
171}
172