satellite/{console,satellitedb}: add PublicID to Project, db method GetByPublicID

github issue: https://github.com/storj/storj/issues/4861

Change-Id: Ia83635c0de751a77cd5a49d641da19ed76132c46
This commit is contained in:
Cameron 2022-06-27 17:20:59 -04:00 committed by Storj Robot
parent 9cd91c6df8
commit 4815cfc09b
5 changed files with 92 additions and 26 deletions

View File

@ -47,8 +47,9 @@ func TestProjectGet(t *testing.T) {
t.Run("OK", func(t *testing.T) { t.Run("OK", func(t *testing.T) {
link := "http://" + address.String() + "/api/projects/" + project.ID.String() link := "http://" + address.String() + "/api/projects/" + project.ID.String()
expected := fmt.Sprintf( expected := fmt.Sprintf(
`{"id":"%s","name":"%s","description":"%s","partnerId":"%s","userAgent":null,"ownerId":"%s","rateLimit":null,"burstLimit":null,"maxBuckets":null,"createdAt":"%s","memberCount":0,"storageLimit":"25.00 GB","bandwidthLimit":"25.00 GB","segmentLimit":150000}`, `{"id":"%s","publicId":"%s","name":"%s","description":"%s","partnerId":"%s","userAgent":null,"ownerId":"%s","rateLimit":null,"burstLimit":null,"maxBuckets":null,"createdAt":"%s","memberCount":0,"storageLimit":"25.00 GB","bandwidthLimit":"25.00 GB","segmentLimit":150000}`,
project.ID.String(), project.ID.String(),
project.PublicID.String(),
project.Name, project.Name,
project.Description, project.Description,
project.PartnerID.String(), project.PartnerID.String(),

View File

@ -26,6 +26,8 @@ type Projects interface {
GetOwn(ctx context.Context, userID uuid.UUID) ([]Project, error) GetOwn(ctx context.Context, userID uuid.UUID) ([]Project, error)
// Get is a method for querying project from the database by id. // Get is a method for querying project from the database by id.
Get(ctx context.Context, id uuid.UUID) (*Project, error) Get(ctx context.Context, id uuid.UUID) (*Project, error)
// GetByPublicID is a method for querying project from the database by public_id.
GetByPublicID(ctx context.Context, publicID uuid.UUID) (*Project, error)
// Insert is a method for inserting project into the database. // Insert is a method for inserting project into the database.
Insert(ctx context.Context, project *Project) (*Project, error) Insert(ctx context.Context, project *Project) (*Project, error)
// Delete is a method for deleting project by Id from the database. // Delete is a method for deleting project by Id from the database.
@ -83,7 +85,8 @@ type ProjectLimitConfig struct {
// Project is a database object that describes Project entity. // Project is a database object that describes Project entity.
type Project struct { type Project struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
PublicID uuid.UUID `json:"publicId"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`

View File

@ -0,0 +1,37 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/satellite/satellitedb/dbx"
)
func TestProjectFromDbx(t *testing.T) {
ctx := context.Background()
t.Run("can't create dbo from nil dbx model", func(t *testing.T) {
project, err := projectFromDBX(ctx, nil)
assert.Nil(t, project)
assert.NotNil(t, err)
assert.Error(t, err)
})
t.Run("can't create dbo from dbx model with invalid ID", func(t *testing.T) {
dbxProject := dbx.Project{
Id: []byte("qweqwe"),
}
project, err := projectFromDBX(ctx, &dbxProject)
assert.Nil(t, project)
assert.NotNil(t, err)
assert.Error(t, err)
})
}

View File

@ -84,6 +84,18 @@ func (projects *projects) Get(ctx context.Context, id uuid.UUID) (_ *console.Pro
return projectFromDBX(ctx, project) return projectFromDBX(ctx, project)
} }
// GetByPublicID is a method for querying project from the database by public_id.
func (projects *projects) GetByPublicID(ctx context.Context, publicID uuid.UUID) (_ *console.Project, err error) {
defer mon.Task()(&ctx)(&err)
project, err := projects.db.Get_Project_By_PublicId(ctx, dbx.Project_PublicId(publicID[:]))
if err != nil {
return nil, err
}
return projectFromDBX(ctx, project)
}
// Insert is a method for inserting project into the database. // Insert is a method for inserting project into the database.
func (projects *projects) Insert(ctx context.Context, project *console.Project) (_ *console.Project, err error) { func (projects *projects) Insert(ctx context.Context, project *console.Project) (_ *console.Project, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
@ -92,6 +104,10 @@ func (projects *projects) Insert(ctx context.Context, project *console.Project)
if err != nil { if err != nil {
return nil, err return nil, err
} }
publicID, err := uuid.New()
if err != nil {
return nil, err
}
createFields := dbx.Project_Create_Fields{} createFields := dbx.Project_Create_Fields{}
if !project.PartnerID.IsZero() { if !project.PartnerID.IsZero() {
@ -111,6 +127,7 @@ func (projects *projects) Insert(ctx context.Context, project *console.Project)
} }
createFields.RateLimit = dbx.Project_RateLimit_Raw(project.RateLimit) createFields.RateLimit = dbx.Project_RateLimit_Raw(project.RateLimit)
createFields.MaxBuckets = dbx.Project_MaxBuckets_Raw(project.MaxBuckets) createFields.MaxBuckets = dbx.Project_MaxBuckets_Raw(project.MaxBuckets)
createFields.PublicId = dbx.Project_PublicId(publicID[:])
createdProject, err := projects.db.Create_Project(ctx, createdProject, err := projects.db.Create_Project(ctx,
dbx.Project_Id(projectID[:]), dbx.Project_Id(projectID[:]),
@ -272,7 +289,7 @@ func (projects *projects) ListByOwnerID(ctx context.Context, ownerID uuid.UUID,
} }
rows, err := projects.sdb.Query(ctx, projects.sdb.Rebind(` rows, err := projects.sdb.Query(ctx, projects.sdb.Rebind(`
SELECT id, name, description, owner_id, rate_limit, max_buckets, created_at, SELECT id, public_id, name, description, owner_id, rate_limit, max_buckets, created_at,
(SELECT COUNT(*) FROM project_members WHERE project_id = projects.id) AS member_count (SELECT COUNT(*) FROM project_members WHERE project_id = projects.id) AS member_count
FROM projects FROM projects
WHERE owner_id = ? WHERE owner_id = ?
@ -297,7 +314,7 @@ func (projects *projects) ListByOwnerID(ctx context.Context, ownerID uuid.UUID,
} }
var rateLimit, maxBuckets sql.NullInt32 var rateLimit, maxBuckets sql.NullInt32
nextProject := &console.Project{} nextProject := &console.Project{}
err = rows.Scan(&nextProject.ID, &nextProject.Name, &nextProject.Description, &nextProject.OwnerID, &rateLimit, &maxBuckets, &nextProject.CreatedAt, &nextProject.MemberCount) err = rows.Scan(&nextProject.ID, &nextProject.PublicID, &nextProject.Name, &nextProject.Description, &nextProject.OwnerID, &rateLimit, &maxBuckets, &nextProject.CreatedAt, &nextProject.MemberCount)
if err != nil { if err != nil {
return console.ProjectsPage{}, err return console.ProjectsPage{}, err
} }
@ -329,6 +346,14 @@ func projectFromDBX(ctx context.Context, project *dbx.Project) (_ *console.Proje
return nil, err return nil, err
} }
var publicID uuid.UUID
if len(project.PublicId) > 0 {
publicID, err = uuid.FromBytes(project.PublicId)
if err != nil {
return nil, err
}
}
var partnerID uuid.UUID var partnerID uuid.UUID
if len(project.PartnerId) > 0 { if len(project.PartnerId) > 0 {
partnerID, err = uuid.FromBytes(project.PartnerId) partnerID, err = uuid.FromBytes(project.PartnerId)
@ -349,6 +374,7 @@ func projectFromDBX(ctx context.Context, project *dbx.Project) (_ *console.Proje
return &console.Project{ return &console.Project{
ID: id, ID: id,
PublicID: publicID,
Name: project.Name, Name: project.Name,
Description: project.Description, Description: project.Description,
PartnerID: partnerID, PartnerID: partnerID,

View File

@ -1,37 +1,36 @@
// Copyright (C) 2019 Storj Labs, Inc. // Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information. // See LICENSE for copying information.
package satellitedb package satellitedb_test
import ( import (
"context"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
"storj.io/storj/satellite/satellitedb/dbx" "storj.io/common/testcontext"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
) )
func TestProjectFromDbx(t *testing.T) { func TestProjectsGetByPublicID(t *testing.T) {
ctx := context.Background() satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
projects := db.Console().Projects()
t.Run("can't create dbo from nil dbx model", func(t *testing.T) { prj, err := projects.Insert(ctx, &console.Project{
project, err := projectFromDBX(ctx, nil) Name: "ProjectName",
Description: "projects description",
})
require.NoError(t, err)
require.NotNil(t, prj)
assert.Nil(t, project) pubID := prj.PublicID
assert.NotNil(t, err) require.NotNil(t, pubID)
assert.Error(t, err) require.False(t, pubID.IsZero())
})
t.Run("can't create dbo from dbx model with invalid ID", func(t *testing.T) { prj, err = projects.GetByPublicID(ctx, pubID)
dbxProject := dbx.Project{ require.NoError(t, err)
Id: []byte("qweqwe"), require.Equal(t, pubID, prj.PublicID)
}
project, err := projectFromDBX(ctx, &dbxProject)
assert.Nil(t, project)
assert.NotNil(t, err)
assert.Error(t, err)
}) })
} }