Sha256: 9b718314b7bbdd7cc12849ae3e157aae21a72014e8a9b8199cdaf0d10512d8d1

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module ForemanResourceQuota
  class HostResources < ApplicationRecord
    self.table_name = 'hosts_resources'

    belongs_to :host, class_name: '::Host::Managed'
    validates :host, { presence: true, uniqueness: true }

    def resources
      {
        cpu_cores: cpu_cores,
        memory_mb: memory_mb,
        disk_gb: disk_gb,
      }
    end

    def resources=(val)
      allowed_attributes = val.slice(:cpu_cores, :memory_mb, :disk_gb)
      assign_attributes(allowed_attributes) # Set multiple attributes at once (given a hash)
    end

    # Returns an array of unknown host resources (returns an empty array if all are known)
    # For example, completely unknown host resources returns:
    #   [
    #     :cpu_cores,
    #     :memory_mb,
    #     :disk_gb,
    #   ]
    # Consider only the resource_quota's active resources by default.
    def missing_resources(only_active_resources: true)
      empty_resources = []
      resources_to_check = %i[cpu_cores memory_mb disk_gb]
      resources_to_check = host.resource_quota.active_resources if only_active_resources && host.resource_quota.present?

      resources_to_check.each do |single_resource|
        empty_resources << single_resource if send(single_resource).nil?
      end

      empty_resources
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foreman_resource_quota-0.3.1 app/models/foreman_resource_quota/host_resources.rb
foreman_resource_quota-0.3.0 app/models/foreman_resource_quota/host_resources.rb