model

package
v0.0.0-...-cd06882 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package model contains all the data models related to the Device Lease service. Each model implements CRUD operations for the entities as necessary.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDeviceNotFound      = errors.New("device not found")
	ErrDeviceAlreadyLeased = errors.New("device already leased")
)

Error types for Device model operations

View Source
var (
	ErrLeaseIdemKeyAlreadyExists = errors.New("idempotency key for lease on Device already used")
)

Error types for DeviceLeaseRecord model operations

Functions

func BulkCreateDeviceLeaseRecords

func BulkCreateDeviceLeaseRecords(ctx context.Context, tx *sql.Tx, records []DeviceLeaseRecord, leaseDurs []time.Duration) (map[string]*DeviceLeaseRecord, map[string]error, error)

BulkCreateDeviceLeaseRecords creates multiple DeviceLeaseRecords in the DB.

BulkCreateDeviceLeaseRecords takes a list of DeviceLeaseRecord models and attempts to bulk insert them into the database. On conflict of the idempotency key, it will do nothing.

func BulkUpdateDevicesToLeased

func BulkUpdateDevicesToLeased(ctx context.Context, tx *sql.Tx, deviceIDs []string, idType DeviceIDType) (map[string]*Device, map[string]error, error)

BulkUpdateDevicesToLeased marks a list of Devices as leased.

BulkUpdateDevicesToLeased takes a list of Device IDs (either dut_id asset tag or hostname) and marks them as LEASED. The query only tries to mark DEVICE_STATE_AVAILABLE Devices. Anything no found by the query is considered to be already leased. A list of updated Devices and associated errors are returned.

TODO (justinsuen): Need a way to know which ones are not found.

func CreateExtendLeaseRequest

func CreateExtendLeaseRequest(ctx context.Context, tx *sql.Tx, request ExtendLeaseRequest) error

CreateExtendLeaseRequest creates a ExtendLeaseRequest in the database.

func ExpireLeasesCron

func ExpireLeasesCron(ctx context.Context, tx *sql.Tx, t time.Time) (leaseIDs, deviceIDs []string, err error)

ExpireLeasesCron expires all leases that haven't been modified since the given timestamp, and returns the expired lease IDs and associated device IDs within a cron job.

func ExtendLease

func ExtendLease(ctx context.Context, tx *sql.Tx, leaseRec DeviceLeaseRecord) error

ExtendLease updates a lease record in a transaction.

ExtendLease uses COALESCE to only update fields with provided values. If there is no value provided, then it will use the current value of the device field in the db.

func ReleaseLease

func ReleaseLease(ctx context.Context, tx *sql.Tx, leaseID string) error

ReleaseLease releases a lease record in a transaction.

func UpsertDeviceFromUFS

func UpsertDeviceFromUFS(ctx context.Context, db *sql.DB, device Device) error

UpsertDeviceFromUFS upserts a Device in a transaction.

UpsertDeviceFromUFS will attempt to insert a Device pulled from UFS into the db. On conflict of the ID, the old device record will be updated with the new information except for device_address, device_type, and device_state. Those three will not be updated on conflict but should be inserted for new Devices.

Types

type Device

type Device struct {
	ID                string
	DutID             string
	DeviceAddress     string
	DeviceType        string
	DeviceState       string
	SchedulableLabels SchedulableLabels `json:"SchedulableLabels"`

	IsActive bool
	// CreatedTime is alwasys not NULL, so we don't have to use sql.NullTime.
	CreatedTime                  time.Time
	LastUpdatedTime              time.Time // Deprecated. Use NullableLastUpdatedTime instead.
	LastUpdatedTimeNullable      sql.NullTime
	LastNotificationTime         time.Time // Deprecated. Use NullableLastNotificationTime instead.
	LastNotificationTimeNullable sql.NullTime
}

Device contains a single row from the Devices table in the database.

func GetDeviceByID

func GetDeviceByID(ctx context.Context, db *sql.DB, idType DeviceIDType, deviceID string) (Device, error)

GetDeviceByID gets a Device from the database by a type of ID.

func ListDevices

func ListDevices(ctx context.Context, db *sql.DB, pageToken database.PageToken, pageSize int, filter string) ([]Device, database.PageToken, error)

ListDevices retrieves Devices with pagination.

func UpdateDeviceToAvailable

func UpdateDeviceToAvailable(ctx context.Context, tx *sql.Tx, device Device) (*Device, error)

UpdateDeviceToAvailable updates a Device to available in a transaction.

UpdateDeviceToAvailable requires both id and dut_id. This is to ensure the Device's unique hostname to asset tag pairing. The function uses COALESCE to only update fields with provided values. If there is no value provided, then it will use the current value of the device field in the db.

func UpdateDeviceToLeased

func UpdateDeviceToLeased(ctx context.Context, tx *sql.Tx, device Device, idType DeviceIDType) (Device, error)

UpdateDeviceToLeased updates a Device to Leased in a transaction.

UpdateDeviceToLeased uses COALESCE to only update fields with provided values. If there is no value provided, then it will use the current value of the device field in the db. If no Device is returned by RETURNING, that means the Device was not found or already leased.

func (*Device) ApplySwarmingDims

func (d *Device) ApplySwarmingDims(ctx context.Context, dims swarming.Dimensions) error

ApplySwarmingDims applies swarming Dimensions to the device object.

Mostly it set the scheduleable labels and the dut id field. If no dut_id dim are found, then the DUT ID will also not be set.

type DeviceIDType

type DeviceIDType string

DeviceIDType indicates the type of ID used to identify a Device in DB.

const (
	IDTypeHostname DeviceIDType = "hostname"
	IDTypeDutID    DeviceIDType = "dut_id"
)

type DeviceLeaseRecord

type DeviceLeaseRecord struct {
	ID             string
	IdempotencyKey string
	DutID          string
	DeviceID       string
	DeviceAddress  string
	DeviceType     string
	OwnerID        string

	LeasedTime      time.Time
	ReleasedTime    time.Time
	ExpirationTime  time.Time
	LastUpdatedTime time.Time
}

DeviceLeaseRecord contains a single row from the DeviceLeaseRecords table in the database.

func CreateDeviceLeaseRecord

func CreateDeviceLeaseRecord(ctx context.Context, tx *sql.Tx, record DeviceLeaseRecord, leaseDur time.Duration) (DeviceLeaseRecord, error)

CreateDeviceLeaseRecord creates a DeviceLeaseRecord in the database.

func ExpireLeases

func ExpireLeases(ctx context.Context, tx *sql.Tx, t time.Time) (releasedLeases []DeviceLeaseRecord, err error)

ExpireLeases expires all leases that haven't been modified since the given timestamp, and returns the expired lease IDs and associated device IDs.

func GetDeviceLeaseRecordByID

func GetDeviceLeaseRecordByID(ctx context.Context, db *sql.DB, recordID string) (*DeviceLeaseRecord, error)

GetDeviceLeaseRecordByID gets a DeviceLeaseRecord from the database by name.

func GetDeviceLeaseRecordByIdemKey

func GetDeviceLeaseRecordByIdemKey(ctx context.Context, db *sql.DB, idemKey string) (DeviceLeaseRecord, error)

GetDeviceLeaseRecordByIdemKey gets a DeviceLeaseRecord from the database by idempotency key.

func ListLeases

func ListLeases(ctx context.Context, db *sql.DB, pageToken database.PageToken, pageSize int, filter string) ([]DeviceLeaseRecord, database.PageToken, error)

ListLeases retrieves DeviceLeaseRecords with pagination.

type DeviceLeaseRecordData

type DeviceLeaseRecordData struct {
	Records []DeviceLeaseRecord
}

DeviceLeaseRecordData is used to pass data to the HTML template.

type ExtendLeaseRequest

type ExtendLeaseRequest struct {
	ID             string
	LeaseID        string
	IdempotencyKey string
	ExtendDuration int64
	RequestTime    time.Time
	ExpirationTime time.Time
}

ExtendLeaseRequest contains a single row from the ExtendLeaseRequests table in the database.

func GetExtendLeaseRequestByIdemKey

func GetExtendLeaseRequestByIdemKey(ctx context.Context, db *sql.DB, idemKey string) (ExtendLeaseRequest, error)

GetExtendLeaseRequestByIdemKey gets a ExtendLeaseRequest from the database by idempotency key.

type LabelValues

type LabelValues struct {
	Values []string
}

LabelValues is the struct containing an array of label values.

type SchedulableLabels

type SchedulableLabels map[string]LabelValues

SchedulableLabels is made up of a label key and LabelValues.

func (SchedulableLabels) GormDataType

func (SchedulableLabels) GormDataType() string

GormDataType expresses SchedulableLabels as a gorm type to db.

func (*SchedulableLabels) Scan

func (s *SchedulableLabels) Scan(value interface{}) error

Scan implements scanner interface for SchedulableLabels.

func (SchedulableLabels) Value

func (s SchedulableLabels) Value() (driver.Value, error)

Value implements Valuer interface for SchedulableLabels.

Jump to

Keyboard shortcuts

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