Sha256: 7d1719e7336d53e57cc00a6d43f731d1225a7e771628a30a21f3a8364e716bac

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module ForemanResourceQuota
  module ResourceOrigins
    class FactsOrigin < ResourceOrigin
      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

      def host_attribute_eager_name
        :fact_values
      end

      def host_attribute_name
        :facts
      end

      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

      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

      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreman_resource_quota-0.3.1 app/services/foreman_resource_quota/resource_origins/facts_origin.rb
foreman_resource_quota-0.3.0 app/services/foreman_resource_quota/resource_origins/facts_origin.rb
foreman_resource_quota-0.2.0 app/services/foreman_resource_quota/resource_origins/facts_origin.rb
foreman_resource_quota-0.1.0 app/services/foreman_resource_quota/resource_origins/facts_origin.rb