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
# File 'app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb', line 22

def host_compute_attrs(host)
  super.tap do |_attrs|
    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'
      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
  end
end

#interface_compute_attributes(interface_attributes) ⇒ Object



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

def interface_compute_attributes(interface_attributes)
  vm_attrs = {}
  vm_attrs.store(:mac, interface_attributes[:macaddr])
  vm_attrs.store(:id, interface_attributes[:id])
  vm_attrs.store(:identifier, interface_attributes[:id])
  vm_attrs.store(:ip, interface_attributes[:ip])
  vm_attrs.store(:ip6, interface_attributes[:ip6])
  vm_attrs[:compute_attributes] = interface_attributes.reject { |k, _v| [:macaddr, :id].include?(k) }
  vm_attrs
end

#not_config_key?(vm, key) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def vm_compute_attributes(vm)
  vm_attrs = {}
  if vm.respond_to?(:config)
    vm_attrs = vm_attrs.merge(vmid: vm.identity, node_id: vm.node_id, type: vm.type)
    vm_attrs[:volumes_attributes] = Hash[vm.config.disks.each_with_index.map { |disk, idx| [idx.to_s, disk.attributes] }] if vm.config.respond_to?(:disks)
    if vm.config.respond_to?(:interfaces)
      vm_attrs[:interfaces_attributes] = Hash[vm.config.interfaces.each_with_index.map { |interface, idx| [idx.to_s, interface_compute_attributes(interface.attributes)] }]
    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