microservice

package
v0.15.10 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 22 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureLogger

func ConfigureLogger(logger *zap.Logger, logPath string)

ConfigureLogger setups the logger with log rotation

Types

type AgentConfiguration

type AgentConfiguration struct {
	Config string `json:"config,omitempty"`
}

AgentConfiguration fragment containing the raw agent configuration string which can be edited by the user in the Device Manager application

type AgentInformation

type AgentInformation struct {
	Model        string `json:"model,omitempty"`
	SerialNumber string `json:"serialNumber,omitempty"`
	Revision     string `json:"revision,omitempty"`
	BuildTime    string `json:"buildTime,omitempty"`
}

AgentInformation meta information about the agent which is displayed in the Device Manager application

type AgentManagedObject

type AgentManagedObject struct {
	// Fragments
	c8y.AgentFragment
	c8y.DeviceFragment

	c8y.ManagedObject

	AgentSupportedOperations AgentSupportedOperations `json:"c8y_SupportedOperations,omitempty"`
	AgentConfiguration       *AgentConfiguration      `json:"c8y_Configuration,omitempty"`
	AgentInformation         *AgentInformation        `json:"c8y_Hardware,omitempty"`
}

AgentManagedObject is the agent representation of the microservice which is stored in Inventory

type AgentSupportedOperations

type AgentSupportedOperations []string

AgentSupportedOperations is a list of operations which are supported by the agent

func (AgentSupportedOperations) AddOperations

func (ops AgentSupportedOperations) AddOperations(operations []string)

AddOperations adds a list of operations (only if they don't already exist)

func (AgentSupportedOperations) Exists

func (ops AgentSupportedOperations) Exists(op string) bool

Exists returns true if the given operation already exists

func (AgentSupportedOperations) ToStringArray

func (ops AgentSupportedOperations) ToStringArray() []string

ToStringArray returns the operations as an array of strings removing any duplicates

type Configuration

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

Configuration represents the micorservice's configuration

func NewConfiguration

func NewConfiguration() *Configuration

NewConfiguration returns a new microservice configuration object

func (*Configuration) AllKeys

func (c *Configuration) AllKeys() []string

AllKeys returns all of the keys in the configuration

func (*Configuration) GetApplicationName

func (c *Configuration) GetApplicationName() string

GetApplicationName returns application's name

func (*Configuration) GetConfigurationString

func (c *Configuration) GetConfigurationString() string

GetConfigurationString returns the whole microservice configuration as text

func (*Configuration) GetHost

func (c *Configuration) GetHost() string

GetHost returns the configured Cumulocity Host

func (*Configuration) GetIdentityType

func (c *Configuration) GetIdentityType() string

GetIdentityType returns the configured Cumulocity Host

func (*Configuration) GetInt

func (c *Configuration) GetInt(key string) int

GetInt returns the values associated to the key as an int

func (*Configuration) GetMicroserviceHost

func (c *Configuration) GetMicroserviceHost() (microserviceHost string)

GetMicroserviceHost returns either a manual url or the manually set url

func (*Configuration) GetMicroserviceURL added in v0.7.0

func (c *Configuration) GetMicroserviceURL(partialPath string) string

GetMicroserviceURL returns the microservices URL given a partial path When the microservice is hosted in the Cumulocity platform, then the url will look like /service/{application.name}/{partialUrl}, otherwise if the nx.microservice.host configuration variable is set, then the url will be returned as is (with a prefixed "/" if not already present)

func (*Configuration) GetString

func (c *Configuration) GetString(key string) string

GetString retrieves a value from the configuration by it's key

func (*Configuration) InitConfiguration

func (c *Configuration) InitConfiguration()

InitConfiguration initializes the configuration (i.e. reads from file / environment variables)

func (*Configuration) SetDefault

func (c *Configuration) SetDefault(key, value string)

SetDefault sets the default value for this key. SetDefault is case-insensitive for a key. Default only used when no value is provided by the user via flag, config or ENV.

type Hooks

type Hooks struct {
	OnConfigurationUpdateFunc func(Configuration)
}

Hooks contains list of lifecycle hooks that can be used in the microservice

type Microservice

type Microservice struct {
	Application         *c8y.Application
	Config              *Configuration
	Client              *c8y.Client
	AgentID             string
	MicroserviceHost    string
	Scheduler           *Scheduler
	Logger              *zap.Logger
	SupportedOperations AgentSupportedOperations
	AgentInformation    AgentInformation
	Hooks               Hooks
	RealtimeClientCache *RealtimeClientCache
}

Microservice contains information and

func NewDefaultMicroservice

func NewDefaultMicroservice(opts Options) *Microservice

NewDefaultMicroservice returns a new microservice instance. The bootstrap user will be automatically read from the environment variables In addition it will read the configuration, and set configure the zap logger NOTE: The microservice agent will not be registered automatically, you need to call RegisterMicroserviceAgent(). Though before you call it be sure to set your default configuration values in the Config.SetDefault()

func NewMicroservice

func NewMicroservice(httpClient *http.Client, host string, skipRealtimeClient bool) *Microservice

NewMicroservice create a new microservice where the user can customize the http client and the host This function will not initialize the logger (.InitializeLogger()) nor call .Config.InitConfiguration(), it is up to the user to call these functions

func (*Microservice) AddHealthEndpointHandlers

func (m *Microservice) AddHealthEndpointHandlers(e *echo.Echo)

AddHealthEndpointHandlers adds a set of health endpoints to the microservice The following endpoints are added:

  • /prometheus
  • /health
  • /env
  • /logfile

func (*Microservice) CheckForNewConfiguration

func (m *Microservice) CheckForNewConfiguration()

CheckForNewConfiguration checks for any pending operations with new configuration

func (*Microservice) CreateMicroserviceRepresentation

func (m *Microservice) CreateMicroserviceRepresentation() (*c8y.ManagedObject, error)

CreateMicroserviceRepresentation Create a microservice representation in the Cumulocity platform, so that the microservice can store its configuration in the managed object

func (*Microservice) DeleteMicroserviceAgent

func (m *Microservice) DeleteMicroserviceAgent() error

DeleteMicroserviceAgent removes the microservice's agent managed object if it exists.

func (*Microservice) EnvironmentVariablesHandler

func (m *Microservice) EnvironmentVariablesHandler(c echo.Context) error

EnvironmentVariablesHandler return the list of environment variables

func (*Microservice) GetAgent

func (m *Microservice) GetAgent() *c8y.ManagedObject

GetAgent returns the agent representation of the microservice

func (*Microservice) GetConfiguration

func (m *Microservice) GetConfiguration() (string, error)

GetConfiguration returns the Agent configuration as text. This needs to be parsed separately by the calling function.

func (*Microservice) GetLogFileHandler

func (m *Microservice) GetLogFileHandler(c echo.Context) error

GetLogFileHandler get the current log file in json format

func (*Microservice) GetOperations

func (m *Microservice) GetOperations(status string) (*c8y.OperationCollection, *c8y.Response, error)

GetOperations returns a list of operations in the given status i.e. PENDING, EXECUTING, SUCCESS, FAILED

func (*Microservice) HealthHandler

func (m *Microservice) HealthHandler(c echo.Context) error

HealthHandler returns health endpoint

func (*Microservice) InitializeLogger

func (m *Microservice) InitializeLogger(logfile ...string)

InitializeLogger starts the logger with custom configuration

func (*Microservice) NewRealtimeClient

func (m *Microservice) NewRealtimeClient(user c8y.ServiceUser) (*c8y.RealtimeClient, error)

NewRealtimeClient creates a new realtime client for the given tenant service user

func (*Microservice) RegisterMicroserviceAgent

func (m *Microservice) RegisterMicroserviceAgent() error

RegisterMicroserviceAgent registers an agent representation of the microservice

func (*Microservice) SaveConfiguration

func (m *Microservice) SaveConfiguration(rawConfiguration string) error

SaveConfiguration save the agent configuration to it's managed object

func (*Microservice) SetMicroserviceHost

func (m *Microservice) SetMicroserviceHost(host string)

SetMicroserviceHost sets the microservice host to a non-default Cumulocity host Useful when the microservice is not deployed in Cumulocity (i.e. local host, or external docker server)

func (*Microservice) StartOperationPolling

func (m *Microservice) StartOperationPolling()

StartOperationPolling start the polling of the operations

func (*Microservice) SubscribeToNotifications

func (m *Microservice) SubscribeToNotifications(user c8y.ServiceUser, realtimeChannel string, onMessageFunc func(*c8y.Message)) error

SubscribeToNotifications subscribes to c8y notifications on the Microservice's agent managed object

func (*Microservice) SubscribeToOperations

func (m *Microservice) SubscribeToOperations(user c8y.ServiceUser, onMessageFunc func(*c8y.Message)) error

SubscribeToOperations subscribes to operations added to the microservice's agent managed object. onMessageFunc is called on every operation

func (*Microservice) TestClientConnection

func (m *Microservice) TestClientConnection() error

TestClientConnection tests if the microservice client has connection to the Cumulocty host

func (*Microservice) UpdateApplicationConfiguration

func (m *Microservice) UpdateApplicationConfiguration(configAsString string)

UpdateApplicationConfiguration updates the application configuration based on a new config which is parsed from a string. Values should be in the form of "<key>=<value>" separated by a \n char

func (*Microservice) WithBootstrapUserCredentials

func (m *Microservice) WithBootstrapUserCredentials() c8y.ServiceUser

WithBootstrapUserCredentials returns the application credentials

func (*Microservice) WithServiceUser

func (m *Microservice) WithServiceUser(tenant ...string) context.Context

WithServiceUser returns the default service user (i.e. the first tenant). Can be used when using a PER_TENANT microservice as there will only ever be one tenant

func (*Microservice) WithServiceUserCredentials

func (m *Microservice) WithServiceUserCredentials(tenant ...string) c8y.ServiceUser

WithServiceUserCredentials returns the service user credentials associated with the tenant. If no tenant is given, then the first service user will be returned

type Options

type Options struct {
	// List of supported operations
	SupportedOperations AgentSupportedOperations
	AgentInformation    AgentInformation
}

Options contains the additional microsevice options

type RealtimeClientCache

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

RealtimeClientCache is a cache to store the different realtime clients used in the microservice

func NewRealtimeClientCache

func NewRealtimeClientCache(host string) *RealtimeClientCache

NewRealtimeClientCache returns a new realtime client cache where realtime clients can be reused for different subscription notifications

func (*RealtimeClientCache) GetClient

func (s *RealtimeClientCache) GetClient(user c8y.ServiceUser) (*c8y.RealtimeClient, error)

GetClient returns a realtime client if it already exists in the cache. If no realtime client already exists for the service user, then an error is returned

func (*RealtimeClientCache) LoadOrNewClient

func (s *RealtimeClientCache) LoadOrNewClient(user c8y.ServiceUser) (*c8y.RealtimeClient, error)

LoadOrNewClient returns a Realtime client for the given service user. If a realtime client already exists for the given service user then it will be returned rather than creating a new client

func (*RealtimeClientCache) SetClient

func (s *RealtimeClientCache) SetClient(user c8y.ServiceUser, client *c8y.RealtimeClient)

SetClient adds the given realtime client in the cache stored under the service user's tenant

type Scheduler

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

Scheduler to control cronjob tasks

func NewScheduler

func NewScheduler() *Scheduler

NewScheduler creates a new scheduler to control cronjob tasks

func (*Scheduler) AddFunc

func (s *Scheduler) AddFunc(spec string, cmd func()) error

AddFunc adds a task to a the scheduler at a specified interval

func (*Scheduler) Start

func (s *Scheduler) Start()

Start the scheduler so all configured cronjob tasks will run at their defined intervals

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop the schedule. No more cronjob tasks will be triggered until Start() is called again. All of the job definitions will still be defined

Jump to

Keyboard shortcuts

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