Skip to content

Commit

Permalink
Add delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfield committed Jul 11, 2018
1 parent d193c98 commit 21eddb0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"log"
"strconv"

uptimerobot "github.com/bitfield/uptimerobot/pkg"
"github.com/spf13/cobra"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete a monitor",
Long: `Delete the monitor with the specified ID`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ID, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Fatal(err)
}
m := uptimerobot.Monitor{
ID: ID,
}
new, err := client.DeleteMonitor(m)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Monitor ID %d deleted\n", new.ID)
},
}

func init() {
RootCmd.AddCommand(deleteCmd)
}
14 changes: 14 additions & 0 deletions pkg/uptimerobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ func (c *Client) NewMonitor(m Monitor) (Monitor, error) {
return r.Monitor, nil
}

// DeleteMonitor takes a Monitor with the ID field set, and deletes the
// corresponding monitor. It returns a Monitor with the ID field set to the ID
// of the deleted monitor, or an error if the operation failed.
func (c *Client) DeleteMonitor(m Monitor) (Monitor, error) {
r := Response{}
p := Params{
"id": strconv.FormatInt(m.ID, 10),
}
if err := c.MakeAPICall("deleteMonitor", &r, p); err != nil {
return Monitor{}, err
}
return r.Monitor, nil
}

// MakeAPICall calls the UptimeRobot API with the specified verb and stores the
// returned data in the Response struct.
func (c *Client) MakeAPICall(verb string, r *Response, params Params) error {
Expand Down
18 changes: 18 additions & 0 deletions pkg/uptimerobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ func TestNewMonitor(t *testing.T) {
}
}

func TestDeleteMonitor(t *testing.T) {
c := New("dummy")
mockClient := MockHTTPClient{
DoFunc: fakeNewMonitorHandler,
}
c.http = &mockClient
want := Monitor{
ID: 777810874,
}
got, err := c.DeleteMonitor(want)
if err != nil {
t.Error(err)
}
if got.ID != want.ID {
t.Errorf("NewMonitor() => ID %d, want %d", got.ID, want.ID)
}
}

func TestBuildAlertContacts(t *testing.T) {
contacts := []string{"2353888", "0132759"}
want := "2353888_0_0-0132759_0_0"
Expand Down

0 comments on commit 21eddb0

Please sign in to comment.