Skip to content

Commit 8c632be

Browse files
committed
Add ensure command
1 parent 7b7a04f commit 8c632be

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

Diff for: README.md

+15
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ uptimerobot new -c 0102759,2053888 https://www.example.com/ "Example.com website
165165
New monitor created with ID 780689019
166166
```
167167

168+
## Ensuring a monitor exists
169+
170+
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.
171+
172+
To do this, run `uptimerobot ensure URL NAME`:
173+
174+
```
175+
uptimerobot ensure https://www.example.com/ "Example.com website"
176+
Monitor ID 780689018
177+
```
178+
179+
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.
180+
181+
You can use the `-c` flag to add alert contacts, just as for the `uptimerobot new` command.
182+
168183
## Using the Go library
169184

170185
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 for: cmd/ensure.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/bitfield/uptimerobot/pkg"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var ensureCmd = &cobra.Command{
12+
Use: "ensure",
13+
Short: "add a new monitor if not present",
14+
Long: `Create a new monitor with the specified URL and friendly name, if the monitor does not already exist`,
15+
Args: cobra.ExactArgs(2),
16+
Run: func(cmd *cobra.Command, args []string) {
17+
m := uptimerobot.Monitor{
18+
URL: args[0],
19+
FriendlyName: args[1],
20+
Type: uptimerobot.MonitorType("HTTP"),
21+
AlertContacts: contacts,
22+
}
23+
result, err := client.EnsureMonitor(m)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
fmt.Printf("Monitor ID %d\n", result.ID)
28+
},
29+
}
30+
31+
func init() {
32+
RootCmd.AddCommand(ensureCmd)
33+
}

Diff for: pkg/uptimerobot.go

+20
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ func (c *Client) NewMonitor(m Monitor) (Monitor, error) {
196196
return r.Monitor, nil
197197
}
198198

199+
// EnsureMonitor takes a Monitor and creates a new UptimeRobot monitor with the
200+
// specified details, if a monitor for the same URL does not already exist. It
201+
// returns a Monitor with the ID field set to the ID of the newly created
202+
// monitor or the existing monitor if it already existed, or an error if the
203+
// operation failed.
204+
func (c *Client) EnsureMonitor(m Monitor) (Monitor, error) {
205+
monitors, err := c.GetMonitorsBySearch(m.URL)
206+
if err != nil {
207+
return Monitor{}, err
208+
}
209+
if len(monitors) == 0 {
210+
new, err := c.NewMonitor(m)
211+
if err != nil {
212+
return Monitor{}, err
213+
}
214+
return new, nil
215+
}
216+
return monitors[0], nil
217+
}
218+
199219
// DeleteMonitor takes a Monitor with the ID field set, and deletes the
200220
// corresponding monitor. It returns a Monitor with the ID field set to the ID
201221
// of the deleted monitor, or an error if the operation failed.

Diff for: pkg/uptimerobot_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ func TestNewMonitor(t *testing.T) {
217217
}
218218
}
219219

220+
func TestEnsureMonitor(t *testing.T) {
221+
c := New("dummy")
222+
mockClient := MockHTTPClient{
223+
DoFunc: fakeGetMonitorsBySearchHandler,
224+
}
225+
c.http = &mockClient
226+
want := Monitor{
227+
FriendlyName: "My Web Page",
228+
URL: "http://mywebpage.com",
229+
Type: MonitorType("HTTP"),
230+
}
231+
got, err := c.EnsureMonitor(want)
232+
if err != nil {
233+
t.Error(err)
234+
}
235+
if got.ID != 777712827 {
236+
t.Errorf("EnsureMonitor() => ID %d, want 777712827", got.ID)
237+
}
238+
}
239+
220240
func TestDeleteMonitor(t *testing.T) {
221241
c := New("dummy")
222242
mockClient := MockHTTPClient{

0 commit comments

Comments
 (0)