LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ruby - Need to select individual properties from objects in spacewalk api (https://www.linuxquestions.org/questions/programming-9/ruby-need-to-select-individual-properties-from-objects-in-spacewalk-api-4175550676/)

Thaidog 08-13-2015 07:56 AM

Ruby - Need to select individual properties from objects in spacewalk api
 
I have a ruby script that connects to spacewalk via API and returns properties about registered systems with an each loop:
Code:

ID.each { |x|

                p @client.call('system.getNetwork', @key, x.to_i)
                p @client.call('system.getRunningKernel', @key, x.to_i)


}

This will return something like this:
Code:

{"ip6"=>"::1", "hostname"=>"server003.domain001", "ip"=>"10.6.1.103"}
"2.6.32.x86_64"
{"ip6"=>"::1", "hostname"=>"server002.domain001", "ip"=>"10.6.1.102"}
"2.6.32.x86_64"
{"ip6"=>"::1", "hostname"=>"server001.domain001", "ip"=>"10.6.1.101"}
"2.6.32.x86_64"

Being new to ruby I don't know how to select individual properties from the objects returned. If you look at the relevant spacewalk api docs:

http://www.spacewalkproject.org/docu...RelevantErrata

You can see that the method system.getNetwork returns several properties:

struct - network info
string "ip" - IPv4 address of server
string "ip6" - IPv6 address of server
string "hostname" - Hostname of server

I would simply like to select maybe one of these properties like hostname... anyone know how to do that?

grail 08-13-2015 11:22 AM

Your @client.call is returning a hash in the first call and a string in the second. The second is obvious :)
The first can be walked through as well using one of the each, each_key, each_pair, each_value commands (see here for more details)

As a quick example:
Code:

@client.call('system.getNetwork', @key, x.to_i).each{ |k,v| puts "key is #{k} - value is #{v}" }

Thaidog 08-13-2015 02:09 PM

Quote:

Originally Posted by grail (Post 5405302)
Your @client.call is returning a hash in the first call and a string in the second. The second is obvious :)
The first can be walked through as well using one of the each, each_key, each_pair, each_value commands (see here for more details)

As a quick example:
Code:

@client.call('system.getNetwork', @key, x.to_i).each{ |k,v| puts "key is #{k} - value is #{v}" }

Thank you yes - I'm looking to just get output like this however:
Code:

server003.domain001
2.6.32.x86_64
server002.domain001
2.6.32.x86_64
server001.domain001
2.6.32.x86_64

Should something like this work?
@client.call('system.getNetwork', @key, x.to_i).select{hostname}

grail 08-14-2015 09:14 AM

I would try something like:
Code:

@client.call('system.getNetwork', @key, x.to_i)["hostname"]


All times are GMT -5. The time now is 11:57 PM.