8000 MF-983 - Add HTTP query param to connections list endpoints to fetch disconnected Things or Channels by manuio · Pull Request #1217 · absmach/supermq · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

MF-983 - Add HTTP query param to connections list endpoints to fetch disconnected Things or Channels #1217

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

Merged
merged 11 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions bootstrap/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ func (svc *mainfluxThings) ListThings(context.Context, string, uint64, uint64, s
panic("not implemented")
}

func (svc *mainfluxThings) ListChannelsByThing(context.Context, string, string, uint64, uint64) (things.ChannelsPage, error) {
func (svc *mainfluxThings) ListChannelsByThing(context.Context, string, string, uint64, uint64, bool) (things.ChannelsPage, error) {
panic("not implemented")
}

func (svc *mainfluxThings) ListThingsByChannel(context.Context, string, string, uint64, uint64) (things.Page, error) {
func (svc *mainfluxThings) ListThingsByChannel(context.Context, string, string, uint64, uint64, bool) (things.Page, error) {
panic("not implemented")
}

Expand Down
27 changes: 23 additions & 4 deletions cli/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,33 @@ var cmdChannels = []cobra.Command{
cobra.Command{
Use: "connections",
Short: "connections <channel_id> <user_auth_token>",
Long: `List of Things connected to Channel`,
Long: `List of Things connected to a Channel`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}

cl, err := sdk.ThingsByChannel(args[1], args[0], uint64(Offset), uint64(Limit))
cl, err := sdk.ThingsByChannel(args[1], args[0], uint64(Offset), uint64(Limit), false)
if err != nil {
logError(err)
return
}

logJSON(cl)
},
},
cobra.Command{
Use: "not-connected",
Short: "not-connected <channel_id> <user_auth_token>",
Long: `List of Things not connected to a Channel`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}

cl, err := sdk.ThingsByChannel(args[1], args[0], uint64(Offset), uint64(Limit), false)
if err != nil {
logError(err)
return
Expand All @@ -134,9 +153,9 @@ func NewChannelsCmd() *cobra.Command {
cmd := cobra.Command{
Use: "channels",
Short: "Channels management",
Long: `Channels management: create, get, update or delete Channel and get list of Things connected to Channel`,
Long: `Channels management: create, get, update or delete Channel and get list of Things connected or not connected to a Channel`,
Run: func(cmd *cobra.Command, args []string) {
logUsage("channels [create | get | update | delete | connections]")
logUsage("channels [create | get | update | delete | connections | not-connected]")
},
}

Expand Down
25 changes: 22 additions & 3 deletions cli/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,26 @@ var cmdThings = []cobra.Command{
return
}

cl, err := sdk.ChannelsByThing(args[1], args[0], uint64(Offset), uint64(Limit))
cl, err := sdk.ChannelsByThing(args[1], args[0], uint64(Offset), uint64(Limit), true)
if err != nil {
logError(err)
return
}

logJSON(cl)
},
},
cobra.Command{
Use: "not-connected",
Short: "not-connected <thing_id> <user_auth_token>",
Long: `List of Channels not connected to a Thing`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}

cl, err := sdk.ChannelsByThing(args[1], args[0], uint64(Offset), uint64(Limit), false)
if err != nil {
logError(err)
return
Expand All @@ -173,9 +192,9 @@ func NewThingsCmd() *cobra.Command {
cmd := cobra.Command{
Use: "things",
Short: "Things management",
Long: `Things management: create, get, update or delete Thing, connect or disconnect Thing from Channel and get the list of Channels connected to Thing`,
Long: `Things management: create, get, update or delete Thing, connect or disconnect Thing from Channel and get the list of Channels connected or disconnected from a Thing`,
Run: func(cmd *cobra.Command, args []string) {
logUsage("things [create | get | update | delete | connect | disconnect | connections]")
logUsage("things [create | get | update | delete | connect | disconnect | connections | not-connected]")
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (sdk mfSDK) Channels(token string, offset, limit uint64, name string) (Chan
return cp, nil
}

func (sdk mfSDK) ChannelsByThing(token, thingID string, offset, limit uint64) (ChannelsPage, error) {
endpoint := fmt.Sprintf("things/%s/channels?offset=%d&limit=%d", thingID, offset, limit)
func (sdk mfSDK) ChannelsByThing(token, thingID string, offset, limit uint64, connected bool) (ChannelsPage, error) {
endpoint := fmt.Sprintf("things/%s/channels?offset=%d&limit=%d&connected=%t", thingID, offset, limit, connected)
url := createURL(sdk.baseURL, sdk.thingsPrefix, endpoint)

req, err := http.NewRequest(http.MethodGet, url, nil)
Expand Down
177 changes: 104 additions & 73 deletions pkg/sdk/go/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,101 +307,132 @@ func TestChannelsByThing(t *testing.T) {
MsgContentType: contentType,
TLSVerification: false,
}
var channels []sdk.Channel
mainfluxSDK := sdk.NewSDK(sdkConf)

th := sdk.Thing{Name: "test_device"}
tid, err := mainfluxSDK.CreateThing(th, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

for i := 1; i < 101; i++ {
ch := sdk.Channel{ID: strconv.Itoa(i), Name: "test"}
var n = 100
var chsDiscoNum = 1
var channels []sdk.Channel
for i := 1; i < n+1; i++ {
ch := sdk.Channel{
ID: strconv.Itoa(i),
Name: "test",
}
cid, err := mainfluxSDK.CreateChannel(ch, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

channels = append(channels, ch)

// Don't connect last Channel
if i == n+1-chsDiscoNum {
break
}

conIDs := sdk.ConnectionIDs{
ChannelIDs: []string{cid},
ThingIDs: []string{tid},
}
err = mainfluxSDK.Connect(conIDs, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
channels = append(channels, ch)
}

cases := []struct {
desc string
thing string
token string
offset uint64
limit uint64
err error
response []sdk.Channel
desc string
thing string
token string
offset uint64
limit uint64
connected bool
err error
response []sdk.Channel
}{
{
desc: "get a list of channels by thing",
thing: tid,
token: token,
offset: 0,
limit: 5,
err: nil,
response: channels[0:5],
},
{
desc: "get a list of channels by thing with invalid token",
thing: tid,
token: wrongValue,
offset: 0,
limit: 5,
err: createError(sdk.ErrFailedFetch, http.StatusForbidden),
response: nil,
},
{
desc: "get a list of channels by thing with empty token",
thing: tid,
token: "",
offset: 0,
limit: 5,
err: createError(sdk.ErrFailedFetch, http.StatusForbidden),
response: nil,
},
{
desc: "get a list of channels by thing with zero limit",
thing: tid,
token: token,
offset: 0,
limit: 0,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
{
desc: "get a list of channels by thing with limit greater than max",
thing: tid,
token: token,
offset: 0,
limit: 110,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
{
desc: "get a list of channels by thing with offset greater than max",
thing: tid,
token: token,
offset: 110,
limit: 5,
err: nil,
response: []sdk.Channel{},
},
{
desc: "get a list of channels by thing with invalid args (zero limit) and invalid token",
thing: tid,
token: wrongValue,
offset: 0,
limit: 0,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
desc: "get a list of channels by thing",
thing: tid,
token: token,
offset: 0,
limit: 5,
connected: true,
err: nil,
response: channels[0:5],
},
{
desc: "get a list of channels by thing with invalid token",
thing: tid,
token: wrongValue,
offset: 0,
limit: 5,
connected: true,
err: createError(sdk.ErrFailedFetch, http.StatusForbidden),
response: nil,
},
{
desc: "get a list of channels by thing with empty token",
thing: tid,
token: "",
offset: 0,
limit: 5,
connected: true,
err: createError(sdk.ErrFailedFetch, http.StatusForbidden),
response: nil,
},
{
desc: "get a list of channels by thing with zero limit",
thing: tid,
token: token,
offset: 0,
limit: 0,
connected: true,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
{
desc: "get a list of channels by thing with limit greater than max",
thing: tid,
token: token,
offset: 0,
limit: 110,
connected: true,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
{
desc: "get a list of channels by thing with offset greater than max",
thing: tid,
token: token,
offset: 110,
limit: 5,
connected: true,
err: nil,
response: []sdk.Channel{},
},
{
desc: "get a list of channels by thing with invalid args (zero limit) and invalid token",
thing: tid,
token: wrongValue,
offset: 0,
limit: 0,
connected: true,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
{
desc: "get a list of not connected channels by thing",
thing: tid,
token: token,
offset: 0,
limit: 100,
connected: false,
err: nil,
response: []sdk.Channel{channels[n-chsDiscoNum]},
},
}

for _, tc := range cases {
page, err := mainfluxSDK.ChannelsByThing(tc.token, tc.thing, tc.offset, tc.limit)
page, err := mainfluxSDK.ChannelsByThing(tc.token, tc.thing, tc.offset, tc.limit, tc.connected)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
assert.Equal(t, tc.response, page.Channels, fmt.Sprintf("%s: expected response channel %s, got %s", tc.desc, tc.response, page.Channels))
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ type SDK interface {
// Things returns page of things.
Things(token string, offset, limit uint64, name string) (ThingsPage, error)

// ThingsByChannel returns page of things that are connected to specified
// channel.
ThingsByChannel(token, chanID string, offset, limit uint64) (ThingsPage, error)
// ThingsByChannel returns page of things that are connected or not connected
// to specified channel.
ThingsByChannel(token, chanID string, offset, limit uint64, connected bool) (ThingsPage, error)

// Thing returns thing object by id.
Thing(id, token string) (Thing, error)
Expand All @@ -150,9 +150,9 @@ type SDK interface {
// Channels returns page of channels.
Channels(token string, offset, limit uint64, name string) (ChannelsPage, error)

// ChannelsByThing returns page of channels that are connected to specified
// thing.
ChannelsByThing(token, thingID string, offset, limit uint64) (ChannelsPage, error)
// ChannelsByThing returns page of channels that are connected or not connected
// to specified thing.
ChannelsByThing(token, thingID string, offset, limit uint64, connected bool) (ChannelsPage, error)

// Channel returns channel data by id.
Channel(id, token string) (Channel, error)
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (sdk mfSDK) Things(token string, offset, limit uint64, name string) (Things
return tp, nil
}

func (sdk mfSDK) ThingsByChannel(token, chanID string, offset, limit uint64) (ThingsPage, error) {
endpoint := fmt.Sprintf("channels/%s/things?offset=%d&limit=%d", chanID, offset, limit)
func (sdk mfSDK) ThingsByChannel(token, chanID string, offset, limit uint64, connected bool) (ThingsPage, error) {
endpoint := fmt.Sprintf("channels/%s/things?offset=%d&limit=%d&connected=%t", chanID, offset, limit, connected)
url := createURL(sdk.baseURL, sdk.thingsPrefix, endpoint)

req, err := http.NewRequest(http.MethodGet, url, nil)
Expand Down
Loading
0