Module: ForemanFogProxmox::ProxmoxVmQueries

Includes:
ProxmoxPools, ProxmoxVmUuidHelper
Included in:
Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_vm_queries.rb

Constant Summary

Constants included from ProxmoxVmUuidHelper

ProxmoxVmUuidHelper::UUID_REGEXP

Instance Method Summary collapse

Methods included from ProxmoxVmUuidHelper

#extract, #extract_vmid, #match_uuid?

Methods included from ProxmoxPools

#add_vm_to_pool, #pool_owner, #pools, #remove_vm_from_pool, #update_pool

Instance Method Details

#bridges(node_id = default_node_id) ⇒ Object



38
39
40
41
42
43
# File 'app/models/foreman_fog_proxmox/proxmox_vm_queries.rb', line 38

def bridges(node_id = default_node_id)
  node = network_client.nodes.get node_id
  node ||= network_client.nodes.first
  bridges = node.networks.all(type: 'any_bridge')
  bridges.sort_by(&:iface)
end

#find_vm_by_uuid(uuid) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/foreman_fog_proxmox/proxmox_vm_queries.rb', line 57

def find_vm_by_uuid(uuid)
  # look for the uuid on all known nodes
  vm = nil
  vmid = extract_vmid(uuid)
  nodes.each do |node|
    vm = find_vm_in_servers_by_vmid(node.servers, vmid)
    vm ||= find_vm_in_servers_by_vmid(node.containers, vmid)
    next if vm.nil?
    logger.debug("found vm #{vmid} on node #{node.node}")
    break
  end
  vm
end

#find_vm_in_servers_by_vmid(servers, vmid) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/models/foreman_fog_proxmox/proxmox_vm_queries.rb', line 71

def find_vm_in_servers_by_vmid(servers, vmid)
  vm = servers.get(vmid) unless ForemanFogProxmox::Value.empty?(vmid)
  pool_owner(vm) if vm
  vm
rescue Fog::Errors::NotFound
  nil
rescue StandardError => e
  Foreman::Logging.exception(format(_('Failed retrieving proxmox server vm by vmid=%<vmid>s'), vmid: vmid), e)
  raise(ActiveRecord::RecordNotFound, e)
end

#nodesObject



25
26
27
28
# File 'app/models/foreman_fog_proxmox/proxmox_vm_queries.rb', line 25

def nodes
  nodes = client.nodes.all if client
  nodes&.sort_by(&:node)
end

#storages(node_id = default_node_id, type = 'images') ⇒ Object



30
31
32
33
34
35
36
# File 'app/models/foreman_fog_proxmox/proxmox_vm_queries.rb', line 30

def storages(node_id = default_node_id, type = 'images')
  node = client.nodes.get node_id
  node ||= default_node
  storages = node.storages.list_by_content_type type
  logger.debug("storages(): node_id #{node_id} type #{type}")
  storages.sort_by(&:storage)
end

#vms(opts = {}) ⇒ Object

TODO: Pagination with filters



46
47
48
49
50
51
52
53
54
55
# File 'app/models/foreman_fog_proxmox/proxmox_vm_queries.rb', line 46

def vms(opts = {})
  vms = []
  nodes.each { |node| vms += node.servers.all + node.containers.all }
  if opts.key?(:eager_loading) && opts[:eager_loading]
    vms_eager = []
    vms.each { |vm| vms_eager << vm.collection.get(vm.identity) }
    vms = vms_eager
  end
  ForemanFogProxmox::Vms.new(vms)
end