Skip to content

Commit 28ad672

Browse files
committed
Add device create-generic command
1 parent ae96aa3 commit 28ad672

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

cli/device/creategeneric.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"fmt"
22+
"os"
23+
24+
"github.com/arduino/arduino-cli/cli/errorcodes"
25+
"github.com/arduino/arduino-cli/cli/feedback"
26+
"github.com/arduino/arduino-cloud-cli/command/device"
27+
"github.com/sirupsen/logrus"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
var createGenericFlags struct {
32+
name string
33+
fqbn string
34+
}
35+
36+
func initCreateGenericCommand() *cobra.Command {
37+
createGenericCommand := &cobra.Command{
38+
Use: "create-generic",
39+
Short: "Create a generic device",
40+
Long: "Create a generic device for Arduino IoT Cloud",
41+
Run: runCreateGenericCommand,
42+
}
43+
createGenericCommand.Flags().StringVarP(&createGenericFlags.name, "name", "n", "", "Device name")
44+
createGenericCommand.Flags().StringVarP(&createGenericFlags.fqbn, "fqbn", "b", "generic", "Device fqbn")
45+
createGenericCommand.MarkFlagRequired("name")
46+
return createGenericCommand
47+
}
48+
49+
func runCreateGenericCommand(cmd *cobra.Command, args []string) {
50+
logrus.Infof("Creating generic device with name %s", createGenericFlags.name)
51+
52+
params := &device.CreateGenericParams{
53+
Name: createGenericFlags.name,
54+
Fqbn: createGenericFlags.fqbn,
55+
}
56+
57+
dev, err := device.CreateGeneric(params)
58+
if err != nil {
59+
feedback.Errorf("Error during device create-generic: %v", err)
60+
os.Exit(errorcodes.ErrGeneric)
61+
}
62+
63+
feedback.PrintResult(createGenericResult{dev})
64+
}
65+
66+
type createGenericResult struct {
67+
device *device.DeviceGenericInfo
68+
}
69+
70+
func (r createGenericResult) Data() interface{} {
71+
return r.device
72+
}
73+
74+
func (r createGenericResult) String() string {
75+
return fmt.Sprintf(
76+
"id: %s\nsecret-key: %s\nname: %s\nboard: %s\nserial-number: %s\nfqbn: %s",
77+
r.device.Name,
78+
r.device.Password,
79+
r.device.ID,
80+
r.device.Board,
81+
r.device.Serial,
82+
r.device.FQBN,
83+
)
84+
}

cli/device/device.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func NewCommand() *cobra.Command {
3636
deviceCommand.AddCommand(tag.InitDeleteTagsCommand())
3737
deviceCommand.AddCommand(initListFrequencyPlansCommand())
3838
deviceCommand.AddCommand(initCreateLoraCommand())
39+
deviceCommand.AddCommand(initCreateGenericCommand())
3940

4041
return deviceCommand
4142
}

command/device/creategeneric.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/arduino/arduino-cloud-cli/internal/config"
24+
"github.com/arduino/arduino-cloud-cli/internal/iot"
25+
)
26+
27+
const (
28+
genericDType = "login_and_secretkey_wifi"
29+
)
30+
31+
// CreateGenericParams contains the parameters needed
32+
// to create a new generic device.
33+
type CreateGenericParams struct {
34+
Name string // Device name
35+
Fqbn string // Board FQBN
36+
}
37+
38+
// DeviceGenericInfo contains the most interesting
39+
// parameters of a generic Arduino IoT Cloud device.
40+
type DeviceGenericInfo struct {
41+
DeviceInfo
42+
Password string `json:"secret-key"`
43+
}
44+
45+
// CreateGeneric command is used to add a new generic device to Arduino IoT Cloud.
46+
func CreateGeneric(params *CreateGenericParams) (*DeviceGenericInfo, error) {
47+
conf, err := config.Retrieve()
48+
if err != nil {
49+
return nil, err
50+
}
51+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
dev, err := iotClient.DeviceCreate(params.Fqbn, params.Name, "", genericDType)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
pass, err := iotClient.DevicePassShow(dev.Id)
62+
if err != nil {
63+
if errDel := iotClient.DeviceDelete(dev.Id); errDel != nil {
64+
return nil, fmt.Errorf(
65+
"device was successfully created on IoT-API but " +
66+
"now we can't fetch its secret key nor delete it - please check " +
67+
"it on the web application.\n\nFetch error: " + err.Error() +
68+
"\nDeletion error: " + errDel.Error(),
69+
)
70+
}
71+
return nil, fmt.Errorf("cannot create generic device: %w", err)
72+
}
73+
74+
devInfo := &DeviceGenericInfo{
75+
DeviceInfo: DeviceInfo{
76+
Name: dev.Name,
77+
ID: dev.Id,
78+
Board: dev.Type,
79+
Serial: dev.Serial,
80+
FQBN: dev.Fqbn,
81+
},
82+
Password: pass.SuggestedPassword,
83+
}
84+
return devInfo, nil
85+
}

internal/iot/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Client interface {
3434
DeviceList(tags map[string]string) ([]iotclient.ArduinoDevicev2, error)
3535
DeviceShow(id string) (*iotclient.ArduinoDevicev2, error)
3636
DeviceOTA(id string, file *os.File, expireMins int) error
37+
DevicePassShow(id string) (*iotclient.ArduinoDevicev2Pass, error)
3738
DeviceTagsCreate(id string, tags map[string]string) error
3839
DeviceTagsDelete(id string, keys []string) error
3940
LoraFrequencyPlansList() ([]iotclient.ArduinoLorafreqplanv1, error)
@@ -105,6 +106,17 @@ func (cl *client) DeviceLoraCreate(name, serial, devType, eui, freq string) (*io
105106
return &dev, nil
106107
}
107108

109+
// DevicePassShow fetches and returns the password associated to a device on Arduino IoT Cloud.
110+
func (cl *client) DevicePassShow(id string) (*iotclient.ArduinoDevicev2Pass, error) {
111+
opts := &iotclient.DevicesV2PassGetOpts{SuggestedPassword: optional.NewBool(true)}
112+
pass, _, err := cl.api.DevicesV2PassApi.DevicesV2PassGet(cl.ctx, id, opts)
113+
if err != nil {
114+
err = fmt.Errorf("fetching device password: %w", errorDetail(err))
115+
return nil, err
116+
}
117+
return &pass, nil
118+
}
119+
108120
// DeviceDelete deletes the device corresponding to the passed ID
109121
// from Arduino IoT Cloud.
110122
func (cl *client) DeviceDelete(id string) error {

0 commit comments

Comments
 (0)