2022-01-11 13:20:02 +00:00
|
|
|
// AUTOGENERATED BY private/apigen
|
|
|
|
// DO NOT EDIT.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-02-17 13:49:07 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrProjectsAPI = errs.Class("consoleapi projects api")
|
|
|
|
|
|
|
|
type ProjectManagementService interface {
|
2022-02-17 13:49:07 +00:00
|
|
|
GenGetUsersProjects(context.Context) ([]console.Project, api.HTTPError)
|
|
|
|
GenGetSingleBucketUsageRollup(context.Context, uuid.UUID, string, time.Time, time.Time) (*accounting.BucketUsageRollup, api.HTTPError)
|
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
|
|
|
GenGetBucketUsageRollups(context.Context, uuid.UUID, time.Time, time.Time) ([]accounting.BucketUsageRollup, api.HTTPError)
|
2022-03-21 12:15:33 +00:00
|
|
|
GenCreateProject(context.Context, console.ProjectInfo) (*console.Project, api.HTTPError)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 15:06:52 +00:00
|
|
|
// Handler is an api handler that exposes all projects related functionality.
|
2022-01-11 13:20:02 +00:00
|
|
|
type Handler struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service ProjectManagementService
|
|
|
|
auth api.Auth
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:06:52 +00:00
|
|
|
func NewProjectManagement(log *zap.Logger, service ProjectManagementService, router *mux.Router, auth api.Auth) *Handler {
|
2022-01-11 13:20:02 +00:00
|
|
|
handler := &Handler{
|
|
|
|
log: log,
|
|
|
|
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-03-21 12:15:33 +00:00
|
|
|
projectsRouter.HandleFunc("/create", handler.handleGenCreateProject).Methods("PUT")
|
2022-02-17 13:49:07 +00:00
|
|
|
projectsRouter.HandleFunc("/", handler.handleGenGetUsersProjects).Methods("GET")
|
|
|
|
projectsRouter.HandleFunc("/bucket-rollup", handler.handleGenGetSingleBucketUsageRollup).Methods("GET")
|
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
|
|
|
projectsRouter.HandleFunc("/bucket-rollups", handler.handleGenGetBucketUsageRollups).Methods("GET")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:15:33 +00:00
|
|
|
func (h *Handler) handleGenCreateProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectInfo := &console.ProjectInfo{}
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&projectInfo); err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal, httpErr := h.service.GenCreateProject(ctx, *projectInfo)
|
|
|
|
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 GenCreateProject response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 11:16:46 +01:00
|
|
|
func (h *Handler) handleGenGetUsersProjects(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal, httpErr := h.service.GenGetUsersProjects(ctx)
|
|
|
|
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 GenGetUsersProjects 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
|
|
|
func (h *Handler) handleGenGetSingleBucketUsageRollup(w http.ResponseWriter, r *http.Request) {
|
2022-01-11 13:20:02 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
projectID, err := uuid.FromString(r.URL.Query().Get("projectID"))
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := r.URL.Query().Get("bucket")
|
|
|
|
if bucket == "" {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'bucket' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Unix(sinceStamp, 0).UTC()
|
|
|
|
|
|
|
|
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
before := time.Unix(beforeStamp, 0).UTC()
|
|
|
|
|
|
|
|
retVal, httpErr := h.service.GenGetSingleBucketUsageRollup(ctx, projectID, bucket, since, before)
|
2022-02-17 13:49:07 +00:00
|
|
|
if httpErr.Err != nil {
|
|
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
2022-01-11 13:20:02 +00:00
|
|
|
if err != nil {
|
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
|
|
|
h.log.Debug("failed to write json GenGetSingleBucketUsageRollup response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
2022-02-17 13:49:07 +00: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
|
|
|
func (h *Handler) handleGenGetBucketUsageRollups(w http.ResponseWriter, r *http.Request) {
|
2022-02-17 13:49:07 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
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-02-17 13:49:07 +00:00
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(r.URL.Query().Get("projectID"))
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Unix(sinceStamp, 0).UTC()
|
|
|
|
|
|
|
|
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
before := time.Unix(beforeStamp, 0).UTC()
|
|
|
|
|
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
|
|
|
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 {
|
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
|
|
|
h.log.Debug("failed to write json GenGetBucketUsageRollups response", zap.Error(ErrProjectsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|