database

package
v0.0.0-...-9235d7d Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database implements a database to store instance information in

Index

Constants

This section is empty.

Variables

View Source
var ErrInstanceNotFound = errors.New("instance not found")

ErrInstanceNotFound is returned from DB methods when an instance with the given ID could not be found.

Functions

This section is empty.

Types

type DB

type DB interface {
	// Inserts the instance into the database, returns the id or an error.
	CreateInstance(instance Instance) (string, error)

	// Removes the instance from the database, returns the id or an error.
	RemoveInstance(instance Instance) (string, error)

	// Retrieves the instance by its ID, or returns an error
	GetInstance(id string) (Instance, error)

	// Retrieves all instances by State
	GetInstancesByState(state string) ([]Instance, error)

	// Updates the instance with the given ID
	UpdateInstance(instance Instance) error

	// GetHashedToken gets the salt and the hashed token for a given token ID.
	// The returned attributes are salt, hash and an error.
	GetSaltAndHashForTokenID(tokenID uint64) ([]byte, []byte, error)

	// Insert a token into the database, returns the ID of the token
	InsertToken(description string, hash, salt []byte) (uint64, error)

	// List all the providers in the database
	ListProviders() ([]Provider, error)

	// Inserts the provider into the database, returns the id or an error. The
	// id will be automatically generated if one is not supplied.
	CreateProvider(provider Provider) (string, error)
}

DB is implemented by the supported database backends.

type Instance

type Instance struct {
	ID           string
	ProviderName string
	Image        string
	InstanceType string
	PublicSSHKey string
	State        string
	IPAddress    string
	UpstreamID   string
	ErrorReason  string
}

Instance contains the data stored about a compute instance in the database.

type MemoryDatabase

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

MemoryDatabase is a DB implementation that stores everything in memory, useful for testing.

func NewMemoryDatabase

func NewMemoryDatabase() *MemoryDatabase

NewMemoryDatabase creates and returns an empty MemoryDatabase.

func (*MemoryDatabase) CreateInstance

func (db *MemoryDatabase) CreateInstance(instance Instance) (string, error)

CreateInstance stores the instance in the database and returns the ID it generated for it. Never returns an error.

func (*MemoryDatabase) CreateProvider

func (db *MemoryDatabase) CreateProvider(provider Provider) (string, error)

CreateProvider always returns an error. It's not implemented yet, it's just here to implement the database.DB interface.

func (*MemoryDatabase) GetInstance

func (db *MemoryDatabase) GetInstance(id string) (Instance, error)

GetInstance returns the instance with the given ID, or ErrInstanceNotFound if no instance exists with that ID.

func (*MemoryDatabase) GetInstancesByState

func (db *MemoryDatabase) GetInstancesByState(state string) ([]Instance, error)

GetInstancesByState returns a slice of instances for a given state

func (*MemoryDatabase) GetSaltAndHashForTokenID

func (db *MemoryDatabase) GetSaltAndHashForTokenID(tokenID uint64) ([]byte, []byte, error)

GetSaltAndHashForTokenID returns the salt and hash for a token with the given ID. Panics if the token doesn't exist.

func (*MemoryDatabase) InsertToken

func (db *MemoryDatabase) InsertToken(description string, hash, salt []byte) (uint64, error)

InsertToken stores a token with the given description, hash and salt in the database. Never returns an error.

func (*MemoryDatabase) ListProviders

func (db *MemoryDatabase) ListProviders() ([]Provider, error)

ListProviders always returns an error. It's not implemented yet, it's just here to implement the database.DB interface.

func (*MemoryDatabase) RemoveInstance

func (db *MemoryDatabase) RemoveInstance(instance Instance) (string, error)

RemoveInstance removes the instance from the database.

func (*MemoryDatabase) UpdateInstance

func (db *MemoryDatabase) UpdateInstance(instance Instance) error

UpdateInstance updates the instance with the given ID, or returns ErrInstanceNotFound if no instance with that ID exists.

type PostgresDB

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

PostgresDB is a DB implementation backed by a Postgres database.

func NewPostgresDB

func NewPostgresDB(encryptionKey [32]byte, db *sql.DB) *PostgresDB

NewPostgresDB creates a new PostgresDB that uses the given sql.DB (must be a postgres connection). The encryptionKey must be provided if getting encrypted data, but can be nil if no encrypted data is needed.

func (*PostgresDB) CreateInstance

func (db *PostgresDB) CreateInstance(instance Instance) (string, error)

CreateInstance stores the given instance in teh database. A new UUID is generated for it and returned. If an error occurrs, the empty string and the error is returned.

func (*PostgresDB) CreateProvider

func (db *PostgresDB) CreateProvider(provider Provider) (string, error)

CreateProvider inserts a provider with the given data into the database.

A valid encryption key must have been provided to NewPostgresDB for this to work, or the stored configuration will not be valid.

func (*PostgresDB) GetInstance

func (db *PostgresDB) GetInstance(id string) (Instance, error)

GetInstance returns the instance with the given ID from the database. If no instance with the given ID exists, ErrInstanceNotFound is returned. If an error occurs, then an empty Instance struct and the error is returned.

func (*PostgresDB) GetInstancesByState

func (db *PostgresDB) GetInstancesByState(state string) ([]Instance, error)

GetInstancesByState returns a slice of instances for a given state

func (*PostgresDB) GetProviderByName

func (db *PostgresDB) GetProviderByName(id string) (*Provider, error)

GetProviderByName fetches a provider and decrypts the config

func (*PostgresDB) GetSaltAndHashForTokenID

func (db *PostgresDB) GetSaltAndHashForTokenID(tokenID uint64) (salt []byte, hash []byte, err error)

GetSaltAndHashForTokenID returns the salt and hash for the token with the given ID.

BUG(sarahhodne): Should return a special error if no token with the given ID exists.

func (*PostgresDB) InsertToken

func (db *PostgresDB) InsertToken(description string, hash, salt []byte) (uint64, error)

InsertToken inserts a token into the database with the given description, hash and salt.

func (*PostgresDB) ListProviders

func (db *PostgresDB) ListProviders() ([]Provider, error)

ListProviders returns a list of all the providers and their configurations.

A valid encryption key must have been provided to NewPostgresDB for this to work, or an error will always be returned.

func (*PostgresDB) RemoveInstance

func (db *PostgresDB) RemoveInstance(instance Instance) (string, error)

RemoveInstance deletes the given instance from the database. If an error occurs, the empty string and the error is returned.

func (*PostgresDB) UpdateInstance

func (db *PostgresDB) UpdateInstance(instance Instance) error

UpdateInstance updates the instane with the given ID in the database to match the given attributes. Returns ErrInstanceNotFound if an instance with the given ID isn't found.

BUG(sarahhodne): ErrInstanceNotFound is not returned when an instance with the given ID doesn't exist.

type Provider

type Provider struct {
	// ID is a UUID for this provider
	ID string

	// Type should match up with the alias passed into cloud.NewProvider
	Type string

	// Name is a unique name passed to the HTTP API to specify which provider to
	// create an instance on.
	Name string

	// Config is a provider-specific configuration, passed to cloud.NewProvider.
	Config []byte
}

Provider contains the data stored about a cloud provider in the database.

Notes

Bugs

  • ErrInstanceNotFound is not returned when an instance with the given ID doesn't exist.

  • Should return a special error if no token with the given ID exists.

Jump to

Keyboard shortcuts

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