Skip to content

Commit

Permalink
NOISSUE - Fix provision test command (#2182)
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo authored Apr 17, 2024
1 parent 28b4087 commit 3cfcf14
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 204 deletions.
12 changes: 11 additions & 1 deletion cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
defDomainsURL string = defURL + ":8189"
defCertsURL string = defURL + ":9019"
defInvitationsURL string = defURL + ":9020"
defHTTPURL string = defURL + ":9016/http"
defHTTPURL string = defURL + ":8008"
defTLSVerification bool = false
defOffset string = "0"
defLimit string = "10"
Expand All @@ -42,6 +42,7 @@ type remotes struct {
HTTPAdapterURL string `toml:"http_adapter_url"`
BootstrapURL string `toml:"bootstrap_url"`
CertsURL string `toml:"certs_url"`
InvitationsURL string `toml:"invitations_url"`
TLSVerification bool `toml:"tls_verification"`
}

Expand Down Expand Up @@ -110,6 +111,7 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
HTTPAdapterURL: defHTTPURL,
BootstrapURL: defBootstrapURL,
CertsURL: defCertsURL,
InvitationsURL: defInvitationsURL,
TLSVerification: defTLSVerification,
},
Filter: filter{
Expand Down Expand Up @@ -176,6 +178,10 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
sdkConf.ReaderURL = config.Remotes.ReaderURL
}

if sdkConf.DomainsURL == "" && config.Remotes.DomainsURL != "" {
sdkConf.DomainsURL = config.Remotes.DomainsURL
}

if sdkConf.HTTPAdapterURL == "" && config.Remotes.HTTPAdapterURL != "" {
sdkConf.HTTPAdapterURL = config.Remotes.HTTPAdapterURL
}
Expand All @@ -188,6 +194,10 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
sdkConf.CertsURL = config.Remotes.CertsURL
}

if sdkConf.InvitationsURL == "" && config.Remotes.InvitationsURL != "" {
sdkConf.InvitationsURL = config.Remotes.InvitationsURL
}

sdkConf.TLSVerification = config.Remotes.TLSVerification || sdkConf.TLSVerification

return sdkConf, nil
Expand Down
72 changes: 55 additions & 17 deletions cli/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"path/filepath"
"time"

"github.com/0x6flab/namegenerator"
mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/spf13/cobra"
)

Expand All @@ -24,6 +24,11 @@ const (
csvExt = ".csv"
)

var (
msgFormat = `[{"bn":"provision:", "bu":"V", "t": %d, "bver":5, "n":"voltage", "u":"V", "v":%d}]`
namesgenerator = namegenerator.NewGenerator()
)

var cmdProvision = []cobra.Command{
{
Use: "things <things_file> <user_token>",
Expand Down Expand Up @@ -71,11 +76,16 @@ var cmdProvision = []cobra.Command{
return
}

channels, err = sdk.CreateChannels(channels, args[1])
if err != nil {
logError(err)
return
var chs []mgxsdk.Channel
for _, c := range channels {
c, err = sdk.CreateChannel(c, args[1])
if err != nil {
logError(err)
return
}
chs = append(chs, c)
}
channels = chs

logJSON(channels)
},
Expand Down Expand Up @@ -122,9 +132,8 @@ var cmdProvision = []cobra.Command{
return
}

rand.Seed(time.Now().UnixNano())
name := namesgenerator.GetRandomName(0)
// Create test user
name := namesgenerator.Generate()
user := mgxsdk.User{
Name: name,
Credentials: mgxsdk.Credentials{
Expand All @@ -146,11 +155,28 @@ var cmdProvision = []cobra.Command{
return
}

// create domain
domain := mgxsdk.Domain{
Name: fmt.Sprintf("%s-domain", name),
Status: mgxsdk.EnabledStatus,
}
domain, err = sdk.CreateDomain(domain, ut.AccessToken)
if err != nil {
logError(err)
return
}

// domain login
ut, err = sdk.CreateToken(mgxsdk.Login{Identity: user.Credentials.Identity, Secret: user.Credentials.Secret, DomainID: domain.ID})
if err != nil {
logError(err)
return
}

// Create things
for i := 0; i < numThings; i++ {
n := fmt.Sprintf("d%d", i)
t := mgxsdk.Thing{
Name: n,
Name: fmt.Sprintf("%s-thing-%d", name, i),
Status: mgxsdk.EnabledStatus,
}

Expand All @@ -164,20 +190,18 @@ var cmdProvision = []cobra.Command{

// Create channels
for i := 0; i < numChan; i++ {
n := fmt.Sprintf("c%d", i)

c := mgxsdk.Channel{
Name: n,
Name: fmt.Sprintf("%s-channel-%d", name, i),
Status: mgxsdk.EnabledStatus,
}
c, err = sdk.CreateChannel(c, ut.AccessToken)
if err != nil {
logError(err)
return
}

channels = append(channels, c)
}
channels, err = sdk.CreateChannels(channels, ut.AccessToken)
if err != nil {
logError(err)
return
}

// Connect things to channels - first thing to both channels, second only to first
conIDs := mgxsdk.Connection{
Expand Down Expand Up @@ -207,6 +231,20 @@ var cmdProvision = []cobra.Command{
return
}

// send message to test connectivity
if err := sdk.SendMessage(channels[0].ID, fmt.Sprintf(msgFormat, time.Now().Unix(), rand.Int()), things[0].Credentials.Secret); err != nil {
logError(err)
return
}
if err := sdk.SendMessage(channels[0].ID, fmt.Sprintf(msgFormat, time.Now().Unix(), rand.Int()), things[1].Credentials.Secret); err != nil {
logError(err)
return
}
if err := sdk.SendMessage(channels[1].ID, fmt.Sprintf(msgFormat, time.Now().Unix(), rand.Int()), things[0].Credentials.Secret); err != nil {
logError(err)
return
}

logJSON(user, ut, things, channels)
},
},
Expand Down
3 changes: 2 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ user_token = ""
bootstrap_url = "http://localhost:9013"
certs_url = "http://localhost:9019"
domains_url = "http://localhost:8189"
http_adapter_url = "http://localhost:9016/http"
http_adapter_url = "http://localhost:8008"
invitations_url = "http://localhost:9020"
reader_url = "http://localhost:9011"
things_url = "http://localhost:9000"
tls_verification = false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/authzed/grpcutil v0.0.0-20240123194739-2ea1e3d2d98b
github.com/caarlos0/env/v10 v10.0.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/docker/docker v26.0.1+incompatible
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/fatih/color v1.16.0
github.com/fiorix/go-smpp v0.0.0-20210403173735-2894b96e70ba
Expand Down Expand Up @@ -82,6 +81,7 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v26.0.0+incompatible // indirect
github.com/docker/docker v26.0.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dsnet/golib/memfile v1.0.0 // indirect
Expand Down
21 changes: 0 additions & 21 deletions pkg/sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,6 @@ func (sdk mgSDK) CreateChannel(c Channel, token string) (Channel, errors.SDKErro
return c, nil
}

func (sdk mgSDK) CreateChannels(chs []Channel, token string) ([]Channel, errors.SDKError) {
data, err := json.Marshal(chs)
if err != nil {
return []Channel{}, errors.NewSDKError(err)
}

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

_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
if sdkerr != nil {
return []Channel{}, sdkerr
}

var ccr createChannelsRes
if err := json.Unmarshal(body, &ccr); err != nil {
return []Channel{}, errors.NewSDKError(err)
}

return ccr.Channels, nil
}

func (sdk mgSDK) Channels(pm PageMetadata, token string) (ChannelsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.thingsURL, channelsEndpoint, pm)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (sdk mgSDK) CreateDomain(domain Domain, token string) (Domain, errors.SDKEr

url := fmt.Sprintf("%s/%s", sdk.domainsURL, domainsEndpoint)

_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusCreated)
if sdkerr != nil {
return Domain{}, sdkerr
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/sdk/go/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ type createThingsRes struct {
Things []Thing `json:"things"`
}

type createChannelsRes struct {
Channels []Channel `json:"channels"`
}

type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Expand Down
21 changes: 0 additions & 21 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,27 +633,6 @@ type SDK interface {
// fmt.Println(channel)
CreateChannel(channel Channel, token string) (Channel, errors.SDKError)

// CreateChannels registers new channels and returns their ids.
//
// example:
// channels := []sdk.Channel{
// {
// Name: "My Channel 1",
// Metadata: sdk.Metadata{
// "key": "value",
// },
// },
// {
// Name: "My Channel 2",
// Metadata: sdk.Metadata{
// "key": "value",
// },
// },
// }
// channels, _ := sdk.CreateChannels(channels, "token")
// fmt.Println(channels)
CreateChannels(channels []Channel, token string) ([]Channel, errors.SDKError)

// Channels returns page of channels.
//
// example:
Expand Down
32 changes: 0 additions & 32 deletions pkg/sdk/mocks/sdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3cfcf14

Please sign in to comment.