softlayer / softlayer-ruby

http://softlayer.github.io/softlayer-ruby/
MIT License
54 stars 35 forks source link

Filter getHardware ResultSet #79

Closed jeunito closed 9 years ago

jeunito commented 9 years ago

Hi

I am trying to filter the result set of Account.getHardware. Is this possible?

obj_filter = SoftLayer::ObjectFilter.new do |filter|
  filter.accept("hardware.fullyQualifiedDomainName").when_it is(hardware)
end
accounts = sl.service_named("Account")

hardware_list =accounts.object_filter(obj_filter).object_mask("mask[id,fullyQualifiedDomainName]").getHardware
puts hardware_list

However I get the full list every time

SLsthompson commented 9 years ago

Returning the full list is the default behavior when the filter is not applied. Let me look into the problem a little bit more. You can solve the same problem a different way with:

softlayer_client = SoftLayer::Client.new()
servers = SoftLayer::BareMetalServer.find_servers(:hostname => "server_hostname", :domain => "server_domain.com", :client=> softlayer_client)
servers.each { |server| puts server.id, server.fullyQualifiedDomainName }
SLsthompson commented 9 years ago

Because the fullyQualifiedDomainName is a computed property (formed by jamming together the host and domain information) it cannot be used in an Object Filter.

You can however, add "accept" clauses to your filter for the hostname and domain name separately (as is done in my example).

jeunito commented 9 years ago

It works! Thanks @SLsthompson

Updated code for others:

obj_filter = SoftLayer::ObjectFilter.new do |filter|
  filter.accept("hardware.hostname").when_it is(hostname)
end
accounts = sl.service_named("Account")

hardware_list =accounts.object_filter(obj_filter).object_mask("mask[id,fullyQualifiedDomainName]").getHardware
puts hardware_list