lib/rubix/models/host.rb in rubix-0.1.1 vs lib/rubix/models/host.rb in rubix-0.1.2

- old
+ new

@@ -10,19 +10,35 @@ # server. BLANK_IP = '0.0.0.0' # The default port. DEFAULT_PORT = 10050 + + # The numeric codes for the various status types. + STATUS_CODES = { + :monitored => 0, + :not_monitored => 1, + :unreachable => 2, + :template => 3, + :deleted => 4, + :proxy_active => 5, + :proxy_passive => 6 + }.freeze + STATUS_NAMES = STATUS_CODES.invert.freeze - attr_accessor :name, :ip, :port, :profile, :status + attr_accessor :name, :ip, :port, :profile, :dns, :status + attr_writer :use_ip, :monitored def initialize properties={} super(properties) @name = properties[:name] @ip = properties[:ip] @port = properties[:port] @profile = properties[:profile] + @monitored = properties[:monitored] + @dns = properties[:dns] + @use_ip = properties[:use_ip] @status = properties[:status] self.host_group_ids = properties[:host_group_ids] self.host_groups = properties[:host_groups] @@ -31,10 +47,20 @@ self.user_macro_ids = properties[:user_macro_ids] self.user_macros = properties[:user_macros] end + def use_ip + return @use_ip if (!@use_ip.nil?) + @use_ip = true + end + + def monitored + return @monitored if (!@monitored.nil?) + @monitored = true + end + # # == Associations == # include Associations::HasManyHostGroups @@ -60,16 +86,21 @@ :groups => host_group_params, :templates => template_params, :macros => user_macro_params }.tap do |hp| hp[:profile] = profile if profile - hp[:status] = status if status + hp[:status] = (monitored ? 0 : 1) unless monitored.nil? - if ip - hp[:ip] = ip - hp[:useip] = true - hp[:port] = port || self.class::DEFAULT_PORT + case + when use_ip && (!ip.nil?) && (!ip.empty?) + hp[:useip] = 1 + hp[:ip] = ip + hp[:port] = port || self.class::DEFAULT_PORT + when (!dns.nil?) && (!dns.empty?) + hp[:useip] = 0 + hp[:dns] = dns + hp[:port] = port || self.class::DEFAULT_PORT else hp[:ip] = self.class::BLANK_IP end end end @@ -108,10 +139,19 @@ :name => host['host'], :host_group_ids => host['groups'].map { |group| group['groupid'].to_i }, :template_ids => host['parentTemplates'].map { |template| (template['templateid'] || template[id_field]).to_i }, :user_macros => host['macros'].map { |um| UserMacro.new(:host_id => um[id_field].to_i, :id => um['hostmacroid'], :value => um['value'], :macro => um['macro']) }, :profile => host['profile'], - :port => host['port'] + :port => host['port'], + :ip => host['ip'], + :dns => host['dns'], + :use_ip => (host['useip'].to_i == '1'), + + # If the status is '1' then this is an unmonitored host. + # Otherwise it's either '0' for monitored and ok or + # something else for monitored and *not* ok. + :monitored => (host['status'].to_i == 1 ? false : true), + :status => self::STATUS_NAMES[host['status'].to_i] }) end def self.get_params super().merge({:select_groups => :refer, :selectParentTemplates => :refer, :select_profile => :refer, :select_macros => :extend})