Skip to content

Add device delete #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ Use this command to provision a device:

## Device commands

Once a device has been created thorugh the provisioning procedure, it can be deleted by using the following command:
`$ iot-cloud-cli device delete --id <deviceID>`

Devices currently present on Arduino IoT Cloud can be retrieved by using this command:
`$ iot-cloud-cli device list`
36 changes: 36 additions & 0 deletions cli/device/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package device

import (
"fmt"

"github.com/arduino/iot-cloud-cli/command/device"
"github.com/spf13/cobra"
)

var deleteFlags struct {
id string
}

func initDeleteCommand() *cobra.Command {
deleteCommand := &cobra.Command{
Use: "delete",
Short: "Delete a device",
Long: "Delete a device from Arduino IoT Cloud",
RunE: runDeleteCommand,
}
deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Device ID")
deleteCommand.MarkFlagRequired("id")
return deleteCommand
}

func runDeleteCommand(cmd *cobra.Command, args []string) error {
fmt.Printf("Deleting device %s\n", deleteFlags.id)

params := &device.DeleteParams{ID: deleteFlags.id}
err := device.Delete(params)
if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions cli/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewCommand() *cobra.Command {

deviceCommand.AddCommand(initCreateCommand())
deviceCommand.AddCommand(initListCommand())
deviceCommand.AddCommand(initDeleteCommand())

return deviceCommand
}
27 changes: 27 additions & 0 deletions command/device/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package device

import (
"github.com/arduino/iot-cloud-cli/internal/config"
"github.com/arduino/iot-cloud-cli/internal/iot"
)

// DeleteParams contains the parameters needed to
// delete a device from Arduino IoT Cloud.
type DeleteParams struct {
ID string
}

// Delete command is used to delete a device
// from Arduino IoT Cloud.
func Delete(params *DeleteParams) error {
conf, err := config.Retrieve()
if err != nil {
return err
}
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
if err != nil {
return err
}

return iotClient.DeleteDevice(params.ID)
}
12 changes: 12 additions & 0 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// Client can be used to perform actions on the arduino iot cloud.
type Client interface {
AddDevice(fqbn, name, serial, devType string) (string, error)
DeleteDevice(id string) error
ListDevices() ([]iotclient.ArduinoDevicev2, error)
AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
}
Expand Down Expand Up @@ -48,6 +49,17 @@ func (cl *client) AddDevice(fqbn, name, serial, dType string) (string, error) {
return dev.Id, nil
}

// DeleteDevice deletes the device corresponding to the passed ID
// from Arduino IoT Cloud.
func (cl *client) DeleteDevice(id string) error {
_, err := cl.api.DevicesV2Api.DevicesV2Delete(cl.ctx, id)
if err != nil {
err = fmt.Errorf("deleting device: %w", err)
return err
}
return nil
}

// ListDevices retrieves and returns a list of all Arduino IoT Cloud devices
// belonging to the user performing the request.
func (cl *client) ListDevices() ([]iotclient.ArduinoDevicev2, error) {
Expand Down