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, default_row_lock_mode=, install_data, install_data_hooks, lock!, unlock!, #with_timestamps?

Instance Method Details

#before_destroyObject



54
55
56
57
58
# File 'lib/dcmgr/models/instance_nic.rb', line 54

def before_destroy
  MacLease.find(:mac_addr=>self.mac_addr).destroy
  ip_dataset.destroy
  super
end

#before_validationObject



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

def before_validation
  newlease=nil
  m = self[:mac_addr] = normalize_mac_addr(self[:mac_addr])
  if m
    if m.size == 6
      newlease = MacLease.lease(m)
    else
      MacLease.create(:mac_addr=>m)
    end
  else
    newlease = MacLease.lease()
  end
  self[:mac_addr] = newlease.mac_addr if newlease
  
  super
end

#pretty_mac_addr(delim = ':') ⇒ Object



68
69
70
# File 'lib/dcmgr/models/instance_nic.rb', line 68

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

#to_hashObject



31
32
33
34
35
# File 'lib/dcmgr/models/instance_nic.rb', line 31

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

#validateObject



60
61
62
63
64
65
66
# File 'lib/dcmgr/models/instance_nic.rb', line 60

def validate
  super

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