1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00
Arvindh cd82cc5a43
NOISSUE: Listing of shared things with users & Update SDK (#1923)
* NOISSUE - Fix Bugs (#20)

* fix bugs

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix bugs

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Add Connect Disconnect endpoints (#23)

* fix bugs

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix bugs

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix list of things in a channel and Add connect disconnect endpoint

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix list of things in a channel and Add connect disconnect endpoint

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Add: Things share with users (#25)

* fix list of things in a channel and Add connect disconnect endpoint

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: things share with other users

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Add: Listing of things, channels, groups, users  (#26)

* add: listing of channels, users, groups, things

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: listing of channels, users, groups, things

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: listing of channels, users, groups, things

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: listing of channels, users, groups, things

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Add: List of user groups & removed repeating code in groups (#29)

* removed repeating code in list groups

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: list of user group

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: otel handler operator name for endpoints

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* add: listing of shared things and users

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: listing of shared things and users

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: new SDK

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: new SDK

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: comment

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: sdk function names

Signed-off-by: Arvindh <arvindh91@gmail.com>

* update: api spec

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: channels connect request

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: listing of clients and groups

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: CLI

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: array len comparision

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: nginx

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2023-10-17 15:38:06 +02:00

369 lines
7.7 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package events
import (
"encoding/json"
"fmt"
"strings"
"time"
mfclients "github.com/mainflux/mainflux/pkg/clients"
"github.com/mainflux/mainflux/pkg/events"
)
const (
clientPrefix = "thing."
clientCreate = clientPrefix + "create"
clientUpdate = clientPrefix + "update"
clientRemove = clientPrefix + "remove"
clientView = clientPrefix + "view"
clientList = clientPrefix + "list"
clientListByGroup = clientPrefix + "list_by_group"
clientIdentify = clientPrefix + "identify"
clientAuthorize = clientPrefix + "authorize"
)
var (
_ events.Event = (*createClientEvent)(nil)
_ events.Event = (*updateClientEvent)(nil)
_ events.Event = (*removeClientEvent)(nil)
_ events.Event = (*viewClientEvent)(nil)
_ events.Event = (*listClientEvent)(nil)
_ events.Event = (*listClientByGroupEvent)(nil)
_ events.Event = (*identifyClientEvent)(nil)
_ events.Event = (*authorizeClientEvent)(nil)
_ events.Event = (*shareClientEvent)(nil)
)
type createClientEvent struct {
mfclients.Client
}
func (cce createClientEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": clientCreate,
"id": cce.ID,
"status": cce.Status.String(),
"created_at": cce.CreatedAt,
}
if cce.Name != "" {
val["name"] = cce.Name
}
if len(cce.Tags) > 0 {
tags := fmt.Sprintf("[%s]", strings.Join(cce.Tags, ","))
val["tags"] = tags
}
if cce.Owner != "" {
val["owner"] = cce.Owner
}
if cce.Metadata != nil {
metadata, err := json.Marshal(cce.Metadata)
if err != nil {
return map[string]interface{}{}, err
}
val["metadata"] = metadata
}
if cce.Credentials.Identity != "" {
val["identity"] = cce.Credentials.Identity
}
return val, nil
}
type updateClientEvent struct {
mfclients.Client
operation string
}
func (uce updateClientEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": clientUpdate,
"updated_at": uce.UpdatedAt,
"updated_by": uce.UpdatedBy,
}
if uce.operation != "" {
val["operation"] = clientUpdate + "_" + uce.operation
}
if uce.ID != "" {
val["id"] = uce.ID
}
if uce.Name != "" {
val["name"] = uce.Name
}
if len(uce.Tags) > 0 {
tags := fmt.Sprintf("[%s]", strings.Join(uce.Tags, ","))
val["tags"] = tags
}
if uce.Owner != "" {
val["owner"] = uce.Owner
}
if uce.Credentials.Identity != "" {
val["identity"] = uce.Credentials.Identity
}
if uce.Metadata != nil {
metadata, err := json.Marshal(uce.Metadata)
if err != nil {
return map[string]interface{}{}, err
}
val["metadata"] = metadata
}
if !uce.CreatedAt.IsZero() {
val["created_at"] = uce.CreatedAt
}
if uce.Status.String() != "" {
val["status"] = uce.Status.String()
}
return val, nil
}
type removeClientEvent struct {
id string
status string
updatedAt time.Time
updatedBy string
}
func (rce removeClientEvent) Encode() (map[string]interface{}, error) {
return map[string]interface{}{
"operation": clientRemove,
"id": rce.id,
"status": rce.status,
"updated_at": rce.updatedAt,
"updated_by": rce.updatedBy,
}, nil
}
type viewClientEvent struct {
mfclients.Client
}
func (vce viewClientEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": clientView,
"id": vce.ID,
}
if vce.Name != "" {
val["name"] = vce.Name
}
if len(vce.Tags) > 0 {
tags := fmt.Sprintf("[%s]", strings.Join(vce.Tags, ","))
val["tags"] = tags
}
if vce.Owner != "" {
val["owner"] = vce.Owner
}
if vce.Credentials.Identity != "" {
val["identity"] = vce.Credentials.Identity
}
if vce.Metadata != nil {
metadata, err := json.Marshal(vce.Metadata)
if err != nil {
return map[string]interface{}{}, err
}
val["metadata"] = metadata
}
if !vce.CreatedAt.IsZero() {
val["created_at"] = vce.CreatedAt
}
if !vce.UpdatedAt.IsZero() {
val["updated_at"] = vce.UpdatedAt
}
if vce.UpdatedBy != "" {
val["updated_by"] = vce.UpdatedBy
}
if vce.Status.String() != "" {
val["status"] = vce.Status.String()
}
return val, nil
}
type listClientEvent struct {
reqUserID string
mfclients.Page
}
func (lce listClientEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": clientList,
"reqUserID": lce.reqUserID,
"total": lce.Total,
"offset": lce.Offset,
"limit": lce.Limit,
}
if lce.Name != "" {
val["name"] = lce.Name
}
if lce.Order != "" {
val["order"] = lce.Order
}
if lce.Dir != "" {
val["dir"] = lce.Dir
}
if lce.Metadata != nil {
metadata, err := json.Marshal(lce.Metadata)
if err != nil {
return map[string]interface{}{}, err
}
val["metadata"] = metadata
}
if lce.Owner != "" {
val["owner"] = lce.Owner
}
if lce.Tag != "" {
val["tag"] = lce.Tag
}
if lce.Permission != "" {
val["permission"] = lce.Permission
}
if lce.Status.String() != "" {
val["status"] = lce.Status.String()
}
if len(lce.IDs) > 0 {
ids := fmt.Sprintf("[%s]", strings.Join(lce.IDs, ","))
val["ids"] = ids
}
if lce.Identity != "" {
val["identity"] = lce.Identity
}
return val, nil
}
type listClientByGroupEvent struct {
mfclients.Page
channelID string
}
func (lcge listClientByGroupEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": clientListByGroup,
"total": lcge.Total,
"offset": lcge.Offset,
"limit": lcge.Limit,
"channel_id": lcge.channelID,
}
if lcge.Name != "" {
val["name"] = lcge.Name
}
if lcge.Order != "" {
val["order"] = lcge.Order
}
if lcge.Dir != "" {
val["dir"] = lcge.Dir
}
if lcge.Metadata != nil {
metadata, err := json.Marshal(lcge.Metadata)
if err != nil {
return map[string]interface{}{}, err
}
val["metadata"] = metadata
}
if lcge.Owner != "" {
val["owner"] = lcge.Owner
}
if lcge.Tag != "" {
val["tag"] = lcge.Tag
}
if lcge.Permission != "" {
val["permission"] = lcge.Permission
}
if lcge.Status.String() != "" {
val["status"] = lcge.Status.String()
}
if lcge.Identity != "" {
val["identity"] = lcge.Identity
}
return val, nil
}
type identifyClientEvent struct {
thingID string
}
func (ice identifyClientEvent) Encode() (map[string]interface{}, error) {
return map[string]interface{}{
"operation": clientIdentify,
"thing_id": ice.thingID,
}, nil
}
type authorizeClientEvent struct {
thingID string
namespace string
subjectType string
subjectKind string
subjectRelation string
subject string
relation string
permission string
object string
objectType string
}
func (ice authorizeClientEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": clientAuthorize,
"thing_id": ice.thingID,
}
if ice.namespace != "" {
val["namespace"] = ice.namespace
}
if ice.subjectType != "" {
val["subject_type"] = ice.subjectType
}
if ice.subjectKind != "" {
val["subject_kind"] = ice.subjectKind
}
if ice.subjectRelation != "" {
val["subject_relation"] = ice.subjectRelation
}
if ice.subject != "" {
val["subject"] = ice.subject
}
if ice.relation != "" {
val["relation"] = ice.relation
}
if ice.permission != "" {
val["permission"] = ice.permission
}
if ice.object != "" {
val["object"] = ice.object
}
if ice.objectType != "" {
val["object_type"] = ice.objectType
}
return val, nil
}
type shareClientEvent struct {
action string
id string
relation string
userIDs []string
}
func (sce shareClientEvent) Encode() (map[string]interface{}, error) {
return map[string]interface{}{
"operation": clientPrefix + sce.action,
"id": sce.id,
"relation": sce.relation,
"user_ids": strings.Join(sce.userIDs, ","),
}, nil
}