diff --git a/README.md b/README.md index f40e446f..dc5f3cac 100644 --- a/README.md +++ b/README.md @@ -66,4 +66,8 @@ Print a *filtered* list of available things, print only things belonging to the Print only the thing associated to the passed device: -`$ iot-cloud-cli thing list --device-id ` \ No newline at end of file +`$ iot-cloud-cli thing list --device-id ` + +Delete a thing with the following command: + +`$ iot-cloud-cli thing delete --device-id ` \ No newline at end of file diff --git a/cli/thing/delete.go b/cli/thing/delete.go new file mode 100644 index 00000000..ef922edc --- /dev/null +++ b/cli/thing/delete.go @@ -0,0 +1,37 @@ +package thing + +import ( + "fmt" + + "github.com/arduino/iot-cloud-cli/command/thing" + "github.com/spf13/cobra" +) + +var deleteFlags struct { + id string +} + +func initDeleteCommand() *cobra.Command { + deleteCommand := &cobra.Command{ + Use: "delete", + Short: "Delete a thing", + Long: "Delete a thing from Arduino IoT Cloud", + RunE: runDeleteCommand, + } + deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Thing ID") + deleteCommand.MarkFlagRequired("id") + return deleteCommand +} + +func runDeleteCommand(cmd *cobra.Command, args []string) error { + fmt.Printf("Deleting thing %s\n", deleteFlags.id) + + params := &thing.DeleteParams{ID: deleteFlags.id} + err := thing.Delete(params) + if err != nil { + return err + } + + fmt.Println("Thing successfully deleted") + return nil +} diff --git a/cli/thing/thing.go b/cli/thing/thing.go index a0ab4259..329c7489 100644 --- a/cli/thing/thing.go +++ b/cli/thing/thing.go @@ -13,6 +13,7 @@ func NewCommand() *cobra.Command { thingCommand.AddCommand(initCreateCommand()) thingCommand.AddCommand(initListCommand()) + thingCommand.AddCommand(initDeleteCommand()) return thingCommand } diff --git a/command/thing/delete.go b/command/thing/delete.go new file mode 100644 index 00000000..d9ea31ee --- /dev/null +++ b/command/thing/delete.go @@ -0,0 +1,27 @@ +package thing + +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 thing from Arduino IoT Cloud. +type DeleteParams struct { + ID string +} + +// Delete command is used to delete a thing +// 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.DeleteThing(params.ID) +} diff --git a/internal/iot/client.go b/internal/iot/client.go index f4b7d463..51688def 100644 --- a/internal/iot/client.go +++ b/internal/iot/client.go @@ -16,6 +16,7 @@ type Client interface { ListDevices() ([]iotclient.ArduinoDevicev2, error) AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error) AddThing(thing *iotclient.Thing, force bool) (string, error) + DeleteThing(id string) error GetThing(id string) (*iotclient.ArduinoThing, error) ListThings(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error) } @@ -107,6 +108,16 @@ func (cl *client) AddThing(thing *iotclient.Thing, force bool) (string, error) { return newThing.Id, nil } +// DeleteThing deletes a thing from Arduino IoT Cloud. +func (cl *client) DeleteThing(id string) error { + _, err := cl.api.ThingsV2Api.ThingsV2Delete(cl.ctx, id, nil) + if err != nil { + err = fmt.Errorf("deleting thing: %w", err) + return err + } + return nil +} + // GetThing allows to retrieve a specific thing, given its id, // from Arduino IoT Cloud. func (cl *client) GetThing(id string) (*iotclient.ArduinoThing, error) {