2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2019-04-08 17:46:07 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package producer
|
|
|
|
|
|
|
|
import (
|
2021-05-20 20:53:56 +02:00
|
|
|
"context"
|
2019-04-08 17:46:07 +02:00
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
"github.com/go-redis/redis/v8"
|
2019-04-08 17:46:07 +02:00
|
|
|
"github.com/mainflux/mainflux/bootstrap"
|
2023-06-14 12:40:37 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2019-04-08 17:46:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
streamID = "mainflux.bootstrap"
|
|
|
|
streamLen = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ bootstrap.Service = (*eventStore)(nil)
|
|
|
|
|
|
|
|
type eventStore struct {
|
|
|
|
svc bootstrap.Service
|
|
|
|
client *redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEventStoreMiddleware returns wrapper around bootstrap service that sends
|
|
|
|
// events to event store.
|
|
|
|
func NewEventStoreMiddleware(svc bootstrap.Service, client *redis.Client) bootstrap.Service {
|
|
|
|
return eventStore{
|
|
|
|
svc: svc,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) Add(ctx context.Context, token string, cfg bootstrap.Config) (bootstrap.Config, error) {
|
|
|
|
saved, err := es.svc.Add(ctx, token, cfg)
|
2019-04-08 17:46:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return saved, err
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
ev := configEvent{
|
|
|
|
saved, configCreate,
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
if err1 := es.add(ctx, ev); err1 != nil {
|
|
|
|
return saved, errors.Wrap(err, err1)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return saved, err
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) View(ctx context.Context, token, id string) (bootstrap.Config, error) {
|
2023-06-14 12:40:37 +02:00
|
|
|
cfg, err := es.svc.View(ctx, token, id)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
ev := configEvent{
|
|
|
|
cfg, configList,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err1 := es.add(ctx, ev); err1 != nil {
|
|
|
|
return cfg, errors.Wrap(err, err1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, err
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) Update(ctx context.Context, token string, cfg bootstrap.Config) error {
|
|
|
|
if err := es.svc.Update(ctx, token, cfg); err != nil {
|
2019-04-08 17:46:07 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
ev := configEvent{
|
|
|
|
cfg, configUpdate,
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
MF-1718 - Use static code analysis in CI (#1729)
* things, twins, and logger lint fixed
Signed-off-by: aryan <aryangodara03@gmail.com>
* all services updated, auth jwt not working, ineffectual assignment issue
Signed-off-by: aryan <aryangodara03@gmail.com>
* handle error from grpc server in endpointtest
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, auth/jwt needs to be resolved
Signed-off-by: aryan <aryangodara03@gmail.com>
* revert back to jwt v4 temporarily
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated jwt tokenizer
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve EOF error for httptest requests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix auth jwt, update to registeredclaims
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix ineffective assignment, auth/api/grpc endpoint failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, remove later
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server setup
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve golangci tests, remove debug statements
Signed-off-by: aryan <aryangodara03@gmail.com>
* update golangci version and modify linters used
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix failing tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server for setup tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix logging and errors inlined
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix remarks, update grpc setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix data race
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc setup down to single simple function
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix linting issues
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve pr comments
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix tests, handle returned errors, go mod tidy vendor
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors from new linters
Signed-off-by: aryan <aryangodara03@gmail.com>
---------
Signed-off-by: aryan <aryangodara03@gmail.com>
2023-04-22 08:14:35 -07:00
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) UpdateCert(ctx context.Context, token, thingKey, clientCert, clientKey, caCert string) error {
|
2023-06-14 12:40:37 +02:00
|
|
|
if err := es.svc.UpdateCert(ctx, token, thingKey, clientCert, clientKey, caCert); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := updateCertEvent{
|
|
|
|
thingKey: thingKey,
|
|
|
|
clientCert: clientCert,
|
|
|
|
clientKey: clientKey,
|
|
|
|
caCert: caCert,
|
|
|
|
}
|
|
|
|
|
|
|
|
return es.add(ctx, ev)
|
2019-05-22 23:22:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) UpdateConnections(ctx context.Context, token, id string, connections []string) error {
|
|
|
|
if err := es.svc.UpdateConnections(ctx, token, id, connections); err != nil {
|
2019-04-08 17:46:07 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := updateConnectionsEvent{
|
|
|
|
mfThing: id,
|
|
|
|
mfChannels: connections,
|
|
|
|
}
|
|
|
|
|
MF-1718 - Use static code analysis in CI (#1729)
* things, twins, and logger lint fixed
Signed-off-by: aryan <aryangodara03@gmail.com>
* all services updated, auth jwt not working, ineffectual assignment issue
Signed-off-by: aryan <aryangodara03@gmail.com>
* handle error from grpc server in endpointtest
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, auth/jwt needs to be resolved
Signed-off-by: aryan <aryangodara03@gmail.com>
* revert back to jwt v4 temporarily
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated jwt tokenizer
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve EOF error for httptest requests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix auth jwt, update to registeredclaims
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix ineffective assignment, auth/api/grpc endpoint failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, remove later
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server setup
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve golangci tests, remove debug statements
Signed-off-by: aryan <aryangodara03@gmail.com>
* update golangci version and modify linters used
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix failing tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server for setup tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix logging and errors inlined
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix remarks, update grpc setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix data race
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc setup down to single simple function
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix linting issues
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve pr comments
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix tests, handle returned errors, go mod tidy vendor
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors from new linters
Signed-off-by: aryan <aryangodara03@gmail.com>
---------
Signed-off-by: aryan <aryangodara03@gmail.com>
2023-04-22 08:14:35 -07:00
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (bootstrap.ConfigsPage, error) {
|
2023-06-14 12:40:37 +02:00
|
|
|
bp, err := es.svc.List(ctx, token, filter, offset, limit)
|
|
|
|
if err != nil {
|
|
|
|
return bp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := listConfigsEvent{
|
|
|
|
offset: offset,
|
|
|
|
limit: limit,
|
|
|
|
fullMatch: filter.FullMatch,
|
|
|
|
partialMatch: filter.PartialMatch,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err1 := es.add(ctx, ev); err1 != nil {
|
|
|
|
return bp, errors.Wrap(err, err1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bp, nil
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) Remove(ctx context.Context, token, id string) error {
|
|
|
|
if err := es.svc.Remove(ctx, token, id); err != nil {
|
2019-04-08 17:46:07 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := removeConfigEvent{
|
2023-06-14 12:40:37 +02:00
|
|
|
mfThing: id,
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
MF-1718 - Use static code analysis in CI (#1729)
* things, twins, and logger lint fixed
Signed-off-by: aryan <aryangodara03@gmail.com>
* all services updated, auth jwt not working, ineffectual assignment issue
Signed-off-by: aryan <aryangodara03@gmail.com>
* handle error from grpc server in endpointtest
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, auth/jwt needs to be resolved
Signed-off-by: aryan <aryangodara03@gmail.com>
* revert back to jwt v4 temporarily
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated jwt tokenizer
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve EOF error for httptest requests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix auth jwt, update to registeredclaims
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix ineffective assignment, auth/api/grpc endpoint failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, remove later
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server setup
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve golangci tests, remove debug statements
Signed-off-by: aryan <aryangodara03@gmail.com>
* update golangci version and modify linters used
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix failing tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server for setup tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix logging and errors inlined
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix remarks, update grpc setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix data race
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc setup down to single simple function
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix linting issues
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve pr comments
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix tests, handle returned errors, go mod tidy vendor
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors from new linters
Signed-off-by: aryan <aryangodara03@gmail.com>
---------
Signed-off-by: aryan <aryangodara03@gmail.com>
2023-04-22 08:14:35 -07:00
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (bootstrap.Config, error) {
|
|
|
|
cfg, err := es.svc.Bootstrap(ctx, externalKey, externalID, secure)
|
2019-04-08 17:46:07 +02:00
|
|
|
|
|
|
|
ev := bootstrapEvent{
|
2023-06-14 12:40:37 +02:00
|
|
|
cfg,
|
|
|
|
externalID,
|
|
|
|
true,
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-16 18:32:05 +02:00
|
|
|
ev.success = false
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
2023-06-14 12:40:37 +02:00
|
|
|
|
|
|
|
if err1 := es.add(ctx, ev); err1 != nil {
|
|
|
|
return cfg, err1
|
|
|
|
}
|
2019-04-08 17:46:07 +02:00
|
|
|
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) ChangeState(ctx context.Context, token, id string, state bootstrap.State) error {
|
|
|
|
if err := es.svc.ChangeState(ctx, token, id, state); err != nil {
|
2019-04-08 17:46:07 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := changeStateEvent{
|
2023-06-14 12:40:37 +02:00
|
|
|
mfThing: id,
|
|
|
|
state: state,
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
MF-1718 - Use static code analysis in CI (#1729)
* things, twins, and logger lint fixed
Signed-off-by: aryan <aryangodara03@gmail.com>
* all services updated, auth jwt not working, ineffectual assignment issue
Signed-off-by: aryan <aryangodara03@gmail.com>
* handle error from grpc server in endpointtest
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, auth/jwt needs to be resolved
Signed-off-by: aryan <aryangodara03@gmail.com>
* revert back to jwt v4 temporarily
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated jwt tokenizer
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve EOF error for httptest requests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix auth jwt, update to registeredclaims
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix ineffective assignment, auth/api/grpc endpoint failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, remove later
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server setup
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve golangci tests, remove debug statements
Signed-off-by: aryan <aryangodara03@gmail.com>
* update golangci version and modify linters used
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix failing tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server for setup tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix logging and errors inlined
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix remarks, update grpc setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix data race
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc setup down to single simple function
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix linting issues
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve pr comments
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix tests, handle returned errors, go mod tidy vendor
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors from new linters
Signed-off-by: aryan <aryangodara03@gmail.com>
---------
Signed-off-by: aryan <aryangodara03@gmail.com>
2023-04-22 08:14:35 -07:00
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) RemoveConfigHandler(ctx context.Context, id string) error {
|
2023-06-14 12:40:37 +02:00
|
|
|
if err := es.svc.RemoveConfigHandler(ctx, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := removeHandlerEvent{
|
|
|
|
id: id,
|
|
|
|
operation: configHandlerRemove,
|
|
|
|
}
|
|
|
|
|
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) RemoveChannelHandler(ctx context.Context, id string) error {
|
2023-06-14 12:40:37 +02:00
|
|
|
if err := es.svc.RemoveChannelHandler(ctx, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := removeHandlerEvent{
|
|
|
|
id: id,
|
|
|
|
operation: channelHandlerRemove,
|
|
|
|
}
|
|
|
|
|
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) error {
|
2023-06-14 12:40:37 +02:00
|
|
|
if err := es.svc.UpdateChannelHandler(ctx, channel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := updateChannelHandlerEvent{
|
|
|
|
channel,
|
|
|
|
}
|
|
|
|
|
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) DisconnectThingHandler(ctx context.Context, channelID, thingID string) error {
|
2023-06-14 12:40:37 +02:00
|
|
|
if err := es.svc.DisconnectThingHandler(ctx, channelID, thingID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := disconnectThingEvent{
|
|
|
|
channelID,
|
|
|
|
thingID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return es.add(ctx, ev)
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
func (es eventStore) add(ctx context.Context, ev event) error {
|
2023-06-14 12:40:37 +02:00
|
|
|
values, err := ev.encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:46:07 +02:00
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
2023-06-14 12:40:37 +02:00
|
|
|
Values: values,
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
return es.client.XAdd(ctx, record).Err()
|
2019-04-08 17:46:07 +02:00
|
|
|
}
|