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

Commit

Permalink
Merge pull request #77 from louy/feat/alert-contact-import
Browse files Browse the repository at this point in the history
Fix alert_contact import in monitor resource
  • Loading branch information
louy authored Sep 6, 2020
2 parents 05bb1fd + 8540f15 commit 0ed7568
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
21 changes: 21 additions & 0 deletions uptimerobot/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var monitorHTTPAuthType = map[string]int{
}
var MonitorHTTPAuthType = mapKeys(monitorHTTPAuthType)

type MonitorAlertContact struct {
ID string `json:"id"`
Recurrence int `json:"recurrence"`
Threshold int `json:"threshold"`
}

type Monitor struct {
ID int `json:"id"`
FriendlyName string `json:"friendly_name"`
Expand All @@ -66,12 +72,15 @@ type Monitor struct {
HTTPAuthType string `json:"http_auth_type"`

CustomHTTPHeaders map[string]string

AlertContacts []MonitorAlertContact
}

func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
data := url.Values{}
data.Add("monitors", fmt.Sprintf("%d", id))
data.Add("custom_http_headers", fmt.Sprintf("%d", 1))
data.Add("alert_contacts", fmt.Sprintf("%d", 1))

body, err := client.MakeCall(
"getMonitors",
Expand Down Expand Up @@ -139,6 +148,18 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
}
m.CustomHTTPHeaders = customHTTPHeaders

if contacts := monitor["alert_contacts"].([]interface{}); contacts != nil {
m.AlertContacts = make([]MonitorAlertContact, len(contacts))
for k, v := range contacts {
contact := v.(map[string]interface{})
var ac MonitorAlertContact
ac.ID = contact["id"].(string)
ac.Recurrence = int(contact["recurrence"].(float64))
ac.Threshold = int(contact["threshold"].(float64))
m.AlertContacts[k] = ac
}
}

return
}

Expand Down
35 changes: 27 additions & 8 deletions uptimerobot/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error {
return err
}
d.SetId(fmt.Sprintf("%d", monitor.ID))
updateMonitorResource(d, monitor)
if err := updateMonitorResource(d, monitor); err != nil {
return err
}
return nil
}

Expand All @@ -175,9 +177,9 @@ func resourceMonitorRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}

updateMonitorResource(d, monitor)

if err := updateMonitorResource(d, monitor); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -237,8 +239,9 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}

updateMonitorResource(d, monitor)
if err := updateMonitorResource(d, monitor); err != nil {
return err
}
return nil
}

Expand All @@ -255,7 +258,7 @@ func resourceMonitorDelete(d *schema.ResourceData, m interface{}) error {
return nil
}

func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) {
func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) error {
d.Set("friendly_name", m.FriendlyName)
d.Set("url", m.URL)
d.Set("type", m.Type)
Expand All @@ -273,5 +276,21 @@ func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) {
// PS: There seems to be a bug in the UR api as it never returns this value
// d.Set("http_auth_type", m.HTTPAuthType)

d.Set("custom_http_headers", m.CustomHTTPHeaders)
if err := d.Set("custom_http_headers", m.CustomHTTPHeaders); err != nil {
return fmt.Errorf("error setting custom_http_headers for resource %s: %s", d.Id(), err)
}

rawContacts := make([]map[string]interface{}, len(m.AlertContacts))
for k, v := range m.AlertContacts {
rawContacts[k] = map[string]interface{}{
"id": v.ID,
"recurrence": v.Recurrence,
"threshold": v.Threshold,
}
}
if err := d.Set("alert_contact", rawContacts); err != nil {
return fmt.Errorf("error setting alert_contact for resource %s: %s", d.Id(), err)
}

return nil
}
15 changes: 7 additions & 8 deletions uptimerobot/resource_uptimerobot_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,9 @@ func TestUptimeRobotDataResourceMonitor_custom_alert_contact_threshold_and_recur
),
},
resource.TestStep{
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
// uptimerobot doesn't support pulling alert_contact
// ImportStateVerify: true,
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -417,8 +416,8 @@ func TestUptimeRobotDataResourceMonitor_http_auth_monitor(t *testing.T) {
),
},
resource.TestStep{
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
// NB: Disabled due to http_auth_type issue
// ImportStateVerify: true,
},
Expand All @@ -443,8 +442,8 @@ func TestUptimeRobotDataResourceMonitor_http_auth_monitor(t *testing.T) {
),
},
resource.TestStep{
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
// NB: Disabled due to http_auth_type issue
// ImportStateVerify: true,
},
Expand Down

0 comments on commit 0ed7568

Please sign in to comment.