Class: ForemanResourceQuota::ResourceOrigins::ComputeResourceOrigin

Inherits:
ForemanResourceQuota::ResourceOrigin show all
Defined in:
app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb

Constant Summary

Constants inherited from ForemanResourceQuota::ResourceOrigin

ForemanResourceQuota::ResourceOrigin::RESOURCE_FUNCTION_MAP

Instance Method Summary collapse

Methods inherited from ForemanResourceQuota::ResourceOrigin

#host_attribute_eager_name, #host_attribute_name

Instance Method Details

#collect_resources!(hosts_resources, missing_hosts_resources, _host_objects) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 23

def collect_resources!(hosts_resources, missing_hosts_resources, _host_objects)
  compute_resource_to_hosts = group_hosts_by_compute_resource(missing_hosts_resources.keys)

  compute_resource_to_hosts.each do |compute_resource_id, hosts|
    next if compute_resource_id == :nil_compute_resource

    host_vms, vm_id_attr = filter_vms_by_hosts(hosts, compute_resource_id)
    next if host_vms.empty?

    hosts.each do |host|
      vm = host_vms.find { |obj| obj.send(vm_id_attr) == host.uuid }
      next unless vm
      process_host_vm!(hosts_resources, missing_hosts_resources, host.name, vm)
    end
  end
end

#extract_cpu_cores(param) ⇒ Object



6
7
8
9
10
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 6

def extract_cpu_cores(param)
  param.cpus
rescue StandardError
  nil
end

#extract_disk_gb(_) ⇒ Object



18
19
20
21
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 18

def extract_disk_gb(_)
  # FIXME: no disk given in VM (compare fog extensions)
  nil
end

#extract_memory_mb(param) ⇒ Object



12
13
14
15
16
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 12

def extract_memory_mb(param)
  param.memory.to_i / ResourceQuotaHelper::FACTOR_B_TO_MB
rescue StandardError
  nil
end

#filter_vms_by_hosts(hosts, compute_resource_id) ⇒ Object

Filters VMs of a compute resource, given a list of hosts. Parameters:

- hosts: An array of host objects.
- compute_resource_id: ID of the compute resource.

Returns:

- filtered_vms: An array of filtered virtual machine objects.
- id_attr: Attribute used for identifying virtual machines (either :vmid or :id).


56
57
58
59
60
61
62
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 56

def filter_vms_by_hosts(hosts, compute_resource_id)
  host_uuids = hosts.map(&:uuid)
  vms = ComputeResource.find_by(id: compute_resource_id).vms.all
  id_attr = vms[0].respond_to?(:vmid) ? :vmid : :id
  filtered_vms = vms.select { |obj| host_uuids.include?(obj.send(id_attr)) } # reduce from all vms
  [filtered_vms, id_attr]
end

#group_hosts_by_compute_resource(host_names) ⇒ Object

Groups hosts with the same compute resource. Parameters: An array of host names. Returns: A hash where keys represent compute resource IDs and values are the list of host objects.



43
44
45
46
47
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 43

def group_hosts_by_compute_resource(host_names)
  Host::Managed.where(name: host_names).includes(:compute_resource).group_by do |host|
    host.compute_resource&.id || :nil_compute_resource
  end
end

#process_host_vm!(hosts_resources, missing_hosts_resources, host_name, host_vm) ⇒ Object

Processes a host’s virtual machines and updates resource allocation. Parameters:

- hosts_resources: Hash containing successfully determined resources per host.
- missing_hosts_resources: Hash containing missing resources per host.
- host: Host object
- vm: Compute resource VM object of given host.

Returns: None.



71
72
73
74
75
76
77
78
79
# File 'app/services/foreman_resource_quota/resource_origins/compute_resource_origin.rb', line 71

def process_host_vm!(hosts_resources, missing_hosts_resources, host_name, host_vm)
  missing_hosts_resources[host_name].reverse_each do |resource_name|
    resource_value = process_resource(resource_name, host_vm)
    next unless resource_value
    hosts_resources[host_name][resource_name] = resource_value
    missing_hosts_resources[host_name].delete(resource_name)
  end
  missing_hosts_resources.delete(host_name) if missing_hosts_resources[host_name].empty?
end