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.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-06 17:07:14 +01:00
|
|
|
"time"
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2018-11-13 08:27:42 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 14:37:39 +00:00
|
|
|
// ensures that projects implements console.Projects.
|
|
|
|
var _ console.Projects = (*projects)(nil)
|
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
// implementation of Projects interface repository using spacemonkeygo/dbx orm.
|
2018-11-13 08:27:42 +00:00
|
|
|
type projects struct {
|
2020-01-29 00:57:15 +00:00
|
|
|
db dbx.Methods
|
|
|
|
sdb *satelliteDB
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll is a method for querying all projects from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) GetAll(ctx context.Context) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
projectsDbx, err := projects.db.All_Project(ctx)
|
2018-11-13 08:27:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// GetOwn is a method for querying all projects created by current user from the database.
|
|
|
|
func (projects *projects) GetOwn(ctx context.Context, userID uuid.UUID) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
projectsDbx, err := projects.db.All_Project_By_OwnerId_OrderBy_Asc_CreatedAt(ctx, dbx.Project_OwnerId(userID[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetCreatedBefore retrieves all projects created before provided date.
|
2019-06-06 17:07:14 +01:00
|
|
|
func (projects *projects) GetCreatedBefore(ctx context.Context, before time.Time) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
projectsDbx, err := projects.db.All_Project_By_CreatedAt_Less_OrderBy_Asc_CreatedAt(ctx, dbx.Project_CreatedAt(before))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:19:47 +00:00
|
|
|
// GetByUserID is a method for querying all projects from the database by userID.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) GetByUserID(ctx context.Context, userID uuid.UUID) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-04 11:32:21 +00:00
|
|
|
projectsDbx, err := projects.db.All_Project_By_ProjectMember_MemberId_OrderBy_Asc_Project_Name(ctx, dbx.ProjectMember_MemberId(userID[:]))
|
2018-12-06 15:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
2018-12-06 15:19:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
// Get is a method for querying project from the database by id.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Get(ctx context.Context, id uuid.UUID) (_ *console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
project, err := projects.db.Get_Project_By_Id(ctx, dbx.Project_Id(id[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectFromDBX(ctx, project)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert is a method for inserting project into the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Insert(ctx context.Context, project *console.Project) (_ *console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
projectID, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:28:13 +01:00
|
|
|
createFields := dbx.Project_Create_Fields{}
|
2019-07-19 16:17:34 +01:00
|
|
|
if !project.PartnerID.IsZero() {
|
2019-08-07 13:28:13 +01:00
|
|
|
createFields.PartnerId = dbx.Project_PartnerId(project.PartnerID[:])
|
2019-07-19 16:17:34 +01:00
|
|
|
}
|
2020-01-17 15:01:36 +00:00
|
|
|
createFields.RateLimit = dbx.Project_RateLimit_Raw(project.RateLimit)
|
2020-06-30 22:49:29 +01:00
|
|
|
createFields.MaxBuckets = dbx.Project_MaxBuckets(project.MaxBuckets)
|
2019-07-19 16:17:34 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
createdProject, err := projects.db.Create_Project(ctx,
|
|
|
|
dbx.Project_Id(projectID[:]),
|
|
|
|
dbx.Project_Name(project.Name),
|
2019-05-28 16:36:52 +01:00
|
|
|
dbx.Project_Description(project.Description),
|
2019-08-07 13:28:13 +01:00
|
|
|
dbx.Project_OwnerId(project.OwnerID[:]),
|
|
|
|
createFields,
|
2019-05-28 16:36:52 +01:00
|
|
|
)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectFromDBX(ctx, createdProject)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is a method for deleting project by Id from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Delete(ctx context.Context, id uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
_, err = projects.db.Delete_Project_By_Id(ctx, dbx.Project_Id(id[:]))
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Update is a method for updating project entity.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Update(ctx context.Context, project *console.Project) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-12-06 15:19:47 +00:00
|
|
|
updateFields := dbx.Project_Update_Fields{
|
2020-07-06 21:28:49 +01:00
|
|
|
Name: dbx.Project_Name(project.Name),
|
2019-01-28 18:20:33 +00:00
|
|
|
Description: dbx.Project_Description(project.Description),
|
2020-01-17 15:01:36 +00:00
|
|
|
RateLimit: dbx.Project_RateLimit_Raw(project.RateLimit),
|
2018-12-06 15:19:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
_, err = projects.db.Update_Project_By_Id(ctx,
|
2018-11-13 08:27:42 +00:00
|
|
|
dbx.Project_Id(project.ID[:]),
|
2018-12-06 15:19:47 +00:00
|
|
|
updateFields)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
// UpdateRateLimit is a method for updating projects rate limit.
|
|
|
|
func (projects *projects) UpdateRateLimit(ctx context.Context, id uuid.UUID, newLimit int) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
rateLimit := &newLimit
|
|
|
|
if newLimit == 0 {
|
|
|
|
rateLimit = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = projects.db.Update_Project_By_Id(ctx,
|
|
|
|
dbx.Project_Id(id[:]),
|
|
|
|
dbx.Project_Update_Fields{
|
|
|
|
RateLimit: dbx.Project_RateLimit_Raw(rateLimit),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:14:56 +01:00
|
|
|
// UpdateBucketLimit is a method for updating projects bucket limit.
|
|
|
|
func (projects *projects) UpdateBucketLimit(ctx context.Context, id uuid.UUID, newLimit int) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = projects.db.Update_Project_By_Id(ctx,
|
|
|
|
dbx.Project_Id(id[:]),
|
|
|
|
dbx.Project_Update_Fields{
|
|
|
|
MaxBuckets: dbx.Project_MaxBuckets(newLimit),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
// List returns paginated projects, created before provided timestamp.
|
|
|
|
func (projects *projects) List(ctx context.Context, offset int64, limit int, before time.Time) (_ console.ProjectsPage, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var page console.ProjectsPage
|
|
|
|
|
|
|
|
dbxProjects, err := projects.db.Limited_Project_By_CreatedAt_Less_OrderBy_Asc_CreatedAt(ctx,
|
|
|
|
dbx.Project_CreatedAt(before.UTC()),
|
|
|
|
limit+1,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return console.ProjectsPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbxProjects) == limit+1 {
|
|
|
|
page.Next = true
|
2020-02-21 16:51:28 +00:00
|
|
|
page.NextOffset = offset + int64(limit)
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
dbxProjects = dbxProjects[:len(dbxProjects)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
projs, err := projectsFromDbxSlice(ctx, dbxProjects)
|
|
|
|
if err != nil {
|
|
|
|
return console.ProjectsPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page.Projects = projs
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// projectFromDBX is used for creating Project entity from autogenerated dbx.Project struct.
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectFromDBX(ctx context.Context, project *dbx.Project) (_ *console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
if project == nil {
|
|
|
|
return nil, errs.New("project parameter is nil")
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
id, err := uuid.FromBytes(project.Id)
|
2018-11-13 08:27:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:29:40 +01:00
|
|
|
var partnerID uuid.UUID
|
|
|
|
if len(project.PartnerId) > 0 {
|
2020-03-31 17:49:16 +01:00
|
|
|
partnerID, err = uuid.FromBytes(project.PartnerId)
|
2019-08-12 22:29:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
ownerID, err := uuid.FromBytes(project.OwnerId)
|
2019-08-07 13:28:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &console.Project{
|
2019-01-28 18:20:33 +00:00
|
|
|
ID: id,
|
|
|
|
Name: project.Name,
|
|
|
|
Description: project.Description,
|
2019-08-12 22:29:40 +01:00
|
|
|
PartnerID: partnerID,
|
2019-08-07 13:28:13 +01:00
|
|
|
OwnerID: ownerID,
|
2020-01-17 15:01:36 +00:00
|
|
|
RateLimit: project.RateLimit,
|
2020-06-30 22:49:29 +01:00
|
|
|
MaxBuckets: project.MaxBuckets,
|
2019-01-28 18:20:33 +00:00
|
|
|
CreatedAt: project.CreatedAt,
|
2019-08-07 13:28:13 +01:00
|
|
|
}, nil
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
2018-11-14 12:45:49 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// projectsFromDbxSlice is used for creating []Project entities from autogenerated []*dbx.Project struct.
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectsFromDbxSlice(ctx context.Context, projectsDbx []*dbx.Project) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
var projects []console.Project
|
2018-11-14 12:45:49 +00:00
|
|
|
var errors []error
|
|
|
|
|
|
|
|
// Generating []dbo from []dbx and collecting all errors
|
|
|
|
for _, projectDbx := range projectsDbx {
|
2019-06-04 12:55:38 +01:00
|
|
|
project, err := projectFromDBX(ctx, projectDbx)
|
2018-11-14 12:45:49 +00:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
projects = append(projects, *project)
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
return projects, errs.Combine(errors...)
|
2018-11-14 12:45:49 +00:00
|
|
|
}
|
2020-06-30 22:49:29 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetMaxBuckets is a method to get the maximum number of buckets allowed for the project.
|
2020-06-30 22:49:29 +01:00
|
|
|
func (projects *projects) GetMaxBuckets(ctx context.Context, id uuid.UUID) (maxBuckets int, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
dbxRow, err := projects.db.Get_Project_MaxBuckets_By_Id(ctx, dbx.Project_Id(id[:]))
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return dbxRow.MaxBuckets, nil
|
|
|
|
}
|