Skip to content

Commit

Permalink
Protect user's work
Browse files Browse the repository at this point in the history
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 ManageIQ/manageiq#18940

Signed-off-by: Miha Pleško <[email protected]>
  • Loading branch information
miha-plesko committed Aug 12, 2019
1 parent 39197be commit c30aa65
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions app/controllers/api/pxe_servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')}"
Expand All @@ -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? }
Expand Down

0 comments on commit c30aa65

Please sign in to comment.