Module: ForemanFogProxmox::ProxmoxInterfaces

Included in:
Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_interfaces.rb

Instance Method Summary collapse

Instance Method Details

#cidr_prefix(nic_compute_attributes, v6 = false) ⇒ Object



46
47
48
49
50
51
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 46

def cidr_prefix(nic_compute_attributes, v6 = false)
  attr_name = 'cidrv'
  attr_name += v6 ? '6' : '4'
  attr_name += '_prefix'
  nic_compute_attributes[attr_name] if nic_compute_attributes[attr_name].presence
end

#container?(host) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 33

def container?(host)
  host.compute_attributes['type'] == 'lxc'
end

#container_nic_name_valid?(nic) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 37

def container_nic_name_valid?(nic)
  /^(eth)(\d+)$/.match?(nic.compute_attributes['name'])
end

#dhcp?(nic_compute_attributes, v6 = false) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 79

def dhcp?(nic_compute_attributes, v6 = false)
  attr_name = 'dhcpv'
  attr_name += v6 ? '6' : '4'
  to_boolean(nic_compute_attributes[attr_name]) if nic_compute_attributes[attr_name].present?
end

#editable_network_interfaces?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 24

def editable_network_interfaces?
  true
end

#host_interfaces_attrs(host) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 85

def host_interfaces_attrs(host)
  host.interfaces.select(&:physical?).each.with_index.reduce({}) do |hash, (nic, index)|
    set_nic_identifier(nic, index)
    set_container_interface_name(host, nic, index) if container?(host)
    nic_compute_attributes = nic.compute_attributes.merge(id: nic.identifier)
    mac = nic.mac
    mac ||= nic.attributes['mac']
    nic_compute_attributes.store(:macaddr, mac) if mac.present?
    interface_compute_attributes = host.compute_attributes['interfaces_attributes'] ? host.compute_attributes['interfaces_attributes'].select { |_k, v| v['id'] == nic.identifier } : {}
    nic_compute_attributes.store(:_delete, interface_compute_attributes[interface_compute_attributes.keys[0]]['_delete']) unless interface_compute_attributes.empty?
    nic_compute_attributes.store(:ip, set_ip(host, nic, nic_compute_attributes))
    nic_compute_attributes.store(:gw, set_gw(nic_compute_attributes))
    nic_compute_attributes.store(:ip6, set_ip(host, nic, nic_compute_attributes, true))
    nic_compute_attributes.store(:gw6, set_gw(nic_compute_attributes, true))
    hash.merge(index.to_s => nic_compute_attributes)
  end
end

#set_container_interface_name(_host, nic, index) ⇒ Object

Raises:

  • (::Foreman::Exception)


41
42
43
44
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 41

def set_container_interface_name(_host, nic, index)
  nic.compute_attributes['name'] = format('eth%<index>s', index: index) if nic.compute_attributes['name'].empty?
  raise ::Foreman::Exception, _(format('Invalid name interface[%<index>s]. Must be eth[n] with n integer >= 0', index: index)) unless container_nic_name_valid?(nic)
end

#set_gw(nic_compute_attributes, v6 = false) ⇒ Object



69
70
71
72
73
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 69

def set_gw(nic_compute_attributes, v6 = false)
  attr_name = 'gwv'
  attr_name += v6 ? '6' : '4'
  nic_compute_attributes[attr_name] if nic_compute_attributes[attr_name].presence
end

#set_ip(host, nic, nic_compute_attributes, v6 = false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 53

def set_ip(host, nic, nic_compute_attributes, v6 = false)
  return 'dhcp' if dhcp?(nic_compute_attributes, v6)

  ip = v6 ? nic.ip6 : nic.ip
  return ip unless container?(host) || cidr_prefix(nic_compute_attributes, v6)

  valid = v6 ? Fog::Proxmox::IpHelper.cidr6_prefix?(cidr_prefix(nic_compute_attributes, v6)) : Fog::Proxmox::IpHelper.cidr_prefix?(cidr_prefix(nic_compute_attributes, v6))
  ipv = v6 ? 'IPv6' : 'IPv4'
  max = v6 ? 128 : 32
  unless valid
    raise ::Foreman::Exception, _(format('Invalid Interface Proxmox CIDR %<ip>s. If %<ip>s is not empty, Proxmox CIDR prefix must be an integer between 0 and %<max>i.', ip: ipv, max: max))
  end

  v6 ? Fog::Proxmox::IpHelper.to_cidr6(nic.ip6, cidr_prefix(nic_compute_attributes, v6)) : Fog::Proxmox::IpHelper.to_cidr(nic.ip, cidr_prefix(nic_compute_attributes, v6))
end

#set_nic_identifier(nic, index) ⇒ Object

Raises:

  • (::Foreman::Exception)


28
29
30
31
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 28

def set_nic_identifier(nic, index)
  nic.identifier = format('net%<index>s', index: index) if nic.identifier.empty?
  raise ::Foreman::Exception, _(format('Invalid identifier interface[%<index>s]. Must be net[n] with n integer >= 0', index: index)) unless Fog::Proxmox::NicHelper.nic?(nic.identifier)
end

#to_boolean(value) ⇒ Object



75
76
77
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 75

def to_boolean(value)
  [1, true, '1', 'true'].include?(value)
end