Skip to content

Commit

Permalink
NOISSUE - Refactor provision tool (#1189)
Browse files Browse the repository at this point in the history
* Use bulk sdk functions for generating Things and Channels
Add prefix option

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Update readme
Remove dead code
Rename variable

Signed-off-by: Ivan Milošević <iva@blokovi.com>
  • Loading branch information
blokovi committed Jun 1, 2020
1 parent 8906943 commit a5fb55c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions tools/provision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Flags:
-p, --password string mainflux users password
--ssl create certificates for mTLS access
-u, --username string mainflux user
--prefix string name prefix for things and channels
```

Example:
Expand Down
1 change: 1 addition & 0 deletions tools/provision/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Complete documentation is available at https://mainflux.readthedocs.io`,

// Root Flags
rootCmd.PersistentFlags().StringVarP(&pconf.Host, "host", "", "https://localhost", "address for mainflux instance")
rootCmd.PersistentFlags().StringVarP(&pconf.Prefix, "prefix", "", "", "name prefix for things and channels")
rootCmd.PersistentFlags().StringVarP(&pconf.Username, "username", "u", "", "mainflux user")
rootCmd.PersistentFlags().StringVarP(&pconf.Password, "password", "p", "", "mainflux users password")
rootCmd.PersistentFlags().IntVarP(&pconf.Num, "num", "", 10, "number of channels and things to create and connect")
Expand Down
38 changes: 21 additions & 17 deletions tools/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,36 @@ func Provision(conf Config) {

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

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

for i := 0; i < conf.Num; i++ {
tid, err := s.CreateThing(sdk.Thing{Name: fmt.Sprintf("%s-thing-%d", conf.Prefix, i)}, token)
if err != nil {
log.Fatalf("Failed to create the thing: %s", err.Error())
}
things[i] = sdk.Thing{Name: fmt.Sprintf("%s-thing-%d", conf.Prefix, i)}
channels[i] = sdk.Channel{Name: fmt.Sprintf("%s-channel-%d", conf.Prefix, i)}
}

thing, err := s.Thing(tid, token)
things[i] = thing
tIDs = append(tIDs, tid)
things, err = s.CreateThings(things, token)
if err != nil {
log.Fatalf("Failed to create the things: %s", err.Error())
}

if err != nil {
log.Fatalf("Failed to fetch the thing: %s", err.Error())
}
channels, err = s.CreateChannels(channels, token)
if err != nil {
log.Fatalf("Failed to create the chennels: %s", err.Error())
}

cid, err := s.CreateChannel(sdk.Channel{Name: fmt.Sprintf("%s-channel-%d", conf.Prefix, i)}, token)
if err != nil {
log.Fatalf("Failed to create the channel: %s", err.Error())
}
for _, t := range things {
tIDs = append(tIDs, t.ID)
}

cIDs = append(cIDs, cid)
for _, c := range channels {
cIDs = append(cIDs, c.ID)
}

for i := 0; i < conf.Num; i++ {
cert := ""
key := ""

Expand All @@ -170,7 +174,7 @@ func Provision(conf Config) {
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Mainflux"},
CommonName: thing.Key,
CommonName: things[i].Key,
OrganizationalUnit: []string{"mainflux"},
},
NotBefore: notBefore,
Expand Down Expand Up @@ -204,7 +208,7 @@ func Provision(conf Config) {
}

// Print output
fmt.Printf("[[things]]\nthing_id = \"%s\"\nthing_key = \"%s\"\n", tid, thing.Key)
fmt.Printf("[[things]]\nthing_id = \"%s\"\nthing_key = \"%s\"\n", things[i].ID, things[i].Key)
if conf.SSL {
fmt.Printf("mtls_cert = \"\"\"%s\"\"\"\n", cert)
fmt.Printf("mtls_key = \"\"\"%s\"\"\"\n", key)
Expand Down

0 comments on commit a5fb55c

Please sign in to comment.