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

Commit a26e669

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 1176116 commit a26e669

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
@@ -78,7 +78,7 @@ type Monitor struct {
7878
AlertContacts []MonitorAlertContact
7979
}
8080

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

110110
for _, i := range monitors {
111111
monitor := i.(map[string]interface{})
112+
monitorID := int(monitor["id"].(float64))
113+
monitorFriendlyName := monitor["friendly_name"].(string)
112114

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

179120
total = int(body["pagination"].(map[string]interface{})["total"].(float64))
180121
offset += maxMonitorRecords
181122
if offset < total {
123+
// get monitors for next page
182124
data.Set("offset", fmt.Sprintf("%d", offset))
183125
} else {
184126
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)