Skip to content

Commit 14e2e1c

Browse files
committed
Change query result descriptions to be a constant, add structs for network monitor types with no services, and cleanup code to use new structs and filter functionality
1 parent a523c3e commit 14e2e1c

1 file changed

Lines changed: 85 additions & 49 deletions

File tree

lib/softlayer/NetworkMonitor.rb

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,51 @@
55
#++
66

77
module SoftLayer
8+
##
9+
# This struct represents the network monitor levels of a server.
10+
# It is roughly equivalent to SoftLayer data type
11+
# SoftLayer_Network_Monitor_Version1_Query_Host_Stratum
12+
class NetworkMonitorLevels < Struct.new(:monitor_level, :response_level)
13+
def initialize(monitor_levels_data)
14+
self.monitor_level = monitor_levels_data['monitorLevel']
15+
self.response_level = monitor_levels_data['responseLevel']
16+
end
17+
end
18+
19+
##
20+
# This struct represents a network monitor query result that shows the last
21+
# state of the network monitor
22+
class NetworkMonitorQueryResult < Struct.new(:finished_at, :responded_in, :response_status)
23+
def initialize(query_result_data)
24+
self.finished_at = query_result_data['finishTime']
25+
self.responded_in = query_result_data['responseTime']
26+
self.response_status = query_result_data['responseStatus']
27+
end
28+
end
29+
30+
##
31+
# This struct represents a network monitor query type used for creating
32+
# new network monitors.
33+
class NetworkMonitorQueryType < Struct.new(:argument_description, :description, :id, :monitor_level, :name)
34+
def initialize(query_type_data)
35+
self.argument_description = query_type_data['arugmentDescription']
36+
self.description = query_type_data['description']
37+
self.id = query_type_data['monitorLevel']
38+
self.name = query_type_data['name']
39+
end
40+
end
41+
42+
##
43+
# This struct represents a network monitor response type used for configuring
44+
# network monitor responses when created.
45+
class NetworkMonitorResponseType < Struct.new(:action_description, :id, :level)
46+
def initialize(response_type_data)
47+
self.action_description = response_type_data['actionDescription']
48+
self.id = response_type_data['id']
49+
self.level = response_type_data['level']
50+
end
51+
end
52+
853
##
954
# Each SoftLayer NetworkMonitor instance provides information about network
1055
# monitors configured to check host ping or host ports of servers.
@@ -15,9 +60,7 @@ module SoftLayer
1560
class NetworkMonitor < ModelBase
1661
include ::SoftLayer::DynamicAttribute
1762

18-
@@available_query_types = nil
19-
@@available_response_actions = nil
20-
@@query_result_descriptions = {
63+
QUERY_RESULT_DESCRIPTIONS = {
2164
0 => "Down/Critical: Server is down and/or has passed the critical response threshold (extremely long ping response, abnormal behavior, etc.).",
2265
1 => "Warning - Server may be recovering from a previous down state, or may have taken too long to respond.",
2366
2 => "Up",
@@ -26,14 +69,17 @@ class NetworkMonitor < ModelBase
2669
5 => "Unknown - An unknown error has occurred. If the problem persists, contact support."
2770
}
2871

72+
@@available_query_types = nil
73+
@@available_response_types = nil
74+
2975
##
30-
# :attr_reader:
76+
# :attr_reader: argument_value
3177
# The argument to be used for this monitor, if necessary. The lowest monitoring levels (like ping)
3278
# ignore this setting, but higher levels like HTTP custom use it.
3379
sl_attr :argument_value, 'arg1Value'
3480

3581
##
36-
# :attr_reader:
82+
# :attr_reader: ip_address
3783
# The IP address to be monitored. Must be attached to the server on this object.
3884
sl_attr :ip_address, 'ipAddress'
3985

@@ -43,69 +89,72 @@ class NetworkMonitor < ModelBase
4389
sl_attr :status
4490

4591
##
46-
# :attr_reader:
92+
# :attr_reader: wait_cycles
4793
# The number of 5-minute cycles to wait before the "responseAction" is taken. If set to 0, the response
4894
# action will be taken immediately.
4995
sl_attr :wait_cycles, 'waitCycles'
5096

5197
##
5298
# The most recent result for this particular monitoring instance.
99+
# :call-seq:
100+
# last_query_result(force_update=false)
53101
sl_dynamic_attr :last_query_result do |resource|
54102
resource.should_update? do
55103
#only retrieved once per instance
56104
@last_query_result == nil
57105
end
58106

59107
resource.to_update do
60-
self.service.object_mask("mask[finishTime,responseStatus,responseTime]").getLastResult
108+
NetworkMonitorQueryResult.new(self.service.object_mask("mask[finishTime,responseStatus,responseTime]").getLastResult)
61109
end
62110
end
63111

64112
##
65113
# The type of monitoring query that is executed when this server is monitored.
114+
# :call-seq:
115+
# query_type(force_update=false)
66116
sl_dynamic_attr :query_type do |resource|
67117
resource.should_update? do
68118
#only retrieved once per instance
69119
@query_type == nil
70120
end
71121

72122
resource.to_update do
73-
self.service.getQueryType
123+
NetworkMonitorQueryType.new(self.service.getQueryType)
74124
end
75125
end
76126

77127
##
78-
# The action taken when a monitor fails.
79-
sl_dynamic_attr :response_action do |resource|
128+
# The response action taken when a monitor fails.
129+
# :call-seq:
130+
# response_type(force_update=false)
131+
sl_dynamic_attr :response_type do |resource|
80132
resource.should_update? do
81133
#only retrieved once per instance
82-
@response_action == nil
134+
@response_type == nil
83135
end
84136

85137
resource.to_update do
86-
self.service.getResponseAction
138+
NetworkMonitorResponseType.new(self.service.getResponseAction)
87139
end
88140
end
89141

90142
##
91143
# Add a network monitor for a host ping or port check to a server.
92144
#
93-
def self.add_network_monitor(server, ip_address, query_type, response_action, wait_cycles = 0, argument_value = nil, options = {})
145+
def self.add_network_monitor(server, ip_address, query_type, response_type, wait_cycles = 0, argument_value = nil, options = {})
94146
softlayer_client = options[:client] || Client.default_client
95147
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
96148
raise "#{__method__} requires a server to monitor but none was given" if !server || !server.kind_of?(Server)
97149
raise "#{__method__} requires an IP address to monitor but none was given" if !ip_address || ip_address.empty?
98-
raise "#{__method__} requires a query type for the monitor but none was given" if !query_type || (query_type.kind_of?(Hash) && !query_type.include?('id'))
99-
raise "#{__method__} requires a response action for the monitor but none was given" if !response_action || (response_action.kind_of?(Hash) && !response_action.include?('id'))
150+
raise "#{__method__} requires a query type for the monitor but none was given" if !query_type || !query_type.kind_of?(NetworkMonitorQueryType)
151+
raise "#{__method__} requires a response type for the monitor but none was given" if !response_type || !response_type.kind_of?(NetworkMonitorResponseType)
100152

101-
query_type_id = query_type.kind_of?(Hash) ? query_type['id'] : query_type
102-
response_action_id = response_action.kind_of?(Hash) ? response_action['id'] : response_action
103-
104-
if available_query_types(:client => softlayer_client, :query_level => server.network_monitor_levels['monitorLevel']).select{ |query_type| query_type['id'] == query_type_id }.empty?
153+
if available_query_types(:client => softlayer_client, :query_level => server.network_monitor_levels.monitor_level).select{ |query| query.id == query_type.id }.empty?
105154
raise "#{__method__} requested monitor query level is not supported for this server"
106155
end
107156

108-
if available_response_actions(:client => softlayer_client, :response_level => server.network_monitor_levels['responseLevel']).select{ |response| response['id'] == response_action_id }.empty?
157+
if available_response_types(:client => softlayer_client, :response_level => server.network_monitor_levels.response_level).select{ |response| response.id == response_type.id }.empty?
109158
raise "#{__method__} requested monitor response level is not supported for this server"
110159
end
111160

@@ -115,17 +164,17 @@ def self.add_network_monitor(server, ip_address, query_type, response_action, wa
115164
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.arg1Value').when_it is(argument_value.to_s) }
116165
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.' + server_id_label).when_it is(server.id) }
117166
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.ipAddress').when_it is(ip_address.to_s) }
118-
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.queryTypeId').when_it is(query_type_id) }
119-
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.responseActionId').when_it is(response_action_id) }
167+
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.queryTypeId').when_it is(query_type.id) }
168+
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.responseActionId').when_it is(response_type.id) }
120169
network_monitor_object_filter.modify { |filter| filter.accept('networkMonitors.waitCycles').when_it is(wait_cycles) }
121170

122171
if server.service.object_filter(network_monitor_object_filter).getNetworkMonitors.empty?
123172
network_monitor = softlayer_client[:Network_Monitor_Version1_Query_Host].createObject({
124173
'arg1Value' => argument_value.to_s,
125174
server_id_label => server.id,
126175
'ipAddress' => ip_address.to_s,
127-
'queryTypeId' => query_type_id,
128-
'responseActionId' => response_action_id,
176+
'queryTypeId' => query_type.id,
177+
'responseActionId' => response_type.id,
129178
'waitCycles' => wait_cycles
130179
})
131180

@@ -175,31 +224,33 @@ def self.available_query_types(options = {})
175224
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
176225

177226
unless @@available_query_types
178-
@@available_query_types = softlayer_client[:Network_Monitor_Version1_Query_Host_Stratum].getAllQueryTypes
227+
available_query_types_data = softlayer_client[:Network_Monitor_Version1_Query_Host_Stratum].getAllQueryTypes
228+
@@available_query_types = available_query_types_data.map{ |query_type| NetworkMonitorQueryType.new(query_type) }
179229
end
180230

181231
if options[:query_level]
182-
@@available_query_types.select { |query_type| query_type['monitorLevel'].to_i <= options[:query_level].to_i }
232+
@@available_query_types.select { |query_type| query_type.monitor_level.to_i <= options[:query_level].to_i }
183233
else
184234
@@available_query_types
185235
end
186236
end
187237

188238
##
189-
# Return the list of available response actions (optionally limited to a max response level)
239+
# Return the list of available response types (optionally limited to a max response level)
190240
#
191-
def self.available_response_actions(options = {})
241+
def self.available_response_types(options = {})
192242
softlayer_client = options[:client] || Client.default_client
193243
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
194244

195-
unless @@available_response_actions
196-
@@available_response_actions = softlayer_client[:Network_Monitor_Version1_Query_Host_Stratum].getAllResponseTypes
245+
unless @@available_response_types
246+
available_response_types_data = softlayer_client[:Network_Monitor_Version1_Query_Host_Stratum].getAllResponseTypes
247+
@@available_response_types = available_response_types_data.map { |response_type| NetworkMonitorResponseType.new(response_type) }
197248
end
198249

199250
if options[:response_level]
200-
@@available_response_actions.select { |response_action| response_action['level'].to_i <= options[:response_level].to_i }
251+
@@available_response_types.select { |response_type| response_type.level.to_i <= options[:response_level].to_i }
201252
else
202-
@@available_response_actions
253+
@@available_response_types
203254
end
204255
end
205256

@@ -223,18 +274,10 @@ def self.remove_network_monitor_notification_users(server, user_customers, optio
223274
user_customer_data
224275
end
225276

226-
current_user_customers = user_customers_data.map { |user_customer| user_customer['id'] }
227-
277+
current_user_customers = user_customers_data.map { |user_customer| user_customer['id'] }
228278
monitor_user_notification_object_filter = ObjectFilter.new()
229279

230-
monitor_user_notification_object_filter.set_criteria_for_key_path('monitoringUserNotification.userId',
231-
{
232-
'operation' => 'in',
233-
'options' => [{
234-
'name' => 'data',
235-
'value' => current_user_customers.map{ |uid| uid.to_s }
236-
}]
237-
})
280+
monitor_user_notification_object_filter.modify { |filter| filter.accept('monitoringUserNotification.userId').when_it is(current_user_customers) }
238281

239282
monitor_user_notification_data = server.service.object_filter(monitor_user_notification_object_filter).object_mask("mask[id]").getMonitoringUserNotification
240283

@@ -261,13 +304,6 @@ def self.remove_network_monitors(network_monitors, options = {})
261304
softlayer_client[:Network_Monitor_Version1_Query_Host].deleteObjects(network_monitors_data)
262305
end
263306

264-
##
265-
# Return the list of descriptions by result id for last query responses.
266-
#
267-
def self.query_result_descriptions
268-
@@query_result_descriptions
269-
end
270-
271307
##
272308
# Returns the service for interacting with this network monitor component through the network API
273309
#

0 commit comments

Comments
 (0)