Dragonfly2
129 строк · 3.1 Кб
1/*
2* Copyright 2022 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 handlers18
19import (20"net/http"21
22"github.com/gin-gonic/gin"23
24// nolint25_ "d7y.io/dragonfly/v2/manager/models"26// nolint27"d7y.io/dragonfly/v2/manager/types"28// nolint29_ "d7y.io/dragonfly/v2/pkg/objectstorage"30)
31
32// @Summary Create Bucket
33// @Description Create by json bucket
34// @Tags Bucket
35// @Accept json
36// @Produce json
37// @Param Bucket body types.CreateBucketRequest true "Bucket"
38// @Success 200
39// @Failure 400
40// @Failure 404
41// @Failure 500
42// @Router /buckets [post]
43func (h *Handlers) CreateBucket(ctx *gin.Context) {44var json types.CreateBucketRequest45if err := ctx.ShouldBindJSON(&json); err != nil {46ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})47return48}49
50if err := h.service.CreateBucket(ctx.Request.Context(), json); err != nil {51ctx.Error(err) // nolint: errcheck52return53}54
55ctx.Status(http.StatusOK)56}
57
58// @Summary Destroy Bucket
59// @Description Destroy by id
60// @Tags Bucket
61// @Accept json
62// @Produce json
63// @Param id path string true "id"
64// @Success 200
65// @Failure 400
66// @Failure 404
67// @Failure 500
68// @Router /buckets/{id} [delete]
69func (h *Handlers) DestroyBucket(ctx *gin.Context) {70var params types.BucketParams71if err := ctx.ShouldBindUri(¶ms); err != nil {72ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})73return74}75
76if err := h.service.DestroyBucket(ctx.Request.Context(), params.ID); err != nil {77ctx.Error(err) // nolint: errcheck78return79}80
81ctx.Status(http.StatusOK)82}
83
84// @Summary Get Bucket
85// @Description Get Bucket by id
86// @Tags Bucket
87// @Accept json
88// @Produce json
89// @Param id path string true "id"
90// @Success 200 {object} objectstorage.BucketMetadata
91// @Failure 400
92// @Failure 404
93// @Failure 500
94// @Router /buckets/{id} [get]
95func (h *Handlers) GetBucket(ctx *gin.Context) {96var params types.BucketParams97if err := ctx.ShouldBindUri(¶ms); err != nil {98ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})99return100}101
102bucket, err := h.service.GetBucket(ctx.Request.Context(), params.ID)103if err != nil {104ctx.Error(err) // nolint: errcheck105return106}107
108ctx.JSON(http.StatusOK, bucket)109}
110
111// @Summary Get Buckets
112// @Description Get Buckets
113// @Tags Bucket
114// @Accept json
115// @Produce json
116// @Success 200 {object} []objectstorage.BucketMetadata
117// @Failure 400
118// @Failure 404
119// @Failure 500
120// @Router /buckets [get]
121func (h *Handlers) GetBuckets(ctx *gin.Context) {122buckets, err := h.service.GetBuckets(ctx.Request.Context())123if err != nil {124ctx.Error(err) // nolint: errcheck125return126}127
128ctx.JSON(http.StatusOK, buckets)129}
130