From 77805ae1262d2931c674a1f671c0e80f1660f25b Mon Sep 17 00:00:00 2001 From: Arvindh Date: Mon, 27 Nov 2023 18:34:46 +0530 Subject: [PATCH] add: users permission sdk Signed-off-by: Arvindh --- api/openapi/users.yml | 2 +- pkg/sdk/go/channels.go | 19 ++++++++++++++++++- pkg/sdk/go/groups.go | 18 +++++++++++++++++- pkg/sdk/go/sdk.go | 21 +++++++++++++++++++++ pkg/sdk/go/things.go | 32 +++++++++++++++++++++++++------- 5 files changed, 82 insertions(+), 10 deletions(-) diff --git a/api/openapi/users.yml b/api/openapi/users.yml index ff118ed3b..c3131effe 100644 --- a/api/openapi/users.yml +++ b/api/openapi/users.yml @@ -1657,7 +1657,7 @@ components: $ref: "#/components/schemas/UserSecret" UserUpdateRoleReq: - description: JSON-formated document describing the owner of user to be update + description: JSON-formated document describing the role of the user to be update required: true content: application/json: diff --git a/pkg/sdk/go/channels.go b/pkg/sdk/go/channels.go index 231c3ce9c..a6ccf1893 100644 --- a/pkg/sdk/go/channels.go +++ b/pkg/sdk/go/channels.go @@ -16,7 +16,7 @@ const channelsEndpoint = "channels" // Channel represents magistrala channel. type Channel struct { - ID string `json:"id"` + ID string `json:"id,omitempty"` OwnerID string `json:"owner_id,omitempty"` ParentID string `json:"parent_id,omitempty"` Name string `json:"name,omitempty"` @@ -28,6 +28,7 @@ type Channel struct { CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` Status string `json:"status,omitempty"` + Permissions []string `json:"permissions,omitempty"` } func (sdk mgSDK) CreateChannel(c Channel, token string) (Channel, errors.SDKError) { @@ -125,6 +126,22 @@ func (sdk mgSDK) Channel(id, token string) (Channel, errors.SDKError) { return c, nil } +func (sdk mgSDK) ChannelPermissions(id, token string) (Channel, errors.SDKError) { + url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, id, permissionsEndpoint) + + _, body, err := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK) + if err != nil { + return Channel{}, err + } + + var c Channel + if err := json.Unmarshal(body, &c); err != nil { + return Channel{}, errors.NewSDKError(err) + } + + return c, nil +} + func (sdk mgSDK) UpdateChannel(c Channel, token string) (Channel, errors.SDKError) { data, err := json.Marshal(c) if err != nil { diff --git a/pkg/sdk/go/groups.go b/pkg/sdk/go/groups.go index 47b1b3106..131722b4c 100644 --- a/pkg/sdk/go/groups.go +++ b/pkg/sdk/go/groups.go @@ -23,7 +23,7 @@ const ( // Path in a tree consisting of group IDs // Paths are unique per owner. type Group struct { - ID string `json:"id"` + ID string `json:"id,omitempty"` OwnerID string `json:"owner_id,omitempty"` ParentID string `json:"parent_id,omitempty"` Name string `json:"name,omitempty"` @@ -35,6 +35,7 @@ type Group struct { CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` Status string `json:"status,omitempty"` + Permissions []string `json:"permissions,omitempty"` } func (sdk mgSDK) CreateGroup(g Group, token string) (Group, errors.SDKError) { @@ -116,6 +117,21 @@ func (sdk mgSDK) Group(id, token string) (Group, errors.SDKError) { return t, nil } +func (sdk mgSDK) GroupPermissions(id, token string) (Group, errors.SDKError) { + url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, id, permissionsEndpoint) + + _, body, err := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK) + if err != nil { + return Group{}, err + } + + var t Group + if err := json.Unmarshal(body, &t); err != nil { + return Group{}, errors.NewSDKError(err) + } + + return t, nil +} func (sdk mgSDK) UpdateGroup(g Group, token string) (Group, errors.SDKError) { data, err := json.Marshal(g) if err != nil { diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 23c8a7a18..664c74ca2 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -351,6 +351,13 @@ type SDK interface { // fmt.Println(thing) Thing(id, token string) (Thing, errors.SDKError) + // ThingPermissions returns user permissions on the thing id. + // + // example: + // thing, _ := sdk.Thing("thingID", "token") + // fmt.Println(thing) + ThingPermissions(id, token string) (Thing, errors.SDKError) + // UpdateThing updates existing thing. // // example: @@ -494,6 +501,13 @@ type SDK interface { // fmt.Println(group) Group(id, token string) (Group, errors.SDKError) + // GroupPermissions returns user permissions by group ID. + // + // example: + // group, _ := sdk.Group("groupID", "token") + // fmt.Println(group) + GroupPermissions(id, token string) (Group, errors.SDKError) + // UpdateGroup updates existing group. // // example: @@ -633,6 +647,13 @@ type SDK interface { // fmt.Println(channel) Channel(id, token string) (Channel, errors.SDKError) + // ChannelPermissions returns user permissions on the channel ID. + // + // example: + // channel, _ := sdk.Channel("channelID", "token") + // fmt.Println(channel) + ChannelPermissions(id, token string) (Channel, errors.SDKError) + // UpdateChannel updates existing channel. // // example: diff --git a/pkg/sdk/go/things.go b/pkg/sdk/go/things.go index e2e60139c..56231333b 100644 --- a/pkg/sdk/go/things.go +++ b/pkg/sdk/go/things.go @@ -13,17 +13,18 @@ import ( ) const ( - thingsEndpoint = "things" - connectEndpoint = "connect" - disconnectEndpoint = "disconnect" - identifyEndpoint = "identify" - shareEndpoint = "share" - unshareEndpoint = "unshare" + permissionsEndpoint = "permissions" + thingsEndpoint = "things" + connectEndpoint = "connect" + disconnectEndpoint = "disconnect" + identifyEndpoint = "identify" + shareEndpoint = "share" + unshareEndpoint = "unshare" ) // Thing represents magistrala thing. type Thing struct { - ID string `json:"id"` + ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Credentials Credentials `json:"credentials"` Tags []string `json:"tags,omitempty"` @@ -32,6 +33,7 @@ type Thing struct { CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` Status string `json:"status,omitempty"` + Permissions []string `json:"permissions,omitempty"` } func (sdk mgSDK) CreateThing(thing Thing, token string) (Thing, errors.SDKError) { @@ -130,6 +132,22 @@ func (sdk mgSDK) Thing(id, token string) (Thing, errors.SDKError) { return t, nil } +func (sdk mgSDK) ThingPermissions(id, token string) (Thing, errors.SDKError) { + url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, thingsEndpoint, id, permissionsEndpoint) + + _, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK) + if sdkerr != nil { + return Thing{}, sdkerr + } + + var t Thing + if err := json.Unmarshal(body, &t); err != nil { + return Thing{}, errors.NewSDKError(err) + } + + return t, nil +} + func (sdk mgSDK) UpdateThing(t Thing, token string) (Thing, errors.SDKError) { data, err := json.Marshal(t) if err != nil {