diff --git a/tools/provision/README.md b/tools/provision/README.md index ebc0882dbc..f52fc42247 100644 --- a/tools/provision/README.md +++ b/tools/provision/README.md @@ -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: diff --git a/tools/provision/cmd/main.go b/tools/provision/cmd/main.go index ce313cae04..ed16ab000a 100644 --- a/tools/provision/cmd/main.go +++ b/tools/provision/cmd/main.go @@ -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") diff --git a/tools/provision/provision.go b/tools/provision/provision.go index 42c882d3bf..fdacef90c1 100644 --- a/tools/provision/provision.go +++ b/tools/provision/provision.go @@ -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 := "" @@ -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, @@ -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)