Class: Staypuft::InterfaceAssigner

Inherits:
Object
  • Object
show all
Defined in:
app/models/staypuft/interface_assigner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deployment, interface, subnet) ⇒ InterfaceAssigner

Returns a new instance of InterfaceAssigner.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/staypuft/interface_assigner.rb', line 5

def initialize(deployment, interface, subnet)
  @deployment = deployment
  if interface.is_a?(Nic::Base)
    @interface = interface
  else
    # interface may be Host::Managed which means primary interface, so we create pseudo-interface object
    @interface = Nic::Managed.new(
        :mac => interface.mac,
        :virtual => false,
        :identifier => interface.primary_interface,
        :host => interface,
        :subnet => interface.subnet)
  end

  @host = @interface.host
  @subnet = subnet
  @errors = []
end

Instance Attribute Details

#deploymentObject

Returns the value of attribute deployment.



3
4
5
# File 'app/models/staypuft/interface_assigner.rb', line 3

def deployment
  @deployment
end

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'app/models/staypuft/interface_assigner.rb', line 3

def errors
  @errors
end

#interfaceObject

Returns the value of attribute interface.



3
4
5
# File 'app/models/staypuft/interface_assigner.rb', line 3

def interface
  @interface
end

#subnetObject

Returns the value of attribute subnet.



3
4
5
# File 'app/models/staypuft/interface_assigner.rb', line 3

def subnet
  @subnet
end

Instance Method Details

#assignObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/staypuft/interface_assigner.rb', line 24

def assign
  if virtual_assignment?
    if (existing = conflicting_interface)
      @errors.push _("Another interface %s on same physical interface with same VLAN exists") % existing.identifier
      return false
    end
    ActiveRecord::Base.transaction do
      unassign_from_other_nics
      assign_virtual
      raise ActiveRecord::Rollback if @errors.present?
      return true
    end
  else
    if @interface.subnet.present? && @interface.subnet != @subnet
      @errors.push _("Interface is already assigned to subnet %s") % @interface.subnet.name
      return false
    end
    ActiveRecord::Base.transaction do
      unassign_from_other_nics
      assign_physical
      raise ActiveRecord::Rollback if @errors.present?
      return true
    end
  end

  return false # we can get there only after rollback
end

#unassignObject



52
53
54
55
56
57
58
59
60
61
# File 'app/models/staypuft/interface_assigner.rb', line 52

def unassign
  base = @host.interfaces
  base = virtual_assignment? ? base.virtual : base.physical
  ActiveRecord::Base.transaction do
    base.where(:subnet_id => @subnet.id).each do |interface|
      virtual_assignment? ? unassign_virtual(interface) : unassign_physical(interface)
    end
    raise ActiveRecord::Rollback if @errors.present?
  end
end

#virtual_assignment?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/staypuft/interface_assigner.rb', line 63

def virtual_assignment?
  @subnet.vlanid.present?
end