From a456cc8981ab36be74c001b12f018fa6c6952f64 Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Tue, 26 Nov 2024 18:07:43 +0100 Subject: [PATCH] Save HostResources after host is saved A Hosts's HostResources are determined during the validation step but not saved, because the Host must exist before its HostResources can be stored (not-null database constraint). We add a routine which saves the HostResources immediately after a Host is saved in case its Host.host_resources changed --- .../foreman_resource_quota/host_managed_extensions.rb | 5 +++++ 1 file changed, 5 insertions(+) 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 989a555..3d9793f 100644 --- a/app/models/concerns/foreman_resource_quota/host_managed_extensions.rb +++ b/app/models/concerns/foreman_resource_quota/host_managed_extensions.rb @@ -19,6 +19,7 @@ module HostManagedExtensions # A host shall always have a .host_resources attribute before_validation :build_host_resources, unless: -> { host_resources.present? } + after_save :save_host_resources, if: -> { host_resources.changed? } end def verify_resource_quota @@ -141,6 +142,10 @@ def quota_assigment_optional? owner.resource_quota_is_optional || Setting[:resource_quota_optional_assignment] end + def save_host_resources + host_resources.save + end + # Wrap into a function for easier testing def call_utilization_helper(resources, hosts) all_host_resources, missing_hosts = utilization_from_resource_origins(resources, hosts)