Skip to content

Commit

Permalink
Add 'pause/start' commands (fixes #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfield committed Dec 8, 2018
1 parent f295a37 commit 8a52db1
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
35 changes: 35 additions & 0 deletions cmd/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"log"
"strconv"

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

var pauseCmd = &cobra.Command{
Use: "pause",
Short: "pause a monitor",
Long: `Pause 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.PauseMonitor(m)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Monitor ID %d paused\n", new.ID)
},
}

func init() {
RootCmd.AddCommand(pauseCmd)
}
35 changes: 35 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"log"
"strconv"

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

var startCmd = &cobra.Command{
Use: "start",
Short: "start a monitor",
Long: `Start (unpause) 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.StartMonitor(m)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Monitor ID %d started\n", new.ID)
},
}

func init() {
RootCmd.AddCommand(startCmd)
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var version = "0.8.0"
var version = "0.9.0"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
37 changes: 37 additions & 0 deletions pkg/uptimerobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ var MonitorSubTypes = map[float64]string{
99: "Custom Port",
}

// StatusPause is the status value which sets a monitor to paused status when calling EditMonitor.
var StatusPause = "0"

// StatusResume is the status value which sets a monitor to resumed (unpaused) status when calling EditMonitor.
var StatusResume = "1"

// HTTPClient represents an http.Client, or a mock equivalent.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
Expand Down Expand Up @@ -258,6 +264,37 @@ func (c *Client) EnsureMonitor(m Monitor) (Monitor, error) {
return monitors[0], nil
}

// PauseMonitor takes a Monitor with the ID field set, and attempts to set the
// monitor status to paused via the API. It returns a Monitor with the ID field
// set to the ID of the monitor, or an error if the operation failed.
func (c *Client) PauseMonitor(m Monitor) (Monitor, error) {
r := Response{}
p := Params{
"id": strconv.FormatInt(m.ID, 10),
"status": StatusPause,
}
if err := c.MakeAPICall("editMonitor", &r, p); err != nil {
return Monitor{}, err
}
return r.Monitor, nil
}

// StartMonitor takes a Monitor with the ID field set, and attempts to set the
// monitor status to resumed (unpaused) via the API. It returns a Monitor with
// the ID field set to the ID of the monitor, or an error if the operation
// failed.
func (c *Client) StartMonitor(m Monitor) (Monitor, error) {
r := Response{}
p := Params{
"id": strconv.FormatInt(m.ID, 10),
"status": StatusResume,
}
if err := c.MakeAPICall("editMonitor", &r, p); err != nil {
return Monitor{}, err
}
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.
Expand Down
50 changes: 50 additions & 0 deletions pkg/uptimerobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,56 @@ func TestNewMonitor(t *testing.T) {
}
}

func fakePauseMonitorHandler(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`{
"stat": "ok",
"monitor": {
"id": 677810870
}
}`)),
}, nil
}

func TestPauseMonitor(t *testing.T) {
c := New("dummy")
mockClient := MockHTTPClient{
DoFunc: fakePauseMonitorHandler,
}
c.http = &mockClient
mon := Monitor{
ID: 677810870,
}
got, err := c.PauseMonitor(mon)
if err != nil {
t.Error(err)
}
if got.ID != mon.ID {
t.Errorf("PauseMonitor() => ID %d, want %d", got.ID, mon.ID)
}

}

func TestStartMonitor(t *testing.T) {
c := New("dummy")
mockClient := MockHTTPClient{
DoFunc: fakePauseMonitorHandler,
}
c.http = &mockClient
mon := Monitor{
ID: 677810870,
}
got, err := c.StartMonitor(mon)
if err != nil {
t.Error(err)
}
if got.ID != mon.ID {
t.Errorf("StartMonitor() => ID %d, want %d", got.ID, mon.ID)
}

}

func TestEnsureMonitor(t *testing.T) {
c := New("dummy")
mockClient := MockHTTPClient{
Expand Down

0 comments on commit 8a52db1

Please sign in to comment.