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: Listing of shared things with users & Update SDK #1923

Merged
merged 21 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
54 changes: 0 additions & 54 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -540,60 +540,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
54 changes: 0 additions & 54 deletions api/openapi/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -690,60 +690,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"

/groups/{groupID}/members/assign:
post:
summary: Assigns a member to a group
description: |
Assigns a specific member to a group that is identifier by the group ID.
tags:
- Groups
parameters:
- $ref: "#/components/parameters/GroupID"
requestBody:
$ref: "#/components/requestBodies/AssignReq"
security:
- bearerAuth: []
responses:
"200":
description: Member assigned.
"400":
description: Failed due to malformed group'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"

/groups/{groupID}/members/unassign:
post:
summary: Unassigns a member to a group
description: |
Unassigns a specific member to a group that is identifier by the group ID.
tags:
- Groups
parameters:
- $ref: "#/components/parameters/GroupID"
requestBody:
$ref: "#/components/requestBodies/AssignReq"
security:
- bearerAuth: []
responses:
"200":
description: Member assigned.
"400":
description: Failed due to malformed group'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"

/groups/{groupID}/users/assign:
post:
summary: Assigns a user to a group
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
3 changes: 3 additions & 0 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ func (svc service) ListGroups(ctx context.Context, token string, memberKind, mem
return groups.Page{}, fmt.Errorf("invalid member kind")
}

if len(ids) <= 0 {
return groups.Page{}, errors.ErrNotFound
}
if len(ids) <= 0 {
return groups.Page{}, errors.ErrNotFound
}
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) AddUserToChannel(channelID string, req UsersRelationRequest, token string) 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) RemoveUserFromChannel(channelID string, req UsersRelationRequest, token string) 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) (UsersPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.thingsURL, fmt.Sprintf("%s/%s/%s", channelsEndpoint, channelID, usersEndpoint), pm)
if err != nil {
return UsersPage{}, errors.NewSDKError(err)
}
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return UsersPage{}, sdkerr
}
up := UsersPage{}
if err := json.Unmarshal(body, &up); err != nil {
return UsersPage{}, errors.NewSDKError(err)
}

return up, nil
}

func (sdk mfSDK) AddUserGroupToChannel(channelID string, req UserGroupsRequest, token string) 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) RemoveUserGroupFromChannel(channelID string, req UserGroupsRequest, token string) 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) (GroupsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.thingsURL, fmt.Sprintf("%s/%s/%s", channelsEndpoint, channelID, groupsEndpoint), 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) 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) AddUserToGroup(groupID string, req UsersRelationRequest, 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) RemoveUserFromGroup(groupID string, req UsersRelationRequest, 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