storage

package
v0.0.0-...-936a01b Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package storage contains an implementation of interface between Go code and (almost any) SQL database like PostgreSQL, SQLite, or MariaDB. An implementation named DBStorage is constructed via New function and it is mandatory to call Close for any opened connection to database.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSuchObj = fmt.Errorf("no such object")

ErrNoSuchObj is indicating no result returned from db

View Source
var ErrUnknown = fmt.Errorf("unknown error during querying db")

ErrUnknown indicates db query failed without details (QueryRow returning Row wasn't populated)

Functions

func ColNames

func ColNames(cols ...ClusterCol) []string

ColNames Creates a list fo string column names from list of typed ClusterCols

Types

type Cluster

type Cluster struct {
	ID   ClusterID   `json:"id"`
	Name ClusterName `json:"name"`
}

Cluster represents cluster record in the controller service.

ID: unique key
Name: cluster GUID in the following format:
    c8590f31-e97e-4b85-b506-c45ce1911a12

type ClusterCol

type ClusterCol Column

ClusterCol is type of cluster column

type ClusterConfiguration

type ClusterConfiguration struct {
	ID            ClusterConfigurationID `json:"id"`
	Cluster       string                 `json:"cluster"`
	Configuration string                 `json:"configuration"`
	ChangedAt     string                 `json:"changed_at"`
	ChangedBy     string                 `json:"changed_by"`
	Active        string                 `json:"active"`
	Reason        string                 `json:"reason"`
}

ClusterConfiguration represents cluster configuration record in the controller service.

ID: unique key
Cluster: cluster ID (not name)
Configuration: a JSON structure stored in a string
ChangeAt: timestamp of the last configuration change
ChangeBy: username of admin that created or updated the configuration
Active: flag indicating whether the configuration is active or not
Reason: a string with any comment(s) about the cluster configuration

type ClusterConfigurationID

type ClusterConfigurationID ID

ClusterConfigurationID represents unique key of cluster configuration stored in database.

type ClusterID

type ClusterID ID

ClusterID represents unique key of cluster stored in database.

type ClusterName

type ClusterName Name

ClusterName represents name of cluster in format c8590f31-e97e-4b85-b506-c45ce1911a12

type ClusterQuery

type ClusterQuery struct {
	Cols Cols

	TableName string
	// contains filtered or unexported fields
}

ClusterQuery is Sql query model for Cluster

func NewClusterQuery

func NewClusterQuery(s Storager) *ClusterQuery

NewClusterQuery creates new ClusterQuery Sql model

func (*ClusterQuery) Map

func (c *ClusterQuery) Map(cols []ClusterCol, r *Cluster) ([]interface{}, error)

Map creates a list of destination struct fields using columns to select

func (*ClusterQuery) Query

func (c *ClusterQuery) Query() ClusterQueryBuilder

Query exposes typed Cluster queryBuilder

func (*ClusterQuery) QueryOne

func (c *ClusterQuery) QueryOne(ctx context.Context, req SearchClusterRequest) (*Cluster, error)

QueryOne will query DB with generated command and return one row as Cluster

type ClusterQueryBuilder

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

ClusterQueryBuilder is just a typed wrapped to build queries more conviniently using only exposed methods

func (ClusterQueryBuilder) Equals

func (b ClusterQueryBuilder) Equals(col ClusterCol, v interface{}) ClusterQueryBuilder

Equals add a SQL Where Predicate using AND (if any exists) using Equals (=) operand. Ignores Zero values in values For example: WHERE col = 3

func (ClusterQueryBuilder) WithPaging

func (b ClusterQueryBuilder) WithPaging(limit, offset int) ClusterQueryBuilder

WithPaging is setting how many recors (limit) and from which record (offset) This can be used for paging. It skips Zero values

type Cols

type Cols struct {
	ID   ClusterCol
	Name ClusterCol
}

Cols defines which columns exist in Clusters table, just for tyoe safe operations with them

type Column

type Column string

Column is typed reference to a sql column, which is further used by particular storage objects

type ConfigurationID

type ConfigurationID ID

ConfigurationID represents unique key of configuration stored in database.

type ConfigurationProfile

type ConfigurationProfile struct {
	ID            ConfigurationID `json:"id"`
	Configuration string          `json:"configuration"`
	ChangedAt     string          `json:"changed_at"`
	ChangedBy     string          `json:"changed_by"`
	Description   string          `json:"description"`
}

ConfigurationProfile represents configuration profile record in the controller service.

ID: unique key
Configuration: a JSON structure stored in a string
ChangeAt: username of admin that created or updated the configuration
ChangeBy: timestamp of the last configuration change
Description: a string with any comment(s) about the configuration

type ID

type ID int

ID represents unique ID for any object.

type ItemNotFoundError

type ItemNotFoundError struct {
	ItemID interface{}
}

ItemNotFoundError shows that item with provided ItemID wasn't found in storage

func (*ItemNotFoundError) Error

func (e *ItemNotFoundError) Error() string

type Name

type Name string

Name represents common name of object stored in database.

type SearchClusterRequest

type SearchClusterRequest struct {
	utils.Pagination
	ID   int    `schema:"id"`
	Name string `schema:"name"`
}

SearchClusterRequest defines type safe SearchCluster request, it is reused and defines request validation tags

type Storage

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

Storage represents an interface to any relational database based on SQL language

func New

func New(driverName, dataSourceName string) (Storage, error)

New function creates and initializes a new instance of Storage structure

func NewFromConnection

func NewFromConnection(connection *sql.DB, driverName string) Storage

NewFromConnection function creates and initializes a new instance of Storage interface from prepared connection

func (Storage) AckTrigger

func (storage Storage) AckTrigger(clusterName string, triggerID int64) error

AckTrigger sets a timestamp to the selected trigger + updates the 'active' flag. and returns error if trigger wasn't found

func (Storage) ChangeConfigurationProfile

func (storage Storage) ChangeConfigurationProfile(id int, username, description, configuration string) ([]ConfigurationProfile, error)

ChangeConfigurationProfile updates the existing configuration profile specified by its ID.

func (Storage) ChangeStateOfTriggerByID

func (storage Storage) ChangeStateOfTriggerByID(id int64, active int) error

ChangeStateOfTriggerByID change the state ('active', 'inactive') of trigger specified by its ID. returns ItemNotFoundError if there weren't rows with such id

func (Storage) Close

func (storage Storage) Close()

Close method closes the connection to database. Needs to be called at the end of application lifecycle.

func (Storage) Connections

func (storage Storage) Connections() *sql.DB

Connections is sql.DB connection

func (Storage) CreateClusterConfiguration

func (storage Storage) CreateClusterConfiguration(cluster, username, reason, description, configuration string) ([]ClusterConfiguration, error)

CreateClusterConfiguration creates new configuration for specified cluster.

func (Storage) CreateNewCluster

func (storage Storage) CreateNewCluster(id int64, name string) error

CreateNewCluster creates a new cluster with specified ID and name. It differs from RegisterNewCluster, because ID is specified explicitly here.

func (Storage) DeactivatePreviousConfigurations

func (storage Storage) DeactivatePreviousConfigurations(tx *sql.Tx, clusterID ClusterID) error

DeactivatePreviousConfigurations deactivate all previous configurations for the specified trigger. To be called inside transaction.

func (Storage) DeleteCluster

func (storage Storage) DeleteCluster(id int64) error

DeleteCluster deletes cluster with specified ID from the database.

func (Storage) DeleteClusterByName

func (storage Storage) DeleteClusterByName(name string) error

DeleteClusterByName deletes cluster with specified name from the database.

func (Storage) DeleteClusterConfigurationByID

func (storage Storage) DeleteClusterConfigurationByID(id int64) error

DeleteClusterConfigurationByID deletes cluster configuration specified by its ID. TODO: copy & paste, needs to be refactored later

func (Storage) DeleteConfigurationProfile

func (storage Storage) DeleteConfigurationProfile(id int) ([]ConfigurationProfile, error)

DeleteConfigurationProfile deletes a configuration profile specified by its name.

func (Storage) DeleteTriggerByID

func (storage Storage) DeleteTriggerByID(id int64) error

DeleteTriggerByID deletes trigger specified by its ID returns ItemNotFoundError if trigger didn't exist

func (Storage) DisableClusterConfiguration

func (storage Storage) DisableClusterConfiguration(cluster, username, reason string) ([]ClusterConfiguration, error)

DisableClusterConfiguration disables the specified cluster configuration (reset the 'active' flag). TODO: copy & paste, needs to be refactored later

func (Storage) EnableClusterConfiguration

func (storage Storage) EnableClusterConfiguration(cluster, username, reason string) ([]ClusterConfiguration, error)

EnableClusterConfiguration enables the specified cluster configuration (set the 'active' flag).

func (Storage) EnableOrDisableClusterConfigurationByID

func (storage Storage) EnableOrDisableClusterConfigurationByID(id int64, active string) error

EnableOrDisableClusterConfigurationByID enables or disables the specified cluster configuration (set or reset the 'active' flag). Please see also EnableClusterConfiguration and DisableClusterConfiguration

func (Storage) GetCluster

func (storage Storage) GetCluster(id int) (Cluster, error)

GetCluster method selects the specified cluster from database. Also see GetClusterByName.

func (Storage) GetClusterActiveConfiguration

func (storage Storage) GetClusterActiveConfiguration(cluster string) (string, error)

GetClusterActiveConfiguration reads one active configuration for the selected cluster.

func (Storage) GetClusterByName

func (storage Storage) GetClusterByName(name string) (Cluster, error)

GetClusterByName selects a cluster specified by its name. Also see GetCluster.

func (Storage) GetClusterConfigurationByID

func (storage Storage) GetClusterConfigurationByID(id int64) (string, error)

GetClusterConfigurationByID reads cluster configuration for the specified configuration ID.

func (Storage) GetConfigurationIDForCluster

func (storage Storage) GetConfigurationIDForCluster(cluster string) (int, error)

GetConfigurationIDForCluster reads the ID for the specified cluster name.

func (Storage) GetConfigurationProfile

func (storage Storage) GetConfigurationProfile(id int) (ConfigurationProfile, error)

GetConfigurationProfile selects one configuration profile identified by its ID.

func (Storage) GetTriggerByID

func (storage Storage) GetTriggerByID(id int64) (Trigger, error)

GetTriggerByID selects all informations about the trigger specified by its ID.

func (Storage) GetTriggerID

func (storage Storage) GetTriggerID(triggerType string) (int, error)

GetTriggerID select ID for specified trigger type (name).

func (Storage) InsertNewConfigurationProfile

func (storage Storage) InsertNewConfigurationProfile(tx *sql.Tx, configuration, username, description string) bool

InsertNewConfigurationProfile inserts new configuration profile into a database (in transaction).

func (Storage) InsertNewOperatorConfiguration

func (storage Storage) InsertNewOperatorConfiguration(tx *sql.Tx, clusterID ClusterID, configurationID int, username, reason string) error

InsertNewOperatorConfiguration inserts the new configuration for selected operator/cluster. To be called inside transaction.

func (Storage) ListActiveClusterTriggers

func (storage Storage) ListActiveClusterTriggers(clusterName string) ([]Trigger, error)

ListActiveClusterTriggers selects all active triggers assigned to the specified cluster.

func (Storage) ListAllClusterConfigurations

func (storage Storage) ListAllClusterConfigurations() ([]ClusterConfiguration, error)

ListAllClusterConfigurations selects all cluster configurations from the database.

func (Storage) ListAllTriggers

func (storage Storage) ListAllTriggers() ([]Trigger, error)

ListAllTriggers selects all triggers from the database.

func (Storage) ListClusterConfiguration

func (storage Storage) ListClusterConfiguration(cluster string) ([]ClusterConfiguration, error)

ListClusterConfiguration selects cluster configuration from the database for the specified cluster.

func (Storage) ListClusterTriggers

func (storage Storage) ListClusterTriggers(clusterName string) ([]Trigger, error)

ListClusterTriggers selects all triggers assigned to the specified cluster.

func (Storage) ListConfigurationProfiles

func (storage Storage) ListConfigurationProfiles() ([]ConfigurationProfile, error)

ListConfigurationProfiles selects list of all configuration profiles from database.

func (Storage) ListOfClusters

func (storage Storage) ListOfClusters() ([]Cluster, error)

ListOfClusters method selects all clusters from database.

func (Storage) Map

func (storage Storage) Map(cols []Column, mapper func(Column, interface{}) (interface{}, error), r interface{}) ([]interface{}, error)

Map creates a list of destination struct fields using columns to select

func (Storage) NewTrigger

func (storage Storage) NewTrigger(clusterName, triggerType, userName, reason, link string) error

NewTrigger constructs new trigger in a database.

func (Storage) NewTriggerType

func (storage Storage) NewTriggerType(ttype, description string) error

NewTriggerType inserts a trigger_type object in the database

func (Storage) Ping

func (storage Storage) Ping() error

Ping checks whether the database connection is really configured properly

func (Storage) Placeholder

func (storage Storage) Placeholder() sq.PlaceholderFormat

Placeholder returns current query argument placeholder (?, or $).It depends on driver used. In squirrel format

func (Storage) QueryOne

func (storage Storage) QueryOne(ctx context.Context, selectCols []Column, selectBuilder sq.SelectBuilder, mapper func(Column, interface{}) (interface{}, error), res interface{}) error

QueryOne is generating Sql query using squirell sql builder, querying it with db store and mapping result to destination object with provided mapper

func (Storage) RegisterNewCluster

func (storage Storage) RegisterNewCluster(name string) error

RegisterNewCluster inserts information about new cluster into the database. It differs from CreateNewCluster, because ID is not specified explicitly here.

func (Storage) SelectConfigurationProfileID

func (storage Storage) SelectConfigurationProfileID(tx *sql.Tx) (int, error)

SelectConfigurationProfileID selects the ID of lately inserted/created configuration profile. To be used in transaction.

func (Storage) StoreConfigurationProfile

func (storage Storage) StoreConfigurationProfile(username, description, configuration string) ([]ConfigurationProfile, error)

StoreConfigurationProfile stores a given configuration profile (string ATM) into the database.

type Storager

type Storager interface {
	Connections() *sql.DB
	Placeholder() sq.PlaceholderFormat
	QueryOne(context.Context, []Column, sq.SelectBuilder, func(Column, interface{}) (interface{}, error), interface{}) error
}

Storager exposes interface for testing

type Trigger

type Trigger struct {
	ID          TriggerID `json:"id"`
	Type        string    `json:"type"`
	Cluster     string    `json:"cluster"`
	Reason      string    `json:"reason"`
	Link        string    `json:"link"`
	TriggeredAt string    `json:"triggered_at"`
	TriggeredBy string    `json:"triggered_by"`
	AckedAt     string    `json:"acked_at"`
	Parameters  string    `json:"parameters"`
	Active      int       `json:"active"`
}

Trigger represents trigger record in the controller service

ID: unique key
Type: ID of trigger type
Cluster: cluster ID (not name)
Reason: a string with any comment(s) about the trigger
Link: link to any document with customer ACK with the trigger
TriggeredAt: timestamp of the last configuration change
TriggeredBy: username of admin that created or updated the trigger
AckedAt: timestamp where the insights operator acked the trigger
Parameters: parameters that needs to be pass to trigger code
Active: flag indicating whether the trigger is still active or not

type TriggerID

type TriggerID ID

TriggerID represents unique key of trigger stored in database.

Jump to

Keyboard shortcuts

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