Module: ProxmoxVMInterfacesHelper

Included in:
ProxmoxFormHelper, ProxmoxVMHelper
Defined in:
app/helpers/proxmox_vm_interfaces_helper.rb

Overview

Convert a foreman form server hash into a fog-proxmox server attributes hash

Instance Method Summary collapse

Instance Method Details

#add_or_delete_typed_interface(interface_attributes, interfaces_to_delete, interfaces_to_add, type) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 86

def add_or_delete_typed_interface(interface_attributes, interfaces_to_delete, interfaces_to_add, type)
  logger.debug("add_or_delete_typed_interface(#{type}): interface_attributes=#{interface_attributes}")
  ForemanFogProxmox::HashCollection.remove_empty_values(interface_attributes)
  if interface_attributes['compute_attributes']
    ForemanFogProxmox::HashCollection.remove_empty_values(interface_attributes['compute_attributes'])
  end
  nic = {}
  id = interface_attributes['id']
  delete = interface_attributes['_delete'].to_i == 1
  if delete
    logger.debug("add_or_delete_typed_interface(#{type}): delete id=#{id}")
    interfaces_to_delete.push(id.to_s)
  else
    interface_common_typed_keys(type).each do |key|
      ForemanFogProxmox::HashCollection.add_and_format_element(nic, key.to_sym, interface_attributes,
        key)
    end
    interface_attributes_h = interface_attributes['compute_attributes']
    if ForemanFogProxmox::Value.empty?(interface_attributes['compute_attributes'])
      interface_attributes_h ||= interface_attributes
    end
    interface_compute_attributes_typed_keys(type).each do |key|
      ForemanFogProxmox::HashCollection.add_and_format_element(nic, key.to_sym, interface_attributes_h, key)
    end
    compute_dhcps(interface_attributes_h)
    logger.debug("add_or_delete_typed_interface(#{type}): add nic=#{nic}")
    interfaces_to_add.push(Fog::Proxmox::NicHelper.flatten(nic))
  end
end

#compute_dhcps(interface_attributes_h) ⇒ Object



62
63
64
65
66
67
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 62

def compute_dhcps(interface_attributes_h)
  return if interface_attributes_h.nil?

  sync_dhcp_for_ip(interface_attributes_h, 'ip', 'dhcp')
  sync_dhcp_for_ip(interface_attributes_h, 'ip6', 'dhcp6')
end

#interface_common_typed_keys(type) ⇒ Object



58
59
60
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 58

def interface_common_typed_keys(type)
  ['id', (type == 'qemu') ? 'macaddr' : 'hwaddr']
end

#interface_compute_attributes_typed_keys(type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 47

def interface_compute_attributes_typed_keys(type)
  keys = ['rate', 'bridge', 'tag']
  case type
  when 'qemu'
    keys += ['model', 'firewall', 'link_down', 'queues']
  when 'lxc'
    keys += ['name', 'ip', 'ip6', 'gw', 'gw6', 'dhcp', 'dhcp6', 'cidr', 'cidr6', 'firewall']
  end
  keys
end

#parse_typed_interfaces(args, type) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 34

def parse_typed_interfaces(args, type)
  interfaces_to_add = []
  interfaces_to_delete = []
  interfaces_attributes = args['interfaces_attributes']
  unless ForemanFogProxmox::Value.empty?(args['config_attributes'])
    interfaces_attributes ||= args['config_attributes']['interfaces_attributes']
  end
  interfaces_attributes&.each_value do |value|
    add_or_delete_typed_interface(value, interfaces_to_delete, interfaces_to_add, type)
  end
  [interfaces_to_add, interfaces_to_delete]
end

#parsed_typed_interfaces(args, type, parsed_vm) ⇒ Object



27
28
29
30
31
32
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 27

def parsed_typed_interfaces(args, type, parsed_vm)
  interfaces_to_add, interfaces_to_delete = parse_typed_interfaces(args, type)
  interfaces_to_add.each { |interface| parsed_vm = parsed_vm.merge(interface) }
  parsed_vm = parsed_vm.merge(delete: interfaces_to_delete.join(',')) unless interfaces_to_delete.empty?
  parsed_vm
end

#sync_dhcp_for_ip(attrs, ip_key, dhcp_key) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 69

def sync_dhcp_for_ip(attrs, ip_key, dhcp_key)
  dhcp_value = attrs[dhcp_key] || attrs[dhcp_key.to_sym]
  if dhcp_value.nil?
    ip_value = attrs[ip_key] || attrs[ip_key.to_sym]
    attrs[dhcp_key.to_sym] = (ip_value == 'dhcp') ? '1' : '0'
    attrs[ip_key.to_sym] = '' if attrs[dhcp_key.to_sym] == '1'
    return
  end

  ip_value = attrs[ip_key] || attrs[ip_key.to_sym]
  if dhcp_value.to_s == '1'
    attrs[ip_key.to_sym] = 'dhcp' if ip_value.to_s.empty?
  elsif ip_value.to_s == 'dhcp'
    attrs[ip_key.to_sym] = ''
  end
end