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!, #to_hash, unlock!, #with_timestamps?

Instance Method Details

#before_destroyObject



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

def before_destroy
  maclease = MacLease.find(:mac_addr=>self.mac_addr)
  maclease.destroy if maclease
  release_ip_lease
  super
end

#before_validationObject



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

def before_validation
  self[:mac_addr] = normalize_mac_addr(self[:mac_addr])

  # set maximum index number if the nic has no index value and
  # is attached to instance.
  if self.instance_id && self.device_index.nil?
    max_idx = self.class.alives.filter(:instance_id=>self.instance_id).max(:device_index)
    self.device_index = max_idx.nil? ? 0 : (max_idx + 1)
  end
  
  super
end

#deleteObject

Override the delete method to keep the row and just mark it as deleted



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

def delete
  self.deleted_at ||= Time.now
  self.save
end

#fqdn_hostnameObject



83
84
85
86
87
# File 'lib/dcmgr/models/instance_nic.rb', line 83

def fqdn_hostname
  raise "Instance is not associated: #{self.canonical_uuid}" if self.instance.nil?
  raise "Network is not associated: #{self.canonical_uuid}" if self.network.nil?
  sprintf("%s.%s.%s", self.instance.hostname, self.instance..uuid, self.network.domain_name)
end

#nat_fqdn_hostnameObject



89
90
91
92
93
# File 'lib/dcmgr/models/instance_nic.rb', line 89

def nat_fqdn_hostname
  raise "Instance is not associated: #{self.canonical_uuid}" if self.instance.nil?
  raise "Network is not associated: #{self.canonical_uuid}" if self.network.nil?
  sprintf("%s.%s.%s", self.instance.hostname, self.instance..uuid, self.nat_network.domain_name)
end

#pretty_mac_addr(delim = ':') ⇒ Object



79
80
81
# File 'lib/dcmgr/models/instance_nic.rb', line 79

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

#release_ip_leaseObject



27
28
29
# File 'lib/dcmgr/models/instance_nic.rb', line 27

def release_ip_lease
  ip_dataset.destroy
end

#to_api_documentObject



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

def to_api_document
  hash = super
  hash.delete(instance_id)
  hash
end

#validateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dcmgr/models/instance_nic.rb', line 57

def validate
  super

  # do not run validation if the row is maked as deleted.
  return true if self.deleted_at

  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
  if MacLease.find(:mac_addr=>self.mac_addr).nil?
    errors.add(:mac_addr, "MAC address is not on the MAC lease database.")
  end

  # find duplicate device index.
  if self.instance_id
    idx = self.class.alives.filter(:instance_id=>self.instance_id).select(:device_index).all
    if idx.uniq.size != idx.size
      errors.add(:device_index, "Duplicate device index.")
    end
  end
end