lib/rubix/models/host_group.rb in rubix-0.0.1 vs lib/rubix/models/host_group.rb in rubix-0.0.2

- old
+ new

@@ -1,74 +1,63 @@ module Rubix class HostGroup < Model - attr_accessor :name, :host_ids + # + # == Properties & Finding == + # def initialize properties={} super(properties) - @name = properties[:name] + @name = properties[:name] + + self.host_ids = properties[:host_ids] + self.hosts = properties[:hosts] end + + attr_accessor :name + def self.find_request options={} + request('hostgroup.get', 'filter' => {'groupid' => options[:id], 'name' => options[:name]}, 'select_hosts' => 'refer', 'output' => 'extend') + end + + def self.build host_group + new({ + :id => host_group['groupid'].to_i, + :name => host_group['name'], + :host_ids => host_group['hosts'].map { |host_info| host_info['hostid'].to_i } + }) + end + def log_name "GROUP #{name || id}" end - - def load - response = request('hostgroup.get', 'filter' => {'groupid' => id, 'name' => name}, 'select_hosts' => 'refer', 'output' => 'extend') - case - when response.has_data? - @id = response.first['groupid'].to_i - @name = response.first['name'] - @host_ids = response.first['hosts'].map { |host_info| host_info['hostid'].to_i } - @exists = true - @loaded = true - when response.success? - @exists = false - @loaded = true - else - error("Could not load: #{response.error_message}") - end - end - def create - response = request('hostgroup.create', [{'name' => name}]) - if response.has_data? - @id = response['groupids'].first.to_i - @exists = true - @loaded = true - info("Created") - else - error("Could not create: #{response.error_message}") - end + def self.id_field + 'groupid' end + - def update - # noop - info("Updated") - end + # + # == Associations == + # + + include Associations::HasManyHosts - def destroy - response = request('hostgroup.delete', [{'groupid' => id}]) - case - when response.has_data? && response['groupids'].first.to_i == id - info("Deleted") - when response.zabbix_error? && response.error_message =~ /does not exist/i - # was never there... - else - error("Could not delete: #{response.error_message}.") - end + # + # == CRUD == + # + + def create_request + request('hostgroup.create', [{'name' => name}]) end - def contains? host - return unless exists? - host_ids.include?(host.id) + def update_request + request('hostgroup.update', [{'groupid' => id, 'name' => name}]) end - def self.find_or_create_by_name name - new(:name => name).tap do |group| - group.create unless group.exists? - end + def destroy_request + request('hostgroup.delete', [{'groupid' => id}]) end - + end end