Skip to content

Commit 3d3fb73

Browse files
committed
Filter things by tags
1 parent 451db0a commit 3d3fb73

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

cli/thing/list.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var listFlags struct {
3333
ids []string
3434
deviceID string
3535
variables bool
36+
tags map[string]string
3637
}
3738

3839
func initListCommand() *cobra.Command {
@@ -47,6 +48,13 @@ func initListCommand() *cobra.Command {
4748
// list only the thing associated to the passed device id
4849
listCommand.Flags().StringVarP(&listFlags.deviceID, "device-id", "d", "", "ID of Device associated to the thing to be retrieved")
4950
listCommand.Flags().BoolVarP(&listFlags.variables, "show-variables", "s", false, "Show thing variables")
51+
// list only the things that have all the passed tags
52+
listCommand.Flags().StringToStringVar(
53+
&listFlags.tags,
54+
"tags",
55+
nil,
56+
"List of comma-separated tags. A tag has this format: <key>=<value>",
57+
)
5058
return listCommand
5159
}
5260

@@ -56,6 +64,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
5664
params := &thing.ListParams{
5765
IDs: listFlags.ids,
5866
Variables: listFlags.variables,
67+
Tags: listFlags.tags,
5968
}
6069
if listFlags.deviceID != "" {
6170
params.DeviceID = &listFlags.deviceID

command/thing/list.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
// ListParams contains the optional parameters needed
2626
// to filter the things to be listed.
2727
type ListParams struct {
28-
IDs []string // If IDs is not nil, only things belonging to that list are returned
29-
DeviceID *string // If DeviceID is provided, only the thing associated to that device is listed.
30-
Variables bool // If Variables is true, variable names are retrieved.
28+
IDs []string // If IDs is not nil, only things belonging to that list are returned
29+
DeviceID *string // If DeviceID is provided, only the thing associated to that device is listed.
30+
Variables bool // If Variables is true, variable names are retrieved.
31+
Tags map[string]string // If tags are provided, only things that have all these tags are listed.
3132
}
3233

3334
// List command is used to list
@@ -42,7 +43,7 @@ func List(params *ListParams) ([]ThingInfo, error) {
4243
return nil, err
4344
}
4445

45-
foundThings, err := iotClient.ThingList(params.IDs, params.DeviceID, params.Variables)
46+
foundThings, err := iotClient.ThingList(params.IDs, params.DeviceID, params.Variables, params.Tags)
4647
if err != nil {
4748
return nil, err
4849
}

internal/iot/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Client interface {
3838
ThingUpdate(id string, thing *iotclient.Thing, force bool) error
3939
ThingDelete(id string) error
4040
ThingShow(id string) (*iotclient.ArduinoThing, error)
41-
ThingList(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error)
41+
ThingList(ids []string, device *string, props bool, tags map[string]string) ([]iotclient.ArduinoThing, error)
4242
DashboardCreate(dashboard *iotclient.Dashboardv2) (*iotclient.ArduinoDashboardv2, error)
4343
DashboardShow(id string) (*iotclient.ArduinoDashboardv2, error)
4444
DashboardDelete(id string) error
@@ -186,7 +186,7 @@ func (cl *client) ThingShow(id string) (*iotclient.ArduinoThing, error) {
186186
}
187187

188188
// ThingList returns a list of things on Arduino IoT Cloud.
189-
func (cl *client) ThingList(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error) {
189+
func (cl *client) ThingList(ids []string, device *string, props bool, tags map[string]string) ([]iotclient.ArduinoThing, error) {
190190
opts := &iotclient.ThingsV2ListOpts{}
191191
opts.ShowProperties = optional.NewBool(props)
192192

@@ -198,6 +198,15 @@ func (cl *client) ThingList(ids []string, device *string, props bool) ([]iotclie
198198
opts.DeviceId = optional.NewString(*device)
199199
}
200200

201+
if tags != nil {
202+
t := make([]string, 0, len(tags))
203+
for key, val := range tags {
204+
// Use the 'key:value' format required from the backend
205+
t = append(t, key+":"+val)
206+
}
207+
opts.Tags = optional.NewInterface(t)
208+
}
209+
201210
things, _, err := cl.api.ThingsV2Api.ThingsV2List(cl.ctx, opts)
202211
if err != nil {
203212
err = fmt.Errorf("retrieving things, %w", errorDetail(err))

0 commit comments

Comments
 (0)