Class: ForemanResourceQuota::ResourceOrigins::FactsOrigin

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

Constant Summary collapse

FACTS_KEYS_CPU_CORES =
[
  'ansible_processor_cores',
  'facter_processors::cores',
  'proc_cpuinfo::common::cpu_cores',
  'processors::cores',
].freeze
FACTS_KEYS_MEMORY_B =
[
  'facter_memory::system::total_bytes',
  'memory::system::total_bytes',
  'memory::memtotal',
].freeze
FACTS_REGEX_DISK_B =
[
  /^disks::(\w+)::size_bytes$/,
  /^facter_disks::(\w+)::size_bytes$/,
].freeze

Constants inherited from ForemanResourceQuota::ResourceOrigin

ForemanResourceQuota::ResourceOrigin::RESOURCE_FUNCTION_MAP

Instance Method Summary collapse

Methods inherited from ForemanResourceQuota::ResourceOrigin

#collect_resources!

Instance Method Details

#extract_cpu_cores(param) ⇒ Object



32
33
34
35
36
37
38
# File 'app/services/foreman_resource_quota/resource_origins/facts_origin.rb', line 32

def extract_cpu_cores(param)
  common_keys = param.keys & FACTS_KEYS_CPU_CORES
  return param[common_keys.first].to_i if common_keys.any?
  nil
rescue StandardError
  nil
end

#extract_disk_gb(param) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/foreman_resource_quota/resource_origins/facts_origin.rb', line 48

def extract_disk_gb(param)
  total_gb = nil

  FACTS_REGEX_DISK_B.each do |regex|
    relevant_keys = param.keys.grep(regex)
    next unless relevant_keys.any?

    total_gb = sum_disk_space(param, relevant_keys)
  end

  total_gb&.to_i
rescue StandardError
  nil
end

#extract_memory_mb(param) ⇒ Object



40
41
42
43
44
45
46
# File 'app/services/foreman_resource_quota/resource_origins/facts_origin.rb', line 40

def extract_memory_mb(param)
  common_keys = param.keys & FACTS_KEYS_MEMORY_B
  return (param[common_keys.first].to_i / ResourceQuotaHelper::FACTOR_B_TO_MB).to_i if common_keys.any?
  nil
rescue StandardError
  nil
end

#host_attribute_eager_nameObject



24
25
26
# File 'app/services/foreman_resource_quota/resource_origins/facts_origin.rb', line 24

def host_attribute_eager_name
  :fact_values
end

#host_attribute_nameObject



28
29
30
# File 'app/services/foreman_resource_quota/resource_origins/facts_origin.rb', line 28

def host_attribute_name
  :facts
end

#sum_disk_space(facts, keys) ⇒ Object



63
64
65
# File 'app/services/foreman_resource_quota/resource_origins/facts_origin.rb', line 63

def sum_disk_space(facts, keys)
  keys.map { |key| facts[key].to_i }.sum / ResourceQuotaHelper::FACTOR_B_TO_GB unless keys.empty?
end