2020-11-13 11:41:35 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2020-11-13 11:41:35 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrBucketsAPI - console buckets api error type.
|
2021-06-24 16:49:15 +01:00
|
|
|
ErrBucketsAPI = errs.Class("console api buckets")
|
2020-11-13 11:41:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Buckets is an api controller that exposes all buckets related functionality.
|
|
|
|
type Buckets struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuckets is a constructor for api buckets controller.
|
|
|
|
func NewBuckets(log *zap.Logger, service *console.Service) *Buckets {
|
|
|
|
return &Buckets{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllBucketNames returns all bucket names for a specific project.
|
|
|
|
func (b *Buckets) AllBucketNames(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
projectIDString := r.URL.Query().Get("projectID")
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDString)
|
|
|
|
if err != nil {
|
|
|
|
b.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketNames, err := b.service.GetAllBucketNames(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
b.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(bucketNames)
|
|
|
|
if err != nil {
|
|
|
|
b.log.Error("failed to write json all bucket names response", zap.Error(ErrBucketsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveJSONError writes JSON error to response output stream.
|
|
|
|
func (b *Buckets) serveJSONError(w http.ResponseWriter, status int, err error) {
|
2022-11-21 18:58:42 +00:00
|
|
|
web.ServeJSONError(b.log, w, status, err)
|
2020-11-13 11:41:35 +00:00
|
|
|
}
|