Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Update Rules Service #32

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/re/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ import (

const (
svcName = "rules_engine"
envPrefixDB = "SMQ_RE_DB_"
envPrefixHTTP = "SMQ_RE_HTTP_"
envPrefixDB = "MG_RE_DB_"
envPrefixHTTP = "MG_RE_HTTP_"
envPrefixAuth = "SMQ_AUTH_GRPC_"
defDB = "r"
defSvcHTTPPort = "9008"
)

type config struct {
LogLevel string `env:"SMQ_RE_LOG_LEVEL" envDefault:"info"`
InstanceID string `env:"SMQ_RE_INSTANCE_ID" envDefault:""`
LogLevel string `env:"MG_RE_LOG_LEVEL" envDefault:"info"`
InstanceID string `env:"MG_RE_INSTANCE_ID" envDefault:""`
JaegerURL url.URL `env:"SMQ_JAEGER_URL" envDefault:"http://localhost:4318/v1/traces"`
SendTelemetry bool `env:"SMQ_SEND_TELEMETRY" envDefault:"true"`
ESURL string `env:"SMQ_ES_URL" envDefault:"nats://localhost:4222"`
CacheURL string `env:"SMQ_RE_CACHE_URL" envDefault:"redis://localhost:6379/0"`
CacheKeyDuration time.Duration `env:"SMQ_RE_CACHE_KEY_DURATION" envDefault:"10m"`
CacheURL string `env:"MG_RE_CACHE_URL" envDefault:"redis://localhost:6379/0"`
CacheKeyDuration time.Duration `env:"MG_RE_CACHE_KEY_DURATION" envDefault:"10m"`
TraceRatio float64 `env:"SMQ_JAEGER_TRACE_RATIO" envDefault:"1.0"`
ConfigPath string `env:"SMQ_RE_CONFIG_PATH" envDefault:"/config.toml"`
ConfigPath string `env:"MG_RE_CONFIG_PATH" envDefault:"/config.toml"`
BrokerURL string `env:"SMQ_MESSAGE_BROKER_URL" envDefault:"nats://localhost:4222"`
}

Expand Down
30 changes: 15 additions & 15 deletions docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,21 @@ SMQ_CHANNELS_DB_SSL_ROOT_CERT=
SMQ_CHANNELS_INSTANCE_ID=

### RE
SMQ_RE_LOG_LEVEL=debug
SMQ_RE_HTTP_HOST=re
SMQ_RE_HTTP_PORT=9008
SMQ_RE_HTTP_SERVER_CERT=
SMQ_RE_HTTP_SERVER_KEY=
SMQ_RE_DB_HOST=re-db
SMQ_RE_DB_PORT=5432
SMQ_RE_DB_USER=magistrala
SMQ_RE_DB_PASS=magistrala
SMQ_RE_DB_NAME=rule_engine
SMQ_RE_DB_SSL_MODE=disable
SMQ_RE_DB_SSL_CERT=
SMQ_RE_DB_SSL_KEY=
SMQ_RE_DB_SSL_ROOT_CERT=
SMQ_RE_INSTANCE_ID=
MG_RE_LOG_LEVEL=debug
MG_RE_HTTP_HOST=re
MG_RE_HTTP_PORT=9008
MG_RE_HTTP_SERVER_CERT=
MG_RE_HTTP_SERVER_KEY=
MG_RE_DB_HOST=re-db
MG_RE_DB_PORT=5432
MG_RE_DB_USER=magistrala
MG_RE_DB_PASS=magistrala
MG_RE_DB_NAME=rule_engine
MG_RE_DB_SSL_MODE=disable
MG_RE_DB_SSL_CERT=
MG_RE_DB_SSL_KEY=
MG_RE_DB_SSL_ROOT_CERT=
MG_RE_INSTANCE_ID=

#### Channels Client Config
SMQ_CHANNELS_URL=http://channels:9005
Expand Down
40 changes: 20 additions & 20 deletions docker/addons/re/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ services:
restart: on-failure
command: postgres -c "max_connections=${SMQ_POSTGRES_MAX_CONNECTIONS}"
environment:
POSTGRES_USER: ${SMQ_RE_DB_USER}
POSTGRES_PASSWORD: ${SMQ_RE_DB_PASS}
POSTGRES_DB: ${SMQ_RE_DB_NAME}
POSTGRES_USER: ${MG_RE_DB_USER}
POSTGRES_PASSWORD: ${MG_RE_DB_PASS}
POSTGRES_DB: ${MG_RE_DB_NAME}
ports:
- 6008:5432
- 6009:5432
networks:
- magistrala-base-net
volumes:
Expand All @@ -39,20 +39,20 @@ services:
- re-db
restart: on-failure
environment:
SMQ_RE_LOG_LEVEL: ${SMQ_RE_LOG_LEVEL}
SMQ_RE_HTTP_PORT: ${SMQ_RE_HTTP_PORT}
SMQ_RE_HTTP_HOST: ${SMQ_RE_HTTP_HOST}
SMQ_RE_HTTP_SERVER_CERT: ${SMQ_RE_HTTP_SERVER_CERT}
SMQ_RE_HTTP_SERVER_KEY: ${SMQ_RE_HTTP_SERVER_KEY}
SMQ_RE_DB_HOST: ${SMQ_RE_DB_HOST}
SMQ_RE_DB_PORT: ${SMQ_RE_DB_PORT}
SMQ_RE_DB_USER: ${SMQ_RE_DB_USER}
SMQ_RE_DB_PASS: ${SMQ_RE_DB_PASS}
SMQ_RE_DB_NAME: ${SMQ_RE_DB_NAME}
SMQ_RE_DB_SSL_MODE: ${SMQ_RE_DB_SSL_MODE}
SMQ_RE_DB_SSL_CERT: ${SMQ_RE_DB_SSL_CERT}
SMQ_RE_DB_SSL_KEY: ${SMQ_RE_DB_SSL_KEY}
SMQ_RE_DB_SSL_ROOT_CERT: ${SMQ_RE_DB_SSL_ROOT_CERT}
MG_RE_LOG_LEVEL: ${MG_RE_LOG_LEVEL}
MG_RE_HTTP_PORT: ${MG_RE_HTTP_PORT}
MG_RE_HTTP_HOST: ${MG_RE_HTTP_HOST}
MG_RE_HTTP_SERVER_CERT: ${MG_RE_HTTP_SERVER_CERT}
MG_RE_HTTP_SERVER_KEY: ${MG_RE_HTTP_SERVER_KEY}
MG_RE_DB_HOST: ${MG_RE_DB_HOST}
MG_RE_DB_PORT: ${MG_RE_DB_PORT}
MG_RE_DB_USER: ${MG_RE_DB_USER}
MG_RE_DB_PASS: ${MG_RE_DB_PASS}
MG_RE_DB_NAME: ${MG_RE_DB_NAME}
MG_RE_DB_SSL_MODE: ${MG_RE_DB_SSL_MODE}
MG_RE_DB_SSL_CERT: ${MG_RE_DB_SSL_CERT}
MG_RE_DB_SSL_KEY: ${MG_RE_DB_SSL_KEY}
MG_RE_DB_SSL_ROOT_CERT: ${MG_RE_DB_SSL_ROOT_CERT}
SMQ_MESSAGE_BROKER_URL: ${SMQ_MESSAGE_BROKER_URL}
SMQ_JAEGER_URL: ${SMQ_JAEGER_URL}
SMQ_JAEGER_TRACE_RATIO: ${SMQ_JAEGER_TRACE_RATIO}
Expand All @@ -65,9 +65,9 @@ services:
SMQ_SPICEDB_PRE_SHARED_KEY: ${SMQ_SPICEDB_PRE_SHARED_KEY}
SMQ_SPICEDB_HOST: ${SMQ_SPICEDB_HOST}
SMQ_SPICEDB_PORT: ${SMQ_SPICEDB_PORT}
SMQ_RE_INSTANCE_ID: ${SMQ_RE_INSTANCE_ID}
MG_RE_INSTANCE_ID: ${MG_RE_INSTANCE_ID}
ports:
- ${SMQ_RE_HTTP_PORT}:${SMQ_RE_HTTP_PORT}
- ${MG_RE_HTTP_PORT}:${MG_RE_HTTP_PORT}
networks:
- magistrala-base-net
volumes:
Expand Down
57 changes: 52 additions & 5 deletions re/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,74 @@ func listRulesEndpoint(s re.Service) endpoint.Endpoint {
return rulesPageRes{}, nil
}
ret := rulesPageRes{
pageRes: pageRes{
Limit: page.Limit,
Offset: page.Offset,
Total: page.Total,
},
Rules: page.Rules,
}
return ret, nil
}
}

func upadateRuleStatusEndpoint(s re.Service) endpoint.Endpoint {
func deleteRuleEndpoint(s re.Service) endpoint.Endpoint {
ianmuchyri marked this conversation as resolved.
Show resolved Hide resolved
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}

req := request.(changeRuleStatusReq)
req := request.(deleteRuleReq)
if err := req.validate(); err != nil {
return updateRoleStatusRes{}, err
return deleteRuleRes{}, err
}
err := s.RemoveRule(ctx, session, req.id)
if err != nil {
return updateRoleStatusRes{false}, err
return deleteRuleRes{false}, err
}
return updateRoleStatusRes{true}, nil
return deleteRuleRes{true}, nil
}
}

func enableRuleEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}

req := request.(updateRuleStatusReq)
if err := req.validate(); err != nil {
return updateRuleStatusRes{}, err
}

rule, err := s.EnableRule(ctx, session, req.id)
if err != nil {
return updateRuleStatusRes{}, err
}

return updateRuleStatusRes{Rule: rule}, err
}
}

func disableRuleEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}

req := request.(updateRuleStatusReq)
if err := req.validate(); err != nil {
return updateRuleStatusRes{}, err
}

rule, err := s.DisableRule(ctx, session, req.id)
if err != nil {
return updateRuleStatusRes{}, err
}

return updateRuleStatusRes{Rule: rule}, err
}
}
30 changes: 25 additions & 5 deletions re/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
apiutil "github.com/absmach/supermq/api/http/util"
)

const maxLimitSize = 1000
const (
maxLimitSize = 1000
MaxNameSize = 1024
)

type addRuleReq struct {
re.Rule
}

func (req addRuleReq) validate() error {
if len(req.Name) > api.MaxNameSize || req.Name == "" {
return apiutil.ErrNameSize
}
return nil
}

Expand Down Expand Up @@ -57,16 +63,30 @@ func (req updateRuleReq) validate() error {
if len(req.Rule.Logic.Value) == 0 {
return apiutil.ErrEmptyList
}
if len(req.Rule.Name) > api.MaxNameSize || req.Rule.Name == "" {
return apiutil.ErrNameSize
}

return nil
}

type updateRuleStatusReq struct {
id string
}

func (req updateRuleStatusReq) validate() error {
if req.id == "" {
return apiutil.ErrMissingID
}

return nil
}

type changeRuleStatusReq struct {
id string
status re.Status
type deleteRuleReq struct {
id string
}

func (req changeRuleStatusReq) validate() error {
func (req deleteRuleReq) validate() error {
if req.id == "" {
return apiutil.ErrMissingID
}
Expand Down
20 changes: 10 additions & 10 deletions re/api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
var (
_ supermq.Response = (*viewRuleRes)(nil)
_ supermq.Response = (*addRuleRes)(nil)
_ supermq.Response = (*changeRuleStatusRes)(nil)
_ supermq.Response = (*updateRuleStatusRes)(nil)
_ supermq.Response = (*rulesPageRes)(nil)
_ supermq.Response = (*updateRuleRes)(nil)
_ supermq.Response = (*updateRoleStatusRes)(nil)
_ supermq.Response = (*deleteRuleRes)(nil)
)

type pageRes struct {
Expand Down Expand Up @@ -102,38 +102,38 @@ func (res rulesPageRes) Empty() bool {
return false
}

type changeRuleStatusRes struct {
type updateRuleStatusRes struct {
re.Rule `json:",inline"`
}

func (res changeRuleStatusRes) Code() int {
func (res updateRuleStatusRes) Code() int {
return http.StatusOK
}

func (res changeRuleStatusRes) Headers() map[string]string {
func (res updateRuleStatusRes) Headers() map[string]string {
return map[string]string{}
}

func (res changeRuleStatusRes) Empty() bool {
func (res updateRuleStatusRes) Empty() bool {
return false
}

type updateRoleStatusRes struct {
type deleteRuleRes struct {
deleted bool
}

func (res updateRoleStatusRes) Code() int {
func (res deleteRuleRes) Code() int {
if res.deleted {
return http.StatusNoContent
}

return http.StatusOK
}

func (res updateRoleStatusRes) Headers() map[string]string {
func (res deleteRuleRes) Headers() map[string]string {
return map[string]string{}
}

func (res updateRoleStatusRes) Empty() bool {
func (res deleteRuleRes) Empty() bool {
return true
}
Loading
Loading