diff --git a/cmd/pause.go b/cmd/pause.go new file mode 100644 index 0000000..31bc57f --- /dev/null +++ b/cmd/pause.go @@ -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) +} diff --git a/cmd/start.go b/cmd/start.go new file mode 100644 index 0000000..ee4637b --- /dev/null +++ b/cmd/start.go @@ -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) +} diff --git a/cmd/version.go b/cmd/version.go index 51f1fb0..8ee0ec5 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -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", diff --git a/pkg/uptimerobot.go b/pkg/uptimerobot.go index 3376a13..d884969 100644 --- a/pkg/uptimerobot.go +++ b/pkg/uptimerobot.go @@ -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) @@ -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. diff --git a/pkg/uptimerobot_test.go b/pkg/uptimerobot_test.go index 5cc19a1..90b0dee 100644 --- a/pkg/uptimerobot_test.go +++ b/pkg/uptimerobot_test.go @@ -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{