Module: ForemanFogProxmox::ProxmoxComputeAttributes

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

Instance Method Summary collapse

Instance Method Details

#host_compute_attrs(host) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb', line 22

def host_compute_attrs(host)
  ostype = host.compute_attributes['config_attributes']['ostype']
  type = host.compute_attributes['type']
  case type
  when 'lxc'
    host.compute_attributes['config_attributes'].store('hostname', host.name)
  when 'qemu'
    host.compute_attributes['config_attributes'].store('name', host.name)
    unless compute_os_types(host).include?(ostype)
      raise ::Foreman::Exception,
        format(_('Operating system family %<type>s is not consistent with %<ostype>s'), type: host.operatingsystem.type,
ostype: ostype)
    end
  end
  super
end

#interface_compute_attributes(interface_attributes) ⇒ Object



43
44
45
46
47
48
# File 'app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb', line 43

def interface_compute_attributes(interface_attributes)
  vm_attrs = ForemanFogProxmox::HashCollection.new_hash_reject_keys(interface_attributes, [:identifier, :macaddr, :hwaddr])
  vm_attrs[:dhcp] = interface_attributes[:ip] == 'dhcp' ? '1' : '0'
  vm_attrs[:dhcp6] = interface_attributes[:ip6] == 'dhcp' ? '1' : '0'
  vm_attrs
end

#not_config_key?(vm, key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb', line 39

def not_config_key?(vm, key)
  [:disks, :interfaces, :vmid, :node_id, :node, :type].include?(key) || !vm.config.respond_to?(key)
end

#vm_compute_attributes(vm) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb', line 50

def vm_compute_attributes(vm)
  vm_attrs = {}
  vm_attrs = vm_attrs.merge(vmid: vm.identity, node_id: vm.node_id, type: vm.type)
  if vm.respond_to?(:config)
    if vm.config.respond_to?(:disks)
      vm_attrs[:volumes_attributes] = Hash[vm.config.disks.each_with_index.map do |disk, idx|
                                             [idx.to_s, disk.attributes]
                                           end ]
    end
    if vm.config.respond_to?(:interfaces)
      vm_attrs[:interfaces_attributes] = Hash[vm.config.interfaces.each_with_index.map do |interface, idx|
                                                [idx.to_s, interface_compute_attributes(interface.attributes)]
                                              end ]
    end
    vm_attrs[:config_attributes] = vm.config.attributes.reject do |key, value|
      not_config_key?(vm, key) || ForemanFogProxmox::Value.empty?(value.to_s) || Fog::Proxmox::DiskHelper.disk?(key.to_s) || Fog::Proxmox::NicHelper.nic?(key.to_s)
    end
  end
  vm_attrs
end