Skip to content

Commit ad346cf

Browse files
committed
Move add/remove monitor notification user methods from VirtualServer/BareMetalServer to NetworkMonitor
1 parent 4a634b3 commit ad346cf

3 files changed

Lines changed: 76 additions & 138 deletions

File tree

lib/softlayer/BareMetalServer.rb

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,6 @@ module SoftLayer
1818
#
1919
class BareMetalServer < Server
2020

21-
##
22-
# Add user customers to the list of users notified on monitor failure. Accepts a list of UserCustomer
23-
# instances or user customer usernames.
24-
#
25-
def add_network_monitor_notification_users(user_customers)
26-
raise "#{__method__} requires a list user customers but none was given" if !user_customers || user_customers.empty?
27-
28-
user_customers_data = user_customers.map do |user_customer|
29-
raise "#{__method__} requires a user customer but none was given" if !user_customer || (!user_customer.class.method_defined?(:username) && user_customer.empty?)
30-
31-
user_customer_data = user_customer.class.method_defined?(:username) ? user_customer : UserCustomer.user_customer_with_username(user_customer, softlayer_client)
32-
33-
raise "#{__method__} user customer with username #{user_customer.inspect} not found" unless user_customer_data
34-
35-
user_customer_data
36-
end
37-
38-
current_user_customers = self.notified_monitor_users.map { |notified_monitor_user| notified_monitor_user['id'] }
39-
40-
user_customers_data.delete_if { |user_customer| current_user_customers.include?(user_customer['id']) }
41-
42-
unless user_customers_data.empty?
43-
user_customer_notifications = user_customers_data.map { |user_customer| { 'hardwareId' => self.id, 'userId' => user_customer['id'] } }
44-
45-
softlayer_client[:User_Customer_Notification_Hardware].createObjects(user_customer_notifications)
46-
47-
@notified_monitor_users = nil
48-
end
49-
end
50-
5121
##
5222
# Returns true if this +BareMetalServer+ is actually a Bare Metal Instance
5323
# a Bare Metal Instance is physical, hardware server that is is provisioned to
@@ -91,45 +61,6 @@ def remote_management_accounts
9161
self['remoteManagementAccounts']
9262
end
9363

94-
##
95-
# Rmove user customers from the list of users notified on monitor failure. Accepts a list of UserCustomer
96-
# instances or user customer usernames.
97-
#
98-
def remove_network_monitor_notification_users(user_customers)
99-
raise "#{__method__} requires a list user customers but none was given" if !user_customers || user_customers.empty?
100-
101-
user_customers_data = user_customers.map do |user_customer|
102-
raise "#{__method__} requires a user customer but none was given" if !user_customer || (!user_customer.class.method_defined?(:username) && user_customer.empty?)
103-
104-
user_customer_data = user_customer.class.method_defined?(:username) ? user_customer : UserCustomer.user_customer_with_username(user_customer, softlayer_client)
105-
106-
raise "#{__method__} user customer with username #{user_customer.inspect} not found" unless user_customer_data
107-
108-
user_customer_data
109-
end
110-
111-
current_user_customers = user_customers_data.map { |user_customer| user_customer['id'] }
112-
113-
monitor_user_notification_object_filter = ObjectFilter.new()
114-
115-
monitor_user_notification_object_filter.set_criteria_for_key_path('monitoringUserNotification.userId',
116-
{
117-
'operation' => 'in',
118-
'options' => [{
119-
'name' => 'data',
120-
'value' => current_user_customers.map{ |uid| uid.to_s }
121-
}]
122-
})
123-
124-
monitor_user_notification_data = self.service.object_filter(monitor_user_notification_object_filter).object_mask("mask[id]").getMonitoringUserNotification
125-
126-
unless monitor_user_notification_data.empty?
127-
softlayer_client[:User_Customer_Notification_Hardware].deleteObjects(monitor_user_notification_data)
128-
129-
@notified_monitor_users = nil
130-
end
131-
end
132-
13364
##
13465
# Returns the typical Service used to work with this Server
13566
# For Bare Metal Servers that is +SoftLayer_Hardware+ though in some special cases

lib/softlayer/NetworkMonitor.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,40 @@ def self.add_network_monitor(server, ip_address, query_type, response_action, wa
133133
end
134134
end
135135

136+
##
137+
# Add user customers to the list of users notified on monitor failure for the specified server. Accepts a list of UserCustomer
138+
# instances or user customer usernames.
139+
#
140+
def self.add_network_monitor_notification_users(server, user_customers, options = {})
141+
softlayer_client = options[:client] || Client.default_client
142+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
143+
raise "#{__method__} requires a server to monitor but none was given" if !server || !server.kind_of?(Server)
144+
raise "#{__method__} requires a list user customers but none was given" if !user_customers || user_customers.empty?
145+
146+
user_customers_data = user_customers.map do |user_customer|
147+
raise "#{__method__} requires a user customer but none was given" if !user_customer || (!user_customer.class.method_defined?(:username) && user_customer.empty?)
148+
149+
user_customer_data = user_customer.class.method_defined?(:username) ? user_customer : UserCustomer.user_customer_with_username(user_customer, softlayer_client)
150+
151+
raise "#{__method__} user customer with username #{user_customer.inspect} not found" unless user_customer_data
152+
153+
user_customer_data
154+
end
155+
156+
current_user_customers = server.notified_network_monitor_users.map { |notified_network_monitor_user| notified_network_monitor_user['id'] }
157+
158+
user_customers_data.delete_if { |user_customer| current_user_customers.include?(user_customer['id']) }
159+
160+
unless user_customers_data.empty?
161+
notification_monitor_user_service = server.kind_of?(VirtualServer) ? :User_Customer_Notification_Virtual_Guest : :User_Customer_Notification_Hardware
162+
server_id_label = server.kind_of?(VirtualServer) ? 'guestId' : 'hardwareId'
163+
164+
user_customer_notifications = user_customers_data.map { |user_customer| { server_id_label => server.id, 'userId' => user_customer['id'] } }
165+
166+
softlayer_client[notification_monitor_user_service].createObjects(user_customer_notifications)
167+
end
168+
end
169+
136170
##
137171
# Return the list of available query types (optionally limited to a max query level)
138172
#
@@ -169,6 +203,48 @@ def self.available_response_actions(options = {})
169203
end
170204
end
171205

206+
##
207+
# Rmove user customers from the list of users notified on monitor failure for the specified server. Accepts a list of UserCustomer
208+
# instances or user customer usernames.
209+
#
210+
def self.remove_network_monitor_notification_users(server, user_customers, options = {})
211+
softlayer_client = options[:client] || Client.default_client
212+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
213+
raise "#{__method__} requires a server to monitor but none was given" if !server || !server.kind_of?(Server)
214+
raise "#{__method__} requires a list user customers but none was given" if !user_customers || user_customers.empty?
215+
216+
user_customers_data = user_customers.map do |user_customer|
217+
raise "#{__method__} requires a user customer but none was given" if !user_customer || (!user_customer.kind_of?(UserCustomer) && user_customer.empty?)
218+
219+
user_customer_data = user_customer.kind_of?(UserCustomer) ? user_customer : UserCustomer.user_customer_with_username(user_customer, softlayer_client)
220+
221+
raise "#{__method__} user customer with username #{user_customer.inspect} not found" unless user_customer_data
222+
223+
user_customer_data
224+
end
225+
226+
current_user_customers = user_customers_data.map { |user_customer| user_customer['id'] }
227+
228+
monitor_user_notification_object_filter = ObjectFilter.new()
229+
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+
})
238+
239+
monitor_user_notification_data = server.service.object_filter(monitor_user_notification_object_filter).object_mask("mask[id]").getMonitoringUserNotification
240+
241+
unless monitor_user_notification_data.empty?
242+
notification_monitor_user_service = server.kind_of?(VirtualServer) ? :User_Customer_Notification_Virtual_Guest : :User_Customer_Notification_Hardware
243+
244+
softlayer_client[notification_monitor_user_service].deleteObjects(monitor_user_notification_data)
245+
end
246+
end
247+
172248
##
173249
# Removes the list of network monitors from their associated servers
174250
#

lib/softlayer/VirtualServer.rb

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,6 @@ class VirtualServer < Server
8484
end
8585
end
8686

87-
##
88-
# Add user customers to the list of users notified on monitor failure. Accepts a list of UserCustomer
89-
# instances or user customer usernames.
90-
#
91-
def add_network_monitor_notification_users(user_customers)
92-
raise "#{__method__} requires a list user customers but none was given" if !user_customers || user_customers.empty?
93-
94-
user_customers_data = user_customers.map do |user_customer|
95-
raise "#{__method__} requires a user customer but none was given" if !user_customer || (!user_customer.class.method_defined?(:username) && user_customer.empty?)
96-
97-
user_customer_data = user_customer.class.method_defined?(:username) ? user_customer : UserCustomer.user_customer_with_username(user_customer, softlayer_client)
98-
99-
raise "#{__method__} user customer with username #{user_customer.inspect} not found" unless user_customer_data
100-
101-
user_customer_data
102-
end
103-
104-
current_user_customers = self.notified_monitor_users.map { |notified_monitor_user| notified_monitor_user['id'] }
105-
106-
user_customers_data.delete_if { |user_customer| current_user_customers.include?(user_customer['id']) }
107-
108-
unless user_customers_data.empty?
109-
user_customer_notifications = user_customers_data.map { |user_customer| { 'guestId' => self.id, 'userId' => user_customer['id'] } }
110-
111-
softlayer_client[:User_Customer_Notification_Virtual_Guest].createObjects(user_customer_notifications)
112-
113-
@notified_monitor_users = nil
114-
end
115-
end
116-
11787
##
11888
# IMMEDIATELY cancel this virtual server
11989
#
@@ -151,45 +121,6 @@ def capture_image(image_name, include_attached_storage = false, image_notes = ''
151121
image_templates[0] if !image_templates.empty?
152122
end
153123

154-
##
155-
# Rmove user customers from the list of users notified on monitor failure. Accepts a list of UserCustomer
156-
# instances or user customer usernames.
157-
#
158-
def remove_network_monitor_notification_users(user_customers)
159-
raise "#{__method__} requires a list user customers but none was given" if !user_customers || user_customers.empty?
160-
161-
user_customers_data = user_customers.map do |user_customer|
162-
raise "#{__method__} requires a user customer but none was given" if !user_customer || (!user_customer.class.method_defined?(:username) && user_customer.empty?)
163-
164-
user_customer_data = user_customer.class.method_defined?(:username) ? user_customer : UserCustomer.user_customer_with_username(user_customer, softlayer_client)
165-
166-
raise "#{__method__} user customer with username #{user_customer.inspect} not found" unless user_customer_data
167-
168-
user_customer_data
169-
end
170-
171-
current_user_customers = user_customers_data.map { |user_customer| user_customer['id'] }
172-
173-
monitor_user_notification_object_filter = ObjectFilter.new()
174-
175-
monitor_user_notification_object_filter.set_criteria_for_key_path('monitoringUserNotification.userId',
176-
{
177-
'operation' => 'in',
178-
'options' => [{
179-
'name' => 'data',
180-
'value' => current_user_customers.map{ |uid| uid.to_s }
181-
}]
182-
})
183-
184-
monitor_user_notification_data = self.service.object_filter(monitor_user_notification_object_filter).object_mask("mask[id]").getMonitoringUserNotification
185-
186-
unless monitor_user_notification_data.empty?
187-
softlayer_client[:User_Customer_Notification_Virtual_Guest].deleteObjects(monitor_user_notification_data)
188-
189-
@notified_monitor_users = nil
190-
end
191-
end
192-
193124
##
194125
# Repeatedly polls the API to find out if this server is 'ready'.
195126
#

0 commit comments

Comments
 (0)