Class: Dcmgr::Models::InstanceNic

Inherits:
BaseNew
  • Object
show all
Defined in:
lib/dcmgr/models/instance_nic.rb

Overview

Network interface for running instance.

Constant Summary

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Instance Method Summary collapse

Methods inherited from BaseNew

Proxy, dataset, install_data, install_data_hooks, lock!, unlock!

Instance Method Details

#before_validationObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dcmgr/models/instance_nic.rb', line 26

def before_validation
  super
  m = normalize_mac_addr(self[:mac_addr])
  if m.size == 6
    # mac_addr looks like to only have vendor ID part so that
    # generate unique value for node ID part.
    mvendor = m
    begin
      m = mvendor + ("%02x%02x%02x" % [rand(0xff),rand(0xff),rand(0xff)])
    end while self.class.find(:mac_addr=> m)
    self[:mac_addr] = m
  end
  true
end

#pretty_mac_addr(delim = ':') ⇒ Object



53
54
55
# File 'lib/dcmgr/models/instance_nic.rb', line 53

def pretty_mac_addr(delim=':')
  self.mac_addr.unpack('A2'*6).join(delim)
end

#to_hashObject



20
21
22
23
24
# File 'lib/dcmgr/models/instance_nic.rb', line 20

def to_hash
  h = values.dup.merge(super)
  h.delete(:instance_id)
  h
end

#validateObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dcmgr/models/instance_nic.rb', line 41

def validate
  super

  unless self.mac_addr.size == 12
    errors.add(:mac_addr, "Invalid mac address length: #{self.mac_addr}")
  end

  unless self.mac_addr =~ /^[0-9a-f]{12}$/
    errors.add(:mac_addr, "Invalid mac address syntax: #{self.mac_addr}")
  end
end