Skip to content

Commit

Permalink
Merge pull request #7 from bitfield/ensure
Browse files Browse the repository at this point in the history
Add ensure command
  • Loading branch information
bitfield authored Oct 8, 2018
2 parents 7b7a04f + 8c632be commit 9afb26e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions cmd/ensure.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions pkg/uptimerobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions pkg/uptimerobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 9afb26e

Please sign in to comment.