Skip to content

Commit da9d34a

Browse files
committed
Add thing delete command (#24)
Things can now be deleted with the following command: $ iot-cloud-cli thing delete --device-id <deviceID> * Add thing delete command * Update readme
1 parent 571e307 commit da9d34a

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ Print a *filtered* list of available things, print only things belonging to the
6666

6767
Print only the thing associated to the passed device:
6868

69-
`$ iot-cloud-cli thing list --device-id <deviceID>`
69+
`$ iot-cloud-cli thing list --device-id <deviceID>`
70+
71+
Delete a thing with the following command:
72+
73+
`$ iot-cloud-cli thing delete --device-id <deviceID>`

Diff for: cli/thing/delete.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package thing
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/arduino/iot-cloud-cli/command/thing"
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 thing",
18+
Long: "Delete a thing from Arduino IoT Cloud",
19+
RunE: runDeleteCommand,
20+
}
21+
deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Thing ID")
22+
deleteCommand.MarkFlagRequired("id")
23+
return deleteCommand
24+
}
25+
26+
func runDeleteCommand(cmd *cobra.Command, args []string) error {
27+
fmt.Printf("Deleting thing %s\n", deleteFlags.id)
28+
29+
params := &thing.DeleteParams{ID: deleteFlags.id}
30+
err := thing.Delete(params)
31+
if err != nil {
32+
return err
33+
}
34+
35+
fmt.Println("Thing successfully deleted")
36+
return nil
37+
}

Diff for: cli/thing/thing.go

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

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

1718
return thingCommand
1819
}

Diff for: command/thing/delete.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package thing
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 thing from Arduino IoT Cloud.
10+
type DeleteParams struct {
11+
ID string
12+
}
13+
14+
// Delete command is used to delete a thing
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.DeleteThing(params.ID)
27+
}

Diff for: internal/iot/client.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Client interface {
1616
ListDevices() ([]iotclient.ArduinoDevicev2, error)
1717
AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
1818
AddThing(thing *iotclient.Thing, force bool) (string, error)
19+
DeleteThing(id string) error
1920
GetThing(id string) (*iotclient.ArduinoThing, error)
2021
ListThings(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error)
2122
}
@@ -107,6 +108,16 @@ func (cl *client) AddThing(thing *iotclient.Thing, force bool) (string, error) {
107108
return newThing.Id, nil
108109
}
109110

111+
// DeleteThing deletes a thing from Arduino IoT Cloud.
112+
func (cl *client) DeleteThing(id string) error {
113+
_, err := cl.api.ThingsV2Api.ThingsV2Delete(cl.ctx, id, nil)
114+
if err != nil {
115+
err = fmt.Errorf("deleting thing: %w", err)
116+
return err
117+
}
118+
return nil
119+
}
120+
110121
// GetThing allows to retrieve a specific thing, given its id,
111122
// from Arduino IoT Cloud.
112123
func (cl *client) GetThing(id string) (*iotclient.ArduinoThing, error) {

0 commit comments

Comments
 (0)