-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add create/delete commands for device/thing tags [IOT-1481] #53
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d610eb6
Add device create-tags command
polldo 2bee75c
Add device delete-tags command
polldo 1e724ae
Move tag commands in tag package
polldo 33cafe2
Add thing create-tags command
polldo 10beb80
Add thing delete-tags command
polldo 2dd7395
Update readme
polldo 53b7241
Update mocks
polldo 9b9c10f
Apply suggestions from code review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package tag | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/tag" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createTagsFlags struct { | ||
id string | ||
tags map[string]string | ||
} | ||
|
||
func InitCreateTagsCommand() *cobra.Command { | ||
createTagsCommand := &cobra.Command{ | ||
Use: "create-tags", | ||
Short: "Create or overwrite tags on a device", | ||
Long: "Create or overwrite tags on a device of Arduino IoT Cloud", | ||
Run: runCreateTagsCommand, | ||
} | ||
createTagsCommand.Flags().StringVarP(&createTagsFlags.id, "id", "i", "", "Device ID") | ||
createTagsCommand.Flags().StringToStringVar( | ||
&createTagsFlags.tags, | ||
"tags", | ||
nil, | ||
"Comma-separated list of tags with format <key>=<value>.", | ||
) | ||
createTagsCommand.MarkFlagRequired("id") | ||
createTagsCommand.MarkFlagRequired("tags") | ||
return createTagsCommand | ||
} | ||
|
||
func runCreateTagsCommand(cmd *cobra.Command, args []string) { | ||
logrus.Infof("Creating tags on device %s\n", createTagsFlags.id) | ||
|
||
params := &tag.CreateTagsParams{ | ||
ID: createTagsFlags.id, | ||
Tags: createTagsFlags.tags, | ||
Resource: tag.Device, | ||
} | ||
|
||
err := tag.CreateTags(params) | ||
if err != nil { | ||
feedback.Errorf("Error during device create-tags: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
logrus.Info("Tags successfully created") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package tag | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/tag" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var deleteTagsFlags struct { | ||
id string | ||
keys []string | ||
} | ||
|
||
func InitDeleteTagsCommand() *cobra.Command { | ||
deleteTagsCommand := &cobra.Command{ | ||
Use: "delete-tags", | ||
Short: "Delete tags of a device", | ||
Long: "Delete tags of a device of Arduino IoT Cloud", | ||
Run: runDeleteTagsCommand, | ||
} | ||
|
||
deleteTagsCommand.Flags().StringVarP(&deleteTagsFlags.id, "id", "i", "", "Device ID") | ||
deleteTagsCommand.Flags().StringSliceVarP(&deleteTagsFlags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete") | ||
|
||
deleteTagsCommand.MarkFlagRequired("id") | ||
deleteTagsCommand.MarkFlagRequired("keys") | ||
return deleteTagsCommand | ||
} | ||
|
||
func runDeleteTagsCommand(cmd *cobra.Command, args []string) { | ||
logrus.Infof("Deleting tags with keys %s\n", deleteTagsFlags.keys) | ||
|
||
params := &tag.DeleteTagsParams{ | ||
ID: deleteTagsFlags.id, | ||
Keys: deleteTagsFlags.keys, | ||
Resource: tag.Device, | ||
} | ||
|
||
err := tag.DeleteTags(params) | ||
if err != nil { | ||
feedback.Errorf("Error during device delete-tags: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
logrus.Info("Tags successfully deleted") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package tag | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/tag" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createTagsFlags struct { | ||
id string | ||
tags map[string]string | ||
} | ||
|
||
func InitCreateTagsCommand() *cobra.Command { | ||
createTagsCommand := &cobra.Command{ | ||
Use: "create-tags", | ||
Short: "Create or overwrite tags on a thing", | ||
Long: "Create or overwrite tags on a thing of Arduino IoT Cloud", | ||
Run: runCreateTagsCommand, | ||
} | ||
createTagsCommand.Flags().StringVarP(&createTagsFlags.id, "id", "i", "", "Thing ID") | ||
createTagsCommand.Flags().StringToStringVar( | ||
&createTagsFlags.tags, | ||
"tags", | ||
nil, | ||
"Comma-separated list of tags with format <key>=<value>.", | ||
) | ||
createTagsCommand.MarkFlagRequired("id") | ||
createTagsCommand.MarkFlagRequired("tags") | ||
return createTagsCommand | ||
} | ||
|
||
func runCreateTagsCommand(cmd *cobra.Command, args []string) { | ||
logrus.Infof("Creating tags on thing %s\n", createTagsFlags.id) | ||
|
||
params := &tag.CreateTagsParams{ | ||
ID: createTagsFlags.id, | ||
Tags: createTagsFlags.tags, | ||
Resource: tag.Thing, | ||
} | ||
|
||
err := tag.CreateTags(params) | ||
if err != nil { | ||
feedback.Errorf("Error during thing create-tags: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
logrus.Info("Tags successfully created") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package tag | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/tag" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var deleteTagsFlags struct { | ||
id string | ||
keys []string | ||
} | ||
|
||
func InitDeleteTagsCommand() *cobra.Command { | ||
deleteTagsCommand := &cobra.Command{ | ||
Use: "delete-tags", | ||
Short: "Delete tags of a thing", | ||
Long: "Delete tags of a thing of Arduino IoT Cloud", | ||
Run: runDeleteTagsCommand, | ||
} | ||
|
||
deleteTagsCommand.Flags().StringVarP(&deleteTagsFlags.id, "id", "i", "", "Thing ID") | ||
deleteTagsCommand.Flags().StringSliceVarP(&deleteTagsFlags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete") | ||
|
||
deleteTagsCommand.MarkFlagRequired("id") | ||
deleteTagsCommand.MarkFlagRequired("keys") | ||
return deleteTagsCommand | ||
} | ||
|
||
func runDeleteTagsCommand(cmd *cobra.Command, args []string) { | ||
logrus.Infof("Deleting tags with keys %s\n", deleteTagsFlags.keys) | ||
|
||
params := &tag.DeleteTagsParams{ | ||
ID: deleteTagsFlags.id, | ||
Keys: deleteTagsFlags.keys, | ||
Resource: tag.Thing, | ||
} | ||
|
||
err := tag.DeleteTags(params) | ||
if err != nil { | ||
feedback.Errorf("Error during thing delete-tags: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
logrus.Info("Tags successfully deleted") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package tag | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/arduino/arduino-cloud-cli/internal/config" | ||
"github.com/arduino/arduino-cloud-cli/internal/iot" | ||
) | ||
|
||
// CreateTagsParams contains the parameters needed to create or overwrite | ||
// tags on a resource of Arduino IoT Cloud. | ||
type CreateTagsParams struct { | ||
ID string // Resource ID | ||
Tags map[string]string // Map of tags to create | ||
Resource ResourceType | ||
} | ||
|
||
// CreateTags allows to create or overwrite tags | ||
// on a resource of Arduino IoT Cloud | ||
func CreateTags(params *CreateTagsParams) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add a validation of What do you think? |
||
conf, err := config.Retrieve() | ||
if err != nil { | ||
return err | ||
polldo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
iotClient, err := iot.NewClient(conf.Client, conf.Secret) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch params.Resource { | ||
case Thing: | ||
err = iotClient.ThingTagsCreate(params.ID, params.Tags) | ||
case Device: | ||
err = iotClient.DeviceTagsCreate(params.ID, params.Tags) | ||
default: | ||
err = errors.New("passed Resource parameter is not valid") | ||
} | ||
return err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.