2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-13 08:27:42 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package console
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-10 10:32:35 +01:00
|
|
|
"errors"
|
2018-11-13 08:27:42 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-22 20:26:59 +00:00
|
|
|
"storj.io/common/memory"
|
2023-06-07 10:25:57 +01:00
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2018-11-13 08:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Projects exposes methods to manage Project table in database.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2018-11-13 08:27:42 +00:00
|
|
|
type Projects interface {
|
|
|
|
// GetAll is a method for querying all projects from the database.
|
|
|
|
GetAll(ctx context.Context) ([]Project, error)
|
2019-11-15 14:27:44 +00:00
|
|
|
// GetCreatedBefore retrieves all projects created before provided date.
|
2019-06-06 17:07:14 +01:00
|
|
|
GetCreatedBefore(ctx context.Context, before time.Time) ([]Project, error)
|
2020-06-03 18:01:54 +01:00
|
|
|
// GetByUserID returns a list of projects where user is a project member.
|
2018-12-06 15:19:47 +00:00
|
|
|
GetByUserID(ctx context.Context, userID uuid.UUID) ([]Project, error)
|
2020-06-03 18:01:54 +01:00
|
|
|
// GetOwn returns a list of projects where user is an owner.
|
2020-01-29 00:57:15 +00:00
|
|
|
GetOwn(ctx context.Context, userID uuid.UUID) ([]Project, error)
|
2018-11-13 08:27:42 +00:00
|
|
|
// Get is a method for querying project from the database by id.
|
|
|
|
Get(ctx context.Context, id uuid.UUID) (*Project, error)
|
2022-09-13 13:45:18 +01:00
|
|
|
// GetSalt returns the project's salt.
|
|
|
|
GetSalt(ctx context.Context, id uuid.UUID) ([]byte, error)
|
2022-06-27 22:20:59 +01:00
|
|
|
// GetByPublicID is a method for querying project from the database by public_id.
|
|
|
|
GetByPublicID(ctx context.Context, publicID uuid.UUID) (*Project, error)
|
2018-11-13 08:27:42 +00:00
|
|
|
// Insert is a method for inserting project into the database.
|
2018-11-14 12:45:49 +00:00
|
|
|
Insert(ctx context.Context, project *Project) (*Project, error)
|
2018-11-13 08:27:42 +00:00
|
|
|
// Delete is a method for deleting project by Id from the database.
|
|
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
|
|
// Update is a method for updating project entity.
|
2018-11-14 12:45:49 +00:00
|
|
|
Update(ctx context.Context, project *Project) error
|
2019-11-05 13:16:02 +00:00
|
|
|
// List returns paginated projects, created before provided timestamp.
|
|
|
|
List(ctx context.Context, offset int64, limit int, before time.Time) (ProjectsPage, error)
|
2021-01-11 21:22:58 +00:00
|
|
|
// ListByOwnerID is a method for querying all projects from the database by ownerID. It also includes the number of members for each project.
|
2021-01-15 22:21:05 +00:00
|
|
|
ListByOwnerID(ctx context.Context, userID uuid.UUID, cursor ProjectsCursor) (ProjectsPage, error)
|
2020-02-07 17:24:58 +00:00
|
|
|
|
|
|
|
// UpdateRateLimit is a method for updating projects rate limit.
|
|
|
|
UpdateRateLimit(ctx context.Context, id uuid.UUID, newLimit int) error
|
2020-06-30 22:49:29 +01:00
|
|
|
|
2021-08-23 22:47:58 +01:00
|
|
|
// UpdateBurstLimit is a method for updating projects burst limit.
|
|
|
|
UpdateBurstLimit(ctx context.Context, id uuid.UUID, newLimit int) error
|
|
|
|
|
2020-06-30 22:49:29 +01:00
|
|
|
// GetMaxBuckets is a method to get the maximum number of buckets allowed for the project
|
2020-09-06 00:02:12 +01:00
|
|
|
GetMaxBuckets(ctx context.Context, id uuid.UUID) (*int, error)
|
2020-07-15 15:14:56 +01:00
|
|
|
// UpdateBucketLimit is a method for updating projects bucket limit.
|
|
|
|
UpdateBucketLimit(ctx context.Context, id uuid.UUID, newLimit int) error
|
2022-12-15 07:11:03 +00:00
|
|
|
|
|
|
|
// UpdateUsageLimits is a method for updating project's usage limits.
|
|
|
|
UpdateUsageLimits(ctx context.Context, id uuid.UUID, limits UsageLimits) error
|
2023-06-13 16:58:24 +01:00
|
|
|
|
|
|
|
// UpdateUserAgent is a method for updating projects user agent.
|
|
|
|
UpdateUserAgent(ctx context.Context, id uuid.UUID, userAgent []byte) error
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 20:26:59 +00:00
|
|
|
// UsageLimitsConfig is a configuration struct for default per-project usage limits.
|
|
|
|
type UsageLimitsConfig struct {
|
2021-07-01 00:13:45 +01:00
|
|
|
Storage StorageLimitConfig
|
|
|
|
Bandwidth BandwidthLimitConfig
|
2021-12-06 19:06:50 +00:00
|
|
|
Segment SegmentLimitConfig
|
2022-02-28 21:37:46 +00:00
|
|
|
Project ProjectLimitConfig
|
2021-07-01 00:13:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// StorageLimitConfig is a configuration struct for default storage per-project usage limits.
|
|
|
|
type StorageLimitConfig struct {
|
2023-03-28 19:30:43 +01:00
|
|
|
Free memory.Size `help:"the default free-tier storage usage limit" default:"25.00GB" testDefault:"25.00 GB"`
|
2021-07-01 00:13:45 +01:00
|
|
|
Paid memory.Size `help:"the default paid-tier storage usage limit" default:"25.00TB" testDefault:"25.00 GB"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BandwidthLimitConfig is a configuration struct for default bandwidth per-project usage limits.
|
|
|
|
type BandwidthLimitConfig struct {
|
2023-03-28 19:30:43 +01:00
|
|
|
Free memory.Size `help:"the default free-tier bandwidth usage limit" default:"25.00GB" testDefault:"25.00 GB"`
|
2021-07-01 00:13:45 +01:00
|
|
|
Paid memory.Size `help:"the default paid-tier bandwidth usage limit" default:"100.00TB" testDefault:"25.00 GB"`
|
2021-03-22 20:26:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 19:06:50 +00:00
|
|
|
// SegmentLimitConfig is a configuration struct for default segments per-project usage limits.
|
|
|
|
type SegmentLimitConfig struct {
|
2023-03-28 19:30:43 +01:00
|
|
|
Free int64 `help:"the default free-tier segment usage limit" default:"10000"`
|
2022-03-02 17:39:37 +00:00
|
|
|
Paid int64 `help:"the default paid-tier segment usage limit" default:"100000000"`
|
2021-12-06 19:06:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 21:37:46 +00:00
|
|
|
// ProjectLimitConfig is a configuration struct for default project limits.
|
|
|
|
type ProjectLimitConfig struct {
|
|
|
|
Free int `help:"the default free-tier project limit" default:"1"`
|
|
|
|
Paid int `help:"the default paid-tier project limit" default:"3"`
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Project is a database object that describes Project entity.
|
2018-11-13 08:27:42 +00:00
|
|
|
type Project struct {
|
2022-06-27 22:20:59 +01:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
PublicID uuid.UUID `json:"publicId"`
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2023-06-07 10:25:57 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
UserAgent []byte `json:"userAgent"`
|
|
|
|
OwnerID uuid.UUID `json:"ownerId"`
|
|
|
|
RateLimit *int `json:"rateLimit"`
|
|
|
|
BurstLimit *int `json:"burstLimit"`
|
|
|
|
MaxBuckets *int `json:"maxBuckets"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
MemberCount int `json:"memberCount"`
|
|
|
|
StorageLimit *memory.Size `json:"storageLimit"`
|
|
|
|
BandwidthLimit *memory.Size `json:"bandwidthLimit"`
|
|
|
|
UserSpecifiedStorageLimit *memory.Size `json:"userSpecifiedStorageLimit"`
|
|
|
|
UserSpecifiedBandwidthLimit *memory.Size `json:"userSpecifiedBandwidthLimit"`
|
|
|
|
SegmentLimit *int64 `json:"segmentLimit"`
|
|
|
|
DefaultPlacement storj.PlacementConstraint `json:"defaultPlacement"`
|
2018-11-26 10:47:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 14:16:04 +01:00
|
|
|
// UpsertProjectInfo holds data needed to create/update Project.
|
|
|
|
type UpsertProjectInfo struct {
|
2021-08-02 23:06:15 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
2022-04-27 12:52:02 +01:00
|
|
|
StorageLimit memory.Size `json:"storageLimit"`
|
|
|
|
BandwidthLimit memory.Size `json:"bandwidthLimit"`
|
2021-08-02 23:06:15 +01:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
2019-11-05 13:16:02 +00:00
|
|
|
|
2023-08-07 14:16:04 +01:00
|
|
|
// ProjectInfo holds data sent via user facing http endpoints.
|
|
|
|
type ProjectInfo struct {
|
2023-09-12 04:16:35 +01:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
OwnerID uuid.UUID `json:"ownerId"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
MemberCount int `json:"memberCount"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
EdgeURLOverrides *EdgeURLOverrides `json:"edgeURLOverrides,omitempty"`
|
2023-08-07 14:16:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:21:05 +00:00
|
|
|
// ProjectsCursor holds info for project
|
|
|
|
// cursor pagination.
|
|
|
|
type ProjectsCursor struct {
|
|
|
|
Limit int
|
|
|
|
Page int
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
// ProjectsPage returns paginated projects,
|
|
|
|
// providing next offset if there are more projects
|
|
|
|
// to retrieve.
|
|
|
|
type ProjectsPage struct {
|
|
|
|
Projects []Project
|
|
|
|
Next bool
|
|
|
|
NextOffset int64
|
2021-01-15 22:21:05 +00:00
|
|
|
|
|
|
|
Limit int
|
|
|
|
Offset int64
|
|
|
|
|
|
|
|
PageCount int
|
|
|
|
CurrentPage int
|
|
|
|
TotalCount int64
|
2019-11-05 13:16:02 +00:00
|
|
|
}
|
2020-09-10 10:32:35 +01:00
|
|
|
|
2023-08-07 14:16:04 +01:00
|
|
|
// ProjectInfoPage is similar to ProjectsPage
|
|
|
|
// except the Projects field is ProjectInfo and is sent over HTTP API.
|
|
|
|
type ProjectInfoPage struct {
|
|
|
|
Projects []ProjectInfo `json:"projects"`
|
|
|
|
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
Offset int64 `json:"offset"`
|
|
|
|
|
|
|
|
PageCount int `json:"pageCount"`
|
|
|
|
CurrentPage int `json:"currentPage"`
|
|
|
|
TotalCount int64 `json:"totalCount"`
|
|
|
|
}
|
|
|
|
|
2023-09-06 13:30:55 +01:00
|
|
|
// LimitRequestInfo holds data needed to request limit increase.
|
|
|
|
type LimitRequestInfo struct {
|
|
|
|
LimitType string `json:"limitType"`
|
|
|
|
CurrentLimit memory.Size `json:"currentLimit"`
|
|
|
|
DesiredLimit memory.Size `json:"desiredLimit"`
|
|
|
|
}
|
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
// ValidateNameAndDescription validates project name and description strings.
|
|
|
|
// Project name must have more than 0 and less than 21 symbols.
|
|
|
|
// Project description can't have more than hundred symbols.
|
|
|
|
func ValidateNameAndDescription(name string, description string) error {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return errors.New("project name can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) > 20 {
|
|
|
|
return errors.New("project name can't have more than 20 symbols")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(description) > 100 {
|
|
|
|
return errors.New("project description can't have more than 100 symbols")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|