2022-01-11 13:20:02 +00:00
|
|
|
// AUTOGENERATED BY private/apigen
|
|
|
|
// DO NOT EDIT.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-08-31 14:55:28 +01:00
|
|
|
"strconv"
|
2022-02-17 13:49:07 +00:00
|
|
|
"time"
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-06-16 03:07:38 +01:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2022-01-11 13:20:02 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-02-17 13:49:07 +00:00
|
|
|
"storj.io/common/uuid"
|
2022-01-11 13:20:02 +00:00
|
|
|
"storj.io/storj/private/api"
|
2022-02-17 13:49:07 +00:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2022-01-11 13:20:02 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
const dateLayout = "2006-01-02T15:04:05.999Z"
|
2022-04-27 12:52:02 +01:00
|
|
|
|
2022-01-11 13:20:02 +00:00
|
|
|
var ErrProjectsAPI = errs.Class("consoleapi projects api")
|
2022-05-06 12:29:59 +01:00
|
|
|
var ErrApikeysAPI = errs.Class("consoleapi apikeys api")
|
2022-05-27 15:31:28 +01:00
|
|
|
var ErrUsersAPI = errs.Class("consoleapi users api")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
type ProjectManagementService interface {
|
2022-08-31 14:55:28 +01:00
|
|
|
GenCreateProject(ctx context.Context, request console.ProjectInfo) (*console.Project, api.HTTPError)
|
|
|
|
GenUpdateProject(ctx context.Context, id uuid.UUID, request console.ProjectInfo) (*console.Project, api.HTTPError)
|
|
|
|
GenDeleteProject(ctx context.Context, id uuid.UUID) api.HTTPError
|
|
|
|
GenGetUsersProjects(ctx context.Context) ([]console.Project, api.HTTPError)
|
|
|
|
GenGetSingleBucketUsageRollup(ctx context.Context, projectID uuid.UUID, bucket string, since, before time.Time) (*accounting.BucketUsageRollup, api.HTTPError)
|
|
|
|
GenGetBucketUsageRollups(ctx context.Context, projectID uuid.UUID, since, before time.Time) ([]accounting.BucketUsageRollup, api.HTTPError)
|
|
|
|
GenGetAPIKeys(ctx context.Context, projectID uuid.UUID, search string, limit, page uint, order console.APIKeyOrder, orderDirection console.OrderDirection) (*console.APIKeyPage, api.HTTPError)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
type APIKeyManagementService interface {
|
2022-08-31 14:55:28 +01:00
|
|
|
GenCreateAPIKey(ctx context.Context, request console.CreateAPIKeyRequest) (*console.CreateAPIKeyResponse, api.HTTPError)
|
|
|
|
GenDeleteAPIKey(ctx context.Context, id uuid.UUID) api.HTTPError
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 15:31:28 +01:00
|
|
|
type UserManagementService interface {
|
2022-08-31 14:55:28 +01:00
|
|
|
GenGetUser(ctx context.Context) (*console.ResponseUser, api.HTTPError)
|
2022-05-27 15:31:28 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
// ProjectManagementHandler is an api handler that exposes all projects related functionality.
|
|
|
|
type ProjectManagementHandler struct {
|
2022-01-11 13:20:02 +00:00
|
|
|
log *zap.Logger
|
2022-06-16 03:07:38 +01:00
|
|
|
mon *monkit.Scope
|
2022-01-11 13:20:02 +00:00
|
|
|
service ProjectManagementService
|
|
|
|
auth api.Auth
|
|
|
|
}
|
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
// APIKeyManagementHandler is an api handler that exposes all apikeys related functionality.
|
|
|
|
type APIKeyManagementHandler struct {
|
|
|
|
log *zap.Logger
|
2022-06-16 03:07:38 +01:00
|
|
|
mon *monkit.Scope
|
2022-05-06 12:29:59 +01:00
|
|
|
service APIKeyManagementService
|
|
|
|
auth api.Auth
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:31:28 +01:00
|
|
|
// UserManagementHandler is an api handler that exposes all users related functionality.
|
|
|
|
type UserManagementHandler struct {
|
|
|
|
log *zap.Logger
|
2022-06-16 03:07:38 +01:00
|
|
|
mon *monkit.Scope
|
2022-05-27 15:31:28 +01:00
|
|
|
service UserManagementService
|
|
|
|
auth api.Auth
|
|
|
|
}
|
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
func NewProjectManagement(log *zap.Logger, mon *monkit.Scope, service ProjectManagementService, router *mux.Router, auth api.Auth) *ProjectManagementHandler {
|
2022-05-06 12:29:59 +01:00
|
|
|
handler := &ProjectManagementHandler{
|
2022-01-11 13:20:02 +00:00
|
|
|
log: log,
|
2022-06-16 03:07:38 +01:00
|
|
|
mon: mon,
|
2022-01-11 13:20:02 +00:00
|
|
|
service: service,
|
2022-02-11 15:06:52 +00:00
|
|
|
auth: auth,
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
projectsRouter := router.PathPrefix("/api/v0/projects").Subrouter()
|
2022-04-27 12:52:02 +01:00
|
|
|
projectsRouter.HandleFunc("/create", handler.handleGenCreateProject).Methods("POST")
|
|
|
|
projectsRouter.HandleFunc("/update/{id}", handler.handleGenUpdateProject).Methods("PATCH")
|
2022-04-28 16:59:55 +01:00
|
|
|
projectsRouter.HandleFunc("/delete/{id}", handler.handleGenDeleteProject).Methods("DELETE")
|
2022-05-06 12:29:59 +01:00
|
|
|
projectsRouter.HandleFunc("/", handler.handleGenGetUsersProjects).Methods("GET")
|
2022-05-27 15:31:28 +01:00
|
|
|
projectsRouter.HandleFunc("/bucket-rollup", handler.handleGenGetSingleBucketUsageRollup).Methods("GET")
|
2022-04-28 16:59:55 +01:00
|
|
|
projectsRouter.HandleFunc("/bucket-rollups", handler.handleGenGetBucketUsageRollups).Methods("GET")
|
2022-08-31 14:55:28 +01:00
|
|
|
projectsRouter.HandleFunc("/apikeys/{projectID}", handler.handleGenGetAPIKeys).Methods("GET")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
func NewAPIKeyManagement(log *zap.Logger, mon *monkit.Scope, service APIKeyManagementService, router *mux.Router, auth api.Auth) *APIKeyManagementHandler {
|
2022-05-06 12:29:59 +01:00
|
|
|
handler := &APIKeyManagementHandler{
|
|
|
|
log: log,
|
2022-06-16 03:07:38 +01:00
|
|
|
mon: mon,
|
2022-05-06 12:29:59 +01:00
|
|
|
service: service,
|
|
|
|
auth: auth,
|
2022-04-07 09:05:28 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
apikeysRouter := router.PathPrefix("/api/v0/apikeys").Subrouter()
|
|
|
|
apikeysRouter.HandleFunc("/create", handler.handleGenCreateAPIKey).Methods("POST")
|
2022-08-31 14:55:28 +01:00
|
|
|
apikeysRouter.HandleFunc("/delete/{id}", handler.handleGenDeleteAPIKey).Methods("DELETE")
|
2022-03-21 12:15:33 +00:00
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
return handler
|
2022-03-21 12:15:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
func NewUserManagement(log *zap.Logger, mon *monkit.Scope, service UserManagementService, router *mux.Router, auth api.Auth) *UserManagementHandler {
|
2022-05-27 15:31:28 +01:00
|
|
|
handler := &UserManagementHandler{
|
|
|
|
log: log,
|
2022-06-16 03:07:38 +01:00
|
|
|
mon: mon,
|
2022-05-27 15:31:28 +01:00
|
|
|
service: service,
|
|
|
|
auth: auth,
|
|
|
|
}
|
|
|
|
|
|
|
|
usersRouter := router.PathPrefix("/api/v0/users").Subrouter()
|
|
|
|
usersRouter.HandleFunc("/", handler.handleGenGetUser).Methods("GET")
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
func (h *ProjectManagementHandler) handleGenCreateProject(w http.ResponseWriter, r *http.Request) {
|
2022-04-28 16:59:55 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-04-28 16:59:55 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
2022-07-14 04:43:33 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-04-28 16:59:55 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
payload := console.ProjectInfo{}
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
2022-04-28 16:59:55 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
retVal, httpErr := h.service.GenCreateProject(ctx, payload)
|
2022-04-28 16:59:55 +01:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-06-09 16:23:08 +01:00
|
|
|
h.log.Debug("failed to write json GenCreateProject response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
2022-04-28 16:59:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
func (h *ProjectManagementHandler) handleGenUpdateProject(w http.ResponseWriter, r *http.Request) {
|
2022-05-27 15:31:28 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-05-27 15:31:28 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
2022-06-05 23:41:38 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-05-27 15:31:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
idParam, ok := mux.Vars(r)["id"]
|
|
|
|
if !ok {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("missing id route param"))
|
2022-05-27 15:31:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
id, err := uuid.FromString(idParam)
|
2022-05-27 15:31:28 +01:00
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
payload := console.ProjectInfo{}
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
2022-05-27 15:31:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
retVal, httpErr := h.service.GenUpdateProject(ctx, id, payload)
|
2022-05-27 15:31:28 +01:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-06-09 16:23:08 +01:00
|
|
|
h.log.Debug("failed to write json GenUpdateProject response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
2022-05-27 15:31:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
func (h *ProjectManagementHandler) handleGenDeleteProject(w http.ResponseWriter, r *http.Request) {
|
2022-03-27 11:16:46 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-03-27 11:16:46 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
2022-06-05 23:41:38 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-03-27 11:16:46 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
idParam, ok := mux.Vars(r)["id"]
|
|
|
|
if !ok {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("missing id route param"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2022-04-07 09:05:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
httpErr := h.service.GenDeleteProject(ctx, id)
|
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectManagementHandler) handleGenGetUsersProjects(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-06-09 16:23:08 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
2022-07-14 04:43:33 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-06-09 16:23:08 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal, httpErr := h.service.GenGetUsersProjects(ctx)
|
2022-03-27 11:16:46 +01:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-06-09 16:23:08 +01:00
|
|
|
h.log.Debug("failed to write json GenGetUsersProjects response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
2022-03-27 11:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
func (h *ProjectManagementHandler) handleGenGetSingleBucketUsageRollup(w http.ResponseWriter, r *http.Request) {
|
2022-01-11 13:20:02 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2022-03-27 11:16:46 +01:00
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
2022-01-11 13:20:02 +00:00
|
|
|
if err != nil {
|
2022-06-05 23:41:38 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-01-11 13:20:02 +00:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
projectIDParam := r.URL.Query().Get("projectID")
|
|
|
|
if projectIDParam == "" {
|
2022-07-14 04:43:33 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'projectID' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDParam)
|
2022-06-09 16:23:08 +01:00
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
2022-04-27 12:52:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
bucket := r.URL.Query().Get("bucket")
|
|
|
|
if bucket == "" {
|
2022-06-09 16:23:08 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'bucket' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
sinceParam := r.URL.Query().Get("since")
|
|
|
|
if sinceParam == "" {
|
2022-07-14 04:43:33 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'since' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
since, err := time.Parse(dateLayout, sinceParam)
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
beforeParam := r.URL.Query().Get("before")
|
|
|
|
if beforeParam == "" {
|
2022-07-14 04:43:33 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'before' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
before, err := time.Parse(dateLayout, beforeParam)
|
2022-06-09 16:23:08 +01:00
|
|
|
if err != nil {
|
2022-04-07 09:05:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
retVal, httpErr := h.service.GenGetSingleBucketUsageRollup(ctx, projectID, bucket, since, before)
|
2022-04-07 09:05:28 +01:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:05:28 +01:00
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-06-09 16:23:08 +01:00
|
|
|
h.log.Debug("failed to write json GenGetSingleBucketUsageRollup response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
2022-04-07 09:05:28 +01:00
|
|
|
}
|
|
|
|
}
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
func (h *ProjectManagementHandler) handleGenGetBucketUsageRollups(w http.ResponseWriter, r *http.Request) {
|
2022-04-07 09:05:28 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-04-07 09:05:28 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
if err != nil {
|
2022-06-05 23:41:38 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-04-07 09:05:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
projectIDParam := r.URL.Query().Get("projectID")
|
|
|
|
if projectIDParam == "" {
|
2022-07-14 04:43:33 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'projectID' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDParam)
|
2022-06-09 16:23:08 +01:00
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
2022-02-17 13:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
sinceParam := r.URL.Query().Get("since")
|
|
|
|
if sinceParam == "" {
|
2022-07-14 04:43:33 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'since' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
since, err := time.Parse(dateLayout, sinceParam)
|
2022-01-11 13:20:02 +00:00
|
|
|
if err != nil {
|
2022-04-28 16:59:55 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
beforeParam := r.URL.Query().Get("before")
|
|
|
|
if beforeParam == "" {
|
2022-07-14 04:43:33 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'before' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
before, err := time.Parse(dateLayout, beforeParam)
|
2022-02-17 13:49:07 +00:00
|
|
|
if err != nil {
|
2022-06-09 16:23:08 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
2022-02-17 13:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
retVal, httpErr := h.service.GenGetBucketUsageRollups(ctx, projectID, since, before)
|
2022-02-17 13:49:07 +00:00
|
|
|
if httpErr.Err != nil {
|
2022-01-11 13:20:02 +00:00
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-06-09 16:23:08 +01:00
|
|
|
h.log.Debug("failed to write json GenGetBucketUsageRollups response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
apigen: endpoint to get all buckets usage by project ID
Added new endpoint to get all bucket rollups by bucket ID.
Example of response:
vitalii:~/Documents$ ./testapi.sh
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 07 Mar 2022 11:18:55 GMT
Content-Length: 671
[{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"demo-bucket","totalStoredData":0.0026272243089674662,"totalSegments":0.05000107166666666,"objectCount":0.03333373083333333,"metadataSize":1.6750359008333334e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"},{"projectID":"a9b2b1b6-714a-4c49-99f1-6a53d0852525","bucketName":"qwe","totalStoredData":0.000018436725422435552,"totalSegments":0.016667081388888887,"objectCount":0.016667081388888887,"metadataSize":1.933381441111111e-9,"repairEgress":0,"getEgress":0,"auditEgress":0,"since":"2022-03-01T11:00:00Z","before":"2022-03-07T11:17:07Z"}]
Change-Id: I8b04b24dbc67b78be5c309ce542bf03d6f67e65d
2022-03-07 11:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 12:29:59 +01:00
|
|
|
|
2022-08-31 14:55:28 +01:00
|
|
|
func (h *ProjectManagementHandler) handleGenGetAPIKeys(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
|
|
|
h.auth.RemoveAuthCookie(w)
|
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
search := r.URL.Query().Get("search")
|
|
|
|
if search == "" {
|
2022-08-31 14:55:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'search' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
limitParam := r.URL.Query().Get("limit")
|
|
|
|
if limitParam == "" {
|
2022-08-31 14:55:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'limit' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limitParamU64, err := strconv.ParseUint(limitParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
limit := uint(limitParamU64)
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
pageParam := r.URL.Query().Get("page")
|
|
|
|
if pageParam == "" {
|
2022-08-31 14:55:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'page' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageParamU64, err := strconv.ParseUint(pageParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
page := uint(pageParamU64)
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
orderParam := r.URL.Query().Get("order")
|
|
|
|
if orderParam == "" {
|
2022-08-31 14:55:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'order' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
orderParamU64, err := strconv.ParseUint(orderParam, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
order := console.APIKeyOrder(orderParamU64)
|
|
|
|
|
2022-09-02 14:26:48 +01:00
|
|
|
orderDirectionParam := r.URL.Query().Get("orderDirection")
|
|
|
|
if orderDirectionParam == "" {
|
2022-08-31 14:55:28 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'orderDirection' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
orderDirectionParamU64, err := strconv.ParseUint(orderDirectionParam, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
orderDirection := console.OrderDirection(orderDirectionParamU64)
|
|
|
|
|
|
|
|
projectIDParam, ok := mux.Vars(r)["projectID"]
|
|
|
|
if !ok {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("missing projectID route param"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDParam)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal, httpErr := h.service.GenGetAPIKeys(ctx, projectID, search, limit, page, order, orderDirection)
|
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
|
|
|
h.log.Debug("failed to write json GenGetAPIKeys response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:31:28 +01:00
|
|
|
func (h *APIKeyManagementHandler) handleGenCreateAPIKey(w http.ResponseWriter, r *http.Request) {
|
2022-05-06 12:29:59 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-05-06 12:29:59 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
2022-06-05 23:41:38 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-05-06 12:29:59 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
payload := console.CreateAPIKeyRequest{}
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
2022-05-06 12:29:59 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
retVal, httpErr := h.service.GenCreateAPIKey(ctx, payload)
|
2022-05-06 12:29:59 +01:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-05-27 15:31:28 +01:00
|
|
|
h.log.Debug("failed to write json GenCreateAPIKey response", zap.Error(ErrApikeysAPI.Wrap(err)))
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:55:28 +01:00
|
|
|
func (h *APIKeyManagementHandler) handleGenDeleteAPIKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
|
|
|
h.auth.RemoveAuthCookie(w)
|
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
idParam, ok := mux.Vars(r)["id"]
|
|
|
|
if !ok {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("missing id route param"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpErr := h.service.GenDeleteAPIKey(ctx, id)
|
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:31:28 +01:00
|
|
|
func (h *UserManagementHandler) handleGenGetUser(w http.ResponseWriter, r *http.Request) {
|
2022-05-06 12:29:59 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2022-06-16 03:07:38 +01:00
|
|
|
defer h.mon.Task()(&ctx)(&err)
|
2022-05-06 12:29:59 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
2022-06-05 23:41:38 +01:00
|
|
|
h.auth.RemoveAuthCookie(w)
|
2022-05-06 12:29:59 +01:00
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:31:28 +01:00
|
|
|
retVal, httpErr := h.service.GenGetUser(ctx)
|
2022-05-06 12:29:59 +01:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
|
|
if err != nil {
|
2022-05-27 15:31:28 +01:00
|
|
|
h.log.Debug("failed to write json GenGetUser response", zap.Error(ErrUsersAPI.Wrap(err)))
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
|
|
|
}
|