Skip to content

Commit

Permalink
WiP: make host_resources relation work lol
Browse files Browse the repository at this point in the history
  • Loading branch information
bastian-src committed Nov 13, 2024
1 parent 9fd80b7 commit a34fedc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)

Check failure on line 83 in app/models/concerns/foreman_resource_quota/host_managed_extensions.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/PreferredHashMethods: Use `Hash#key?` instead of `Hash#has_key?`.

Check failure on line 83 in app/models/concerns/foreman_resource_quota/host_managed_extensions.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/AndOr: Use `||` instead of `or`.

Check failure on line 83 in app/models/concerns/foreman_resource_quota/host_managed_extensions.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/PreferredHashMethods: Use `Hash#key?` instead of `Hash#has_key?`.
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)
Expand Down Expand Up @@ -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)

Check failure on line 149 in app/models/concerns/foreman_resource_quota/host_managed_extensions.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/PreferredHashMethods: Use `Hash#key?` instead of `Hash#has_key?`.
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
5 changes: 1 addition & 4 deletions app/models/foreman_resource_quota/host_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 7 in app/models/foreman_resource_quota/host_resources.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Rails/InverseOf: Specify an `:inverse_of` option.

Check failure on line 7 in app/models/foreman_resource_quota/host_resources.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Rails/RedundantForeignKey: Specifying the default value for `foreign_key` is redundant.
validates :host, { presence: true, uniqueness: true }

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/models/foreman_resource_quota/resource_quota.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
}
Expand Down
7 changes: 2 additions & 5 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/models/resource_quota_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a34fedc

Please sign in to comment.