diff options
author | Darryl L. Pierce <dpierce@redhat.com> | 2008-08-13 13:09:27 -0400 |
---|---|---|
committer | Darryl L. Pierce <dpierce@redhat.com> | 2008-08-14 08:27:27 -0400 |
commit | d7d731ffa00bbd50a49750940006ab53f6cad2fa (patch) | |
tree | 332a1493de538d1b2c6ae902fc8fcfcabc80f547 /contrib/ruby/lib | |
parent | 5ec6348575e18f942902fc039cca24480d14984b (diff) | |
download | cobbler-d7d731ffa00bbd50a49750940006ab53f6cad2fa.tar.gz cobbler-d7d731ffa00bbd50a49750940006ab53f6cad2fa.tar.xz cobbler-d7d731ffa00bbd50a49750940006ab53f6cad2fa.zip |
The auth_token is now cached as Cobbler::Base@@auth_token.
Also fixed up some documentation and how properties are handled that
are either arrays of values or hashes.
Diffstat (limited to 'contrib/ruby/lib')
-rw-r--r-- | contrib/ruby/lib/cobbler/base.rb | 73 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler/distro.rb | 2 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler/network_interface.rb | 2 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler/profile.rb | 2 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler/system.rb | 6 |
5 files changed, 57 insertions, 28 deletions
diff --git a/contrib/ruby/lib/cobbler/base.rb b/contrib/ruby/lib/cobbler/base.rb index ea75ef36..7446ad28 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,13 +105,14 @@ 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. # def self.make_call(*args) raise Exception.new('No connection established.') unless @@connection + @@connection.call(*args) end @@ -105,6 +120,7 @@ module Cobbler # def self.end_transaction @@connection = nil + @@auth_token = nil end def definition(key) @@ -122,7 +138,7 @@ module Cobbler def self.hostname=(hostname) @@hostname = hostname end - + class << self # Creates a complete finder method # @@ -234,24 +250,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 c66d29d7..b0e36fec 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 3437714f..3308249a 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 c8df8a33..d6338d76 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 c15d2257..255391c7 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) |