Skip to content

Commit

Permalink
add: listing of shared things and users
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <arvindh91@gmail.com>
  • Loading branch information
arvindh123 committed Oct 13, 2023
1 parent 3c26836 commit fcc7412
Show file tree
Hide file tree
Showing 35 changed files with 598 additions and 1,848 deletions.
54 changes: 0 additions & 54 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -564,60 +564,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"

/channels/{chanID}/assign:
post:
summary: Assigns a member to a channel
description: |
Assigns a specific member to a channel that is identifier by the channel ID.
tags:
- Channels
parameters:
- $ref: "#/components/parameters/chanID"
requestBody:
$ref: "#/components/requestBodies/AssignReq"
security:
- bearerAuth: []
responses:
"200":
description: Thing shared.
"400":
description: Failed due to malformed thing's ID.
"401":
description: Missing or invalid access token provided.
"404":
description: A non-existent entity request.
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

/channels/{chanID}/unassign:
post:
summary: Unassigns a member from a channel
description: |
Unassigns a specific member from a channel that is identifier by the channel ID.
tags:
- Channels
parameters:
- $ref: "#/components/parameters/chanID"
requestBody:
$ref: "#/components/requestBodies/AssignReq"
security:
- bearerAuth: []
responses:
"200":
description: Thing unshared.
"400":
description: Failed due to malformed thing's ID.
"401":
description: Missing or invalid access token provided.
"404":
description: A non-existent entity request.
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

/channels/{chanID}/users/assign:
post:
summary: Assigns a member to a channel
Expand Down
18 changes: 9 additions & 9 deletions docker/nginx/nginx-key.conf
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ http {
# Proxy pass to users & groups id to things service for listing of channels
# /users/{userID}/channels - Listing of channels belongs to userID
# /groups/{userGroupID}/channels - Listing of channels belongs to userGroupID
location ~ ^/(users|groups)/(.+)/channels {
location ~ ^/(users|groups)/(.+)/(channels|things) {
include snippets/proxy-headers.conf;
add_header Access-Control-Expose-Headers Location;
if ($request_method = GET) {
Expand All @@ -66,14 +66,14 @@ http {
# Proxy pass to channel id to users service for listing of channels
# /channels/{channelID}/users - Listing of Users belongs to channelID
# /channels/{channelID}/groups - Listing of User Groups belongs to channelID
location ~ ^/channels/(.+)/(users|groups) {
include snippets/proxy-headers.conf;
add_header Access-Control-Expose-Headers Location;
if ($request_method = GET) {
proxy_pass http://users:${MF_USERS_HTTP_PORT};
break;
}
proxy_pass http://things:${MF_THINGS_HTTP_PORT};
location ~ ^/(channels|things)/(.+)/(users|groups) {
include snippets/proxy-headers.conf;
add_header Access-Control-Expose-Headers Location;
if ($request_method = GET) {
proxy_pass http://users:${MF_USERS_HTTP_PORT};
break;
}
proxy_pass http://things:${MF_THINGS_HTTP_PORT};
}
# Proxy pass to users service
location ~ ^/(users|groups|password|authorize) {
Expand Down
125 changes: 125 additions & 0 deletions pkg/sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,131 @@ func (sdk mfSDK) UpdateChannel(c Channel, token string) (Channel, errors.SDKErro
return c, nil
}

func (sdk mfSDK) AddUsersToChannel(token, channelID string, req addUsersToChannelReq) errors.SDKError {
data, err := json.Marshal(req)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, usersEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
}

func (sdk mfSDK) RemoveUsersFromChannel(token, channelID string, req removeUsersFromChannelReq) errors.SDKError {
data, err := json.Marshal(req)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, usersEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, data, nil, http.StatusOK)
return sdkerr
}

func (sdk mfSDK) ListChannelUsers(channelID string, pm PageMetadata, token string) (ChannelsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.thingsURL, fmt.Sprintf("%s/%s/%s", channelsEndpoint, channelID, usersEndpoint), pm)
if err != nil {
return ChannelsPage{}, errors.NewSDKError(err)
}
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return ChannelsPage{}, sdkerr
}
cp := ChannelsPage{}
if err := json.Unmarshal(body, &cp); err != nil {
return ChannelsPage{}, errors.NewSDKError(err)
}

return cp, nil
}

func (sdk mfSDK) AddUserGroupsToChannel(token, channelID string, req addUserGroupsToChannelReq) errors.SDKError {
data, err := json.Marshal(req)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, groupsEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
}

func (sdk mfSDK) RemoveUserGroupsToChannel(token, channelID string, req removeUserGroupsFromChannelReq) errors.SDKError {
data, err := json.Marshal(req)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, groupsEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, data, nil, http.StatusOK)
return sdkerr
}

func (sdk mfSDK) ListChannelUserGroups(channelID string, pm PageMetadata, token string) (ChannelsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.thingsURL, fmt.Sprintf("%s/%s/%s", channelsEndpoint, channelID, groupsEndpoint), pm)
if err != nil {
return ChannelsPage{}, errors.NewSDKError(err)
}
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return ChannelsPage{}, sdkerr
}
cp := ChannelsPage{}
if err := json.Unmarshal(body, &cp); err != nil {
return ChannelsPage{}, errors.NewSDKError(err)
}

return cp, nil
}

func (sdk mfSDK) Connect(conn Connection, token string) errors.SDKError {
data, err := json.Marshal(conn)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s", sdk.thingsURL, connectEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)

return sdkerr
}

func (sdk mfSDK) Disconnect(connIDs Connection, token string) errors.SDKError {
data, err := json.Marshal(connIDs)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s", sdk.thingsURL, disconnectEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusNoContent)

return sdkerr
}

func (sdk mfSDK) ConnectThing(thingID, channelID, token string) errors.SDKError {
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, thingsEndpoint, thingID)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, nil, nil, http.StatusNoContent)

return sdkerr

}

func (sdk mfSDK) DisconnectThing(thingID, channelID, token string) errors.SDKError {
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, thingsEndpoint, thingID)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, nil, nil, http.StatusNoContent)

return sdkerr
}

func (sdk mfSDK) EnableChannel(id, token string) (Channel, errors.SDKError) {
return sdk.changeChannelStatus(id, enableEndpoint, token)
}
Expand Down
77 changes: 58 additions & 19 deletions pkg/sdk/go/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,6 @@ func (sdk mfSDK) CreateGroup(g Group, token string) (Group, errors.SDKError) {
return g, nil
}

func (sdk mfSDK) Memberships(clientID string, pm PageMetadata, token string) (MembershipsPage, errors.SDKError) {
url, err := sdk.withQueryParams(fmt.Sprintf("%s/%s/%s", sdk.usersURL, usersEndpoint, clientID), "memberships", pm)
if err != nil {
return MembershipsPage{}, errors.NewSDKError(err)
}

_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return MembershipsPage{}, sdkerr
}

var tp MembershipsPage
if err := json.Unmarshal(body, &tp); err != nil {
return MembershipsPage{}, errors.NewSDKError(err)
}

return tp, nil
}

func (sdk mfSDK) Groups(pm PageMetadata, token string) (GroupsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.usersURL, groupsEndpoint, pm)
if err != nil {
Expand Down Expand Up @@ -164,6 +145,64 @@ func (sdk mfSDK) DisableGroup(id, token string) (Group, errors.SDKError) {
return sdk.changeGroupStatus(id, disableEndpoint, token)
}

func (sdk mfSDK) AddUsersToGroup(groupID string, req addUsersToGroupReq, token string) errors.SDKError {
data, err := json.Marshal(req)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, groupID, usersEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
}

func (sdk mfSDK) RemoveUsersToGroup(groupID string, req removeUsersToGroupReq, token string) errors.SDKError {
data, err := json.Marshal(req)
if err != nil {
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, groupID, usersEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, data, nil, http.StatusOK)
return sdkerr
}

func (sdk mfSDK) ListGroupUsers(groupID string, pm PageMetadata, token string) (GroupsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.usersURL, fmt.Sprintf("%s/%s/%s", groupsEndpoint, groupID, usersEndpoint), pm)
if err != nil {
return GroupsPage{}, errors.NewSDKError(err)
}
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return GroupsPage{}, sdkerr
}
gp := GroupsPage{}
if err := json.Unmarshal(body, &gp); err != nil {
return GroupsPage{}, errors.NewSDKError(err)
}

return gp, nil
}

func (sdk mfSDK) ListGroupChannels(groupID string, pm PageMetadata, token string) (GroupsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.usersURL, fmt.Sprintf("%s/%s/%s", groupsEndpoint, groupID, channelsEndpoint), pm)
if err != nil {
return GroupsPage{}, errors.NewSDKError(err)
}
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return GroupsPage{}, sdkerr
}
gp := GroupsPage{}
if err := json.Unmarshal(body, &gp); err != nil {
return GroupsPage{}, errors.NewSDKError(err)
}

return gp, nil
}

func (sdk mfSDK) changeGroupStatus(id, status, token string) (Group, errors.SDKError) {
url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, id, status)

Expand Down
Loading

0 comments on commit fcc7412

Please sign in to comment.