Skip to content

Commit ee88678

Browse files
authored
Add device delete (#22)
This PR introduces the command to delete a device from arduino iot cloud Thus, now devices can be created, deleted and listed To delete a device, a device id should be specified $ iot-cloud-cli device delete -i <DEVICE_ID> * Add device delete command * Update readme
1 parent 1590b5f commit ee88678

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ Use this command to provision a device:
3535

3636
## Device commands
3737

38+
Once a device has been created thorugh the provisioning procedure, it can be deleted by using the following command:
39+
`$ iot-cloud-cli device delete --id <deviceID>`
40+
3841
Devices currently present on Arduino IoT Cloud can be retrieved by using this command:
3942
`$ iot-cloud-cli device list`

cli/device/delete.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package device
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/arduino/iot-cloud-cli/command/device"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var deleteFlags struct {
11+
id string
12+
}
13+
14+
func initDeleteCommand() *cobra.Command {
15+
deleteCommand := &cobra.Command{
16+
Use: "delete",
17+
Short: "Delete a device",
18+
Long: "Delete a device from Arduino IoT Cloud",
19+
RunE: runDeleteCommand,
20+
}
21+
deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Device ID")
22+
deleteCommand.MarkFlagRequired("id")
23+
return deleteCommand
24+
}
25+
26+
func runDeleteCommand(cmd *cobra.Command, args []string) error {
27+
fmt.Printf("Deleting device %s\n", deleteFlags.id)
28+
29+
params := &device.DeleteParams{ID: deleteFlags.id}
30+
err := device.Delete(params)
31+
if err != nil {
32+
return err
33+
}
34+
35+
return nil
36+
}

cli/device/device.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func NewCommand() *cobra.Command {
1313

1414
deviceCommand.AddCommand(initCreateCommand())
1515
deviceCommand.AddCommand(initListCommand())
16+
deviceCommand.AddCommand(initDeleteCommand())
1617

1718
return deviceCommand
1819
}

command/device/delete.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package device
2+
3+
import (
4+
"github.com/arduino/iot-cloud-cli/internal/config"
5+
"github.com/arduino/iot-cloud-cli/internal/iot"
6+
)
7+
8+
// DeleteParams contains the parameters needed to
9+
// delete a device from Arduino IoT Cloud.
10+
type DeleteParams struct {
11+
ID string
12+
}
13+
14+
// Delete command is used to delete a device
15+
// from Arduino IoT Cloud.
16+
func Delete(params *DeleteParams) error {
17+
conf, err := config.Retrieve()
18+
if err != nil {
19+
return err
20+
}
21+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
22+
if err != nil {
23+
return err
24+
}
25+
26+
return iotClient.DeleteDevice(params.ID)
27+
}

internal/iot/client.go

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// Client can be used to perform actions on the arduino iot cloud.
1111
type Client interface {
1212
AddDevice(fqbn, name, serial, devType string) (string, error)
13+
DeleteDevice(id string) error
1314
ListDevices() ([]iotclient.ArduinoDevicev2, error)
1415
AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
1516
}
@@ -48,6 +49,17 @@ func (cl *client) AddDevice(fqbn, name, serial, dType string) (string, error) {
4849
return dev.Id, nil
4950
}
5051

52+
// DeleteDevice deletes the device corresponding to the passed ID
53+
// from Arduino IoT Cloud.
54+
func (cl *client) DeleteDevice(id string) error {
55+
_, err := cl.api.DevicesV2Api.DevicesV2Delete(cl.ctx, id)
56+
if err != nil {
57+
err = fmt.Errorf("deleting device: %w", err)
58+
return err
59+
}
60+
return nil
61+
}
62+
5163
// ListDevices retrieves and returns a list of all Arduino IoT Cloud devices
5264
// belonging to the user performing the request.
5365
func (cl *client) ListDevices() ([]iotclient.ArduinoDevicev2, error) {

0 commit comments

Comments
 (0)