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