From c30aa65d65b3f6e75d6da09d9efd695db86e6780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Ple=C5=A1ko?= Date: Fri, 9 Aug 2019 15:23:02 +0200 Subject: [PATCH] Protect user's work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, when user updates PXE Server details and submits form, all related PxeMenus and PxImages are discarded so she needs to run "Recreate Relationships" to inventory them again. This approach works but unfortunately user has option to manually edit PxeImages when they are inventoried, and by discarding them we also discard her work. With this commit we therefore go out of our way and double check which PxeMenus (and subsequent PxeImages) are *really* affected by update and only discard those. This way, if user only updates PxeServer's name, nothing else will be recreated hence user's manual work will be preserved. Fixes https://github.com/ManageIQ/manageiq/issues/18940 Signed-off-by: Miha Pleško --- app/controllers/api/pxe_servers_controller.rb | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/pxe_servers_controller.rb b/app/controllers/api/pxe_servers_controller.rb index b064a9adae..ddec1598ee 100644 --- a/app/controllers/api/pxe_servers_controller.rb +++ b/app/controllers/api/pxe_servers_controller.rb @@ -26,7 +26,7 @@ def create_resource(_type, _id, data = {}) server.update_authentication({:default => authentication.compact}, {:save => true}) end - server.pxe_menus = create_pxe_menus(menus) if menus + server.ensure_menu_list(menus.map { |el| el['file_name'] }) if menus if server.invalid? raise BadRequestError, "Failed to add a pxe server - #{server.errors.full_messages.join(', ')}" @@ -48,27 +48,15 @@ def edit_resource(type, id, data) menus = data.delete('pxe_menus') authentication = data.delete('authentication') PxeServer.transaction do - if menus - server.pxe_menus.destroy_all - data['pxe_menus'] = create_pxe_menus(menus) - end server.update!(data) server.update_authentication({:default => authentication.transform_keys(&:to_sym)}, {:save => true}) if authentication && server.requires_credentials? + server.ensure_menu_list(menus.map { |el| el['file_name'] }) if menus server end end private - def create_pxe_menus(menus) - menus.each do |menu| - validate_data_for('PxeMenu', menu) - end - menus.map do |menu| - collection_class(:pxe_menus).create(menu) - end - end - def validate_data_for(klass, data) bad_attrs = [] PXE_ATTRIBUTES[klass].each { |attr| bad_attrs << attr if data[attr].blank? }