diff --git a/uptimerobot/api/monitor.go b/uptimerobot/api/monitor.go index ad66ebe..568320e 100644 --- a/uptimerobot/api/monitor.go +++ b/uptimerobot/api/monitor.go @@ -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) @@ -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 @@ -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])) @@ -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)) @@ -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(), @@ -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", diff --git a/uptimerobot/resource_uptimerobot_monitor.go b/uptimerobot/resource_uptimerobot_monitor.go index a60fc98..b917520 100644 --- a/uptimerobot/resource_uptimerobot_monitor.go +++ b/uptimerobot/resource_uptimerobot_monitor.go @@ -27,7 +27,7 @@ func resourceMonitor() *schema.Resource { }, "url": { Type: schema.TypeString, - Required: true, + Optional: true, }, "type": { Type: schema.TypeString, @@ -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), @@ -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), diff --git a/uptimerobot/resource_uptimerobot_monitor_test.go b/uptimerobot/resource_uptimerobot_monitor_test.go index 6678fcc..10b330c 100644 --- a/uptimerobot/resource_uptimerobot_monitor_test.go +++ b/uptimerobot/resource_uptimerobot_monitor_test.go @@ -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, + }, + }, + }) +} diff --git a/website/docs/r/monitor.html.markdown b/website/docs/r/monitor.html.markdown index 411804e..b2a7973 100644 --- a/website/docs/r/monitor.html.markdown +++ b/website/docs/r/monitor.html.markdown @@ -18,22 +18,30 @@ 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` @@ -41,13 +49,14 @@ resource "uptimerobot_monitor" "my_website" { - `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