dao

package
v0.0.0-...-4263410 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package dao provides a Database Access Object interface to stored data.

All functions require the following variables to be set in the context:

* Logger: for all context-aware logging. * Account ID: for multi-tenancy, unless marked with UNSCOPED word.

Functions marked as UNSCOPED can be safely used from contexts where there is exactly zero function arguments coming from an user (e.g. ID was retrieved via another DAO call that was scoped).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRows is returned when there are no rows in the result
	// Typically, REST requests should end up with 404 error
	ErrNoRows = pgx.ErrNoRows

	// ErrAffectedMismatch is returned when unexpected number of affected rows
	// was returned for INSERT, UPDATE and DELETE queries.
	// Typically, REST requests should end up with 409 error
	ErrAffectedMismatch = errors.New("unexpected affected rows")

	// ErrValidation is returned when model does not validate
	ErrValidation = usrerr.New(400, "validation error", "invalid input")

	// ErrTransformation is returned when model transformation fails
	ErrTransformation = errors.New("transformation error")

	// ErrWrongAccount is returned on DAO operations with not matching account id in the context
	ErrWrongAccount = usrerr.New(403, "wrong account", "incorrect user account")

	// ErrStubGeneric is a generic error returned for test-related cases
	ErrStubGeneric = errors.New("generic stub error")

	// ErrStubMissingContext is returned when stub object is missing from the context
	ErrStubMissingContext = errors.New("missing variable in context")

	// ErrStubContextAlreadySet is returned when stub object was already added to the context
	ErrStubContextAlreadySet = errors.New("context object already set")

	// ErrReservationRateExceeded is returned when SQL constraint does not allow to insert more reservations
	ErrReservationRateExceeded = usrerr.New(429, "rate limit exceeded", "too many reservations, wait and retry")

	// ErrPubkeyNotFound is returned when a nil pointer to a pubkey is used for reservation detail
	ErrPubkeyNotFound = usrerr.New(404, "pubkey not found", "no pubkey found, it may have been already deleted")
)
View Source
var GetAccountDao func(ctx context.Context) AccountDao
View Source
var GetPubkeyDao func(ctx context.Context) PubkeyDao
View Source
var GetReservationDao func(ctx context.Context) ReservationDao
View Source
var GetServiceDao func(ctx context.Context) ServiceDao
View Source
var GetStatDao func(ctx context.Context) StatDao

Functions

func WithTransaction

func WithTransaction(ctx context.Context, fn TxFn) error

WithTransaction creates a new transaction and handles rollback/commit based on the error object returned by the `TxFn` or when it panics.

Types

type AccountDao

type AccountDao interface {
	// Create is meant for integration tests, use GetOrCreateByIdentity instead.
	Create(ctx context.Context, pk *models.Account) error
	GetById(ctx context.Context, id int64) (*models.Account, error)
	GetOrCreateByIdentity(ctx context.Context, orgId string, accountNumber string) (*models.Account, error)
	GetByOrgId(ctx context.Context, orgId string) (*models.Account, error)
	List(ctx context.Context, limit, offset int64) ([]*models.Account, error)
}

AccountDao represents an account (tenant)

type PubkeyDao

type PubkeyDao interface {
	Create(ctx context.Context, pk *models.Pubkey) error
	Update(ctx context.Context, pk *models.Pubkey) error
	GetById(ctx context.Context, id int64) (*models.Pubkey, error)
	List(ctx context.Context, limit, offset int64) ([]*models.Pubkey, error)
	Count(ctx context.Context) (int, error)
	Delete(ctx context.Context, id int64) error

	UnscopedCreateResource(ctx context.Context, pkr *models.PubkeyResource) error
	UnscopedGetResourceBySourceAndRegion(ctx context.Context, pubkeyId int64, sourceId string, region string) (*models.PubkeyResource, error)
	UnscopedListResourcesByPubkeyId(ctx context.Context, pkId int64) ([]*models.PubkeyResource, error)
	UnscopedDeleteResource(ctx context.Context, id int64) error
}

PubkeyDao represents Pubkeys (public part of ssh key pair) and corresponding Resources (uploaded pubkeys to specific cloud providers in specific regions).

type ReservationDao

type ReservationDao interface {
	// CreateNoop creates no operation reservation with details in a single transaction.
	CreateNoop(ctx context.Context, reservation *models.NoopReservation) error

	// CreateAWS creates AWS reservation with details in a single transaction.
	CreateAWS(ctx context.Context, reservation *models.AWSReservation) error

	// CreateAzure creates Azure reservation with details in a single transaction.
	CreateAzure(ctx context.Context, reservation *models.AzureReservation) error

	// CreateGCP creates GCP reservation with details in a single transaction.
	CreateGCP(ctx context.Context, reservation *models.GCPReservation) error

	// CreateInstance inserts instance associated to a reservation.
	CreateInstance(ctx context.Context, reservation *models.ReservationInstance) error

	// GetById returns reservation for a particular account.
	GetById(ctx context.Context, id int64) (*models.Reservation, error)

	// GetAWSById returns reservation for a particular account.
	GetAWSById(ctx context.Context, id int64) (*models.AWSReservation, error)

	// GetAzureById returns Azure reservation for a given id.
	GetAzureById(ctx context.Context, id int64) (*models.AzureReservation, error)

	// GetGCPById returns reservation for a particular account.
	GetGCPById(ctx context.Context, id int64) (*models.GCPReservation, error)

	// Count returns total reservations for a particular account.
	Count(ctx context.Context) (int, error)

	// List returns reservation for a particular account.
	List(ctx context.Context, limit, offset int64) ([]*models.Reservation, error)

	// ListInstances returns instances associated to a reservation. UNSCOPED.
	// It currently lists all instances and not instances for a reservation, this is a TODO.
	ListInstances(ctx context.Context, reservationId int64) ([]*models.ReservationInstance, error)

	// UpdateStatus sets status field and increment step counter by addSteps. UNSCOPED.
	UpdateStatus(ctx context.Context, id int64, status string, addSteps int32) error

	// UnscopedUpdateAWSDetail updates details of the AWS reservation. UNSCOPED.
	UnscopedUpdateAWSDetail(ctx context.Context, id int64, awsDetail *models.AWSDetail) error

	// UpdateReservationIDForAWS updates AWS reservation id field. UNSCOPED.
	UpdateReservationIDForAWS(ctx context.Context, id int64, awsReservationId string) error

	// UpdateOperationNameForGCP updates GCP operation name field. UNSCOPED.
	UpdateOperationNameForGCP(ctx context.Context, id int64, gcpOperationName string) error

	// UpdateReservationInstance updates an instance with its description
	UpdateReservationInstance(ctx context.Context, reservationID int64, instance *clients.InstanceDescription) error

	// FinishWithSuccess sets Success flag. UNSCOPED.
	FinishWithSuccess(ctx context.Context, id int64) error

	// FinishWithError sets Success flag and Error flag. UNSCOPED.
	FinishWithError(ctx context.Context, id int64, errorString string) error

	// Delete deletes a reservation. Only used in tests and background cleanup job. UNSCOPED.
	Delete(ctx context.Context, id int64) error

	// Cleanup old reservations
	Cleanup(ctx context.Context) error
}

ReservationDao represents a reservation, an abstraction of one or more background jobs with associated detail information different for different cloud providers (like number of vCPUs, instance IDs created etc).

type ServiceDao

type ServiceDao interface {
	RecalculatePubkeyFingerprints(ctx context.Context) (int, error)
}

ServiceDao is used for service operations like migrations. All operations are UNSCOPED. See pgx/service_pgx.go for documentation.

type StatDao

type StatDao interface {
	Get(ctx context.Context, delayMin int) (*models.Statistics, error)
}

StatDao represents stats about the application run

type TxFn

type TxFn func(tx pgx.Tx) error

A TxFn is a function that will be called with an initialized `Transaction` object that can be used for executing statements and queries against a database.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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