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 - Fix connect CLI command and remove ConnectThing func from SDK #1051

Merged
merged 3 commits into from
Mar 3, 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
12 changes: 10 additions & 2 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ func (bs bootstrapService) UpdateConnections(key, id string, connections []strin
}

for _, c := range connect {
if err := bs.sdk.ConnectThing(id, c, key); err != nil {
conIDs := mfsdk.ConnectionIDs{
ChannelIDs: []string{c},
ThingIDs: []string{id},
}
if err := bs.sdk.Connect(conIDs, key); err != nil {
if err == mfsdk.ErrNotFound {
return ErrMalformedEntity
}
Expand Down Expand Up @@ -302,7 +306,11 @@ func (bs bootstrapService) ChangeState(key, id string, state State) error {
switch state {
case Active:
for _, c := range cfg.MFChannels {
if err := bs.sdk.ConnectThing(cfg.MFThing, c.ID, key); err != nil {
conIDs := mfsdk.ConnectionIDs{
ChannelIDs: []string{c.ID},
ThingIDs: []string{cfg.MFThing},
}
if err := bs.sdk.Connect(conIDs, key); err != nil {
return ErrThings
}
}
Expand Down
29 changes: 15 additions & 14 deletions cli/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,22 @@ var cmdProvision = []cobra.Command{
}

// Connect things to channels - first thing to both channels, second only to first
for i := 0; i < numThings; i++ {
if err := sdk.ConnectThing(things[i].ID, channels[i].ID, ut); err != nil {
logError(err)
return
}
conIDs := mfxsdk.ConnectionIDs{
ChannelIDs: []string{channels[0].ID, channels[1].ID},
ThingIDs: []string{things[0].ID},
}
if err := sdk.Connect(conIDs, ut); err != nil {
logError(err)
return
}

if i%2 == 0 {
if i+1 >= len(channels) {
break
}
if err := sdk.ConnectThing(things[i].ID, channels[i+1].ID, ut); err != nil {
logError(err)
return
}
}
conIDs = mfxsdk.ConnectionIDs{
ChannelIDs: []string{channels[0].ID},
ThingIDs: []string{things[1].ID},
}
if err := sdk.Connect(conIDs, ut); err != nil {
logError(err)
return
}

logJSON(user, ut, things, channels)
Expand Down
5 changes: 2 additions & 3 deletions cli/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ var cmdThings = []cobra.Command{
}

connIDs := mfxsdk.ConnectionIDs{
[]string{args[0]},
[]string{args[1]},
ChannelIDs: []string{args[1]},
ThingIDs: []string{args[0]},
}

if err := sdk.Connect(connIDs, args[2]); err != nil {
logError(err)
return
Expand Down
4 changes: 2 additions & 2 deletions sdk/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (sdk *MfxSDK) Channel(id, token string) (things.Channel, error)
func (sdk *MfxSDK) Channels(token string) ([]things.Channel, error)
Channels - gets all channels

func (sdk *MfxSDK) ConnectThing(thingID, chanID, token string) error
ConnectThing - connect thing to a channel
func (sdk *MfxSDK) Connect(struct{[]string, []string}, token string) error
Connect - connect things to channels

func (sdk *MfxSDK) CreateChannel(data, token string) (string, error)
CreateChannel - creates new channel and generates UUID
Expand Down
6 changes: 5 additions & 1 deletion sdk/go/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ func TestChannelsByThing(t *testing.T) {
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))
err = mainfluxSDK.ConnectThing(tid, cid, token)
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)
}
Expand Down
3 changes: 0 additions & 3 deletions sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ type SDK interface {
// DeleteThing removes existing thing.
DeleteThing(id, token string) error

// ConnectThing connects thing to specified channel by id.
ConnectThing(thingID, chanID, token string) error

// Connect bulk connects things to channels specified by id.
Connect(conns ConnectionIDs, token string) error

Expand Down
28 changes: 0 additions & 28 deletions sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,34 +283,6 @@ func (sdk mfSDK) DeleteThing(id, token string) error {
return nil
}

func (sdk mfSDK) ConnectThing(thingID, chanID, token string) error {
endpoint := fmt.Sprintf("%s/%s/%s/%s", channelsEndpoint, chanID, thingsEndpoint, thingID)
url := createURL(sdk.baseURL, sdk.thingsPrefix, endpoint)

req, err := http.NewRequest(http.MethodPut, url, nil)
if err != nil {
return err
}

resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
default:
return ErrFailedConnection
}
}

return nil
}

func (sdk mfSDK) Connect(connIDs ConnectionIDs, token string) error {
data, err := json.Marshal(connIDs)
if err != nil {
Expand Down
24 changes: 20 additions & 4 deletions sdk/go/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,25 @@ func TestThingsByChannel(t *testing.T) {
cid, err := mainfluxSDK.CreateChannel(ch, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

tIDs := []string{}

for i := 1; i < 101; i++ {
th := sdk.Thing{Name: "test_device", Metadata: metadata}
tid, err := mainfluxSDK.CreateThing(th, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
th.ID = tid
tIDs = append(tIDs, tid)
th.Key = fmt.Sprintf("%s%012d", keyPrefix, 2*i+1)
err = mainfluxSDK.ConnectThing(tid, cid, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
things = append(things, th)
}

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

cases := []struct {
desc string
channel string
Expand Down Expand Up @@ -691,7 +699,11 @@ func TestConnectThing(t *testing.T) {
}

for _, tc := range cases {
err := mainfluxSDK.ConnectThing(tc.thingID, tc.chanID, tc.token)
conIDs := sdk.ConnectionIDs{
ChannelIDs: []string{tc.chanID},
ThingIDs: []string{tc.thingID},
}
err := mainfluxSDK.Connect(conIDs, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
}
}
Expand Down Expand Up @@ -826,7 +838,11 @@ func TestDisconnectThing(t *testing.T) {
chanID1, err := mainfluxSDK.CreateChannel(channel, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

err = mainfluxSDK.ConnectThing(thingID, chanID1, token)
conIDs := sdk.ConnectionIDs{
ChannelIDs: []string{chanID1},
ThingIDs: []string{thingID},
}
err = mainfluxSDK.Connect(conIDs, token)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

chanID2, err := mainfluxSDK.CreateChannel(channel, otherToken)
Expand Down
37 changes: 16 additions & 21 deletions tools/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"log"
"math/big"
"os"
"sync"
"time"

"github.com/docker/docker/pkg/namesgenerator"
sdk "github.com/mainflux/mainflux/sdk/go"
)

const defPass = "123"
const defPass = "12345678"

// MfConn - structure describing Mainflux connection set
type MfConn struct {
Expand Down Expand Up @@ -120,8 +119,9 @@ func Provision(conf Config) {
}

// Create things and channels
things := make([]*sdk.Thing, conf.Num)
channels := make([]*string, conf.Num)
things := make([]sdk.Thing, conf.Num)
cIDs := []string{}
tIDs := []string{}

fmt.Println("# List of things that can be connected to MQTT broker")

Expand All @@ -132,7 +132,8 @@ func Provision(conf Config) {
}

thing, err := s.Thing(tid, token)
things[i] = &thing
things[i] = thing
tIDs = append(tIDs, tid)

if err != nil {
log.Fatalf("Failed to fetch the thing: %s", err.Error())
Expand All @@ -143,7 +144,7 @@ func Provision(conf Config) {
log.Fatalf("Failed to create the channel: %s", err.Error())
}

channels[i] = &cid
cIDs = append(cIDs, cid)

cert := ""
key := ""
Expand Down Expand Up @@ -211,25 +212,19 @@ func Provision(conf Config) {
fmt.Println("")
}

var wg sync.WaitGroup
fmt.Printf("# List of channels that things can publish to\n" +
"# each channel is connected to each thing from things list\n")
for i := 0; i < conf.Num; i++ {
// Creating a new routine for each connect
// might be heavy on the network.
go func(wg *sync.WaitGroup, i int) {
wg.Add(1)
defer wg.Done()

for j := 0; j < conf.Num; j++ {
if err := s.ConnectThing(things[j].ID, *channels[i], token); err != nil {
log.Fatalf("Failed to connect thing %s to channel %s: %s", things[j].ID, *channels[i], err)
}
}
}(&wg, i)
fmt.Printf("[[channels]]\nchannel_id = \"%s\"\n\n", *channels[i])
fmt.Printf("[[channels]]\nchannel_id = \"%s\"\n\n", cIDs[i])
}

conIDs := sdk.ConnectionIDs{
ChannelIDs: cIDs,
ThingIDs: tIDs,
}
if err := s.Connect(conIDs, token); err != nil {
log.Fatalf("Failed to connect things %s to channels %s: %s", conIDs.ThingIDs, conIDs.ChannelIDs, err)
}
wg.Wait()
}

func publicKey(priv interface{}) interface{} {
Expand Down