Skip to content

Add thing delete command #24

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 26, 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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <deviceID>`
`$ iot-cloud-cli thing list --device-id <deviceID>`

Delete a thing with the following command:

`$ iot-cloud-cli thing delete --device-id <deviceID>`
37 changes: 37 additions & 0 deletions cli/thing/delete.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cli/thing/thing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewCommand() *cobra.Command {

thingCommand.AddCommand(initCreateCommand())
thingCommand.AddCommand(initListCommand())
thingCommand.AddCommand(initDeleteCommand())

return thingCommand
}
27 changes: 27 additions & 0 deletions command/thing/delete.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down