diff --git a/README.md b/README.md index 84d1be2e..fe9748e5 100644 --- a/README.md +++ b/README.md @@ -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 ` + Devices currently present on Arduino IoT Cloud can be retrieved by using this command: `$ iot-cloud-cli device list` diff --git a/cli/device/delete.go b/cli/device/delete.go new file mode 100644 index 00000000..5b10765e --- /dev/null +++ b/cli/device/delete.go @@ -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 +} diff --git a/cli/device/device.go b/cli/device/device.go index b149bea5..020d4935 100644 --- a/cli/device/device.go +++ b/cli/device/device.go @@ -13,6 +13,7 @@ func NewCommand() *cobra.Command { deviceCommand.AddCommand(initCreateCommand()) deviceCommand.AddCommand(initListCommand()) + deviceCommand.AddCommand(initDeleteCommand()) return deviceCommand } diff --git a/command/device/delete.go b/command/device/delete.go new file mode 100644 index 00000000..527d1bf2 --- /dev/null +++ b/command/device/delete.go @@ -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) +} diff --git a/internal/iot/client.go b/internal/iot/client.go index 3fd584c4..16899c75 100644 --- a/internal/iot/client.go +++ b/internal/iot/client.go @@ -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) } @@ -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) {