Skip to content
Open
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
33 changes: 21 additions & 12 deletions docs/resources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,38 @@ 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`)
- `port` - the port monitored (only if subtype is `custom` or `port`)
* `http_method` - available for HTTP monitoring. Can be one of the following:
- `HEAD` (default for non-keyword)
- `GET` (default for keyword)
Expand All @@ -48,12 +56,13 @@ resource "uptimerobot_monitor" "my_website" {
- `PATCH`
- `DELETE`
- `OPTIONS`
* `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:
* `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
1 change: 0 additions & 1 deletion internal/provider/api/alert_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const page_limit = 10
var alertContactType = map[string]int{
"sms": 1,
"e-mail": 2,
"email": 2,
"twitter": 3,
"twitter-dm": 3,
"boxcar": 4,
Expand Down
121 changes: 34 additions & 87 deletions internal/provider/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,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 @@ -144,7 +145,11 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
// PS: There seems to be a bug in the UR api as it never returns this value
m.HTTPAuthType = intToString(monitorHTTPAuthType, int(val.(float64)))
}
m.HTTPMethod = intToString(monitorHTTPMethod, int(monitor["http_method"].(float64)))
// TODO(mnaser): The UptimeRobot API does not return `http_method` when
// the monitor type is `http`. I've sent them an email, we
// should comment this out once that's fixed.
//
// m.HTTPMethod = intToString(monitorHTTPMethod, int(monitor["http_method"].(float64)))
m.HTTPUsername = monitor["http_username"].(string)
m.HTTPPassword = monitor["http_password"].(string)
break
Expand All @@ -153,9 +158,11 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
// PS: There seems to be a bug in the UR api as it never returns this value
m.HTTPAuthType = intToString(monitorHTTPAuthType, int(val.(float64)))
}
if method := monitor["http_method"]; method != nil {
m.HTTPMethod = intToString(monitorHTTPMethod, int(monitor["http_method"].(float64)))
}
// TODO(mnaser): The UptimeRobot API does not return `http_method` when
// the monitor type is `http`. I've sent them an email, we
// should comment this out once that's fixed.
//
// m.HTTPMethod = intToString(monitorHTTPMethod, int(monitor["http_method"].(float64)))
m.HTTPUsername = monitor["http_username"].(string)
m.HTTPPassword = monitor["http_password"].(string)
break
Expand Down Expand Up @@ -194,7 +201,9 @@ type MonitorRequestAlertContact struct {
Threshold int
Recurrence int
}
type MonitorCreateRequest struct {

type MonitorRequest struct {
ID int
FriendlyName string
URL string
Type string
Expand All @@ -218,12 +227,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 @@ -249,12 +261,12 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
data.Add("post_value", "{}")
}
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 @@ -271,10 +283,17 @@ 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(),
)

if err != nil {
return
}
Expand All @@ -285,87 +304,15 @@ 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

HTTPMethod 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_method", fmt.Sprintf("%d", monitorHTTPMethod[req.HTTPMethod]))
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_method", fmt.Sprintf("%d", monitorHTTPMethod[req.HTTPMethod]))
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",
data.Encode(),
)

if err != nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions internal/provider/resource_uptimerobot_alert_contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestUptimeRobotDataResourceAlertContact_email(t *testing.T) {
type = "%s"
value = "%s"
}
`, friendlyName, "email", email),
`, friendlyName, "e-mail", email),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "friendly_name", friendlyName),
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "type", "email"),
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "type", "e-mail"),
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "value", email),
),
},
Expand Down Expand Up @@ -57,10 +57,10 @@ func TestUptimeRobotDataResourceAlertContact_update_email(t *testing.T) {
type = "%s"
value = "%s"
}
`, friendlyName, "email", email),
`, friendlyName, "e-mail", email),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "friendly_name", friendlyName),
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "type", "email"),
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "type", "e-mail"),
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "value", email),
),
},
Expand All @@ -71,7 +71,7 @@ func TestUptimeRobotDataResourceAlertContact_update_email(t *testing.T) {
type = "%s"
value = "%s"
}
`, friendlyName, "email", email2),
`, friendlyName, "e-mail", email2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("uptimerobot_alert_contact.test", "value", email2),
),
Expand Down
9 changes: 5 additions & 4 deletions internal/provider/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func resourceMonitor() *schema.Resource {
},
"url": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -121,7 +121,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 @@ -205,7 +205,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 Expand Up @@ -290,7 +290,8 @@ func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) err
d.Set("keyword_type", m.KeywordType)
d.Set("keyword_value", m.KeywordValue)

d.Set("http_method", m.HTTPMethod)
// PS: There seems to be a bug in the UR api as it never returns this value
// d.Set("http_method", m.HTTPMethod)
d.Set("http_username", m.HTTPUsername)
d.Set("http_password", m.HTTPPassword)
// PS: There seems to be a bug in the UR api as it never returns this value
Expand Down
Loading