Signed-off-by: Darryl L. Pierce <dpierce(a)redhat.com>
---
contrib/ruby/TODO | 2 +
contrib/ruby/examples/list_systems.rb | 2 +-
contrib/ruby/lib/cobbler/base.rb | 71 +++++++++++++++++--------
contrib/ruby/lib/cobbler/distro.rb | 2 +-
contrib/ruby/lib/cobbler/network_interface.rb | 2 +-
contrib/ruby/lib/cobbler/profile.rb | 2 +-
contrib/ruby/lib/cobbler/system.rb | 6 +-
contrib/ruby/test/test_system.rb | 6 +-
8 files changed, 61 insertions(+), 32 deletions(-)
diff --git a/contrib/ruby/TODO b/contrib/ruby/TODO
index 02bea04..f865e8d 100644
--- a/contrib/ruby/TODO
+++ b/contrib/ruby/TODO
@@ -1,3 +1,5 @@
This is a list of features to be developed in future.
* Cache auth tokens to avoid unnecessary logins.
+* Add hierarchical relationships, so that a Profile will load it's
+ Distro, a System will load it's Profile, etc.
\ No newline at end of file
diff --git a/contrib/ruby/examples/list_systems.rb b/contrib/ruby/examples/list_systems.rb
index fbee42c..ad277f8 100755
--- a/contrib/ruby/examples/list_systems.rb
+++ b/contrib/ruby/examples/list_systems.rb
@@ -57,7 +57,7 @@ System.find do |system|
if details
puts "\tOwner: #{system.owners}"
- system.interfaces.each { |nic| puts "\tNIC: #{nic.mac_address}"}
+ system.interfaces.each_pair { |id,nic| puts "\tNIC[#{id}]: #{nic.mac_address}"}
end
end
\ No newline at end of file
diff --git a/contrib/ruby/lib/cobbler/base.rb b/contrib/ruby/lib/cobbler/base.rb
index ea75ef3..52bf8d7 100644
--- a/contrib/ruby/lib/cobbler/base.rb
+++ b/contrib/ruby/lib/cobbler/base.rb
@@ -33,26 +33,40 @@ module Cobbler
# class System < Base
# cobbler_lifecycle :find_all => 'get_systems'
# cobbler_field :name
- # cobbler_field :owner, :array => 'String'
+ # cobbler_collection :owners, :type => 'String', :packing => :hash
# end
#
- # declares a class named Farkle that contains two fields. The first, "name",
- # will be one that is searchable on Cobbler; i.e., a method named "find_by_name"
- # will be generated and will use the "get_farkle" remote method to retrieve
- # that instance from Cobbler.
+ # declares a class named System that contains two fields and a class-level
+ # method.
#
- # The second field, "owner", will simply be a field named Farkle.owner that
- # returns a character string.
- #
- # +Base+ provides some common functionality for all child classes:
- #
+ # The first field, "name", is a simple property. It will be retrieved from
+ # the value "name" in the remote definition for a system, identifyed by the
+ # +:owner+ argument.
+ #
+ # The second field, "owners", is similarly retrieved from a property also
+ # named "owners" in the remote definition. However, this property is a
+ # collection: in this case, it is an array of definitions itself. The
+ # +:type+ argument identifies what the +local+ class type is that will be
+ # used to represent each element in the collection.
+ #
+ # A +cobbler_collection+ is packed in one of two ways: either as an array
+ # of values or as a hash of keys and associated values. These are defined by
+ # the +:packing+ argument with the values +Array+ and +Hash+, respectively.
+ #
+ # The +cobbler_lifecycle+ method allows for declaring different methods for
+ # retrieving remote instances of the class. These methods are:
+ #
+ # +find_one+ - the remote method to find a single instance,
+ # +find_all+ - the remote method to find all instances,
+ # +remove+ - the remote method to remote an instance
#
class Base
@@hostname = nil
@@connection = nil
+ @@auth_token = nil
- @defintions = nil
+ @definitions = nil
def initialize(definitions)
@definitions = definitions
@@ -60,8 +74,8 @@ module Cobbler
# Sets the connection. This method is only needed during unit testing.
#
- def self.connection=(mock)
- @@connection = mock
+ def self.connection=(connection)
+ @@connection = connection
end
# Returns or creates a new connection.
@@ -91,7 +105,7 @@ module Cobbler
# Logs into the Cobbler server.
#
def self.login
- make_call('login', @@username, @@password)
+ (@@auth_token || make_call('login', @@username, @@password))
end
# Makes a remote call.
@@ -122,7 +136,7 @@ module Cobbler
def self.hostname=(hostname)
@@hostname = hostname
end
-
+
class << self
# Creates a complete finder method
#
@@ -234,24 +248,37 @@ module Cobbler
#
def cobbler_collection(field, *args) # :nodoc:
classname = 'String'
- packing = :array
+ packing = 'Array'
# process collection definition
args.each do |arg|
classname = arg[:type] if arg[:type]
- packing = arg[:packing] if arg[:packing]
+ if arg[:packing]
+ case arg[:packing]
+ when :hash then packing = 'Hash'
+ when :array then packing = 'Array'
+ end
+ end
end
module_eval <<-"end;"
def #{field.to_s}(&block)
unless @#{field.to_s}
- @#{field.to_s} = Array.new
+ @#{field.to_s} = #{packing}.new
- definition('#{field.to_s}').each do |value|
- if value
- @#{field.to_s} << #{classname}.new(value)
- end
+ values = definition('#{field.to_s}')
+
+ case "#{packing}"
+ when "Array" then
+ values.each do |value|
+ @#{field.to_s} << #{classname}.new(value)
+ end
+
+ when "Hash" then
+ values.keys.each do |key|
+ @#{field.to_s}[key] = #{classname}.new(values[key])
+ end
end
end
diff --git a/contrib/ruby/lib/cobbler/distro.rb b/contrib/ruby/lib/cobbler/distro.rb
index c66d29d..b0e36fe 100644
--- a/contrib/ruby/lib/cobbler/distro.rb
+++ b/contrib/ruby/lib/cobbler/distro.rb
@@ -28,7 +28,7 @@ module Cobbler
:remove => 'remove_distro'
cobbler_field :name
- cobbler_field :owners
+ cobbler_collection :owners, :packing => :array
cobbler_field :kernel
cobbler_field :breed
cobbler_field :depth
diff --git a/contrib/ruby/lib/cobbler/network_interface.rb b/contrib/ruby/lib/cobbler/network_interface.rb
index 3437714..3308249 100644
--- a/contrib/ruby/lib/cobbler/network_interface.rb
+++ b/contrib/ruby/lib/cobbler/network_interface.rb
@@ -31,7 +31,7 @@ module Cobbler
cobbler_field :ip_address
def initialize(args)
- @definitions = args[1]
+ @definitions = args
end
# A hack for getting the NIC's details over the wire.
diff --git a/contrib/ruby/lib/cobbler/profile.rb b/contrib/ruby/lib/cobbler/profile.rb
index c8df8a3..d6338d7 100644
--- a/contrib/ruby/lib/cobbler/profile.rb
+++ b/contrib/ruby/lib/cobbler/profile.rb
@@ -31,7 +31,7 @@ module Cobbler
cobbler_field :name, :findable => 'get_profile'
cobbler_field :parent
- cobbler_field :owners
+ cobbler_collection :owners, :packing => :array
cobbler_field :dhcp_tag
cobbler_field :depth
cobbler_field :virt_file_size
diff --git a/contrib/ruby/lib/cobbler/system.rb b/contrib/ruby/lib/cobbler/system.rb
index c15d225..255391c 100644
--- a/contrib/ruby/lib/cobbler/system.rb
+++ b/contrib/ruby/lib/cobbler/system.rb
@@ -32,19 +32,19 @@ module Cobbler
cobbler_field :parent
cobbler_field :profile
cobbler_field :depth
- cobbler_field :kernel_options
+ cobbler_collection :kernel_options, :packing => :hash
cobbler_field :kickstart
- cobbler_field :ks_meta
+ cobbler_collection :ks_meta, :packing => :hash
cobbler_field :netboot_enabled
cobbler_collection :owners
cobbler_field :server
+ cobbler_collection :interfaces, :type => 'NetworkInterface', :packing => :hash
cobbler_field :virt_cpus
cobbler_field :virt_file_size
cobbler_field :virt_path
cobbler_field :virt_ram
cobbler_field :virt_type
cobbler_field :virt_bridge
- cobbler_collection :interfaces, :type => 'NetworkInterface', :packing => :hash
def initialize(definitions)
super(definitions)
diff --git a/contrib/ruby/test/test_system.rb b/contrib/ruby/test/test_system.rb
index cc64e84..e28da8b 100644
--- a/contrib/ruby/test/test_system.rb
+++ b/contrib/ruby/test/test_system.rb
@@ -44,7 +44,7 @@ module Cobbler
@profile = 'profile1'
@nics = Array.new
@nic_details = {'mac_address' => '00:11:22:33:44:55:66:77'}
- @nic = NetworkInterface.new(['intf0',@nic_details])
+ @nic = NetworkInterface.new(@nic_details)
@nics << @nic
@systems = Array.new
@@ -103,11 +103,11 @@ module Cobbler
@connection.should_receive(:call).with('get_systems').once.returns(@systems)
result = System.find
-
+
assert result, 'Expected a result set.'
assert_equal 2, result.size, 'Did not receive the right number of results.'
assert_equal 2, result[0].interfaces.size, 'Did not parse the NICs correctly.'
- result[0].interfaces.collect do |nic| assert_equal "00:11:22:33:44:55", nic.mac_address end
+ result[0].interfaces.keys.each { |intf| assert_equal "00:11:22:33:44:55", result[0].interfaces[intf].mac_address }
assert_equal 3, result[0].owners.size, 'Did not parse the owners correctly.'
end
--
1.5.5.1