Skip to content

Commit c473376

Browse files
committed
Merge pull request #94 from ju2wheels/object_filters
Add new ObjectFilter helper methods and cleanup usage of ObjectFilters
2 parents 8882c35 + 2503599 commit c473376

13 files changed

Lines changed: 261 additions & 340 deletions

lib/softlayer/AccountPassword.rb

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ def password=(password)
7474
# If no client can be found the routine will raise an error.
7575
#
7676
# You may filter the list returned by adding options:
77-
# * <b>+:datacenter+</b> (string) - Include network storage account passwords from servers matching this datacenter
78-
# * <b>+:domain+</b> (string) - Include network storage account passwords from servers matching this domain
79-
# * <b>+:hostname+</b> (string) - Include network storage account passwords from servers matching this hostname
80-
# * <b>+:network_storage_server_type+</b> (string) - Include network storage account passwords attached to this server type
81-
# * <b>+:network_storage_type+</b> (string) - Include network storage account passwords from devices of this storage type
82-
# * <b>+:tags+</b> (Array) - Include network storage account passwords from servers matching these tags
83-
# * <b>+:username+</b> (string) - Include network storage account passwords with this username only
77+
# * <b>+:datacenter+</b> (string/array) - Include network storage account passwords from servers matching this datacenter
78+
# * <b>+:domain+</b> (string/array) - Include network storage account passwords from servers matching this domain
79+
# * <b>+:hostname+</b> (string/array) - Include network storage account passwords from servers matching this hostname
80+
# * <b>+:network_storage_server_type+</b> (string) - Include network storage account passwords attached to this server type
81+
# * <b>+:network_storage_type+</b> (string) - Include network storage account passwords from devices of this storage type
82+
# * <b>+:tags+</b> (string/array) - Include network storage account passwords from servers matching these tags
83+
# * <b>+:username+</b> (string/array) - Include network storage account passwords with this username only
8484
#
8585
def self.find_network_storage_account_passwords(options_hash = {})
8686
softlayer_client = options_hash[:client] || Client.default_client
@@ -119,10 +119,12 @@ def self.find_network_storage_account_passwords(options_hash = {})
119119
:account_password => {
120120
:username => "accountPassword.username"
121121
},
122-
:datacenter => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.datacenter.name' ].join },
123-
:domain => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.domain' ].join },
124-
:hostname => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.hostname' ].join },
125-
:tags => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.tagReferences.tag.name' ].join }
122+
:network_storage => {
123+
:datacenter => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.datacenter.name' ].join },
124+
:domain => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.domain' ].join },
125+
:hostname => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.hostname' ].join },
126+
:tags => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.tagReferences.tag.name' ].join }
127+
}
126128
}
127129

128130
if options_hash[:network_storage_type]
@@ -134,24 +136,13 @@ def self.find_network_storage_account_passwords(options_hash = {})
134136
if options_hash[:network_storage_server_type]
135137
network_storage_type = options_hash[:network_storage_type] || :network_storage
136138

137-
[ :datacenter, :domain, :hostname ].each do |option|
139+
option_to_filter_path[:network_storage].keys.each do |option|
138140
if options_hash[option]
139141
network_storage_object_filter.modify do |filter|
140-
filter.accept(option_to_filter_path[option].call(network_storage_type, options_hash[:network_storage_server_type])).when_it is(options_hash[option])
142+
filter.accept(option_to_filter_path[:network_storage][option].call(network_storage_type, options_hash[:network_storage_server_type])).when_it is(options_hash[option])
141143
end
142144
end
143145
end
144-
145-
if options_hash[:tags]
146-
network_storage_object_filter.set_criteria_for_key_path(option_to_filter_path[:tags].call(network_storage_type, options_hash[:network_storage_server_type]),
147-
{
148-
'operation' => 'in',
149-
'options' => [{
150-
'name' => 'data',
151-
'value' => options_hash[:tags].collect{ |tag_value| tag_value.to_s }
152-
}]
153-
})
154-
end
155146
end
156147

157148
option_to_filter_path[:account_password].each do |option, filter_path|
@@ -201,13 +192,13 @@ def self.find_network_storage_account_passwords(options_hash = {})
201192
# If no client can be found the routine will raise an error.
202193
#
203194
# You may filter the list returned by adding options:
204-
# * <b>+:datacenter+</b> (string) - Include network storage webcc passwords from servers matching this datacenter
205-
# * <b>+:domain+</b> (string) - Include network storage webcc passwords from servers matching this domain
206-
# * <b>+:hostname+</b> (string) - Include network storage webcc passwords from servers matching this hostname
207-
# * <b>+:network_storage_server_type+</b> (string) - Include network storage webcc passwords attached to this server type
208-
# * <b>+:network_storage_type+</b> (string) - Include network storage webcc passwords from devices of this storage type
209-
# * <b>+:tags+</b> (Array) - Include network storage webcc passwords from servers matching these tags
210-
# * <b>+:username+</b> (string) - Include network storage webcc passwords with this username only
195+
# * <b>+:datacenter+</b> (string/array) - Include network storage webcc passwords from servers matching this datacenter
196+
# * <b>+:domain+</b> (string/array) - Include network storage webcc passwords from servers matching this domain
197+
# * <b>+:hostname+</b> (string/array) - Include network storage webcc passwords from servers matching this hostname
198+
# * <b>+:network_storage_server_type+</b> (string) - Include network storage webcc passwords attached to this server type
199+
# * <b>+:network_storage_type+</b> (string) - Include network storage webcc passwords from devices of this storage type
200+
# * <b>+:tags+</b> (string/array) - Include network storage webcc passwords from servers matching these tags
201+
# * <b>+:username+</b> (string/array) - Include network storage webcc passwords with this username only
211202
#
212203
def self.find_network_storage_webcc_passwords(options_hash = {})
213204
softlayer_client = options_hash[:client] || Client.default_client
@@ -243,11 +234,13 @@ def self.find_network_storage_webcc_passwords(options_hash = {})
243234
}
244235

245236
option_to_filter_path = {
246-
:datacenter => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.datacenter.name' ].join },
247-
:domain => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.domain' ].join },
248-
:hostname => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.hostname' ].join },
249-
:tags => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.tagReferences.tag.name' ].join },
250-
:webcc_password => {
237+
:network_storage => {
238+
:datacenter => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.datacenter.name' ].join },
239+
:domain => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.domain' ].join },
240+
:hostname => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.hostname' ].join },
241+
:tags => lambda { |storage_type, server_type| return [ filter_label[storage_type], '.', filter_label[server_type], '.tagReferences.tag.name' ].join }
242+
},
243+
:webcc_password => {
251244
:username => "webccAccount.username"
252245
}
253246
}
@@ -261,24 +254,13 @@ def self.find_network_storage_webcc_passwords(options_hash = {})
261254
if options_hash[:network_storage_server_type]
262255
network_storage_type = options_hash[:network_storage_type] || :network_storage
263256

264-
[ :datacenter, :domain, :hostname ].each do |option|
257+
option_to_filter_path[:network_storage].keys.each do |option|
265258
if options_hash[option]
266259
network_storage_object_filter.modify do |filter|
267-
filter.accept(option_to_filter_path[option].call(network_storage_type, options_hash[:network_storage_server_type])).when_it is(options_hash[option])
260+
filter.accept(option_to_filter_path[:network_storage][option].call(network_storage_type, options_hash[:network_storage_server_type])).when_it is(options_hash[option])
268261
end
269262
end
270263
end
271-
272-
if options_hash[:tags]
273-
network_storage_object_filter.set_criteria_for_key_path(option_to_filter_path[:tags].call(network_storage_type, options_hash[:network_storage_server_type]),
274-
{
275-
'operation' => 'in',
276-
'options' => [{
277-
'name' => 'data',
278-
'value' => options_hash[:tags].collect{ |tag_value| tag_value.to_s }
279-
}]
280-
})
281-
end
282264
end
283265

284266
option_to_filter_path[:webcc_password].each do |option, filter_path|

lib/softlayer/BareMetalServer.rb

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ def self.server_with_id(server_id, options = {})
183183
#
184184
# You may filter the list returned by adding options:
185185
#
186-
# * <b>+:tags+</b> (array) - an array of strings representing tags to search for on the instances
187-
# * <b>+:cpus+</b> (int) - return servers with the given number of (virtual) CPUs
188-
# * <b>+:memory+</b> (int) - return servers with at least the given amount of memory (in Gigabytes)
189-
# * <b>+:hostname+</b> (string) - return servers whose hostnames match the query string given (see ObjectFilter::query_to_filter_operation)
190-
# * <b>+:domain+</b> (string) - filter servers to those whose domain matches the query string given (see ObjectFilter::query_to_filter_operation)
191-
# * <b>+:datacenter+</b> (string) - find servers whose data center name matches the query string given (see ObjectFilter::query_to_filter_operation)
192-
# * <b>+:nic_speed+</b> (int) - include servers with the given nic speed (in Mbps)
193-
# * <b>+:public_ip+</b> (string) - return servers whose public IP address matches the query string given (see ObjectFilter::query_to_filter_operation)
194-
# * <b>+:private_ip+</b> (string) - same as :public_ip, but for private IP addresses
186+
# * <b>+:tags+</b> (string/array) - an array of strings representing tags to search for on the instances
187+
# * <b>+:cpus+</b> (int/array) - return servers with the given number of (virtual) CPUs
188+
# * <b>+:memory+</b> (int/array) - return servers with at least the given amount of memory (in Gigabytes)
189+
# * <b>+:hostname+</b> (string/array) - return servers whose hostnames match the query string given (see ObjectFilter::query_to_filter_operation)
190+
# * <b>+:domain+</b> (string/array) - filter servers to those whose domain matches the query string given (see ObjectFilter::query_to_filter_operation)
191+
# * <b>+:datacenter+</b> (string/array) - find servers whose data center name matches the query string given (see ObjectFilter::query_to_filter_operation)
192+
# * <b>+:nic_speed+</b> (int/array) - include servers with the given nic speed (in Mbps)
193+
# * <b>+:public_ip+</b> (string/array) - return servers whose public IP address matches the query string given (see ObjectFilter::query_to_filter_operation)
194+
# * <b>+:private_ip+</b> (string/array) - same as :public_ip, but for private IP addresses
195195
#
196196
# Additionally you may provide options related to the request itself:
197197
#
@@ -210,14 +210,15 @@ def self.find_servers(options_hash = {})
210210
end
211211

212212
option_to_filter_path = {
213-
:cpus => "hardware.processorPhysicalCoreAmount",
214-
:memory => "hardware.memoryCapacity",
215-
:hostname => "hardware.hostname",
216-
:domain => "hardware.domain",
213+
:cpus => "hardware.processorPhysicalCoreAmount",
214+
:memory => "hardware.memoryCapacity",
215+
:hostname => "hardware.hostname",
216+
:domain => "hardware.domain",
217217
:datacenter => "hardware.datacenter.name",
218-
:nic_speed => "hardware.networkComponents.maxSpeed",
219-
:public_ip => "hardware.primaryIpAddress",
220-
:private_ip => "hardware.primaryBackendIpAddress"
218+
:nic_speed => "hardware.networkComponents.maxSpeed",
219+
:public_ip => "hardware.primaryIpAddress",
220+
:private_ip => "hardware.primaryBackendIpAddress",
221+
:tags => "hardware.tagReferences.tag.name"
221222
}
222223

223224
# For each of the options in the option_to_filter_path map, if the options hash includes
@@ -227,17 +228,6 @@ def self.find_servers(options_hash = {})
227228
object_filter.modify { |filter| filter.accept(filter_path).when_it is(options_hash[option])} if options_hash[option]
228229
end
229230

230-
# Tags get a much more complex object filter operation so we handle them separately
231-
if options_hash.has_key?(:tags)
232-
object_filter.set_criteria_for_key_path("hardware.tagReferences.tag.name", {
233-
'operation' => 'in',
234-
'options' => [{
235-
'name' => 'data',
236-
'value' => options_hash[:tags].collect{ |tag_value| tag_value.to_s }
237-
}]
238-
} );
239-
end
240-
241231
account_service = softlayer_client[:Account]
242232
account_service = account_service.object_filter(object_filter) unless object_filter.empty?
243233
account_service = account_service.object_mask(default_object_mask.to_sl_object_mask)

0 commit comments

Comments
 (0)