Skip to content

Commit ba89da9

Browse files
authored
Merge pull request #648 from PascalBourdier/may-be-not-nil
fix: avoid nil exception
2 parents a389cfe + 3298b7b commit ba89da9

3 files changed

Lines changed: 54 additions & 49 deletions

File tree

‎pkg/monitors/gcloud/gcloud-monitor.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ func (service *MonitorService) Add(monitor models.Monitor) {
9999
portString := url.Port()
100100
var port int
101101
if portString == "" {
102-
if url.Scheme == "http" {
102+
switch url.Scheme {
103+
case "http":
103104
port = 80
104-
} else if url.Scheme == "https" {
105+
case "https":
105106
port = 443
106-
} else {
107+
default:
107108
log.Info("Error Adding Monitor: unknown protocol " + url.Scheme)
108109
return
109110
}

‎pkg/monitors/pingdom/pingdom-monitor.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,16 @@ func (service *PingdomMonitorService) createHttpCheck(monitor models.Monitor) pi
126126
log.Info(fmt.Sprintf("Error parsing url '%s' of monitor %s", service.url, monitor.Name))
127127
}
128128

129-
if url.Scheme == "https" {
130-
httpCheck.Encryption = true
131-
} else {
129+
if url != nil {
132130
httpCheck.Encryption = false
131+
if url.Scheme == "https" {
132+
httpCheck.Encryption = true
133+
}
134+
135+
httpCheck.Hostname = url.Host
136+
httpCheck.Url = url.Path
133137
}
134138

135-
httpCheck.Hostname = url.Host
136-
httpCheck.Url = url.Path
137139
httpCheck.Name = monitor.Name
138140
// Set the default values if they are present in provider config
139141
// all of them can be overridden via EndpointMonitor specific options

‎pkg/monitors/updown/updown-monitor.go‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type UpdownMonitorService struct {
2929
client *updown.Client
3030
}
3131

32-
func (monitor *UpdownMonitorService) Equal(oldMonitor models.Monitor, newMonitor models.Monitor) bool {
32+
func (updownService *UpdownMonitorService) Equal(oldMonitor models.Monitor, newMonitor models.Monitor) bool {
3333
// TODO: Retrieve oldMonitor config and compare it here
3434
return false
3535
}
@@ -59,26 +59,25 @@ func (updownService *UpdownMonitorService) GetAll() []models.Monitor {
5959
updownChecks, httpResponse, err := updownService.client.Check.List()
6060
log.Info("Monitors (updown checks) object list has been pulled")
6161

62-
if (httpResponse.StatusCode == http.StatusOK) && (err == nil) {
63-
log.Info("Populating monitors list using the updownChecks object given in updownChecks list")
64-
65-
// populating a monitors slice using the updownChecks objects given in updownChecks slice
66-
for _, updownCheck := range updownChecks {
67-
newMonitor := models.Monitor{
68-
URL: updownCheck.URL,
69-
Name: updownCheck.Alias,
70-
ID: updownCheck.Token,
71-
}
72-
monitors = append(monitors, newMonitor)
73-
}
74-
return monitors
75-
76-
} else {
62+
if err != nil || httpResponse.StatusCode != http.StatusOK {
7763
log.Info("Unable to get updown provider checks(monitor) list")
64+
7865
return nil
66+
}
67+
68+
log.Info("Populating monitors list using the updownChecks object given in updownChecks list")
7969

70+
// populating a monitors slice using the updownChecks objects given in updownChecks slice
71+
for _, updownCheck := range updownChecks {
72+
newMonitor := models.Monitor{
73+
URL: updownCheck.URL,
74+
Name: updownCheck.Alias,
75+
ID: updownCheck.Token,
76+
}
77+
monitors = append(monitors, newMonitor)
8078
}
8179

80+
return monitors
8281
}
8382

8483
// GetByName function will return a monitor(updown check) object based on the name provided
@@ -101,26 +100,25 @@ func (updownService *UpdownMonitorService) GetByName(monitorName string) (*model
101100
}
102101

103102
// Add function method will add a monitor (updown check)
104-
func (service *UpdownMonitorService) Add(updownMonitor models.Monitor) {
103+
func (updownService *UpdownMonitorService) Add(updownMonitor models.Monitor) {
105104

106105
log.Info("Updown monitor's Add method has been called")
107106

108-
updownCheckItemObj := service.createHttpCheck(updownMonitor)
107+
updownCheckItemObj := updownService.createHttpCheck(updownMonitor)
109108

110-
_, httpResponse, err := service.client.Check.Add(updownCheckItemObj)
109+
_, httpResponse, err := updownService.client.Check.Add(updownCheckItemObj)
111110
log.Info("Monitor addition request has been completed")
112111

113-
if (httpResponse.StatusCode == http.StatusCreated) && (err == nil) {
114-
log.Info(fmt.Sprintf("Monitor %s has been added.", updownMonitor.Name))
115-
116-
} else if (httpResponse.StatusCode == http.StatusBadRequest) && (err != nil) {
117-
log.Info(fmt.Sprintf("Monitor %s is not created because of invalid parameters or it exists.", updownMonitor.Name))
118-
119-
} else {
112+
if err != nil {
113+
if httpResponse != nil && httpResponse.StatusCode == http.StatusBadRequest {
114+
log.Info(fmt.Sprintf("Monitor %s is not created because of invalid parameters or it exists.", updownMonitor.Name))
115+
return
116+
}
120117
log.Info(fmt.Sprintf("Unable to create monitor %s ", updownMonitor.Name))
121-
118+
return
122119
}
123120

121+
log.Info(fmt.Sprintf("Monitor %s has been added.", updownMonitor.Name))
124122
}
125123

126124
// createHttpCheck method it will populate updown CheckItem object using updownMonitor's attributes
@@ -152,7 +150,7 @@ func (updownService *UpdownMonitorService) createHttpCheck(updownMonitor models.
152150
}
153151

154152
// addConfigToHttpCheck method will populate Updown's CheckItem object attributes using provider config
155-
func (service *UpdownMonitorService) addConfigToHttpCheck(updownCheckItemObj *updown.CheckItem, config interface{}) {
153+
func (updownService *UpdownMonitorService) addConfigToHttpCheck(updownCheckItemObj *updown.CheckItem, config interface{}) {
156154
// Read provider config, try to map them to updown check configs
157155
// set some default values if we can't find them
158156

@@ -182,22 +180,25 @@ func (service *UpdownMonitorService) addConfigToHttpCheck(updownCheckItemObj *up
182180
}
183181

184182
// Update method will update a monitor (updown check)
185-
func (service *UpdownMonitorService) Update(updownMonitor models.Monitor) {
183+
func (updownService *UpdownMonitorService) Update(updownMonitor models.Monitor) {
186184

187185
log.Info("Updown's Update method has been called")
188186

189-
httpCheckItemObj := service.createHttpCheck(updownMonitor)
190-
_, httpResponse, err := service.client.Check.Update(updownMonitor.ID, httpCheckItemObj)
187+
httpCheckItemObj := updownService.createHttpCheck(updownMonitor)
188+
_, httpResponse, err := updownService.client.Check.Update(updownMonitor.ID, httpCheckItemObj)
191189
log.Info("Updown's check Update request has been completed")
192190

193-
if (httpResponse.StatusCode == http.StatusOK) && (err == nil) {
194-
log.Info(fmt.Sprintf("Monitor %s has been updated with following parameters", updownMonitor.Name))
195-
196-
} else {
191+
if err != nil {
197192
log.Info(fmt.Sprintf("Monitor %s is not updated because of %s", updownMonitor.Name, err.Error()))
193+
return
194+
}
198195

196+
if httpResponse.StatusCode != http.StatusOK {
197+
log.Info(fmt.Sprintf("Unable to update monitor %s, status code is %s", updownMonitor.Name, httpResponse.Status))
198+
return
199199
}
200200

201+
log.Info(fmt.Sprintf("Monitor %s has been updated with following parameters", updownMonitor.Name))
201202
}
202203

203204
// Remove method will remove a monitor (updown check)
@@ -208,14 +209,15 @@ func (updownService *UpdownMonitorService) Remove(updownMonitor models.Monitor)
208209
_, httpResponse, err := updownService.client.Check.Remove(updownMonitor.ID)
209210
log.Info("Updown's check Remove request has been completed")
210211

211-
if (httpResponse.StatusCode == http.StatusOK) && (err == nil) {
212-
log.Info(fmt.Sprintf("Monitor %v has been deleted.", updownMonitor.Name))
212+
if err != nil {
213+
log.Info(fmt.Sprintf("Unable to delete %v monitor: %s", updownMonitor.Name, err.Error()))
214+
return
215+
}
213216

214-
} else if (httpResponse.StatusCode == http.StatusNotFound) && (err != nil) {
217+
if httpResponse.StatusCode == http.StatusNotFound {
215218
log.Info(fmt.Sprintf("Monitor %v is not found.", updownMonitor.Name))
216-
217-
} else {
218-
log.Info("Unable to delete %v monitor: " + updownMonitor.Name)
219+
return
219220
}
220221

222+
log.Info(fmt.Sprintf("Monitor %v has been deleted.", updownMonitor.Name))
221223
}

0 commit comments

Comments
 (0)