From 8c632bebfd2617b3c007c7f0251fe8958bb2d7dd Mon Sep 17 00:00:00 2001 From: John Arundel Date: Mon, 8 Oct 2018 19:02:46 +0100 Subject: [PATCH] Add ensure command --- README.md | 15 +++++++++++++++ cmd/ensure.go | 33 +++++++++++++++++++++++++++++++++ pkg/uptimerobot.go | 20 ++++++++++++++++++++ pkg/uptimerobot_test.go | 20 ++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 cmd/ensure.go diff --git a/README.md b/README.md index d6145a7..fba6be0 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,21 @@ uptimerobot new -c 0102759,2053888 https://www.example.com/ "Example.com website New monitor created with ID 780689019 ``` +## Ensuring a monitor exists + +Sometimes you want to create a new monitor only if a monitor doesn't already exist for the same URL. This is especially useful in automation. + +To do this, run `uptimerobot ensure URL NAME`: + +``` +uptimerobot ensure https://www.example.com/ "Example.com website" +Monitor ID 780689018 +``` + +If a monitor already existed for the same URL, its ID will be returned. Otherwise, a new monitor will be created, and its ID returned. + +You can use the `-c` flag to add alert contacts, just as for the `uptimerobot new` command. + ## Using the Go library If the command-line client doesn't do quite what you need, or if you want to use UptimeRobot API access in your own programs, import the library using: diff --git a/cmd/ensure.go b/cmd/ensure.go new file mode 100644 index 0000000..7f63313 --- /dev/null +++ b/cmd/ensure.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "fmt" + "log" + + "github.com/bitfield/uptimerobot/pkg" + "github.com/spf13/cobra" +) + +var ensureCmd = &cobra.Command{ + Use: "ensure", + Short: "add a new monitor if not present", + Long: `Create a new monitor with the specified URL and friendly name, if the monitor does not already exist`, + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + m := uptimerobot.Monitor{ + URL: args[0], + FriendlyName: args[1], + Type: uptimerobot.MonitorType("HTTP"), + AlertContacts: contacts, + } + result, err := client.EnsureMonitor(m) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Monitor ID %d\n", result.ID) + }, +} + +func init() { + RootCmd.AddCommand(ensureCmd) +} diff --git a/pkg/uptimerobot.go b/pkg/uptimerobot.go index c3c910c..5d1e14a 100644 --- a/pkg/uptimerobot.go +++ b/pkg/uptimerobot.go @@ -196,6 +196,26 @@ func (c *Client) NewMonitor(m Monitor) (Monitor, error) { return r.Monitor, nil } +// EnsureMonitor takes a Monitor and creates a new UptimeRobot monitor with the +// specified details, if a monitor for the same URL does not already exist. It +// returns a Monitor with the ID field set to the ID of the newly created +// monitor or the existing monitor if it already existed, or an error if the +// operation failed. +func (c *Client) EnsureMonitor(m Monitor) (Monitor, error) { + monitors, err := c.GetMonitorsBySearch(m.URL) + if err != nil { + return Monitor{}, err + } + if len(monitors) == 0 { + new, err := c.NewMonitor(m) + if err != nil { + return Monitor{}, err + } + return new, nil + } + return monitors[0], 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 f004141..8b6b829 100644 --- a/pkg/uptimerobot_test.go +++ b/pkg/uptimerobot_test.go @@ -217,6 +217,26 @@ func TestNewMonitor(t *testing.T) { } } +func TestEnsureMonitor(t *testing.T) { + c := New("dummy") + mockClient := MockHTTPClient{ + DoFunc: fakeGetMonitorsBySearchHandler, + } + c.http = &mockClient + want := Monitor{ + FriendlyName: "My Web Page", + URL: "http://mywebpage.com", + Type: MonitorType("HTTP"), + } + got, err := c.EnsureMonitor(want) + if err != nil { + t.Error(err) + } + if got.ID != 777712827 { + t.Errorf("EnsureMonitor() => ID %d, want 777712827", got.ID) + } +} + func TestDeleteMonitor(t *testing.T) { c := New("dummy") mockClient := MockHTTPClient{