Dragonfly2

Форк
0
183 строки · 4.7 Кб
1
/*
2
 *     Copyright 2020 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
	"github.com/gin-gonic/gin/binding"
24

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

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

48
	switch json.Type {
49
	case job.PreheatJob:
50
		var json types.CreatePreheatJobRequest
51
		if err := ctx.ShouldBindBodyWith(&json, binding.JSON); err != nil {
52
			ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
53
			return
54
		}
55

56
		job, err := h.service.CreatePreheatJob(ctx.Request.Context(), json)
57
		if err != nil {
58
			ctx.Error(err) // nolint: errcheck
59
			return
60
		}
61

62
		ctx.JSON(http.StatusOK, job)
63
	default:
64
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": "Unknow type"})
65
	}
66
}
67

68
// @Summary Destroy Job
69
// @Description Destroy by id
70
// @Tags Job
71
// @Accept json
72
// @Produce json
73
// @Param id path string true "id"
74
// @Success 200
75
// @Failure 400
76
// @Failure 404
77
// @Failure 500
78
// @Router /jobs/{id} [delete]
79
func (h *Handlers) DestroyJob(ctx *gin.Context) {
80
	var params types.JobParams
81
	if err := ctx.ShouldBindUri(&params); err != nil {
82
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
83
		return
84
	}
85

86
	if err := h.service.DestroyJob(ctx.Request.Context(), params.ID); err != nil {
87
		ctx.Error(err) // nolint: errcheck
88
		return
89
	}
90

91
	ctx.Status(http.StatusOK)
92
}
93

94
// @Summary Update Job
95
// @Description Update by json config
96
// @Tags Job
97
// @Accept json
98
// @Produce json
99
// @Param id path string true "id"
100
// @Param Job body types.UpdateJobRequest true "Job"
101
// @Success 200 {object} models.Job
102
// @Failure 400
103
// @Failure 404
104
// @Failure 500
105
// @Router /jobs/{id} [patch]
106
func (h *Handlers) UpdateJob(ctx *gin.Context) {
107
	var params types.JobParams
108
	if err := ctx.ShouldBindUri(&params); err != nil {
109
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
110
		return
111
	}
112

113
	var json types.UpdateJobRequest
114
	if err := ctx.ShouldBindJSON(&json); err != nil {
115
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
116
		return
117
	}
118

119
	job, err := h.service.UpdateJob(ctx.Request.Context(), params.ID, json)
120
	if err != nil {
121
		ctx.Error(err) // nolint: errcheck
122
		return
123
	}
124

125
	ctx.JSON(http.StatusOK, job)
126
}
127

128
// @Summary Get Job
129
// @Description Get Job by id
130
// @Tags Job
131
// @Accept json
132
// @Produce json
133
// @Param id path string true "id"
134
// @Success 200 {object} models.Job
135
// @Failure 400
136
// @Failure 404
137
// @Failure 500
138
// @Router /jobs/{id} [get]
139
func (h *Handlers) GetJob(ctx *gin.Context) {
140
	var params types.JobParams
141
	if err := ctx.ShouldBindUri(&params); err != nil {
142
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
143
		return
144
	}
145

146
	job, err := h.service.GetJob(ctx.Request.Context(), params.ID)
147
	if err != nil {
148
		ctx.Error(err) // nolint: errcheck
149
		return
150
	}
151

152
	ctx.JSON(http.StatusOK, job)
153
}
154

155
// @Summary Get Jobs
156
// @Description Get Jobs
157
// @Tags Job
158
// @Accept json
159
// @Produce json
160
// @Param page query int true "current page" default(0)
161
// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
162
// @Success 200 {object} []models.Job
163
// @Failure 400
164
// @Failure 404
165
// @Failure 500
166
// @Router /jobs [get]
167
func (h *Handlers) GetJobs(ctx *gin.Context) {
168
	var query types.GetJobsQuery
169
	if err := ctx.ShouldBindQuery(&query); err != nil {
170
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
171
		return
172
	}
173

174
	h.setPaginationDefault(&query.Page, &query.PerPage)
175
	jobs, count, err := h.service.GetJobs(ctx.Request.Context(), query)
176
	if err != nil {
177
		ctx.Error(err) // nolint: errcheck
178
		return
179
	}
180

181
	h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
182
	ctx.JSON(http.StatusOK, jobs)
183
}
184

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

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

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

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