Module: ProxmoxVmInterfacesHelper

Included in:
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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/proxmox_vm_interfaces_helper.rb', line 69

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)
  interface_attributes_h[:dhcp] = interface_attributes_h[:ip] == 'dhcp' ? '1' : '0'
  interface_attributes_h[:ip] = '' if interface_attributes_h[:dhcp] == '1'
  interface_attributes_h[:dhcp6] = interface_attributes_h[:ip6] == 'dhcp' ? '1' : '0'
  interface_attributes_h[:ip6] = '' if interface_attributes_h[:dhcp6] == '1'
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