Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
add: users permission sdk
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 committed Nov 27, 2023
1 parent 86bc0fb commit 35b5193
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/openapi/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 18 additions & 1 deletion pkg/sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 17 additions & 1 deletion pkg/sdk/go/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Check failure on line 134 in pkg/sdk/go/groups.go

View workflow job for this annotation

GitHub Actions / Continuous Integration

File is not `gofumpt`-ed (gofumpt)
func (sdk mgSDK) UpdateGroup(g Group, token string) (Group, errors.SDKError) {
data, err := json.Marshal(g)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,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:
Expand Down Expand Up @@ -504,6 +511,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:
Expand Down Expand Up @@ -643,6 +657,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:
Expand Down
32 changes: 25 additions & 7 deletions pkg/sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 35b5193

Please sign in to comment.