Skip to content

Add dashboard delete command #46

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
Oct 12, 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 @@ -123,4 +123,8 @@ The default OTA upload should complete in 10 minutes. Use `--deferred` flag to e

Print a list of available dashboards and their widgets by using this command:

`$ arduino-cloud-cli dashboard list --show-widgets`
`$ arduino-cloud-cli dashboard list --show-widgets`

Delete a dashboard with the following command:

`$ arduino-cloud-cli dashboard delete --id <dashboardID>`
1 change: 1 addition & 0 deletions cli/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func NewCommand() *cobra.Command {
}

dashboardCommand.AddCommand(initListCommand())
dashboardCommand.AddCommand(initDeleteCommand())

return dashboardCommand
}
57 changes: 57 additions & 0 deletions cli/dashboard/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 dashboard

import (
"os"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/command/dashboard"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var deleteFlags struct {
id string
}

func initDeleteCommand() *cobra.Command {
deleteCommand := &cobra.Command{
Use: "delete",
Short: "Delete a dashboard",
Long: "Delete a dashboard from Arduino IoT Cloud",
Run: runDeleteCommand,
}
deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Dashboard ID")
deleteCommand.MarkFlagRequired("id")
return deleteCommand
}

func runDeleteCommand(cmd *cobra.Command, args []string) {
logrus.Infof("Deleting dashboard %s\n", deleteFlags.id)

params := &dashboard.DeleteParams{ID: deleteFlags.id}
err := dashboard.Delete(params)
if err != nil {
feedback.Errorf("Error during dashboard delete: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

logrus.Info("Dashboard successfully deleted")
}
44 changes: 44 additions & 0 deletions command/dashboard/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 dashboard

import (
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
)

// DeleteParams contains the parameters needed to
// delete a dashboard from Arduino IoT Cloud.
type DeleteParams struct {
ID string
}

// Delete command is used to delete a dashboard
// 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.DashboardDelete(params.ID)
}
11 changes: 11 additions & 0 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Client interface {
ThingDelete(id string) error
ThingShow(id string) (*iotclient.ArduinoThing, error)
ThingList(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error)
DashboardDelete(id string) error
DashboardList() ([]iotclient.ArduinoDashboardv2, error)
}

Expand Down Expand Up @@ -213,6 +214,16 @@ func (cl *client) DashboardList() ([]iotclient.ArduinoDashboardv2, error) {
return dashboards, nil
}

// DashboardDelete deletes a dashboard from Arduino IoT Cloud.
func (cl *client) DashboardDelete(id string) error {
_, err := cl.api.DashboardsV2Api.DashboardsV2Delete(cl.ctx, id)
if err != nil {
err = fmt.Errorf("deleting dashboard: %w", errorDetail(err))
return err
}
return nil
}

func (cl *client) setup(client, secret string) error {
// Get the access token in exchange of client_id and client_secret
tok, err := token(client, secret)
Expand Down