Skip to content

Commit cbf2d62

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Add create-tags and delete-tags commands for device and thing (#53)
Introduce tags for things and devices. Implements the following commands: - device create-tags - device delete-tags - thing create-tags - thing delete-tags
1 parent 455121e commit cbf2d62

File tree

12 files changed

+534
-0
lines changed

12 files changed

+534
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ Once a device has been created thorugh the provisioning procedure, it can be del
7171
Devices currently present on Arduino IoT Cloud can be retrieved by using this command:
7272
`$ arduino-cloud-cli device list`
7373

74+
Add tags to a device. Tags should be passed as a comma-separated list of `<key>=<value>` items:
75+
76+
`$ arduino-cloud-cli device create-tags --id <deviceID> --tags <key0>=<value0>,<key1>=<value1>`
77+
78+
Delete specific tags of a device. The keys of the tags to delete should be passed in a comma-separated list of strings:
79+
80+
`$ arduino-cloud-cli device delete-tags --id <deviceID> --keys <key0>,<key1>`
81+
7482
## Thing commands
7583

7684
Things can be created starting from a template or by cloning another thing.
@@ -110,6 +118,15 @@ Bind a thing to an existing device:
110118

111119
`$ arduino-cloud-cli thing bind --id <thingID> --device-id <deviceID>`
112120

121+
Add tags to a thing. Tags should be passed as a comma-separated list of `<key>=<value>` items:
122+
123+
`$ arduino-cloud-cli thing create-tags --id <thingID> --tags <key0>=<value0>,<key1>=<value1>`
124+
125+
Delete specific tags of a thing. The keys of the tags to delete should be passed in a comma-separated list of strings:
126+
127+
`$ arduino-cloud-cli thing delete-tags --id <thingID> --keys <key0>,<key1>`
128+
129+
113130
## Ota commands
114131

115132
Perform an OTA firmware update. Note that the binary file (`.bin`) should be compiled using an arduino core that supports the specified device.

cli/device/device.go

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package device
1919

2020
import (
21+
"github.com/arduino/arduino-cloud-cli/cli/device/tag"
2122
"github.com/spf13/cobra"
2223
)
2324

@@ -31,6 +32,8 @@ func NewCommand() *cobra.Command {
3132
deviceCommand.AddCommand(initCreateCommand())
3233
deviceCommand.AddCommand(initListCommand())
3334
deviceCommand.AddCommand(initDeleteCommand())
35+
deviceCommand.AddCommand(tag.InitCreateTagsCommand())
36+
deviceCommand.AddCommand(tag.InitDeleteTagsCommand())
3437

3538
return deviceCommand
3639
}

cli/device/tag/create.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tag
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/command/tag"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
var createTagsFlags struct {
31+
id string
32+
tags map[string]string
33+
}
34+
35+
func InitCreateTagsCommand() *cobra.Command {
36+
createTagsCommand := &cobra.Command{
37+
Use: "create-tags",
38+
Short: "Create or overwrite tags on a device",
39+
Long: "Create or overwrite tags on a device of Arduino IoT Cloud",
40+
Run: runCreateTagsCommand,
41+
}
42+
createTagsCommand.Flags().StringVarP(&createTagsFlags.id, "id", "i", "", "Device ID")
43+
createTagsCommand.Flags().StringToStringVar(
44+
&createTagsFlags.tags,
45+
"tags",
46+
nil,
47+
"Comma-separated list of tags with format <key>=<value>.",
48+
)
49+
createTagsCommand.MarkFlagRequired("id")
50+
createTagsCommand.MarkFlagRequired("tags")
51+
return createTagsCommand
52+
}
53+
54+
func runCreateTagsCommand(cmd *cobra.Command, args []string) {
55+
logrus.Infof("Creating tags on device %s\n", createTagsFlags.id)
56+
57+
params := &tag.CreateTagsParams{
58+
ID: createTagsFlags.id,
59+
Tags: createTagsFlags.tags,
60+
Resource: tag.Device,
61+
}
62+
63+
err := tag.CreateTags(params)
64+
if err != nil {
65+
feedback.Errorf("Error during device create-tags: %v", err)
66+
os.Exit(errorcodes.ErrGeneric)
67+
}
68+
69+
logrus.Info("Tags successfully created")
70+
}

cli/device/tag/delete.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tag
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/command/tag"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
var deleteTagsFlags struct {
31+
id string
32+
keys []string
33+
}
34+
35+
func InitDeleteTagsCommand() *cobra.Command {
36+
deleteTagsCommand := &cobra.Command{
37+
Use: "delete-tags",
38+
Short: "Delete tags of a device",
39+
Long: "Delete tags of a device of Arduino IoT Cloud",
40+
Run: runDeleteTagsCommand,
41+
}
42+
43+
deleteTagsCommand.Flags().StringVarP(&deleteTagsFlags.id, "id", "i", "", "Device ID")
44+
deleteTagsCommand.Flags().StringSliceVarP(&deleteTagsFlags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete")
45+
46+
deleteTagsCommand.MarkFlagRequired("id")
47+
deleteTagsCommand.MarkFlagRequired("keys")
48+
return deleteTagsCommand
49+
}
50+
51+
func runDeleteTagsCommand(cmd *cobra.Command, args []string) {
52+
logrus.Infof("Deleting tags with keys %s\n", deleteTagsFlags.keys)
53+
54+
params := &tag.DeleteTagsParams{
55+
ID: deleteTagsFlags.id,
56+
Keys: deleteTagsFlags.keys,
57+
Resource: tag.Device,
58+
}
59+
60+
err := tag.DeleteTags(params)
61+
if err != nil {
62+
feedback.Errorf("Error during device delete-tags: %v", err)
63+
os.Exit(errorcodes.ErrGeneric)
64+
}
65+
66+
logrus.Info("Tags successfully deleted")
67+
}

cli/thing/tag/create.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tag
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/command/tag"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
var createTagsFlags struct {
31+
id string
32+
tags map[string]string
33+
}
34+
35+
func InitCreateTagsCommand() *cobra.Command {
36+
createTagsCommand := &cobra.Command{
37+
Use: "create-tags",
38+
Short: "Create or overwrite tags on a thing",
39+
Long: "Create or overwrite tags on a thing of Arduino IoT Cloud",
40+
Run: runCreateTagsCommand,
41+
}
42+
createTagsCommand.Flags().StringVarP(&createTagsFlags.id, "id", "i", "", "Thing ID")
43+
createTagsCommand.Flags().StringToStringVar(
44+
&createTagsFlags.tags,
45+
"tags",
46+
nil,
47+
"Comma-separated list of tags with format <key>=<value>.",
48+
)
49+
createTagsCommand.MarkFlagRequired("id")
50+
createTagsCommand.MarkFlagRequired("tags")
51+
return createTagsCommand
52+
}
53+
54+
func runCreateTagsCommand(cmd *cobra.Command, args []string) {
55+
logrus.Infof("Creating tags on thing %s\n", createTagsFlags.id)
56+
57+
params := &tag.CreateTagsParams{
58+
ID: createTagsFlags.id,
59+
Tags: createTagsFlags.tags,
60+
Resource: tag.Thing,
61+
}
62+
63+
err := tag.CreateTags(params)
64+
if err != nil {
65+
feedback.Errorf("Error during thing create-tags: %v", err)
66+
os.Exit(errorcodes.ErrGeneric)
67+
}
68+
69+
logrus.Info("Tags successfully created")
70+
}

cli/thing/tag/delete.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tag
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/command/tag"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
var deleteTagsFlags struct {
31+
id string
32+
keys []string
33+
}
34+
35+
func InitDeleteTagsCommand() *cobra.Command {
36+
deleteTagsCommand := &cobra.Command{
37+
Use: "delete-tags",
38+
Short: "Delete tags of a thing",
39+
Long: "Delete tags of a thing of Arduino IoT Cloud",
40+
Run: runDeleteTagsCommand,
41+
}
42+
43+
deleteTagsCommand.Flags().StringVarP(&deleteTagsFlags.id, "id", "i", "", "Thing ID")
44+
deleteTagsCommand.Flags().StringSliceVarP(&deleteTagsFlags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete")
45+
46+
deleteTagsCommand.MarkFlagRequired("id")
47+
deleteTagsCommand.MarkFlagRequired("keys")
48+
return deleteTagsCommand
49+
}
50+
51+
func runDeleteTagsCommand(cmd *cobra.Command, args []string) {
52+
logrus.Infof("Deleting tags with keys %s\n", deleteTagsFlags.keys)
53+
54+
params := &tag.DeleteTagsParams{
55+
ID: deleteTagsFlags.id,
56+
Keys: deleteTagsFlags.keys,
57+
Resource: tag.Thing,
58+
}
59+
60+
err := tag.DeleteTags(params)
61+
if err != nil {
62+
feedback.Errorf("Error during thing delete-tags: %v", err)
63+
os.Exit(errorcodes.ErrGeneric)
64+
}
65+
66+
logrus.Info("Tags successfully deleted")
67+
}

cli/thing/thing.go

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package thing
1919

2020
import (
21+
"github.com/arduino/arduino-cloud-cli/cli/thing/tag"
2122
"github.com/spf13/cobra"
2223
)
2324

@@ -34,6 +35,8 @@ func NewCommand() *cobra.Command {
3435
thingCommand.AddCommand(initDeleteCommand())
3536
thingCommand.AddCommand(initExtractCommand())
3637
thingCommand.AddCommand(initBindCommand())
38+
thingCommand.AddCommand(tag.InitCreateTagsCommand())
39+
thingCommand.AddCommand(tag.InitDeleteTagsCommand())
3740

3841
return thingCommand
3942
}

command/tag/create.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tag
19+
20+
import (
21+
"errors"
22+
23+
"github.com/arduino/arduino-cloud-cli/internal/config"
24+
"github.com/arduino/arduino-cloud-cli/internal/iot"
25+
)
26+
27+
// CreateTagsParams contains the parameters needed to create or overwrite
28+
// tags on a resource of Arduino IoT Cloud.
29+
type CreateTagsParams struct {
30+
ID string // Resource ID
31+
Tags map[string]string // Map of tags to create
32+
Resource ResourceType
33+
}
34+
35+
// CreateTags allows to create or overwrite tags
36+
// on a resource of Arduino IoT Cloud
37+
func CreateTags(params *CreateTagsParams) error {
38+
conf, err := config.Retrieve()
39+
if err != nil {
40+
return err
41+
}
42+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
43+
if err != nil {
44+
return err
45+
}
46+
47+
switch params.Resource {
48+
case Thing:
49+
err = iotClient.ThingTagsCreate(params.ID, params.Tags)
50+
case Device:
51+
err = iotClient.DeviceTagsCreate(params.ID, params.Tags)
52+
default:
53+
err = errors.New("passed Resource parameter is not valid")
54+
}
55+
return err
56+
}

0 commit comments

Comments
 (0)