Skip to content

Commit

Permalink
Merge pull request #10 from lapras-inc/feat/timeout-again
Browse files Browse the repository at this point in the history
Feat/timeout
  • Loading branch information
yktakaha4 authored Oct 27, 2022
2 parents 6495bc4 + b2b54c9 commit 8d3e9b6
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions internal/provider/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)))
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -228,6 +232,7 @@ type MonitorCreateRequest struct {
URL string
Type string
Interval int
Timeout int

SubType string
Port int
Expand Down Expand Up @@ -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]))
Expand All @@ -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]))
Expand All @@ -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
}

Expand Down Expand Up @@ -336,6 +344,7 @@ type MonitorUpdateRequest struct {
URL string
Type string
Interval int
Timeout int

SubType string
Port int
Expand Down Expand Up @@ -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]))
Expand All @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions internal/provider/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -327,6 +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)
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)
Expand Down
192 changes: 192 additions & 0 deletions internal/provider/resource_uptimerobot_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8d3e9b6

Please sign in to comment.