models

package
v0.0.0-...-824b32f Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2021 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SyncServiceWitManifest

func SyncServiceWitManifest(sm ServiceManifest) (err error)

SyncServiceWitManifest assert a service along with all of its depndencies in a single transactions

Types

type Base

type Base struct {
	ID uint `json:"id" gorm:"primarykey"`
	NIDBase
}

Base contains common columns for all tables with UUID

type Callback

type Callback struct {
	Base
	CallbackAssignable
	Name            string         `json:"name" gorm:"<-:create;uniqueIndex:idx_unique_service_callback"`
	ServiceID       string         `json:"-" gorm:"<-:create;notNull;uniqueIndex:idx_unique_service_callback;uniqueIndex:idx_unique_service_path;"`
	Service         *Service       `json:"-" gorm:"foreignKey:ServiceID;references:Name;"`
	RetryPolicyID   sql.NullString `json:"-"`
	RetryPolicy     *RetryPolicy   `json:"retry_policy,omitempty" gorm:"foreignKey:RetryPolicyID,ServiceID;references:Name,ServiceID"`
	PriorityGroupID string         `json:"-"`
	PriorityGroup   *PriorityGroup `` /* 130-byte string literal not displayed */
	Events          []*Event       `json:"-" gorm:"many2many:callbacks_events;constraint:OnDelete:CASCADE;"`
}

Callback model

func (*Callback) BeforeSave

func (c *Callback) BeforeSave(tx *gorm.DB) (err error)

BeforeSave prepares Callback model and resolves relationships before inserting it into database

func (*Callback) Delete

func (c *Callback) Delete() error

Delete removes the callback

func (*Callback) Load

func (c *Callback) Load() error

Load refreshes the callback instance along with its associations

func (Callback) MarshalJSON

func (c Callback) MarshalJSON() ([]byte, error)

MarshalJSON serializes Callback struct to JSON representation

func (*Callback) Store

func (c *Callback) Store(delegatedTx *gorm.DB) (err error)

Store persists new Callback mode into database TODO: Find a better structure impose transaction context

type CallbackAssignable

type CallbackAssignable struct {
	Path              string             `json:"path" gorm:"notNull;uniqueIndex:idx_unique_service_path;"`
	Method            string             `json:"method" gorm:"notNull;check: method in ('GET', 'POST');"`
	Headers           modelTypes.Headers `json:"headers"`
	RetryPolicyName   string             `json:"retry_policy_name" gorm:"-"`
	PriorityGroupName string             `json:"priority_group_name" gorm:"-"`
	ListensTo         []string           `json:"listens_to" gorm:"-"`
}

CallbackAssignable specifies mass assignable fields for

type Event

type Event struct {
	Base
	Topic
	NamespaceID string      `json:"-" gorm:"notNull;index;uniqueIndex:idx_unique_event_namespace;"`
	Namespace   *Namespace  `json:"-" gorm:"foreignKey:NamespaceID;references:Name;"`
	Callbacks   []*Callback `json:"-" gorm:"many2many:callbacks_events;"`
}

Event model

func AssertEvents

func AssertEvents(n *Namespace, topics []string) ([]*Event, error)

AssertEvents inserts a set of events into databasae

func (Event) MarshalJSON

func (e Event) MarshalJSON() ([]byte, error)

MarshalJSON serializes Event struct to JSON representation

func (*Event) Store

func (e *Event) Store() error

Store persists new Event mode into database

func (*Event) TopicString

func (e *Event) TopicString() string

TopicString returns event topic as a string

type NIDBase

type NIDBase struct {
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

NIDBase contains common columns for all tables without UUID

type Namespace

type Namespace struct {
	Base
	Name      string     `json:"name" gorm:"unique;index;notNull;"`
	AccountID string     `json:"account_id" gorm:"notNull;"`
	Events    []*Event   `json:"-" gorm:"foreignKey:NamespaceID;references:Name;constraint:OnDelete:CASCADE;"`
	Services  []*Service `json:"-" gorm:"foreignKey:NamespaceID;references:Name;constraint:OnDelete:CASCADE;"`
}

Namespace Model

func FindNamespace

func FindNamespace(n string) (*Namespace, error)

FindNamespace queries a namespaces by name and returns the first match

func (*Namespace) AlreadyTaken

func (n *Namespace) AlreadyTaken() bool

AlreadyTaken checks if the namespace is already registerd

func (*Namespace) Load

func (n *Namespace) Load() error

Load refreshes the namespace instance along with its associations

func (Namespace) MarshalJSON

func (n Namespace) MarshalJSON() ([]byte, error)

MarshalJSON serializes Namespace struct into JSON representation

func (*Namespace) Store

func (n *Namespace) Store(t *Token) error

Store inserts the namespace into database and associates it to the provided token

type PriorityGroup

type PriorityGroup struct {
	NIDBase
	PriorityGroupAssignable
	Name      string      `json:"name" gorm:"<-:create;primaryKey;"`
	ServiceID string      `json:"-" gorm:"primaryKey;"`
	Service   *Service    `json:"-" gorm:"foreignKey:ServiceID;references:Name;"`
	Callbacks []*Callback `json:"-" gorm:"foreignKey:PriorityGroupID,ServiceID;references:Name,ServiceID;constraint:OnDelete:RESTRICT;"`
}

PriorityGroup model

func FindPriorityGroupInService

func FindPriorityGroupInService(s string, p string) (*PriorityGroup, error)

FindPriorityGroupInService queries priority groups by name and service, returns the first exact match

func (*PriorityGroup) Delete

func (p *PriorityGroup) Delete() error

Delete removes the priority group

func (*PriorityGroup) GetCallbacks

func (p *PriorityGroup) GetCallbacks() ([]Callback, error)

GetCallbacks retrieves callbacks of a priority groups from database

func (PriorityGroup) MarshalJSON

func (p PriorityGroup) MarshalJSON() ([]byte, error)

MarshalJSON returns the augmented JSON represnation of PriorityGroup model

func (*PriorityGroup) NumOfCallbacks

func (p *PriorityGroup) NumOfCallbacks() (count int64, err error)

NumOfCallbacks returns count of callbacks associated with a priority group

func (*PriorityGroup) Store

func (p *PriorityGroup) Store() error

Store inserts a new priority group record into the database

func (*PriorityGroup) Update

func (p *PriorityGroup) Update() error

Update saves the current state of a priority record into the database

type PriorityGroupAssignable

type PriorityGroupAssignable struct {
	MinWorkers           uint `json:"min_workers" gorm:"default:1;check: min_workers > 0;"`
	MaxWorkers           uint `json:"max_workers" gorm:"default:1;check: max_workers > 0;"`
	ConcurrencyPerWorker uint `json:"concurrency_per_worker" gorm:"default:4;check: max_workers > 0;"`
	ScaleUpThreshold     uint `json:"scale_up_threshold" gorm:"default:150;"`
	ScaleDownThreshold   uint `json:"scale_down_threshold" gorm:"default:50;"`
}

PriorityGroupAssignable is specifies mass assignable fields for PriorityGroup model

type RetryPolicy

type RetryPolicy struct {
	NIDBase
	RetryPolicyAssignable
	Name      string      `json:"name" gorm:"<-:create;primaryKey;"`
	ServiceID string      `json:"-" gorm:"primaryKey;"`
	Service   *Service    `json:"-" gorm:"foreignKey:ServiceID;references:Name;constraint:false;"`
	Callbacks []*Callback `json:"-" gorm:"foreignKey:RetryPolicyID,ServiceID;references:Name,ServiceID;constraint:OnDelete:RESTRICT;"`
}

RetryPolicy model

func FindRetryPolicyInService

func FindRetryPolicyInService(s string, r string) (*RetryPolicy, error)

FindRetryPolicyInService queries priority groups by name and namespace, returns the first exact match

func (*RetryPolicy) Delete

func (r *RetryPolicy) Delete() error

Delete removes the retry policy

func (*RetryPolicy) NumOfCallbacks

func (r *RetryPolicy) NumOfCallbacks() (count int64, err error)

NumOfCallbacks returns count of callbacks associated with the retry policy

func (*RetryPolicy) Store

func (r *RetryPolicy) Store() error

Store persiste the new Retry Policy in the database

func (*RetryPolicy) Update

func (r *RetryPolicy) Update() error

Update saves the current state of the retry policy record into the database

type RetryPolicyAssignable

type RetryPolicyAssignable struct {
	MaxRetries    uint   `json:"max_retries" gorm:"default:3;check: max_retries > 1;"`
	ExpireAfter   uint   `json:"expire_after" gorm:"default:0;"`
	BackoffType   string `json:"backoff_type" gorm:"notNull;check: backoff_type in ('linear', 'exponential');"`
	BackoffBase   uint   `json:"backoff_base" gorm:"default:2;check: backoff_base > 1;"`
	BackoffOffset uint   `json:"backoff_offset" gorm:"default:4;check: backoff_offset > 1;"`
}

RetryPolicyAssignable is specifies mass assignable fields for RetryPolicy model

type Service

type Service struct {
	Base
	Name           string             `json:"name" gorm:"<-:create;notNull;index;unique;uniqueIndex:idx_unique_service_name_namespace;"`
	Scheme         string             `json:"-" gorm:"notNull;"`
	Host           string             `json:"-" gorm:"notNull;unique;index;"`
	URL            string             `json:"url" gorm:"-"`
	Headers        modelTypes.Headers `json:"headers"`
	NamespaceID    string             `json:"-" gorm:"notNull;index;uniqueIndex:idx_unique_service_name_namespace;"`
	Namespace      *Namespace         `json:"-" gorm:"foreignKey:NamespaceID;references:Name;"`
	PriorityGroups []*PriorityGroup   `json:"-" gorm:"foreignKey:ServiceID;references:Name;constraint:OnDelete:CASCADE;"`
	Callbacks      []*Callback        `json:"-" gorm:"foreignKey:ServiceID;references:Name;constraint:OnDelete:CASCADE;"`
	RetryPolicies  []*RetryPolicy     `json:"-" gorm:"foreignKey:ServiceID;references:Name;constraint:OnDelete:CASCADE;"`
}

Service model

func FindServiceInNamespace

func FindServiceInNamespace(n string, s string) (*Service, error)

FindServiceInNamespace queries servcies by name and namespace and returns the first exact match

func (*Service) BeforeCreate

func (s *Service) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate prefixes service name with namespace to ensure its uniquness

func (*Service) BeforeSave

func (s *Service) BeforeSave(tx *gorm.DB) error

BeforeSave prepares Service model and sanitizes data before being inserted into database

func (*Service) Delete

func (s *Service) Delete() (err error)

Delete removes the service

func (*Service) FindCallback

func (s *Service) FindCallback(n string) (*Callback, error)

FindCallback queries associated callbacks of the service by name. It returns an exact match.

func (*Service) GetCallbacks

func (s *Service) GetCallbacks() (callbacks []Callback, err error)

GetCallbacks retrieves callbacks of a service from database

func (*Service) GetPriorityGroups

func (s *Service) GetPriorityGroups() ([]PriorityGroup, error)

GetPriorityGroups retrieves service priority groups from database

func (*Service) GetRetryPolicies

func (s *Service) GetRetryPolicies() ([]RetryPolicy, error)

GetRetryPolicies retrieves service retry policies from database

func (*Service) GetServiceManifest

func (s *Service) GetServiceManifest() (ServiceManifest, error)

GetServiceManifest returns manifest representation of the service

func (*Service) GetURL

func (s *Service) GetURL() *url.URL

GetURL returns the URL object of the service. Service URL objects are consited of `Scheme` and `Host` only.

func (Service) MarshalJSON

func (s Service) MarshalJSON() ([]byte, error)

MarshalJSON returns the augmented JSON represnation of Service model

func (*Service) PK

func (s *Service) PK() string

PK returns normalized primary key value for current instance The name of the service is the primary key and it's expected to be in the form of "namespace.service_name" format

func (*Service) Store

func (s *Service) Store() error

Store inserts a new service record into the database

func (*Service) Update

func (s *Service) Update() error

Update saves the current state of a service record

type ServiceManifest

type ServiceManifest struct {
	Service        Service         `json:"service"`
	PriorityGroups []PriorityGroup `json:"priority_groups"`
	RetryPolicies  []RetryPolicy   `json:"retry_policies"`
	Callbacks      []Callback      `json:"callbacks"`
}

ServiceManifest value-object is used to describe the entirety of a service setup

type Token

type Token struct {
	Base
	UUID      uuid.UUID `gorm:"type:uuid;index;default:uuid_generate_v4();"`
	Key       uuid.UUID `gorm:"type:uuid;index;default:uuid_generate_v4();"`
	ExpiresAt sql.NullTime
	AccountID string `gorm:"notNull;index;"`
	Service   string `gorm:"notNull;index;default:'*'"`
}

Token Model

func (*Token) AccountAlreadyRegisterd

func (t *Token) AccountAlreadyRegisterd() bool

AccountAlreadyRegisterd checks to see if any token has been generated for account ID.

func (*Token) ExpireAt

func (t *Token) ExpireAt(exp time.Time) error

ExpireAt sets the token for expiry

func (*Token) IsMaster

func (t *Token) IsMaster() bool

IsMaster determines whether a token can manage a namesapce or not

func (*Token) Namespaces

func (t *Token) Namespaces() ([]*Namespace, error)

Namespaces returns associated namespaces with the token account

func (*Token) Replicate

func (t *Token) Replicate() *Token

Replicate creates a copy of the token while only preserving the Account ID

func (*Token) Store

func (t *Token) Store() error

Store inserts the token into database

func (*Token) ToJWT

func (t *Token) ToJWT() string

ToJWT returns JWT token string that can be used in HTTP request headers

type Topic

type Topic struct {
	Domain    string `json:"-" gorm:"uniqueIndex:idx_unique_event_namespace"`
	Resource  string `json:"-" gorm:"uniqueIndex:idx_unique_event_namespace"`
	Operation string `json:"-" gorm:"uniqueIndex:idx_unique_event_namespace"`
}

Topic is an auxilary type for Event model

func ParseTopicString

func ParseTopicString(ts string) (t Topic, err error)

ParseTopicString converts a string into a Topic struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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