storj/satellite/satellitedb/projects.go
Michal Niewrzal f731267e8c Per-project usage limiting (#2036)
What: Changes to support custom usage limit for the project. With this implementation by default project usage limit is taken from configuration flag. If project DB field usage_limit will be set to value larger than 0 it will become custom usage limit and we will be used to verify is limit was exceeded.

Whats changed:

usage_limit (bigint) field added to projects table (with migration)
things related to project usage moved from metainfo endpoint to project usage type
accounting.ProjectAccounting extended with GetProjectUsageLimits() method
Why: We need to have different usage limits per project. https://storjlabs.atlassian.net/browse/V3-1814
2019-05-28 09:36:52 -06:00

132 lines
3.5 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"storj.io/storj/satellite/console"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
// implementation of Projects interface repository using spacemonkeygo/dbx orm
type projects struct {
db dbx.Methods
}
// GetAll is a method for querying all projects from the database.
func (projects *projects) GetAll(ctx context.Context) ([]console.Project, error) {
projectsDbx, err := projects.db.All_Project(ctx)
if err != nil {
return nil, err
}
return projectsFromDbxSlice(projectsDbx)
}
// GetByUserID is a method for querying all projects from the database by userID.
func (projects *projects) GetByUserID(ctx context.Context, userID uuid.UUID) ([]console.Project, error) {
projectsDbx, err := projects.db.All_Project_By_ProjectMember_MemberId_OrderBy_Asc_Project_Name(ctx, dbx.ProjectMember_MemberId(userID[:]))
if err != nil {
return nil, err
}
return projectsFromDbxSlice(projectsDbx)
}
// Get is a method for querying project from the database by id.
func (projects *projects) Get(ctx context.Context, id uuid.UUID) (*console.Project, error) {
project, err := projects.db.Get_Project_By_Id(ctx, dbx.Project_Id(id[:]))
if err != nil {
return nil, err
}
return projectFromDBX(project)
}
// Insert is a method for inserting project into the database.
func (projects *projects) Insert(ctx context.Context, project *console.Project) (*console.Project, error) {
projectID, err := uuid.New()
if err != nil {
return nil, err
}
createdProject, err := projects.db.Create_Project(ctx,
dbx.Project_Id(projectID[:]),
dbx.Project_Name(project.Name),
dbx.Project_Description(project.Description),
dbx.Project_UsageLimit(0),
)
if err != nil {
return nil, err
}
return projectFromDBX(createdProject)
}
// Delete is a method for deleting project by Id from the database.
func (projects *projects) Delete(ctx context.Context, id uuid.UUID) error {
_, err := projects.db.Delete_Project_By_Id(ctx, dbx.Project_Id(id[:]))
return err
}
// Update is a method for updating project entity
func (projects *projects) Update(ctx context.Context, project *console.Project) error {
updateFields := dbx.Project_Update_Fields{
Description: dbx.Project_Description(project.Description),
UsageLimit: dbx.Project_UsageLimit(project.UsageLimit),
}
_, err := projects.db.Update_Project_By_Id(ctx,
dbx.Project_Id(project.ID[:]),
updateFields)
return err
}
// projectFromDBX is used for creating Project entity from autogenerated dbx.Project struct
func projectFromDBX(project *dbx.Project) (*console.Project, error) {
if project == nil {
return nil, errs.New("project parameter is nil")
}
id, err := bytesToUUID(project.Id)
if err != nil {
return nil, err
}
u := &console.Project{
ID: id,
Name: project.Name,
Description: project.Description,
CreatedAt: project.CreatedAt,
}
return u, nil
}
// projectsFromDbxSlice is used for creating []Project entities from autogenerated []*dbx.Project struct
func projectsFromDbxSlice(projectsDbx []*dbx.Project) ([]console.Project, error) {
var projects []console.Project
var errors []error
// Generating []dbo from []dbx and collecting all errors
for _, projectDbx := range projectsDbx {
project, err := projectFromDBX(projectDbx)
if err != nil {
errors = append(errors, err)
continue
}
projects = append(projects, *project)
}
return projects, errs.Combine(errors...)
}