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

Commit

Permalink
Add thresholds for alert contacts
Browse files Browse the repository at this point in the history
Sadly the uptimerobot API doesn't support fetching them
  • Loading branch information
drubin committed Mar 10, 2019
1 parent 91f7d84 commit 5d6932a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
10 changes: 6 additions & 4 deletions uptimerobot/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
switch m.Type {
case "port":
m.SubType = intToString(monitorSubType, int(monitor["sub_type"].(float64)))
if (m.SubType != "custom") {
if m.SubType != "custom" {
m.Port = 0
} else {
m.Port = int(monitor["port"].(float64))
Expand All @@ -119,7 +119,9 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
}

type MonitorRequestAlertContact struct {
ID int
ID int
Threshold int
Recurrence int
}
type MonitorCreateRequest struct {
FriendlyName string
Expand Down Expand Up @@ -164,7 +166,7 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
}
acStrings := make([]string, len(req.AlertContacts))
for k, v := range req.AlertContacts {
acStrings[k] = fmt.Sprintf("%d", v.ID)
acStrings[k] = fmt.Sprintf("%d_%d_%d", v.ID, v.Threshold, v.Recurrence)
}
data.Add("alert_contacts", strings.Join(acStrings, "-"))

Expand Down Expand Up @@ -227,7 +229,7 @@ func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Mo
}
acStrings := make([]string, len(req.AlertContacts))
for k, v := range req.AlertContacts {
acStrings[k] = fmt.Sprintf("%d", v.ID)
acStrings[k] = fmt.Sprintf("%d_%d_%d", v.ID, v.Threshold, v.Recurrence)
}
data.Add("alert_contacts", strings.Join(acStrings, "-"))

Expand Down
6 changes: 4 additions & 2 deletions uptimerobot/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
uptimerobotapi "github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
)

func resourceMonitor() *schema.Resource {
Expand Down Expand Up @@ -133,7 +133,9 @@ func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error {
req.AlertContacts = make([]uptimerobotapi.MonitorRequestAlertContact, len(d.Get("alert_contact").([]interface{})))
for k, v := range d.Get("alert_contact").([]interface{}) {
req.AlertContacts[k] = uptimerobotapi.MonitorRequestAlertContact{
ID: v.(map[string]interface{})["id"].(int),
ID: v.(map[string]interface{})["id"].(int),
Threshold: v.(map[string]interface{})["threshold"].(int),
Recurrence: v.(map[string]interface{})["recurrence"].(int),
}
}

Expand Down
49 changes: 48 additions & 1 deletion uptimerobot/resource_uptimerobot_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
uptimerobotapi "github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
)

func TestUptimeRobotDataResourceMonitor_http_monitor(t *testing.T) {
Expand Down Expand Up @@ -116,6 +116,53 @@ func TestUptimeRobotDataResourceMonitor_custom_port_monitor(t *testing.T) {
})
}

func TestUptimeRobotDataResourceMonitorAlertContact(t *testing.T) {
var FriendlyName = "TF Test: http monitor"
var Type = "http"
var URL = "https://google.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMonitorDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(`
resource "uptimerobot_alert_contact" "test" {
friendly_name = "#infrastructure"
type = "slack"
value = "https://slack.com/services/xxxx"
}
resource "uptimerobot_monitor" "test" {
friendly_name = "%s"
type = "%s"
url = "%s"
alert_contact {
id = "${uptimerobot_alert_contact.test.id}"
threshold = 0
recurrence = 0
}
}
`, FriendlyName, Type, URL),
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", "alert_contact.#", "1"),
resource.TestCheckResourceAttrSet("uptimerobot_monitor.test", "alert_contact.0.id"),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "alert_contact.0.threshold", "0"),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "alert_contact.0.recurrence", "0"),
),
},
// uptime robot doesn't support pulling alert_contact
// resource.TestStep{
// ResourceName: "uptimerobot_monitor.test",
// ImportState: true,
// ImportStateVerify: true,
// },
},
})
}

func TestUptimeRobotDataResourceMonitor_change_url(t *testing.T) {
var FriendlyName = "TF Test: http monitor"
var Type = "http"
Expand Down

0 comments on commit 5d6932a

Please sign in to comment.