tenant

package
v0.0.0-...-9cf9cea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MinTenantDistributionAccountAmount is the minimum amount of the native asset that the host distribution account is allowed to
	// send to the tenant distribution account at a time. It is also used as the default amount to bootstrap a tenant distribution account,
	// when non is specified.
	MinTenantDistributionAccountAmount = 5

	// MaxTenantDistributionAccountAmount is the maximum amount of the native asset that the host distribution account is allowed to
	// send to the tenant distribution account at a time.
	MaxTenantDistributionAccountAmount = 50
)

Variables

View Source
var (
	ErrTenantDoesNotExist      = errors.New("tenant does not exist")
	ErrDuplicatedTenantName    = errors.New("duplicated tenant name")
	ErrEmptyTenantName         = errors.New("tenant name cannot be empty")
	ErrEmptyUpdateTenant       = errors.New("provide at least one field to be updated")
	ErrTenantNotFoundInContext = errors.New("tenant not found in context")
	ErrTooManyDefaultTenants   = errors.New("too many default tenants. Expected at most one default tenant")
)
View Source
var ErrNoDataSourcesAvailable = errors.New("no data sources are available")

Functions

func ApplyMigrationsForTenantFixture

func ApplyMigrationsForTenantFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, tenantName string)

func AssertRegisteredAssetsFixture

func AssertRegisteredAssetsFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, expectedAssets []string)

func AssertRegisteredUserFixture

func AssertRegisteredUserFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, userFirstName, userLastName, userEmail string)

func AssertRegisteredWalletsFixture

func AssertRegisteredWalletsFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, expectedWallets []string)

func CheckSchemaExistsFixture

func CheckSchemaExistsFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, schemaName string) bool

func DeleteAllTenantsFixture

func DeleteAllTenantsFixture(t *testing.T, ctx context.Context, adminDBConnectionPool db.DBConnectionPool)

func LoadDefaultTenantInContext

func LoadDefaultTenantInContext(t *testing.T, dbConnectionPool db.DBConnectionPool) context.Context

func PrepareDBForTenant

func PrepareDBForTenant(t *testing.T, dbt *dbtest.DB, tenantName string) string

func SaveTenantInContext

func SaveTenantInContext(ctx context.Context, t *Tenant) context.Context

SaveTenantInContext stores the tenant information in the context.

func TenantSchemaMatchTablesFixture

func TenantSchemaMatchTablesFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, schemaName string, tableNames []string)

TenantSchemaMatchTablesFixture asserts if the new tenant database schema has the tables passed by parameter.

func ValidateNativeAssetBootstrapAmount

func ValidateNativeAssetBootstrapAmount(amount int) error

Types

type FilterKey

type FilterKey string
const (
	FilterKeyOutStatus FilterKey = "status"
	FilterKeyName      FilterKey = "name"
	FilterKeyID        FilterKey = "id"
	FilterKeyNameOrID  FilterKey = "name_or_id"
	FilterKeyIsDefault FilterKey = "is_default"
)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func NewManager

func NewManager(opts ...Option) *Manager

func (*Manager) AddTenant

func (m *Manager) AddTenant(ctx context.Context, name string) (*Tenant, error)

func (*Manager) CreateTenantSchema

func (m *Manager) CreateTenantSchema(ctx context.Context, tenantName string) error

func (*Manager) DeleteTenantByName

func (m *Manager) DeleteTenantByName(ctx context.Context, name string) error

func (*Manager) DropTenantSchema

func (m *Manager) DropTenantSchema(ctx context.Context, tenantName string) error

func (*Manager) GetAllTenants

func (m *Manager) GetAllTenants(ctx context.Context, queryParams *QueryParams) ([]Tenant, error)

GetAllTenants returns all tenants in the database.

func (*Manager) GetDSNForTenant

func (m *Manager) GetDSNForTenant(ctx context.Context, tenantName string) (string, error)

func (*Manager) GetDSNForTenantByID

func (m *Manager) GetDSNForTenantByID(ctx context.Context, id string) (string, error)

func (*Manager) GetDefault

func (m *Manager) GetDefault(ctx context.Context) (*Tenant, error)

GetDefault returns the tenant where is_default is true. Returns an error if more than one tenant is set as default.

func (*Manager) GetTenant

func (m *Manager) GetTenant(ctx context.Context, queryParams *QueryParams) (*Tenant, error)

GetTenant is a generic method that fetches a tenant based on queryParams.

func (*Manager) GetTenantByID

func (m *Manager) GetTenantByID(ctx context.Context, id string) (*Tenant, error)

func (*Manager) GetTenantByIDOrName

func (m *Manager) GetTenantByIDOrName(ctx context.Context, arg string) (*Tenant, error)

GetTenantByIDOrName returns the tenant with a given id or name.

func (*Manager) GetTenantByName

func (m *Manager) GetTenantByName(ctx context.Context, name string) (*Tenant, error)

func (*Manager) SetDefault

func (m *Manager) SetDefault(ctx context.Context, sqlExec db.SQLExecuter, id string) (*Tenant, error)

SetDefault sets the is_default = true for the given tenant id.

func (*Manager) UpdateTenantConfig

func (m *Manager) UpdateTenantConfig(ctx context.Context, tu *TenantUpdate) (*Tenant, error)

type ManagerInterface

type ManagerInterface interface {
	GetDSNForTenant(ctx context.Context, tenantName string) (string, error)
	GetDSNForTenantByID(ctx context.Context, id string) (string, error)
	GetAllTenants(ctx context.Context, queryParams *QueryParams) ([]Tenant, error)
	GetTenant(ctx context.Context, queryParams *QueryParams) (*Tenant, error)
	GetTenantByID(ctx context.Context, id string) (*Tenant, error)
	GetTenantByName(ctx context.Context, name string) (*Tenant, error)
	GetTenantByIDOrName(ctx context.Context, arg string) (*Tenant, error)
	GetDefault(ctx context.Context) (*Tenant, error)
	SetDefault(ctx context.Context, sqlExec db.SQLExecuter, id string) (*Tenant, error)
	AddTenant(ctx context.Context, name string) (*Tenant, error)
	DeleteTenantByName(ctx context.Context, name string) error
	CreateTenantSchema(ctx context.Context, tenantName string) error
	DropTenantSchema(ctx context.Context, tenantName string) error
	UpdateTenantConfig(ctx context.Context, tu *TenantUpdate) (*Tenant, error)
}

type MultiTenantDataSourceRouter

type MultiTenantDataSourceRouter struct {
	// contains filtered or unexported fields
}

func NewMultiTenantDataSourceRouter

func NewMultiTenantDataSourceRouter(tenantManager ManagerInterface) *MultiTenantDataSourceRouter

func (*MultiTenantDataSourceRouter) AnyDataSource

func (m *MultiTenantDataSourceRouter) AnyDataSource() (db.DBConnectionPool, error)

AnyDataSource returns any database connection pool.

func (*MultiTenantDataSourceRouter) GetAllDataSources

func (m *MultiTenantDataSourceRouter) GetAllDataSources() ([]db.DBConnectionPool, error)

GetAllDataSources returns all the database connection pools.

func (*MultiTenantDataSourceRouter) GetDataSource

func (*MultiTenantDataSourceRouter) GetDataSourceForTenant

func (m *MultiTenantDataSourceRouter) GetDataSourceForTenant(
	ctx context.Context,
	currentTenant Tenant,
) (db.DBConnectionPool, error)

GetDataSourceForTenant returns the database connection pool for the given tenant if it exists, otherwise create a new one.

type Option

type Option func(m *Manager)

func WithDatabase

func WithDatabase(dbConnectionPool db.DBConnectionPool) Option

type QueryParams

type QueryParams struct {
	Query     string
	Page      int
	PageLimit int
	SortBy    data.SortField
	SortOrder data.SortOrder
	Filters   map[FilterKey]interface{}
}

type Tenant

type Tenant struct {
	ID                  string       `json:"id" db:"id"`
	Name                string       `json:"name" db:"name"`
	BaseURL             *string      `json:"base_url" db:"base_url"`
	SDPUIBaseURL        *string      `json:"sdp_ui_base_url" db:"sdp_ui_base_url"`
	Status              TenantStatus `json:"status" db:"status"`
	DistributionAccount *string      `json:"distribution_account" db:"distribution_account"`
	IsDefault           bool         `json:"is_default" db:"is_default"`
	CreatedAt           time.Time    `json:"created_at" db:"created_at"`
	UpdatedAt           time.Time    `json:"updated_at" db:"updated_at"`
}

func CreateTenantFixture

func CreateTenantFixture(t *testing.T, ctx context.Context, sqlExec db.SQLExecuter, name, distributionPubKey string) *Tenant

func GetTenantFromContext

func GetTenantFromContext(ctx context.Context) (*Tenant, error)

GetTenantFromContext retrieves the tenant information from the context.

func ResetTenantConfigFixture

func ResetTenantConfigFixture(t *testing.T, ctx context.Context, dbConnectionPool db.DBConnectionPool, tenantID string) *Tenant

type TenantManagerMock

type TenantManagerMock struct {
	mock.Mock
}

func (*TenantManagerMock) AddTenant

func (m *TenantManagerMock) AddTenant(ctx context.Context, name string) (*Tenant, error)

func (*TenantManagerMock) CreateTenantSchema

func (m *TenantManagerMock) CreateTenantSchema(ctx context.Context, name string) error

func (*TenantManagerMock) DeleteTenantByName

func (m *TenantManagerMock) DeleteTenantByName(ctx context.Context, name string) error

func (*TenantManagerMock) DropTenantSchema

func (m *TenantManagerMock) DropTenantSchema(ctx context.Context, name string) error

func (*TenantManagerMock) GetAllTenants

func (m *TenantManagerMock) GetAllTenants(ctx context.Context, queryParams *QueryParams) ([]Tenant, error)

func (*TenantManagerMock) GetDSNForTenant

func (m *TenantManagerMock) GetDSNForTenant(ctx context.Context, tenantName string) (string, error)

func (*TenantManagerMock) GetDSNForTenantByID

func (m *TenantManagerMock) GetDSNForTenantByID(ctx context.Context, id string) (string, error)

func (*TenantManagerMock) GetDefault

func (m *TenantManagerMock) GetDefault(ctx context.Context) (*Tenant, error)

func (*TenantManagerMock) GetTenant

func (m *TenantManagerMock) GetTenant(ctx context.Context, queryParams *QueryParams) (*Tenant, error)

func (*TenantManagerMock) GetTenantByID

func (m *TenantManagerMock) GetTenantByID(ctx context.Context, id string) (*Tenant, error)

func (*TenantManagerMock) GetTenantByIDOrName

func (m *TenantManagerMock) GetTenantByIDOrName(ctx context.Context, arg string) (*Tenant, error)

func (*TenantManagerMock) GetTenantByName

func (m *TenantManagerMock) GetTenantByName(ctx context.Context, name string) (*Tenant, error)

func (*TenantManagerMock) SetDefault

func (m *TenantManagerMock) SetDefault(ctx context.Context, sqlExec db.SQLExecuter, id string) (*Tenant, error)

func (*TenantManagerMock) UpdateTenantConfig

func (m *TenantManagerMock) UpdateTenantConfig(ctx context.Context, tu *TenantUpdate) (*Tenant, error)

type TenantStatus

type TenantStatus string
const (
	CreatedTenantStatus     TenantStatus = "TENANT_CREATED"
	ProvisionedTenantStatus TenantStatus = "TENANT_PROVISIONED"
	ActivatedTenantStatus   TenantStatus = "TENANT_ACTIVATED"
	DeactivatedTenantStatus TenantStatus = "TENANT_DEACTIVATED"
)

func (TenantStatus) IsValid

func (s TenantStatus) IsValid() bool

type TenantUpdate

type TenantUpdate struct {
	ID                  string        `db:"id"`
	BaseURL             *string       `db:"base_url"`
	SDPUIBaseURL        *string       `db:"sdp_ui_base_url"`
	Status              *TenantStatus `db:"status"`
	DistributionAccount *string       `db:"distribution_account"`
}

func (*TenantUpdate) Validate

func (tu *TenantUpdate) Validate() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL