diff --git a/app/models/concerns/foreman_resource_quota/host_managed_extensions.rb b/app/models/concerns/foreman_resource_quota/host_managed_extensions.rb index 3f44236..ca0016a 100644 --- a/app/models/concerns/foreman_resource_quota/host_managed_extensions.rb +++ b/app/models/concerns/foreman_resource_quota/host_managed_extensions.rb @@ -9,8 +9,9 @@ module HostManagedExtensions included do validate :verify_resource_quota - has_one :host_resources, class_name: '::ForemanResourceQuota::HostResources', - inverse_of: :host, dependent: :destroy + # has_one :host_resources, class_name: '::ForemanResourceQuota::HostResources', + # inverse_of: :host, dependent: :destroy + has_one :host_resources, inverse_of: :host, dependent: :destroy has_one :resource_quota_host, class_name: '::ForemanResourceQuota::ResourceQuotaHost', inverse_of: :host, dependent: :destroy has_one :resource_quota, class_name: '::ForemanResourceQuota::ResourceQuota', @@ -69,22 +70,22 @@ def handle_error(error_module, error_message, log_message) end def determine_quota_utilization(quota) - missing_hosts = quota.missing_hosts(exclude: [self.name]) + missing_hosts = quota.missing_hosts(exclude: [name]) unless missing_hosts.empty? raise ResourceQuotaUtilizationException, "Resource Quota '#{quota.name}' cannot determine resources for #{missing_hosts.size} hosts." end - quota.utilization(exclude: [self.name]) + quota.utilization(exclude: [name]) end def determine_host_resources(active_resources) new_host_resources, missing_hosts = call_utilization_helper(active_resources, [self]) - if missing_hosts.has_key?(self.name) or missing_hosts.has_key?(self.name.to_sym) + if missing_hosts.has_key?(name) or missing_hosts.has_key?(name.to_sym) raise HostResourcesException, "Cannot determine host resources for #{name}: #{missing_hosts[name]}" end - self.host_resources.resources = new_host_resources - self.host_resources.resources + host_resources.resources = new_host_resources + host_resources.resources end def check_resource_quota_limits(quota, quota_utilization, current_host_resources) @@ -145,12 +146,12 @@ def quota_assigment_optional? # Wrap into a function for easier testing def call_utilization_helper(resources, hosts) all_host_resources, missing_hosts = utilization_from_resource_origins(resources, hosts) - if not all_host_resources.has_key?(self.name) + unless all_host_resources.has_key?(name) raise HostResourcesException, "Host #{name} was not included when determining host resources." end - current_host_resources = all_host_resources[self.name] - return current_host_resources, missing_hosts + current_host_resources = all_host_resources[name] + [current_host_resources, missing_hosts] end end end diff --git a/app/models/foreman_resource_quota/host_resources.rb b/app/models/foreman_resource_quota/host_resources.rb index c1924df..83969b8 100644 --- a/app/models/foreman_resource_quota/host_resources.rb +++ b/app/models/foreman_resource_quota/host_resources.rb @@ -4,9 +4,6 @@ module ForemanResourceQuota class HostResources < ApplicationRecord self.table_name = 'hosts_resources' - # Make this become the relational one again - # Make ResourceQuota.hosts - # AND ResourceQuota.hosts_resources available -> to easily sum the hosts_resources(:disk) for example. belongs_to :host, class_name: '::Host::Managed', foreign_key: :host_id validates :host, { presence: true, uniqueness: true } @@ -23,7 +20,7 @@ def resources=(val) assign_attributes(allowed_attributes) # Set multiple attributes at once (given a hash) end - # Return a Array of unknown host resources (returns an empty Array if all are known) + # Returns an array of unknown host resources (returns an empty array if all are known) # For example, completely unknown host resources returns: # [ # :cpu_cores, diff --git a/app/models/foreman_resource_quota/resource_quota.rb b/app/models/foreman_resource_quota/resource_quota.rb index ea48f51..216e4c9 100644 --- a/app/models/foreman_resource_quota/resource_quota.rb +++ b/app/models/foreman_resource_quota/resource_quota.rb @@ -12,11 +12,11 @@ class ResourceQuota < ApplicationRecord self.table_name = 'resource_quotas' - has_many :resource_quota_hosts, class_name: 'ResourceQuotaHost', inverse_of: :resource_quota, dependent: :destroy + has_many :resource_quotas_hosts, class_name: 'ResourceQuotaHost', inverse_of: :resource_quota, dependent: :destroy has_many :resource_quotas_users, class_name: 'ResourceQuotaUser', inverse_of: :resource_quota, dependent: :destroy has_many :resource_quotas_usergroups, class_name: 'ResourceQuotaUsergroup', inverse_of: :resource_quota, dependent: :destroy - has_many :hosts, -> { distinct }, class_name: '::Host::Managed', through: :resource_quota_hosts + has_many :hosts, -> { distinct }, class_name: '::Host::Managed', through: :resource_quotas_hosts scope :hosts_resources, -> { HostResources.joins(:host).where(hosts: { id: hosts.select(:id) }) } diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index dbf4924..9fb7ac9 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -3,9 +3,6 @@ ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'resource_quota', 'resource_quotas' inflect.irregular 'host_resources', 'hosts_resources' - inflect.singular 'host_resources', 'host_resources' - inflect.plural 'hosts_resources', 'hosts_resources' - inflect.irregular 'resource_quota_host', 'resource_quota_hosts' - inflect.singular 'resource_quota_host', 'resource_quota_host' - inflect.plural 'resource_quota_hosts', 'resource_quota_hosts' + inflect.irregular 'HostResources', 'HostsResources' + # inflect.uncountable 'host_resources' end diff --git a/test/models/resource_quota_test.rb b/test/models/resource_quota_test.rb index fafb5f2..9a678d5 100644 --- a/test/models/resource_quota_test.rb +++ b/test/models/resource_quota_test.rb @@ -8,6 +8,7 @@ class ResourceQuotaTest < ActiveSupport::TestCase def setup Setting[:resource_quota_optional_assignment] = true + # require 'byebug'; byebug @quota = FactoryBot.create :resource_quota @user = FactoryBot.create :user @usergroup = FactoryBot.create :usergroup