From a4e97544533622f2e40c10b58e8d82479a4995f2 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Wed, 28 Feb 2024 15:36:04 +0100
Subject: [PATCH] Restructure UUID facet creation

---
 .../foreman_uuid_boot/uuidboot_host_facet.rb      | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/app/models/foreman_uuid_boot/uuidboot_host_facet.rb b/app/models/foreman_uuid_boot/uuidboot_host_facet.rb
index 79756b7..49cb8b7 100644
--- a/app/models/foreman_uuid_boot/uuidboot_host_facet.rb
+++ b/app/models/foreman_uuid_boot/uuidboot_host_facet.rb
@@ -5,10 +5,17 @@ module ForemanUuidBoot
     include Facets::Base
 
     def self.populate_fields_from_facts(host, parser, _type, _proxy)
-      facet = host.uuidboot_facet || host.build_uuidboot_facet
-      facet.uuid = parser.facts.dig('dmi', 'product', 'uuid') || parser.facts['uuid']
-      facet.save if facet.uuid && facet.changed?
-      facet.destroy if !facet.uuid && facet.persisted?
+      uuid = parser.facts.dig('dmi', 'product', 'uuid') || parser.facts['uuid']
+
+      if uuid
+        facet = host.uuidboot_facet || host.build_uuidboot_facet
+        facet.uuid = uuid
+        facet.save if facet.changed? || !facet.persisted?
+        facet
+      else
+        host.uuidboot_facet&.destroy
+        nil
+      end
     end
   end
 end
-- 
GitLab