connect

package
v0.0.1-3 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 10 Imported by: 24

Documentation

Index

Constants

View Source
const (
	SectionNameConnections      = "connections"
	SectionNameConnection       = "connection"
	ConnectionParamDiscoveryKey = "discovery_key"
	ConnectionParamProtocol     = "protocol"
	ConnectionParamHost         = "host"
	ConnectionParamIp           = "ip"
	ConnectionParamPort         = "port"
	ConnectionParamURI          = "uri"
	ConnectionParamCluster      = "cluster"
)

Variables

View Source
var ConnectionUtils = _TConnectionUtils{}

Functions

This section is empty.

Types

type CompositeConnectionResolver

type CompositeConnectionResolver struct {
	Overrides ICompositeConnectionResolverOverrides

	// The connection options
	Options *config.ConfigParams

	// The connections resolver.
	ConnectionResolver *ConnectionResolver

	// The credentials resolver.
	CredentialResolver *auth.CredentialResolver

	// The cluster support (multiple connections)
	ClusterSupported bool

	// The default protocol
	DefaultProtocol string

	// The default port number
	DefaultPort int

	// The list of supported protocols
	SupportedProtocols []string
}

CompositeConnectionResolver helper class that resolves connection and credential parameters, validates them and generates connection options.

Configuration parameters
	- connection(s):
		- discovery_key:               (optional) a key to retrieve the connection from [IDiscovery]]
		- protocol:                    communication protocol
		- host:                        host name or IP address
		- port:                        port number
		- uri:                         resource URI or connection string with all parameters in it
	- credential(s):
		- store_key:                   (optional) a key to retrieve the credentials from [ICredentialStore]]
		- username:                    user name
		- password:                    user password

References
	- *:discovery:*:*:1.0          (optional) [IDiscovery]] services to resolve connections
	- *:credential-store:*:*:1.0   (optional) Credential stores to resolve credentials

func InheritCompositeConnectionResolver

func InheritCompositeConnectionResolver(overrides ICompositeConnectionResolverOverrides) *CompositeConnectionResolver

InheritCompositeConnectionResolver creates new CompositeConnectionResolver

Parameters:
	- overrides a child reference with overrides for virtual methods
return *CompositeConnectionResolver

func (*CompositeConnectionResolver) Compose

func (c *CompositeConnectionResolver) Compose(ctx context.Context, connections []*ConnectionParams, credential *auth.CredentialParams,
	parameters *config.ConfigParams) (options *config.ConfigParams, err error)

Compose Composite connection options from connection and credential parameters.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- connections connection parameters
	- credential parameters
	- parameters optional parameters
Returns: resolved options or error.

func (*CompositeConnectionResolver) ComposeOptions

func (c *CompositeConnectionResolver) ComposeOptions(connections []*ConnectionParams, credential *auth.CredentialParams, parameters *config.ConfigParams) *config.ConfigParams

ComposeOptions composes connection and credential parameters into connection options. This method can be override in child classes.

Parameters:
	- connections a list of connection parameters
	- credential parameters
	- parameters optional parameters
Returns: a composed connection options.

func (*CompositeConnectionResolver) Configure

func (c *CompositeConnectionResolver) Configure(ctx context.Context, config *config.ConfigParams)

Configure configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config configuration parameters to be set.

func (*CompositeConnectionResolver) FinalizeOptions

func (c *CompositeConnectionResolver) FinalizeOptions(options *config.ConfigParams) *config.ConfigParams

FinalizeOptions finalize merged options This method can be overriden in child classes.

Parameters:
	- options connection options
Returns: finalized connection options

func (*CompositeConnectionResolver) MergeConnection

func (c *CompositeConnectionResolver) MergeConnection(options *config.ConfigParams, connection *ConnectionParams) *config.ConfigParams

MergeConnection merges connection options with connection parameters This method can be override in child classes.

Parameters:
	-  options connection options
	-  connection parameters to be merged
Returns: merged connection options.

func (*CompositeConnectionResolver) MergeCredential

func (c *CompositeConnectionResolver) MergeCredential(options *config.ConfigParams, credential *auth.CredentialParams) *config.ConfigParams

MergeCredential merges connection options with credential parameters This method can be overriden in child classes.

Parameters:
	- options connection options
	- credential parameters to be merged
Returns: merged connection options.

func (*CompositeConnectionResolver) MergeOptional

func (c *CompositeConnectionResolver) MergeOptional(options *config.ConfigParams, parameters *config.ConfigParams) *config.ConfigParams

MergeOptional merges connection options with optional parameters This method can be overriden in child classes.

Parameters:
	- options connection options
	- parameters optional parameters to be merged
Returns merged connection options.

func (*CompositeConnectionResolver) Resolve

func (c *CompositeConnectionResolver) Resolve(ctx context.Context) (options *config.ConfigParams, err error)

Resolve connection options from connection and credential parameters.

  • ctx context.Context execution context to trace execution through call chain.
  • return resolved options or error.

func (*CompositeConnectionResolver) SetReferences

func (c *CompositeConnectionResolver) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences sets references to dependent components.

Parameters:
	- ctx context.Context
	- references references to locate the component dependencies.

func (*CompositeConnectionResolver) ValidateConnection

func (c *CompositeConnectionResolver) ValidateConnection(ctx context.Context, connection *ConnectionParams) error

ValidateConnection validates connection parameters. This method can be override in child classes.

Parameters:
	- ctx context.Context a context to trace execution through call chain.
	- connection    parameters to be validated
Returns: error or nil if validation was successful

func (*CompositeConnectionResolver) ValidateCredential

func (c *CompositeConnectionResolver) ValidateCredential(ctx context.Context, credential *auth.CredentialParams) error

ValidateCredential parameters. This method can be override in child classes.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- credential    parameters to be validated
Returns: error or nil if validation was successful

type ConnectionParams

type ConnectionParams struct {
	*config.ConfigParams
}

ConnectionParams contains connection parameters to connect to external services. They are used together with credential parameters, but usually stored separately from more protected sensitive values.

Configuration parameters:
	- discovery_key: key to retrieve parameters from discovery service
	- protocol: connection protocol like http, https, tcp, udp
	- host: host name or IP address
	- port: port number
	- uri: resource URI or connection string with all parameters in it

In addition to standard parameters ConnectionParams may contain any number of custom parameters

see ConfigParams
see CredentialParams
see ConnectionResolver
see IDiscovery
Example ConnectionParams object usage:
	connection := NewConnectionParamsFromTuples(
		ConnectionParamProtocol, "http",
		ConnectionParamHost, "10.1.1.100",
		ConnectionParamPort, "8080",
		ConnectionParamCluster, "mycluster"
	);
	host := connection.Host();                             				// Result: "10.1.1.100"
	port := connection.Port();                             				// Result: 8080
	cluster := connection.GetAsNullableString(ConnectionParamCluster);  // Result: "mycluster"

func NewConnectionParams

func NewConnectionParams(values map[string]string) *ConnectionParams

NewConnectionParams creates a new connection parameters and fills it with values.

Parameters:
	- values map[string]string an object to be
	converted into key-value pairs to initialize this connection.
Returns: *ConnectionParams

func NewConnectionParamsFromConfig

func NewConnectionParamsFromConfig(config *config.ConfigParams) *ConnectionParams

NewConnectionParamsFromConfig retrieves a single ConnectionParams from configuration parameters from "connection" section. If "connections" section is present instead, then is returns only the first connection element.

Parameters:
	- config *config.ConfigParams ConnectionParams, containing a section named "connection(s)".
Returns: *ConnectionParams the generated ConnectionParams object.

func NewConnectionParamsFromMaps

func NewConnectionParamsFromMaps(maps ...map[string]string) *ConnectionParams

NewConnectionParamsFromMaps static method for creating a StringValueMap using the maps passed as parameters.

Parameters:
	- maps ...map[string]string the maps passed to this method to create a StringValueMap with.
Returns: ConnectionParams the ConnectionParams created.

func NewConnectionParamsFromString

func NewConnectionParamsFromString(line string) *ConnectionParams

NewConnectionParamsFromString creates a new ConnectionParams object filled with key-value pairs serialized as a string.

Parameters:
	- line string a string with serialized key-value pairs as
	"key1=value1;key2=value2;..." Example: "Key1=123;Key2=ABC;Key3=2016-09-16T00:00:00.00Z"
Returns: *ConnectionParams a new ConnectionParams object.

func NewConnectionParamsFromTuples

func NewConnectionParamsFromTuples(tuples ...any) *ConnectionParams

NewConnectionParamsFromTuples creates a new ConnectionParams object filled with provided key-value pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs.

Parameters:
	- tuples ...any the tuples to fill a new ConnectionParams object.
Returns: *ConnectionParams a new ConnectionParams object.

func NewConnectionParamsFromTuplesArray

func NewConnectionParamsFromTuplesArray(tuples []any) *ConnectionParams

NewConnectionParamsFromTuplesArray method for creating a StringValueMap from an array of tuples.

Parameters:
	- tuples []any the key-value tuples array to initialize the new StringValueMap with.
Returns: *ConnectionParams the ConnectionParams created and filled by the 'tuples' array provided.

func NewConnectionParamsFromValue

func NewConnectionParamsFromValue(value any) *ConnectionParams

NewConnectionParamsFromValue method that creates a ConfigParams object based on the values that are stored in the 'value' object's properties.

see RecursiveObjectReader.GetProperties
Parameters:
	- value any configuration parameters in the form of an object with properties.
Returns: ConnectionParams generated ConnectionParams.

func NewEmptyConnectionParams

func NewEmptyConnectionParams() *ConnectionParams

NewEmptyConnectionParams creates a new connection parameters and fills it with values.

Returns: *ConnectionParams

func NewManyConnectionParamsFromConfig

func NewManyConnectionParamsFromConfig(config *config.ConfigParams) []*ConnectionParams

NewManyConnectionParamsFromConfig retrieves all ConnectionParams from configuration parameters from "connections" section. If "connection" section is present instead, than it returns a list with only one ConnectionParams.

Parameters:
	- config *config.ConfigParams a configuration parameters to retrieve connections
Returns: []*ConnectionParams a list of retrieved ConnectionParams

func (*ConnectionParams) DiscoveryKey

func (c *ConnectionParams) DiscoveryKey() string

DiscoveryKey gets the key to retrieve this connection from DiscoveryService. If this key is null, then all parameters are already present.

see UseDiscovery
Returns: string the discovery key to retrieve connection.

func (*ConnectionParams) Host

func (c *ConnectionParams) Host() string

Host gets the host name or IP address.

Returns: string the host name or IP address.

func (*ConnectionParams) Port

func (c *ConnectionParams) Port() int

Port gets the port number.

Returns int the port number.

func (*ConnectionParams) PortWithDefault

func (c *ConnectionParams) PortWithDefault(defaultValue int) int

PortWithDefault gets the port number.

Parameters:
	- defaultValue int default port number
Returns: int the port number.

func (*ConnectionParams) Protocol

func (c *ConnectionParams) Protocol() string

Protocol gets the connection protocol.

Returns: string the connection protocol or the default value if it's not set.

func (*ConnectionParams) ProtocolWithDefault

func (c *ConnectionParams) ProtocolWithDefault(defaultValue string) string

ProtocolWithDefault gets the connection protocol.

Parameters:
	- defaultValue string the default protocol
Returns: string the connection protocol or the default value if it's not set.

func (*ConnectionParams) SetDiscoveryKey

func (c *ConnectionParams) SetDiscoveryKey(value string)

SetDiscoveryKey sets the key to retrieve these parameters from DiscoveryService.

Parameters:
	- value string a new key to retrieve connection.

func (*ConnectionParams) SetHost

func (c *ConnectionParams) SetHost(value string)

SetHost sets the host name or IP address.

Parameters:
	- value string a new host name or IP address.

func (*ConnectionParams) SetPort

func (c *ConnectionParams) SetPort(value int)

SetPort sets the port number.

see Host
Parameters: value int a new port number.

func (*ConnectionParams) SetProtocol

func (c *ConnectionParams) SetProtocol(value string)

SetProtocol sets the connection protocol.

Parameters:
	- value string a new connection protocol.

func (*ConnectionParams) SetUri

func (c *ConnectionParams) SetUri(value string)

SetUri sets the resource URI or connection string.

Parameters:
	- value string a new resource URI or connection string.

func (*ConnectionParams) Uri

func (c *ConnectionParams) Uri() string

Uri gets the resource URI or connection string. Usually it includes all connection parameters in it.

Returns: string the resource URI or connection string.

func (*ConnectionParams) UseDiscovery

func (c *ConnectionParams) UseDiscovery() bool

UseDiscovery checks if these connection parameters shall be retrieved from DiscoveryService. The connection parameters are redirected to DiscoveryService when discovery_key parameter is set.

Returns: bool true if connection shall be retrieved from DiscoveryService

type ConnectionResolver

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

ConnectionResolver helper class to retrieve component connections. If connections are configured to be retrieved from IDiscovery, it automatically locates IDiscovery in component references and retrieve connections from there using discovery_key parameter.

Configuration parameters
	- connection:
		- discovery_key: (optional) a key to retrieve the connection from IDiscovery
		- ... other connection parameters
	- connections: alternative to connection
		- [connection params 1]: first connection parameters
			- ... connection parameters for key 1
		- [connection params N]: Nth connection parameters
			- ... connection parameters for key N
References:
	- *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connections

see ConnectionParams
see IDiscovery

Example:
	config = NewConfigParamsFromTuples(
		"connection.host", "10.1.1.100",
		"connection.port", 8080
	);
	connectionResolver := NewConnectionResolver();
	connectionResolver.Configure(context.Background(), config);
	connectionResolver.SetReferences(context.Background(), references);
	res, err := connectionResolver.Resolve("123");

func NewConnectionResolver

func NewConnectionResolver(ctx context.Context, config *config.ConfigParams, references refer.IReferences) *ConnectionResolver

NewConnectionResolver Creates a new instance of connection resolver.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams component configuration parameters
	- references refer.IReferences component references
Returns: *ConnectionResolver

func NewEmptyConnectionResolver

func NewEmptyConnectionResolver() *ConnectionResolver

NewEmptyConnectionResolver creates a new instance of connection resolver.

Returns: *ConnectionResolver

func (*ConnectionResolver) Add

func (c *ConnectionResolver) Add(connection *ConnectionParams)

Add a new connection to component connections

Parameters:
	- connection *ConnectionParams new connection parameters to be added

func (*ConnectionResolver) Configure

func (c *ConnectionResolver) Configure(ctx context.Context, config *config.ConfigParams)

Configure Configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration parameters to be set.

func (*ConnectionResolver) GetAll

func (c *ConnectionResolver) GetAll() []*ConnectionParams

GetAll gets all connections configured in component configuration. Redirect to Discovery services is not done at this point. If you need fully fleshed connection use resolve method instead.

Returns: []*ConnectionParams a list with connection parameters

func (*ConnectionResolver) Register

func (c *ConnectionResolver) Register(ctx context.Context, connection *ConnectionParams) error

Register the given connection in all referenced discovery services. This method can be used for dynamic service discovery.

see IDiscovery
Parameters:
	-  transaction id to trace execution through call chain.
	- connection *ConnectionParams a connection to register.
Returns: error

func (*ConnectionResolver) Resolve

Resolve a single component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there.

see IDiscovery
Parameters:
	- ctx context.Context execution context to trace execution through call chain.
Returns: *ConnectionParams, error resolved connection or error.

func (*ConnectionResolver) ResolveAll

func (c *ConnectionResolver) ResolveAll(ctx context.Context) ([]*ConnectionParams, error)

ResolveAll resolves all component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there.

see IDiscovery
Parameters:
	-  transaction id to trace execution through call chain.
Returns: []*ConnectionParams, error resolved connections or error.

func (*ConnectionResolver) SetReferences

func (c *ConnectionResolver) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences sets references to dependent components.

Parameters:
	- ctx context.Context
	- references refer.IReferences references to locate the component dependencies.

type HttpConnectionResolver

type HttpConnectionResolver struct {
	//The base connection resolver.
	ConnectionResolver *ConnectionResolver
	//The base credential resolver.
	CredentialResolver *cauth.CredentialResolver
}

HttpConnectionResolver helper class to retrieve connections for HTTP-based services abd clients.

In addition to regular functions of ConnectionResolver is able to parse http:// URIs and validate connection parameters before returning them.

Configuration parameters:
	- connection:
	- discovery_key:               (optional) a key to retrieve the connection from IDiscovery
	- ...                          other connection parameters

	- connections:                   alternative to connection
		- [connection params 1]:       first connection parameters
		-  ...
		- [connection params N]:       Nth connection parameters
		-  ...

References:
	- *:discovery:*:*:1.0            (optional) IDiscovery services

see: ConnectionParams
see: ConnectionResolver

Example:
	config := cconf.NewConfigParamsFromTuples(
		"connection.host", "10.1.1.100",
		"connection.port", 8080,
	);

	connectionResolver = NewHttpConnectionResolver();
	connectionResolver.Configure(context.Background(), config);
	connectionResolver.SetReferences(context.Background(), references);

	connection, err := connectionResolver.Resolve(ctx)
	// Now use connection...

func NewHttpConnectionResolver

func NewHttpConnectionResolver() *HttpConnectionResolver

NewHttpConnectionResolver creates new instance NewHttpConnectionResolver

Returns: pointer on NewHttpConnectionResolver

func (*HttpConnectionResolver) Configure

func (c *HttpConnectionResolver) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure method are configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config  *cconf.ConfigParams  configuration parameters to be set.

func (*HttpConnectionResolver) Register

func (c *HttpConnectionResolver) Register(ctx context.Context) error

Register method are registers the given connection in all referenced discovery services. c method can be used for dynamic service discovery.

Parameters:
	- ctx context.Context	transaction id to trace execution through call chain.
Returns: error nil if registered connection or error.

func (*HttpConnectionResolver) Resolve

func (c *HttpConnectionResolver) Resolve(ctx context.Context) (connection *ConnectionParams, credential *cauth.CredentialParams, err error)

Resolve method are resolves a single component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there.

Parameters:
	- ctx context.Context	transaction id to trace execution through call chain.
Returns: connection *ConnectionParams, credential *cauth.CredentialParams, err error
	resolved connection and credential or error.

func (*HttpConnectionResolver) ResolveAll

func (c *HttpConnectionResolver) ResolveAll(ctx context.Context) (connections []*ConnectionParams, credential *cauth.CredentialParams, err error)

ResolveAll method are resolves all component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there.

Parameters:
	- ctx context.Context   transaction id to trace execution through call chain.
Returns:  connections []*ConnectionParams, credential *cauth.CredentialParams, err error
	resolved connections and credential or error.

func (*HttpConnectionResolver) SetReferences

func (c *HttpConnectionResolver) SetReferences(ctx context.Context, references crefer.IReferences)

SetReferences method are sets references to dependent components.

Parameters:
	- ctx context.Context
	- references crefer.IReferences	references to locate the component dependencies.

type ICompositeConnectionResolverOverrides

type ICompositeConnectionResolverOverrides interface {
	ValidateConnection(ctx context.Context, connection *ConnectionParams) error

	ValidateCredential(ctx context.Context, credential *auth.CredentialParams) error

	ComposeOptions(connections []*ConnectionParams, credential *auth.CredentialParams, parameters *config.ConfigParams) *config.ConfigParams

	MergeConnection(options *config.ConfigParams, connection *ConnectionParams) *config.ConfigParams

	MergeCredential(options *config.ConfigParams, credential *auth.CredentialParams) *config.ConfigParams

	MergeOptional(options *config.ConfigParams, parameters *config.ConfigParams) *config.ConfigParams

	FinalizeOptions(options *config.ConfigParams) *config.ConfigParams
}

type IDiscovery

type IDiscovery interface {

	// Register connection parameters into the discovery service.
	Register(ctx context.Context, key string,
		connection *ConnectionParams) (result *ConnectionParams, err error)

	// ResolveOne a single connection parameters by its key.
	ResolveOne(ctx context.Context, key string) (result *ConnectionParams, err error)

	// ResolveAll all connection parameters by their key.
	ResolveAll(ctx context.Context, key string) (result []*ConnectionParams, err error)
}

IDiscovery interface for discovery services which are used to store and resolve connection parameters to connect to external services.

type MemoryDiscovery

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

MemoryDiscovery discovery service that keeps connections in memory.

Configuration parameters
	[connection key 1]:
	... connection parameters for key 1
	[connection key 2]:
	... connection parameters for key N
see IDiscovery
see ConnectionParams
Example
	config := NewConfigParamsFromTuples(
		"key1.host", "10.1.1.100",
		"key1.port", "8080",
		"key2.host", "10.1.1.100",
		"key2.port", "8082"
	);
	discovery := NewMemoryDiscovery();
	discovery.ReadConnections(config);
	conn, err := discovery.ResolveOne("123", "key1");

Result: host=10.1.1.100;port=8080

func NewEmptyMemoryDiscovery

func NewEmptyMemoryDiscovery() *MemoryDiscovery

NewEmptyMemoryDiscovery creates a new instance of discovery service.

Returns: *MemoryDiscovery

func NewMemoryDiscovery

func NewMemoryDiscovery(ctx context.Context, config *config.ConfigParams) *MemoryDiscovery

NewMemoryDiscovery creates a new instance of discovery service.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration with connection parameters.
Returns: *MemoryDiscovery

func (*MemoryDiscovery) Configure

func (c *MemoryDiscovery) Configure(ctx context.Context, config *config.ConfigParams)

Configure component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration parameters to be set.

func (*MemoryDiscovery) ReadConnections

func (c *MemoryDiscovery) ReadConnections(config *config.ConfigParams)

ReadConnections from configuration parameters. Each section represents an individual Connectionparams

Parameters:
	- ctx context.Context
	- config *configure.ConfigParams configuration parameters to be read

func (*MemoryDiscovery) Register

func (c *MemoryDiscovery) Register(ctx context.Context, key string,
	connection *ConnectionParams) (result *ConnectionParams, err error)

Register connection parameters into the discovery service.

Parameters:
	-  transaction id to trace execution through call chain.
	- key string a key to uniquely identify the connection parameters.
	- connection *ConnectionParams
Returns: *ConnectionParams, error registered connection or error.

func (*MemoryDiscovery) ResolveAll

func (c *MemoryDiscovery) ResolveAll(ctx context.Context,
	key string) (result []*ConnectionParams, err error)

ResolveAll connection parameters by its key.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- key: string a key to uniquely identify the connection.
Returns: *ConnectionParams, error receives found connection or error.

func (*MemoryDiscovery) ResolveOne

func (c *MemoryDiscovery) ResolveOne(ctx context.Context,
	key string) (result *ConnectionParams, err error)

ResolveOne a single connection parameters by its key.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- key: string a key to uniquely identify the connection.
Returns: *ConnectionParams, error receives found connection or error.

Jump to

Keyboard shortcuts

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