2020-02-07 17:24:58 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2020-08-13 13:40:05 +01:00
|
|
|
"context"
|
2020-07-06 21:28:49 +01:00
|
|
|
"database/sql"
|
2020-02-07 17:24:58 +00:00
|
|
|
"encoding/json"
|
2020-07-06 21:28:49 +01:00
|
|
|
"errors"
|
2020-02-07 17:24:58 +00:00
|
|
|
"fmt"
|
2020-05-11 17:05:36 +01:00
|
|
|
"io/ioutil"
|
2020-02-07 17:24:58 +00:00
|
|
|
"net/http"
|
2020-07-06 21:15:55 +01:00
|
|
|
"time"
|
2020-02-07 17:24:58 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
|
2020-05-18 18:36:09 +01:00
|
|
|
"storj.io/common/macaroon"
|
2020-02-07 17:24:58 +00:00
|
|
|
"storj.io/common/memory"
|
2020-05-18 18:36:09 +01:00
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-05-11 17:05:36 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-07-06 21:15:55 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2020-02-07 17:24:58 +00:00
|
|
|
)
|
|
|
|
|
2020-08-13 13:40:05 +01:00
|
|
|
func (server *Server) checkProjectUsage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectUUIDString, ok := vars["project"]
|
|
|
|
if !ok {
|
|
|
|
httpJSONError(w, "project-uuid missing",
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectUUID, err := uuid.FromString(projectUUIDString)
|
|
|
|
if err != nil {
|
|
|
|
httpJSONError(w, "invalid project-uuid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !server.checkUsage(ctx, w, projectUUID) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, _ = w.Write([]byte(`{"result":"no project usage exist"}`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 20:17:18 +01:00
|
|
|
func (server *Server) getProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectUUIDString, ok := vars["project"]
|
|
|
|
if !ok {
|
|
|
|
httpJSONError(w, "project-uuid missing",
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectUUID, err := uuid.FromString(projectUUIDString)
|
|
|
|
if err != nil {
|
|
|
|
httpJSONError(w, "invalid project-uuid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
httpJSONError(w, "invalid form",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := server.db.Console().Projects().Get(ctx, projectUUID)
|
|
|
|
if err != nil {
|
|
|
|
httpJSONError(w, "unable to fetch project details",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-09-03 22:12:26 +01:00
|
|
|
return
|
2020-09-05 20:17:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(project)
|
|
|
|
if err != nil {
|
|
|
|
httpJSONError(w, "json encoding failed",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectUUIDString, ok := vars["project"]
|
|
|
|
if !ok {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "project-uuid missing",
|
|
|
|
"", http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
projectUUID, err := uuid.FromString(projectUUIDString)
|
2020-02-07 17:24:58 +00:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid project-uuid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:01:15 +01:00
|
|
|
usagelimit, err := server.db.ProjectAccounting().GetProjectStorageLimit(ctx, projectUUID)
|
2020-02-07 17:24:58 +00:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to get usage limit",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:01:15 +01:00
|
|
|
bandwidthlimit, err := server.db.ProjectAccounting().GetProjectBandwidthLimit(ctx, projectUUID)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to get bandwidth limit",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-12 14:01:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
project, err := server.db.Console().Projects().Get(ctx, projectUUID)
|
2020-02-07 17:24:58 +00:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to get project",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var output struct {
|
|
|
|
Usage struct {
|
|
|
|
Amount memory.Size `json:"amount"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
} `json:"usage"`
|
2020-05-12 14:01:15 +01:00
|
|
|
Bandwidth struct {
|
|
|
|
Amount memory.Size `json:"amount"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
} `json:"bandwidth"`
|
2020-02-07 17:24:58 +00:00
|
|
|
Rate struct {
|
|
|
|
RPS int `json:"rps"`
|
|
|
|
} `json:"rate"`
|
2020-07-15 15:14:56 +01:00
|
|
|
Buckets int `json:"maxBuckets"`
|
2020-02-07 17:24:58 +00:00
|
|
|
}
|
2020-09-06 00:02:12 +01:00
|
|
|
if usagelimit != nil {
|
|
|
|
output.Usage.Amount = memory.Size(*usagelimit)
|
|
|
|
output.Usage.Bytes = *usagelimit
|
|
|
|
}
|
|
|
|
if bandwidthlimit != nil {
|
|
|
|
output.Bandwidth.Amount = memory.Size(*bandwidthlimit)
|
|
|
|
output.Bandwidth.Bytes = *bandwidthlimit
|
|
|
|
}
|
|
|
|
if project.MaxBuckets != nil {
|
|
|
|
output.Buckets = *project.MaxBuckets
|
|
|
|
}
|
2020-02-07 17:24:58 +00:00
|
|
|
if project.RateLimit != nil {
|
|
|
|
output.Rate.RPS = *project.RateLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(output)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "json encoding failed",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-05-11 17:05:36 +01:00
|
|
|
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
2020-02-07 17:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectUUIDString, ok := vars["project"]
|
|
|
|
if !ok {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "project-uuid missing",
|
|
|
|
"", http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
projectUUID, err := uuid.FromString(projectUUIDString)
|
2020-02-07 17:24:58 +00:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid project-uuid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var arguments struct {
|
2020-05-12 14:01:15 +01:00
|
|
|
Usage *memory.Size `schema:"usage"`
|
|
|
|
Bandwidth *memory.Size `schema:"bandwidth"`
|
|
|
|
Rate *int `schema:"rate"`
|
2020-07-15 15:14:56 +01:00
|
|
|
Buckets *int `schema:"buckets"`
|
2020-02-07 17:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid form",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := schema.NewDecoder()
|
|
|
|
err = decoder.Decode(&arguments, r.Form)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid arguments",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if arguments.Usage != nil {
|
|
|
|
if *arguments.Usage < 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "negative usage",
|
|
|
|
fmt.Sprintf("%v", arguments.Usage), http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
err = server.db.ProjectAccounting().UpdateProjectUsageLimit(ctx, projectUUID, *arguments.Usage)
|
2020-02-07 17:24:58 +00:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to update usage",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:01:15 +01:00
|
|
|
if arguments.Bandwidth != nil {
|
|
|
|
if *arguments.Bandwidth < 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "negative bandwidth",
|
|
|
|
fmt.Sprintf("%v", arguments.Usage), http.StatusBadRequest)
|
2020-05-12 14:01:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.db.ProjectAccounting().UpdateProjectBandwidthLimit(ctx, projectUUID, *arguments.Bandwidth)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to update bandwidth",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-12 14:01:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
if arguments.Rate != nil {
|
|
|
|
if *arguments.Rate < 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "negative rate",
|
|
|
|
fmt.Sprintf("%v", arguments.Rate), http.StatusBadRequest)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
err = server.db.Console().Projects().UpdateRateLimit(ctx, projectUUID, *arguments.Rate)
|
2020-02-07 17:24:58 +00:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to update rate",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-02-07 17:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 15:14:56 +01:00
|
|
|
|
|
|
|
if arguments.Buckets != nil {
|
|
|
|
if *arguments.Buckets < 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "negative bucket coun",
|
|
|
|
fmt.Sprintf("t: %v", arguments.Buckets), http.StatusBadRequest)
|
2020-07-15 15:14:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.db.Console().Projects().UpdateBucketLimit(ctx, projectUUID, *arguments.Buckets)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to update bucket limit",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-15 15:14:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 17:24:58 +00:00
|
|
|
}
|
2020-05-11 17:05:36 +01:00
|
|
|
|
|
|
|
func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to read body",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var input struct {
|
|
|
|
OwnerID uuid.UUID `json:"ownerId"`
|
|
|
|
ProjectName string `json:"projectName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var output struct {
|
|
|
|
ProjectID uuid.UUID `json:"projectId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &input)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to unmarshal request",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.OwnerID.IsZero() {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "OwnerID is not set",
|
|
|
|
"", http.StatusBadRequest)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.ProjectName == "" {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "ProjectName is not set",
|
|
|
|
"", http.StatusBadRequest)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := server.db.Console().Projects().Insert(ctx, &console.Project{
|
|
|
|
Name: input.ProjectName,
|
|
|
|
OwnerID: input.OwnerID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to insert project",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = server.db.Console().ProjectMembers().Insert(ctx, project.OwnerID, project.ID)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to insert project member",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output.ProjectID = project.ID
|
|
|
|
data, err := json.Marshal(output)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "json encoding failed",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-11 17:05:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
|
|
|
}
|
2020-05-18 18:36:09 +01:00
|
|
|
|
2020-07-06 21:28:49 +01:00
|
|
|
func (server *Server) renameProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectUUIDString, ok := vars["project"]
|
|
|
|
if !ok {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "project-uuid missing",
|
|
|
|
"", http.StatusBadRequest)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectUUID, err := uuid.FromString(projectUUIDString)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid project-uuid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := server.db.Console().Projects().Get(ctx, projectUUID)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "project with specified uuid does not exist",
|
|
|
|
"", http.StatusBadRequest)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "error getting project",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "ailed to read body",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var input struct {
|
|
|
|
ProjectName string `json:"projectName"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &input)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "failed to unmarshal request",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.ProjectName == "" {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "ProjectName is not set",
|
|
|
|
"", http.StatusBadRequest)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project.Name = input.ProjectName
|
|
|
|
project.Description = input.Description
|
|
|
|
|
|
|
|
err = server.db.Console().Projects().Update(ctx, project)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "error renaming project",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 21:28:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 18:36:09 +01:00
|
|
|
func (server *Server) deleteProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectUUIDString, ok := vars["project"]
|
|
|
|
if !ok {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "project-uuid missing",
|
|
|
|
"", http.StatusBadRequest)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectUUID, err := uuid.FromString(projectUUIDString)
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid project-uuid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "invalid form",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:13:11 +01:00
|
|
|
options := storj.BucketListOptions{Limit: 1, Direction: storj.Forward}
|
|
|
|
buckets, err := server.db.Buckets().ListBuckets(ctx, projectUUID, options, macaroon.AllowedBuckets{All: true})
|
2020-05-18 18:36:09 +01:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "unable to list buckets",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(buckets.Items) > 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "buckets still exist",
|
|
|
|
fmt.Sprintf("%v", bucketNames(buckets.Items)), http.StatusConflict)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := server.db.Console().APIKeys().GetPagedByProjectID(ctx, projectUUID, console.APIKeyCursor{Limit: 1, Page: 1})
|
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "unable to list api-keys",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if keys.TotalCount > 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "api-keys still exist",
|
|
|
|
fmt.Sprintf("count %d", keys.TotalCount), http.StatusConflict)
|
2020-05-18 18:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:40:05 +01:00
|
|
|
// if usage exist, return error to client and exit
|
|
|
|
if server.checkUsage(ctx, w, projectUUID) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.db.Console().Projects().Delete(ctx, projectUUID)
|
|
|
|
if err != nil {
|
|
|
|
httpJSONError(w, "unable to delete project",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) checkUsage(ctx context.Context, w http.ResponseWriter, projectID uuid.UUID) (hasUsage bool) {
|
2020-07-06 21:15:55 +01:00
|
|
|
// do not delete projects that have usage for the current month.
|
2020-09-03 22:12:26 +01:00
|
|
|
year, month, _ := server.nowFn().UTC().Date()
|
2020-07-06 21:15:55 +01:00
|
|
|
firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
2020-09-03 22:12:26 +01:00
|
|
|
currentUsage, err := server.db.ProjectAccounting().GetProjectTotal(ctx, projectID, firstOfMonth, server.nowFn())
|
2020-07-06 21:15:55 +01:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "unable to list project usage",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
if currentUsage.Storage > 0 || currentUsage.Egress > 0 || currentUsage.ObjectCount > 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "usage for current month exists",
|
|
|
|
"", http.StatusConflict)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if usage of last month exist, make sure to look for billing records
|
2020-08-13 13:40:05 +01:00
|
|
|
lastMonthUsage, err := server.db.ProjectAccounting().GetProjectTotal(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth.AddDate(0, 0, -1))
|
2020-07-06 21:15:55 +01:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "error getting project totals",
|
|
|
|
"", http.StatusInternalServerError)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if lastMonthUsage.Storage > 0 || lastMonthUsage.Egress > 0 || lastMonthUsage.ObjectCount > 0 {
|
2020-10-13 13:47:55 +01:00
|
|
|
// time passed into the check function need to be the UTC midnight dates of the first and last day of the month
|
2020-09-03 22:12:26 +01:00
|
|
|
err := server.db.StripeCoinPayments().ProjectRecords().Check(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth.Add(-time.Hour*24))
|
2020-07-06 21:15:55 +01:00
|
|
|
switch err {
|
|
|
|
case stripecoinpayments.ErrProjectRecordExists:
|
2020-09-03 22:12:26 +01:00
|
|
|
record, err := server.db.StripeCoinPayments().ProjectRecords().Get(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth.Add(-time.Hour*24))
|
2020-07-06 21:15:55 +01:00
|
|
|
if err != nil {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "unable to get project records",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
// state = 0 means unapplied and not invoiced yet.
|
|
|
|
if record.State == 0 {
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "unapplied project invoice record exist",
|
|
|
|
"", http.StatusConflict)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
case nil:
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "usage for last month exist, but is not billed yet",
|
|
|
|
"", http.StatusConflict)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
default:
|
2020-08-05 14:13:11 +01:00
|
|
|
httpJSONError(w, "unable to get project records",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-08-13 13:40:05 +01:00
|
|
|
return true
|
2020-07-06 21:15:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:40:05 +01:00
|
|
|
return false
|
2020-05-18 18:36:09 +01:00
|
|
|
}
|
2020-06-05 11:58:00 +01:00
|
|
|
|
|
|
|
func bucketNames(buckets []storj.Bucket) []string {
|
|
|
|
var xs []string
|
|
|
|
for _, b := range buckets {
|
|
|
|
xs = append(xs, b.Name)
|
|
|
|
}
|
|
|
|
return xs
|
|
|
|
}
|