From af1fb9a99704894ad9dbf24528c0bfa527c0030e Mon Sep 17 00:00:00 2001 From: Yuuki Takahashi <20282867+yktakaha4@users.noreply.github.com> Date: Wed, 12 Oct 2022 00:09:09 +0900 Subject: [PATCH 1/2] Revert "Revert "Feat/timeout"" This reverts commit dd059481aba6c4a4abd6e81e35ec5407a6946ccc. --- docs/resources/monitor.md | 1 + internal/provider/api/monitor.go | 12 ++ .../provider/resource_uptimerobot_monitor.go | 12 ++ .../resource_uptimerobot_monitor_test.go | 192 ++++++++++++++++++ 4 files changed, 217 insertions(+) diff --git a/docs/resources/monitor.md b/docs/resources/monitor.md index 520c957..82ac843 100644 --- a/docs/resources/monitor.md +++ b/docs/resources/monitor.md @@ -57,6 +57,7 @@ resource "uptimerobot_monitor" "my_website" { * `custom_http_statuses` - `up` - Array of HTTP status codes that make the status of the monitor up - `down` - Array of HTTP status codes that make the status of the monitor down +* `timeout` - the timeout for the monitoring check (30 seconds by default). Available for HTTP, port and keyword monitoring. ## Attributes Reference diff --git a/internal/provider/api/monitor.go b/internal/provider/api/monitor.go index cd43462..45a893c 100644 --- a/internal/provider/api/monitor.go +++ b/internal/provider/api/monitor.go @@ -76,6 +76,7 @@ type Monitor struct { Type string `json:"type"` Status string `json:"status"` Interval int `json:"interval"` + Timeout int `json:"timeout"` SubType string `json:"sub_type"` Port int `json:"port"` @@ -143,6 +144,7 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) { } else { m.Port = int(monitor["port"].(float64)) } + m.Timeout = int(monitor["timeout"].(float64)) break case "keyword": m.KeywordType = intToString(monitorKeywordType, int(monitor["keyword_type"].(float64))) @@ -159,6 +161,7 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) { // m.HTTPMethod = intToString(monitorHTTPMethod, int(monitor["http_method"].(float64))) m.HTTPUsername = monitor["http_username"].(string) m.HTTPPassword = monitor["http_password"].(string) + m.Timeout = int(monitor["timeout"].(float64)) break case "http": if val := monitor["http_auth_type"]; val != nil { @@ -172,6 +175,7 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) { // m.HTTPMethod = intToString(monitorHTTPMethod, int(monitor["http_method"].(float64))) m.HTTPUsername = monitor["http_username"].(string) m.HTTPPassword = monitor["http_password"].(string) + m.Timeout = int(monitor["timeout"].(float64)) break } @@ -228,6 +232,7 @@ type MonitorCreateRequest struct { URL string Type string Interval int + Timeout int SubType string Port int @@ -259,6 +264,7 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo case "port": data.Add("sub_type", fmt.Sprintf("%d", monitorSubType[req.SubType])) data.Add("port", fmt.Sprintf("%d", req.Port)) + data.Add("timeout", fmt.Sprintf("%d", req.Timeout)) break case "keyword": data.Add("keyword_type", fmt.Sprintf("%d", monitorKeywordType[req.KeywordType])) @@ -268,6 +274,7 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo data.Add("http_auth_type", fmt.Sprintf("%d", monitorHTTPAuthType[req.HTTPAuthType])) data.Add("http_username", req.HTTPUsername) data.Add("http_password", req.HTTPPassword) + data.Add("timeout", fmt.Sprintf("%d", req.Timeout)) break case "http": data.Add("http_method", fmt.Sprintf("%d", monitorHTTPMethod[req.HTTPMethod])) @@ -279,6 +286,7 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo data.Add("post_content_type", "0") data.Add("post_value", "{}") } + data.Add("timeout", fmt.Sprintf("%d", req.Timeout)) break } @@ -336,6 +344,7 @@ type MonitorUpdateRequest struct { URL string Type string Interval int + Timeout int SubType string Port int @@ -368,6 +377,7 @@ func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Mo case "port": data.Add("sub_type", fmt.Sprintf("%d", monitorSubType[req.SubType])) data.Add("port", fmt.Sprintf("%d", req.Port)) + data.Add("timeout", fmt.Sprintf("%d", req.Timeout)) break case "keyword": data.Add("keyword_type", fmt.Sprintf("%d", monitorKeywordType[req.KeywordType])) @@ -377,12 +387,14 @@ func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Mo data.Add("http_auth_type", fmt.Sprintf("%d", monitorHTTPAuthType[req.HTTPAuthType])) data.Add("http_username", req.HTTPUsername) data.Add("http_password", req.HTTPPassword) + data.Add("timeout", fmt.Sprintf("%d", req.Timeout)) 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) + data.Add("timeout", fmt.Sprintf("%d", req.Timeout)) break } diff --git a/internal/provider/resource_uptimerobot_monitor.go b/internal/provider/resource_uptimerobot_monitor.go index 155e765..1ecb43d 100644 --- a/internal/provider/resource_uptimerobot_monitor.go +++ b/internal/provider/resource_uptimerobot_monitor.go @@ -61,6 +61,11 @@ func resourceMonitor() *schema.Resource { Optional: true, Default: 300, }, + "timeout": { + Type: schema.TypeInt, + Optional: true, + Default: 30, + }, "http_method": { Type: schema.TypeString, Optional: true, @@ -153,6 +158,7 @@ func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error { case "port": req.SubType = d.Get("sub_type").(string) req.Port = d.Get("port").(int) + req.Timeout = d.Get("timeout").(int) break case "keyword": req.KeywordType = d.Get("keyword_type").(string) @@ -162,12 +168,14 @@ func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error { req.HTTPUsername = d.Get("http_username").(string) req.HTTPPassword = d.Get("http_password").(string) req.HTTPAuthType = d.Get("http_auth_type").(string) + req.Timeout = d.Get("timeout").(int) break case "http": req.HTTPMethod = d.Get("http_method").(string) req.HTTPUsername = d.Get("http_username").(string) req.HTTPPassword = d.Get("http_password").(string) req.HTTPAuthType = d.Get("http_auth_type").(string) + req.Timeout = d.Get("timeout").(int) break } @@ -249,6 +257,7 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error { case "port": req.SubType = d.Get("sub_type").(string) req.Port = d.Get("port").(int) + req.Timeout = d.Get("timeout").(int) break case "keyword": req.KeywordType = d.Get("keyword_type").(string) @@ -258,12 +267,14 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error { req.HTTPUsername = d.Get("http_username").(string) req.HTTPPassword = d.Get("http_password").(string) req.HTTPAuthType = d.Get("http_auth_type").(string) + req.Timeout = d.Get("timeout").(int) break case "http": req.HTTPMethod = d.Get("http_method").(string) req.HTTPUsername = d.Get("http_username").(string) req.HTTPPassword = d.Get("http_password").(string) req.HTTPAuthType = d.Get("http_auth_type").(string) + req.Timeout = d.Get("timeout").(int) break } @@ -327,6 +338,7 @@ func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) err d.Set("type", m.Type) d.Set("status", m.Status) d.Set("interval", m.Interval) + d.Set("timeout", m.Timeout) d.Set("sub_type", m.SubType) d.Set("port", m.Port) diff --git a/internal/provider/resource_uptimerobot_monitor_test.go b/internal/provider/resource_uptimerobot_monitor_test.go index 1bacf0c..3e88fff 100644 --- a/internal/provider/resource_uptimerobot_monitor_test.go +++ b/internal/provider/resource_uptimerobot_monitor_test.go @@ -546,6 +546,198 @@ func TestUptimeRobotDataResourceMonitor_custom_interval(t *testing.T) { }) } +func TestUptimeRobotDataResourceMonitor_custom_timeout_http(t *testing.T) { + var FriendlyName = "TF Test: custom timeout http" + var Type = "http" + var URL = "https://google.com" + var Timeout = 60 + var Timeout2 = 45 + 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" { + friendly_name = "%s" + type = "%s" + url = "%s" + timeout = "%d" + } + `, FriendlyName, Type, URL, Timeout), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "timeout", fmt.Sprintf(`%d`, Timeout)), + ), + }, + resource.TestStep{ + ResourceName: "uptimerobot_monitor.test", + ImportState: true, + // NB: Disabled due to http_method issue + // ImportStateVerify: true, + }, + resource.TestStep{ + Config: fmt.Sprintf(` + resource "uptimerobot_monitor" "test" { + friendly_name = "%s" + type = "%s" + url = "%s" + timeout = "%d" + } + `, FriendlyName, Type, URL, Timeout2), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "timeout", fmt.Sprintf(`%d`, Timeout2)), + ), + }, + resource.TestStep{ + ResourceName: "uptimerobot_monitor.test", + ImportState: true, + // NB: Disabled due to http_method issue + // ImportStateVerify: true, + }, + }, + }) +} + +func TestUptimeRobotDataResourceMonitor_custom_timeout_port(t *testing.T) { + var FriendlyName = "TF Test: custom timeout port" + var Type = "port" + var URL = "google.com" + var SubType = "http" + var Timeout = 60 + var Timeout2 = 45 + 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" { + friendly_name = "%s" + type = "%s" + url = "%s" + sub_type = "%s" + timeout = "%d" + } + `, FriendlyName, Type, URL, SubType, Timeout), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "sub_type", SubType), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "timeout", fmt.Sprintf(`%d`, Timeout)), + ), + }, + resource.TestStep{ + ResourceName: "uptimerobot_monitor.test", + ImportState: true, + // NB: Disabled due to http_method issue + // ImportStateVerify: true, + }, + resource.TestStep{ + Config: fmt.Sprintf(` + resource "uptimerobot_monitor" "test" { + friendly_name = "%s" + type = "%s" + url = "%s" + sub_type = "%s" + timeout = "%d" + } + `, FriendlyName, Type, URL, SubType, Timeout2), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "sub_type", SubType), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "timeout", fmt.Sprintf(`%d`, Timeout2)), + ), + }, + resource.TestStep{ + ResourceName: "uptimerobot_monitor.test", + ImportState: true, + // NB: Disabled due to http_method issue + // ImportStateVerify: true, + }, + }, + }) +} + +func TestUptimeRobotDataResourceMonitor_custom_timeout_keyword(t *testing.T) { + var FriendlyName = "TF Test: custom timeout keyword" + var Type = "keyword" + var URL = "https://google.com" + var KeywordType = "not exists" + var KeywordValue = "yahoo" + var Timeout = 60 + var Timeout2 = 45 + 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" { + friendly_name = "%s" + type = "%s" + url = "%s" + keyword_type = "%s" + keyword_value = "%s" + timeout = "%d" + } + `, FriendlyName, Type, URL, KeywordType, KeywordValue, Timeout), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "keyword_type", KeywordType), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "keyword_value", KeywordValue), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "timeout", fmt.Sprintf(`%d`, Timeout)), + ), + }, + resource.TestStep{ + ResourceName: "uptimerobot_monitor.test", + ImportState: true, + // NB: Disabled due to http_method issue + // ImportStateVerify: true, + }, + resource.TestStep{ + Config: fmt.Sprintf(` + resource "uptimerobot_monitor" "test" { + friendly_name = "%s" + type = "%s" + url = "%s" + keyword_type = "%s" + keyword_value = "%s" + timeout = "%d" + } + `, FriendlyName, Type, URL, KeywordType, KeywordValue, Timeout2), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "keyword_type", KeywordType), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "keyword_value", KeywordValue), + resource.TestCheckResourceAttr("uptimerobot_monitor.test", "timeout", fmt.Sprintf(`%d`, Timeout2)), + ), + }, + resource.TestStep{ + ResourceName: "uptimerobot_monitor.test", + ImportState: true, + // NB: Disabled due to http_method issue + // ImportStateVerify: true, + }, + }, + }) +} + func TestUptimeRobotDataResourceMonitor_http_method(t *testing.T) { var FriendlyName = "TF Test: http method monitor" var Type = "http" From b2b54c920c338443ce2147da1182e88c45833eea Mon Sep 17 00:00:00 2001 From: Yuuki Takahashi <20282867+yktakaha4@users.noreply.github.com> Date: Wed, 12 Oct 2022 00:58:12 +0900 Subject: [PATCH 2/2] set timeout if status is not ping --- internal/provider/resource_uptimerobot_monitor.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/provider/resource_uptimerobot_monitor.go b/internal/provider/resource_uptimerobot_monitor.go index 1ecb43d..175a014 100644 --- a/internal/provider/resource_uptimerobot_monitor.go +++ b/internal/provider/resource_uptimerobot_monitor.go @@ -338,7 +338,9 @@ func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) err d.Set("type", m.Type) d.Set("status", m.Status) d.Set("interval", m.Interval) - d.Set("timeout", m.Timeout) + if m.Type == "port" || m.Type == "keyword" || m.Type == "http" { + d.Set("timeout", m.Timeout) + } d.Set("sub_type", m.SubType) d.Set("port", m.Port)