28

I'm trying to write a script that will get a system ID from Red Hat Satellite/Spacewalk, which uses XMLRPC. I'm trying to get the ID which is the first value when using the XMLRPC client using the system name.

I'm referencing the documentation from Red Hat for the method used below:

#!/usr/bin/env ruby
require "xmlrpc/client"


@SATELLITE_URL = "satellite.rdu.salab.redhat.com"
@SATELLITE_API = "/rpc/api"
@SATELLITE_LOGIN = "********"
@SATELLITE_PASSWORD = "*******"

@client = XMLRPC::Client.new(@SATELLITE_URL, @SATELLITE_API)

@key = @client.call("auth.login", @SATELLITE_LOGIN, @SATELLITE_PASSWORD)

@getsystemid = @client.call("system.getId", @key, 'cfme038')

print "#{@getsystemid}"

@systemid = @getsystemid ['id']

The output of getsystemid looks like this:

[{"id"=>1000010466, "name"=>"cfme038", "last_checkin"=>#<XMLRPC::DateTime:0x007f9581042428 @year=2013, @month=12, @day=26, @hour=14, @min=31, @sec=28>}]

But when I try to just get just id I get this error:

no implicit conversion of String into Integer (TypeError)

Any help is appreciated

1 Answer 1

69

Write as

@systemid = @getsystemid[0]['id']

Your @getsystemid is not a Hash, it is an Array of Hash. @getsystemid[0] will give you the intended hash {"id"=>1000010466, "name"=>"cfme038", "last_checkin"=>#}. Now you can use Hash#[] method to access the value of the hash by using its keys.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.