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

Commit 0a1d497

Browse files
committed
Retrieve monitor IDs that match given friendly name
Avoid constructing Monitor struct for each and every monitor record. Return IDs of monitor records that match the friendly name provided and build the Monitor object for the single monitor found.
1 parent 2aad3cb commit 0a1d497

File tree

2 files changed

+18
-77
lines changed

2 files changed

+18
-77
lines changed

uptimerobot/api/monitor.go

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type Monitor struct {
7676
AlertContacts []MonitorAlertContact
7777
}
7878

79-
func (client UptimeRobotApiClient) GetMonitors() (ms []Monitor, err error) {
79+
func (client UptimeRobotApiClient) GetMonitorIDs(friendlyName string) (ids []int, err error) {
8080
data := url.Values{}
8181
data.Add("custom_http_headers", fmt.Sprintf("%d", 1))
8282
data.Add("alert_contacts", fmt.Sprintf("%d", 1))
@@ -107,76 +107,18 @@ func (client UptimeRobotApiClient) GetMonitors() (ms []Monitor, err error) {
107107

108108
for _, i := range monitors {
109109
monitor := i.(map[string]interface{})
110+
monitorID := int(monitor["id"].(float64))
111+
monitorFriendlyName := monitor["friendly_name"].(string)
110112

111-
var m Monitor
112-
m.ID = int(monitor["id"].(float64))
113-
m.FriendlyName = monitor["friendly_name"].(string)
114-
m.URL = monitor["url"].(string)
115-
m.Type = intToString(monitorType, int(monitor["type"].(float64)))
116-
m.Status = intToString(monitorStatus, int(monitor["status"].(float64)))
117-
m.Interval = int(monitor["interval"].(float64))
118-
119-
switch m.Type {
120-
case "port":
121-
m.SubType = intToString(monitorSubType, int(monitor["sub_type"].(float64)))
122-
if m.SubType != "custom" {
123-
m.Port = 0
124-
} else {
125-
m.Port = int(monitor["port"].(float64))
126-
}
127-
break
128-
case "keyword":
129-
m.KeywordType = intToString(monitorKeywordType, int(monitor["keyword_type"].(float64)))
130-
m.KeywordValue = monitor["keyword_value"].(string)
131-
132-
if val := monitor["http_auth_type"]; val != nil {
133-
// PS: There seems to be a bug in the UR api as it never returns this value
134-
m.HTTPAuthType = intToString(monitorHTTPAuthType, int(val.(float64)))
135-
}
136-
m.HTTPUsername = monitor["http_username"].(string)
137-
m.HTTPPassword = monitor["http_password"].(string)
138-
break
139-
case "http":
140-
if val := monitor["http_auth_type"]; val != nil {
141-
// PS: There seems to be a bug in the UR api as it never returns this value
142-
m.HTTPAuthType = intToString(monitorHTTPAuthType, int(val.(float64)))
143-
}
144-
m.HTTPUsername = monitor["http_username"].(string)
145-
m.HTTPPassword = monitor["http_password"].(string)
146-
break
113+
if monitorFriendlyName == friendlyName {
114+
ids = append(ids, monitorID)
147115
}
148-
149-
customHTTPHeaders := make(map[string]string)
150-
for k, v := range monitor["custom_http_headers"].(map[string]interface{}) {
151-
customHTTPHeaders[k] = v.(string)
152-
}
153-
m.CustomHTTPHeaders = customHTTPHeaders
154-
155-
if contacts := monitor["alert_contacts"].([]interface{}); contacts != nil {
156-
m.AlertContacts = make([]MonitorAlertContact, len(contacts))
157-
for k, v := range contacts {
158-
contact := v.(map[string]interface{})
159-
var ac MonitorAlertContact
160-
ac.ID = contact["id"].(string)
161-
// Recurrence and Threshold may be null
162-
ac.Recurrence = 0
163-
if recurrence := contact["recurrence"]; recurrence != nil {
164-
ac.Recurrence = int(recurrence.(float64))
165-
}
166-
ac.Threshold = 0
167-
if threshold := contact["threshold"]; threshold != nil {
168-
ac.Threshold = int(threshold.(float64))
169-
}
170-
m.AlertContacts[k] = ac
171-
}
172-
}
173-
174-
ms = append(ms, m)
175116
}
176117

177118
total = int(body["pagination"].(map[string]interface{})["total"].(float64))
178119
offset += maxMonitorRecords
179120
if offset < total {
121+
// get monitors for next page
180122
data.Set("offset", fmt.Sprintf("%d", offset))
181123
} else {
182124
break

uptimerobot/data_source_uptimerobot_monitor.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package uptimerobot
22

33
import (
44
"fmt"
5-
"reflect"
65

76
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
87
uptimerobotapi "github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
@@ -42,25 +41,25 @@ func dataSourceMonitor() *schema.Resource {
4241
}
4342

4443
func dataSourceMonitorRead(d *schema.ResourceData, m interface{}) error {
45-
monitors, err := m.(uptimerobotapi.UptimeRobotApiClient).GetMonitors()
44+
friendlyName := d.Get("friendly_name").(string)
45+
46+
ids, err := m.(uptimerobotapi.UptimeRobotApiClient).GetMonitorIDs(friendlyName)
4647
if err != nil {
4748
return err
4849
}
4950

50-
friendlyName := d.Get("friendly_name").(string)
51-
52-
var monitor uptimerobotapi.Monitor
53-
54-
for _, m := range monitors {
55-
if friendlyName != "" && m.FriendlyName == friendlyName {
56-
monitor = m
57-
break
58-
}
59-
}
60-
if reflect.DeepEqual(monitor, uptimerobotapi.Monitor{}) {
51+
if len(ids) < 1 {
6152
return fmt.Errorf("Failed to find monitor by name %s", friendlyName)
6253
}
6354

55+
if len(ids) > 1 {
56+
return fmt.Errorf("More than one monitor with name %s exists", friendlyName)
57+
}
58+
59+
monitor, err := m.(uptimerobotapi.UptimeRobotApiClient).GetMonitor(ids[0])
60+
if err != nil {
61+
return err
62+
}
6463
d.SetId(fmt.Sprintf("%d", monitor.ID))
6564
if err := updateMonitorResource(d, monitor); err != nil {
6665
return err

0 commit comments

Comments
 (0)