@@ -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
0 commit comments