Sha256: 79b6850f4a98f51300497e8565825d7dcd2d7939021813ccbe564a9682c0508f

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

# Represents a Host's network interface
# This class is the both parent
module Nic
  class Base < ActiveRecord::Base
    include Authorization
    include Foreman::STI

    self.table_name = 'nics'

    attr_accessible :host_id, :host,
                    :mac, :name,
                    :_destroy # used for nested_attributes

    before_validation :normalize_mac

    validates_uniqueness_of :mac
    validates_presence_of :mac
    validates_format_of :mac, :with => Net::Validations::MAC_REGEXP

    validate :uniq_with_hosts

    validates_presence_of :host

    scope :bootable, where(:type => "Nic::Bootable")
    scope :bmc, where(:type => "Nic::BMC")
    scope :interfaces, where(:type => "Nic::Interface")
    scope :managed, where(:type => "Nic::Managed")

    belongs_to_host :inverse_of => :interfaces, :class_name => "Host::Managed"
    # keep extra attributes needed for sub classes.
    serialize :attrs, Hash

    protected

    def uniq_fields_with_hosts
      [:mac]
    end

    # make sure we don't have a conflicting interface with an host record
    def uniq_with_hosts
      failed = false
      uniq_fields_with_hosts.each do |attr|
        value = self.send(attr)
        unless value.blank?
          if host.send(attr) == value
            errors.add(attr, _("Can't use the same value as the primary interface"))
            failed = true
          elsif Host.where(attr => value).limit(1).pluck(attr).any?
            errors.add(attr, _("already in use"))
            failed = true
          end
        end
      end
      !failed
    end

    def normalize_mac
      self.mac = Net::Validations.normalize_mac(mac)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman_discovery-1.0.0 test/foreman_app/app/models/nic.rb
foreman_discovery-1.0.0.rc4 test/foreman_app/app/models/nic.rb
foreman_discovery-1.0.0.rc3 test/foreman_app/app/models/nic.rb
foreman_discovery-1.0.0.rc2 test/foreman_app/app/models/nic.rb
foreman_discovery-1.0.0.rc1 test/foreman_app/app/models/nic.rb