models

package
v0.3.16 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAccountNotFound = errors.New("account not found")
View Source
var ErrNoClaim = errors.New("no claim")
View Source
var ErrNoEnoughAccountsAvailable = errors.New("no enough accounts available")

Functions

func CountAvailable

func CountAvailable[T AvailabilityMarker](resources []T) int

CountAvailable return the number of resources not in use

func CountOlder

func CountOlder(duration time.Duration, accounts []Resource) (int, error)

CountOlder returns the number of accounts in use for more than N day

func CountToCleanup

func CountToCleanup[T AvailabilityMarker](resources []T) int

CountToCleanup return the number of accounts to cleanup

func CountUsed

func CountUsed[T AvailabilityMarker](resources []T) int

CountUsed return the number of resources in use

func DeletePlacementByServiceUuid

func DeletePlacementByServiceUuid(dbpool *pgxpool.Pool, accountProvider AwsAccountProvider, serviceUuid string) error

DeletePlacementByServiceUuid deletes a placement by ServiceUuid

func FilterByReservation added in v0.3.5

func FilterByReservation[T AvailabilityMarker](resources []T, reservation string) []T

func Sort

func Sort[T Sortable](accounts []T, by string) []T

func Used

func Used[T AvailabilityMarker](resources []T) []T

Used return the resources in use

Types

type Account

type Account struct {
	Resource
}

type AvailabilityMarker

type AvailabilityMarker interface {
	GetReservation() string
	// contains filtered or unexported methods
}

type AwsAccount

type AwsAccount struct {
	Account
	Kind         string `json:"kind"` // "AwsSandbox"
	Name         string `json:"name"`
	Reservation  string `json:"reservation,omitempty"`
	AccountID    string `json:"account_id"`
	Zone         string `json:"zone"`
	HostedZoneID string `json:"hosted_zone_id"`

	ConanStatus       string    `json:"conan_status,omitempty"`
	ConanTimestamp    time.Time `json:"conan_timestamp,omitempty"`
	ConanHostname     string    `json:"conan_hostname,omitempty"`
	ConanCleanupCount int       `json:"conan_cleanup_count,omitempty"`
}

func (AwsAccount) CloseAccount added in v0.3.16

func (a AwsAccount) CloseAccount() error

func (AwsAccount) GetLastStatus added in v0.3.0

func (a AwsAccount) GetLastStatus(dbpool *pgxpool.Pool) (*LifecycleResourceJob, error)

func (AwsAccount) GetReservation added in v0.3.5

func (a AwsAccount) GetReservation() string

func (AwsAccount) GetUpdatedAt

func (a AwsAccount) GetUpdatedAt() time.Time

func (AwsAccount) NameInt

func (a AwsAccount) NameInt() int

func (*AwsAccount) Render added in v0.3.5

func (a *AwsAccount) Render(w http.ResponseWriter, r *http.Request) error

func (AwsAccount) Start added in v0.3.0

func (a AwsAccount) Start(ctx context.Context, creds *ststypes.Credentials) error

Start method starts all the stopped instances in the account

func (AwsAccount) Status added in v0.3.0

Status method returns the status of all the instances in the account

func (AwsAccount) Stop added in v0.3.0

func (a AwsAccount) Stop(ctx context.Context, creds *ststypes.Credentials) error

Stop method stops all the running instances in the account

type AwsAccountProvider

type AwsAccountProvider interface {
	//Annotations(account AwsAccount) (map[string]string, error)
	Count() (int, error)
	CountAvailable(reservation string) (int, error)
	DecryptSecret(encrypted string) (string, error)
	Delete(name string) error
	FetchAll() ([]AwsAccount, error)
	FetchAllActiveByServiceUuid(serviceUuid string) ([]AwsAccount, error)
	FetchAllActiveByServiceUuidWithCreds(serviceUuid string) ([]AwsAccountWithCreds, error)
	FetchAllAvailable() ([]AwsAccount, error)
	FetchAllByReservation(reservation string) ([]AwsAccount, error)
	FetchAllByServiceUuid(serviceUuid string) ([]AwsAccount, error)
	FetchAllByServiceUuidWithCreds(serviceUuid string) ([]AwsAccountWithCreds, error)
	FetchAllSorted(by string) ([]AwsAccount, error)
	FetchAllToCleanup() ([]AwsAccount, error)
	FetchByName(name string) (AwsAccount, error)
	MarkForCleanup(name string) error
	MarkForCleanupByServiceUuid(serviceUuid string) error
	Request(service_uuid string, reservation string, count int, annotations map[string]string) ([]AwsAccountWithCreds, error)
	Reserve(reservation string, count int) ([]AwsAccount, error)
	ScaleDownReservation(reservation string, count int) error
}

AwsAccountProvider interface to interact with different databases: dynamodb and postgresql

type AwsAccountWithCreds

type AwsAccountWithCreds struct {
	AwsAccount

	Credentials []any `json:"credentials"`
}

type AwsAccounts added in v0.3.5

type AwsAccounts []AwsAccount

func (*AwsAccounts) Render added in v0.3.5

func (a *AwsAccounts) Render(w http.ResponseWriter, r *http.Request) error

type AwsIamKey

type AwsIamKey struct {
	Kind               string `json:"kind"` // "aws_iam_key"
	Name               string `json:"name"`
	AwsAccessKeyID     string `json:"aws_access_key_id"`
	AwsSecretAccessKey string `json:"aws_secret_access_key"`
}

type Instance added in v0.3.0

type Instance struct {
	InstanceId   string `json:"instance_id,omitempty"`
	InstanceName string `json:"instance_name,omitempty"`
	InstanceType string `json:"instance_type,omitempty"`
	Region       string `json:"region,omitempty"`
	State        string `json:"state,omitempty"`
}

type LifecyclePlacementJob added in v0.3.0

type LifecyclePlacementJob struct {
	Model

	PlacementID int           `json:"placement_id"`
	Status      string        `json:"status"`
	Action      string        `json:"lifecycle_action"`
	Request     any           `json:"request,omitempty"`
	RequestID   string        `json:"request_id,omitempty"`
	DbPool      *pgxpool.Pool `json:"dbpool"`
	Locality    string        `json:"locality,omitempty"`
}

func GetLifecyclePlacementJob added in v0.3.0

func GetLifecyclePlacementJob(dbpool *pgxpool.Pool, id int) (*LifecyclePlacementJob, error)

GetLifecyclePlacementJob returns a LifecyclePlacementJob by ID

func GetLifecyclePlacementJobByRequestID added in v0.3.3

func GetLifecyclePlacementJobByRequestID(dbpool *pgxpool.Pool, requestID string) (*LifecyclePlacementJob, error)

GetLifecyclePlacementJob returns a LifecyclePlacementJob by ID

func (*LifecyclePlacementJob) Claim added in v0.3.0

func (j *LifecyclePlacementJob) Claim() error

ClaimPlacementJob claims a placement job by setting the status to initializing

func (*LifecyclePlacementJob) Create added in v0.3.3

func (j *LifecyclePlacementJob) Create() error

Create creates a new LifecyclePlacementJob by inserting it into the database

func (*LifecyclePlacementJob) GlobalStatus added in v0.3.3

func (j *LifecyclePlacementJob) GlobalStatus() (string, error)

GlobalStatus returns the status of a LifecyclePlacementJob considering all it's children

func (*LifecyclePlacementJob) SetStatus added in v0.3.0

func (j *LifecyclePlacementJob) SetStatus(status string) error

SetLifecycleResourceJobStatus sets the status of a LifecycleResourceJob

type LifecycleResourceJob added in v0.3.0

type LifecycleResourceJob struct {
	Model

	ResourceName string        `json:"resource_name"`
	ResourceType string        `json:"resource_type"`
	ParentID     int           `json:"parent_id,omitempty"`
	Status       string        `json:"status"`
	Action       string        `json:"lifecycle_action"`
	Request      any           `json:"request,omitempty"`
	RequestID    string        `json:"request_id,omitempty"`
	DbPool       *pgxpool.Pool `json:"dbpool,omitempty"`
	Result       Status        `json:"lifecycle_result,omitempty"`
	Locality     string        `json:"locality,omitempty"`
}

func GetLifecycleResourceJob added in v0.3.0

func GetLifecycleResourceJob(dbpool *pgxpool.Pool, id int) (*LifecycleResourceJob, error)

GetLifecycleResourceJob returns a LifecycleResourceJob by ID

func GetLifecycleResourceJobByRequestID added in v0.3.3

func GetLifecycleResourceJobByRequestID(dbpool *pgxpool.Pool, requestID string) (*LifecycleResourceJob, error)

func (*LifecycleResourceJob) Claim added in v0.3.0

func (j *LifecycleResourceJob) Claim() error

ClaimResourceJob claims a resource job by setting the status to initializing

func (*LifecycleResourceJob) Create added in v0.3.0

func (j *LifecycleResourceJob) Create() error

Create creates a new LifecycleResourceJob by inserting it into the database

func (*LifecycleResourceJob) SetStatus added in v0.3.0

func (j *LifecycleResourceJob) SetStatus(status string) error

SetLifecycleResourceJobStatus sets the status of a LifecycleResourceJob

type Model

type Model struct {
	ID        int       `json:"id,omitempty"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

type Placement

type Placement struct {
	Model

	ServiceUuid string            `json:"service_uuid"`
	Resources   []any             `json:"resources,omitempty"`
	Annotations map[string]string `json:"annotations"`
	Request     any               `json:"request"`
}

func GetPlacement

func GetPlacement(dbpool *pgxpool.Pool, id int) (*Placement, error)

GetPlacement returns a placement by ID

func GetPlacementByServiceUuid

func GetPlacementByServiceUuid(dbpool *pgxpool.Pool, serviceUuid string) (*Placement, error)

GetPlacementByServiceUuid returns a placement by service_uuid

func (*Placement) Delete

func (p *Placement) Delete(dbpool *pgxpool.Pool, accountProvider AwsAccountProvider) error

func (*Placement) GetLastStatus added in v0.3.3

func (p *Placement) GetLastStatus(dbpool *pgxpool.Pool) ([]*LifecycleResourceJob, error)

func (*Placement) LoadActiveResources added in v0.3.5

func (p *Placement) LoadActiveResources(accountProvider AwsAccountProvider) error

func (*Placement) LoadActiveResourcesWithCreds added in v0.3.5

func (p *Placement) LoadActiveResourcesWithCreds(accountProvider AwsAccountProvider) error

func (*Placement) LoadResources

func (p *Placement) LoadResources(accountProvider AwsAccountProvider) error

func (*Placement) LoadResourcesWithCreds added in v0.3.1

func (p *Placement) LoadResourcesWithCreds(accountProvider AwsAccountProvider) error

func (*Placement) Render

func (p *Placement) Render(w http.ResponseWriter, r *http.Request) error

func (*Placement) Save

func (p *Placement) Save(dbpool *pgxpool.Pool) error

type PlacementWithCreds

type PlacementWithCreds struct {
	Placement

	Resources []any `json:"resources"`
}

type Placements

type Placements []Placement

func GetAllPlacements

func GetAllPlacements(dbpool *pgxpool.Pool) (Placements, error)

func (Placements) Render

func (p Placements) Render(w http.ResponseWriter, r *http.Request) error

type Reservation added in v0.3.5

type Reservation struct {
	Model

	Name    string             `json:"name"`
	Status  string             `json:"status"`
	Request ReservationRequest `json:"request"`
}

func GetReservationByName added in v0.3.5

func GetReservationByName(dbpool *pgxpool.Pool, name string) (Reservation, error)

GetReservationByName fetches a reservation by its name returns the reservation and an error

func (*Reservation) Delete added in v0.3.5

func (r *Reservation) Delete(dbpool *pgxpool.Pool) error

Delete deletes a reservation from the DB

func (*Reservation) Initialize added in v0.3.5

func (r *Reservation) Initialize(dbpool *pgxpool.Pool, a AwsAccountProvider)

Initialize initializes a reservation It's an async operation that takes care of keeping the resources reserved Status up to date and also update the resources to match what's defined in Request. When everything is in sync, Status is set to 'success' If something goes wrong, Status is set to 'error'

func (*Reservation) Remove added in v0.3.5

func (r *Reservation) Remove(dbpool *pgxpool.Pool, a AwsAccountProvider)

Delete deletes a reservation This is an async operation that goes through all the reserved resources and unmark them. Then the reservation is deleted from the DB is all goes well. If something goes wrong, the reservation is marked as 'error'

func (*Reservation) Render added in v0.3.5

func (o *Reservation) Render(w http.ResponseWriter, r *http.Request) error

func (*Reservation) Save added in v0.3.5

func (r *Reservation) Save(dbpool *pgxpool.Pool) error

Save saves a reservation to the database Reservations implies async operations. This function is low level and doesn't deal with async operations. For that, see the Initialize and Synchronize methods

func (*Reservation) Update added in v0.3.5

func (r *Reservation) Update(dbpool *pgxpool.Pool, a AwsAccountProvider, req ReservationRequest)

Update is an async operation to update a reservation from a reservationRequest

func (*Reservation) UpdateStatus added in v0.3.5

func (r *Reservation) UpdateStatus(dbpool *pgxpool.Pool, status string) error

UpdateStatus updates the status of a reservation

type ReservationRequest added in v0.3.5

type ReservationRequest struct {
	Name      string            `json:"name"`
	Resources []ResourceRequest `json:"resources"`
}

func (*ReservationRequest) Bind added in v0.3.5

func (r *ReservationRequest) Bind(r2 *http.Request) error

func (*ReservationRequest) Validate added in v0.3.5

type Reservations added in v0.3.5

type Reservations []Reservation

func (Reservations) Render added in v0.3.5

type Resource

type Resource struct {
	Model

	ServiceUuid string `json:"service_uuid"`
	Available   bool   `json:"available"`
	ToCleanup   bool   `json:"to_cleanup"`

	Annotations map[string]string `json:"annotations"`
}

type ResourceRequest added in v0.3.5

type ResourceRequest struct {
	Kind  string `json:"kind"`
	Count int    `json:"count"`
}

type ResourceWithCreds

type ResourceWithCreds struct {
	Resource

	Credentials []any `json:"credentials"`
}

type Sortable

type Sortable interface {
	NameInt() int
	GetUpdatedAt() time.Time
}

type Status added in v0.3.0

type Status struct {
	AccountName string     `json:"account_name"`
	AccountKind string     `json:"account_kind"`
	Instances   []Instance `json:"instances"`
	UpdatedAt   time.Time  `json:"updated_at,omitempty"`
	Status      string     `json:"status,omitempty"`
}

Status type

func MakeStatus added in v0.3.3

func MakeStatus(job *LifecycleResourceJob) Status

type Token

type Token struct {
	Model

	Kind       string    `json:"kind"`
	Name       string    `json:"name"`
	Role       string    `json:"role"`
	Iat        int64     `json:"iat"`
	Exp        int64     `json:"exp"`
	Expiration time.Time `json:"expiration"`
	Valid      bool      `json:"valid"`
}

func CreateToken

func CreateToken(claims map[string]any) (Token, error)

func FetchTokenById

func FetchTokenById(dbpool *pgxpool.Pool, id int) (Token, error)

func (Token) Invalidate

func (t Token) Invalidate(dbpool *pgxpool.Pool) error

Invalidate the token

func (Token) Save

func (t Token) Save(dbpool *pgxpool.Pool) (id int, err error)

type Tokens

type Tokens []Token

func FetchAllTokens

func FetchAllTokens(dbpool *pgxpool.Pool) (Tokens, error)

func (*Tokens) Render

func (t *Tokens) Render(w http.ResponseWriter, r *http.Request) error

Jump to

Keyboard shortcuts

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