Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Adding support to heartbeat monitor type #119

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 22 additions & 80 deletions uptimerobot/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
)

var monitorType = map[string]int{
"http": 1,
"keyword": 2,
"ping": 3,
"port": 4,
"http": 1,
"keyword": 2,
"ping": 3,
"port": 4,
"heartbeat": 5,
}
var MonitorType = mapKeys(monitorType)

Expand Down Expand Up @@ -182,7 +183,9 @@ type MonitorRequestAlertContact struct {
Threshold int
Recurrence int
}
type MonitorCreateRequest struct {

type MonitorRequest struct {
ID int
FriendlyName string
URL string
Type string
Expand All @@ -205,12 +208,15 @@ type MonitorCreateRequest struct {
CustomHTTPHeaders map[string]string
}

func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Monitor, err error) {
func (client UptimeRobotApiClient) setRequest(req *MonitorRequest) url.Values {
data := url.Values{}
data.Add("friendly_name", req.FriendlyName)
data.Add("url", req.URL)
data.Add("type", fmt.Sprintf("%d", monitorType[req.Type]))
data.Add("interval", fmt.Sprintf("%d", req.Interval))
data.Add("ignore_ssl_errors", "0")
data.Add("custom_http_headers", "{}")

switch req.Type {
case "port":
data.Add("sub_type", fmt.Sprintf("%d", monitorSubType[req.SubType]))
Expand All @@ -229,12 +235,12 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
data.Add("http_username", req.HTTPUsername)
data.Add("http_password", req.HTTPPassword)
break
case "heartbeat":
data.Del("url")
}

if req.IgnoreSSLErrors {
data.Add("ignore_ssl_errors", "1")
} else {
data.Add("ignore_ssl_errors", "0")
}

acStrings := make([]string, len(req.AlertContacts))
Expand All @@ -251,6 +257,12 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
}
}

return data
}

func (client UptimeRobotApiClient) CreateMonitor(req MonitorRequest) (m Monitor, err error) {
data := client.setRequest(&req)

body, err := client.MakeCall(
"newMonitor",
data.Encode(),
Expand All @@ -265,79 +277,9 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
return client.GetMonitor(id)
}

type MonitorUpdateRequest struct {
ID int
FriendlyName string
URL string
Type string
Interval int

SubType string
Port int

KeywordType string
KeywordValue string

HTTPUsername string
HTTPPassword string
HTTPAuthType string

IgnoreSSLErrors bool

AlertContacts []MonitorRequestAlertContact

CustomHTTPHeaders map[string]string
}

func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Monitor, err error) {
data := url.Values{}
func (client UptimeRobotApiClient) UpdateMonitor(req MonitorRequest) (m Monitor, err error) {
data := client.setRequest(&req)
data.Add("id", fmt.Sprintf("%d", req.ID))
data.Add("friendly_name", req.FriendlyName)
data.Add("url", req.URL)
data.Add("type", fmt.Sprintf("%d", monitorType[req.Type]))
data.Add("interval", fmt.Sprintf("%d", req.Interval))
switch req.Type {
case "port":
data.Add("sub_type", fmt.Sprintf("%d", monitorSubType[req.SubType]))
data.Add("port", fmt.Sprintf("%d", req.Port))
break
case "keyword":
data.Add("keyword_type", fmt.Sprintf("%d", monitorKeywordType[req.KeywordType]))
data.Add("keyword_value", req.KeywordValue)

data.Add("http_auth_type", fmt.Sprintf("%d", monitorHTTPAuthType[req.HTTPAuthType]))
data.Add("http_username", req.HTTPUsername)
data.Add("http_password", req.HTTPPassword)
break
case "http":
data.Add("http_auth_type", fmt.Sprintf("%d", monitorHTTPAuthType[req.HTTPAuthType]))
data.Add("http_username", req.HTTPUsername)
data.Add("http_password", req.HTTPPassword)
break
}

if req.IgnoreSSLErrors {
data.Add("ignore_ssl_errors", "1")
} else {
data.Add("ignore_ssl_errors", "0")
}

acStrings := make([]string, len(req.AlertContacts))
for k, v := range req.AlertContacts {
acStrings[k] = fmt.Sprintf("%s_%d_%d", v.ID, v.Threshold, v.Recurrence)
}
data.Add("alert_contacts", strings.Join(acStrings, "-"))

// custom http headers
if len(req.CustomHTTPHeaders) > 0 {
jsonData, err := json.Marshal(req.CustomHTTPHeaders)
if err == nil {
data.Add("custom_http_headers", string(jsonData))
}
} else {
//delete custom http headers when it is empty
data.Add("custom_http_headers", "{}")
}

_, err = client.MakeCall(
"editMonitor",
Expand Down
6 changes: 3 additions & 3 deletions uptimerobot/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceMonitor() *schema.Resource {
},
"url": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -116,7 +116,7 @@ func resourceMonitor() *schema.Resource {
}

func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error {
req := uptimerobotapi.MonitorCreateRequest{
req := uptimerobotapi.MonitorRequest{
FriendlyName: d.Get("friendly_name").(string),
URL: d.Get("url").(string),
Type: d.Get("type").(string),
Expand Down Expand Up @@ -196,7 +196,7 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error {
return err
}

req := uptimerobotapi.MonitorUpdateRequest{
req := uptimerobotapi.MonitorRequest{
ID: id,
FriendlyName: d.Get("friendly_name").(string),
URL: d.Get("url").(string),
Expand Down
32 changes: 32 additions & 0 deletions uptimerobot/resource_uptimerobot_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,35 @@ func testAccCheckMonitorDestroy(s *terraform.State) error {

return nil
}

func TestUptimeRobotDataResourceMonitor_heartbeat_monitor(t *testing.T) {
var FriendlyName = "TF Test: heartbeat monitor"
var Type = "heartbeat"
var Interval = "600"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMonitorDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(`
resource "uptimerobot_monitor" "test_heart_monitor" {
friendly_name = "%s"
type = "%s"
interval = "%s"
}
`, FriendlyName, Type),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "interval", Interval),
),
},
resource.TestStep{
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
33 changes: 21 additions & 12 deletions website/docs/r/monitor.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,45 @@ resource "uptimerobot_monitor" "my_website" {
type = "http"
url = "http://example.com"
}

resource "uptimerobot_monitor" "my_cron" {
friendly_name = "My Cronjob"
type = "heartbeat"
interval = 300
}
```

## Arguments Reference

* `friendly_name` - friendly name of the monitor (for making it easier to distinguish from others).
* `url` - the URL/IP of the monitor.
* `type` - the type of the monitor. Can be one of the following:
* `api_key` - (Required) uptimerobot API key
* `friendly_name` - (Required) friendly name of the monitor (for making it easier to distinguish from others).
* `url` - (Required for all types EXCEPT for the heartbeat one) the URL/IP of the monitor.
* `type` - (Required) the type of the monitor. Can be one of the following:
- *`http`*
- *`heartbeat`*
- *`keyword`* - will also enable the following options:
- `keyword_type` - if the monitor will be flagged as down when the keyword exists or not exists. Can be one of the following:
- `exists`
- `not exists`
- `keyword_value` - the value of the keyword.
- `exists` - (required for keyword monitoring)
- `not exists` - (required for keyword monitoring)
- `keyword_value` - (required for keyword monitoring) the value of the keyword.
- *`ping`*
- *`port`* - will also enable the following options:
- `sub_type` - which pre-defined port/service is monitored or if a custom port is monitored. Can be one of the following:
- `sub_type` - (Required for port monitoring and custom) which pre-defined port/service is monitored or if a custom port is monitored. Can be one of the following:
- `http`
- `https`
- `ftp`
- `smtp`
- `pop3`
- `imap`
- `custom`
- `port` - the port monitored (only if subtype is `custom`)
* `http_username` - used for password-protected web pages (HTTP basic or digest). Available for HTTP and keyword monitoring.
* `http_password` - used for password-protected web pages (HTTP basic or digest). Available for HTTP and keyword monitoring.
* `http_auth_type` - Used for password-protected web pages (HTTP basic or digest). Available for HTTP and keyword monitoring. Can be one of the following:
- `port` - the port monitored (only if subtype is `custom` or `port`)
* `http_username` - (Optional) used for password-protected web pages (HTTP basic or digest). Available for HTTP and keyword monitoring.
* `http_password` - (Optional) used for password-protected web pages (HTTP basic or digest). Available for HTTP and keyword monitoring.
* `http_auth_type` - (Optional) used for password-protected web pages (HTTP basic or digest). Available for HTTP and keyword monitoring. Can be one of the following:
- `basic`
- `digest`
* `interval` - the interval for the monitoring check (300 seconds by default).
* `interval` - (Optional) the interval for the monitoring check (300 seconds by default).
* `alert_contact` - (Optional) the alert contacts to be notified when the monitor goes up/down.Multiple

## Attributes Reference

Expand Down