Skip to content

Commit

Permalink
Fixes #11767 - avoid cleaning of interface attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ares authored and Dominic Cleal committed Sep 25, 2015
1 parent 0f43655 commit 585329f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
19 changes: 8 additions & 11 deletions app/controllers/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def process_hostgroup

def process_taxonomy
return head(:not_found) unless @location || @organization
@host = Host.new(clean_interfaces_attributes)
@host = Host.new(params[:host])
# revert compute resource to "Bare Metal" (nil) if selected
# compute resource is not included taxonomy
Taxonomy.as_taxonomy @organization, @location do
Expand All @@ -575,8 +575,14 @@ def process_taxonomy
render :partial => 'form'
end

# on persisted record calling has_many `relations=` triggers saving/deletion immediately
# so we have to filter such parameters
# we don't need any has_many relation to determine what proxies are used and the view
# renders only resulting templates set so the rest of form is unaffected
def template_used
host = Host.new(clean_interfaces_attributes.except(:host_parameters_attributes))
host = params[:id] ? Host.readonly.find(params[:id]) : Host.new
association_keys = params[:host].keys.select { |k| k =~ /.*_ids\Z/ }
host.attributes = params[:host].except(:host_parameters_attributes).except(*association_keys)
templates = host.available_template_kinds(params[:provisioning])
return not_found if templates.empty?
render :partial => 'provisioning', :locals => { :templates => templates }
Expand Down Expand Up @@ -744,13 +750,4 @@ def merge_search_filter(filter)
def offer_to_overwrite_conflicts
@host.overwrite = "true" if @host.errors.any? and @host.errors.are_all_conflicts?
end

def clean_interfaces_attributes
attributes = params[:host].dup
if params[:host][:interfaces_attributes]
attributes[:interfaces_attributes] = params[:host][:interfaces_attributes].dup.except(:created_at, :updated_at, :attrs)
attributes[:interfaces_attributes][:id] = nil
end
attributes
end
end
2 changes: 1 addition & 1 deletion app/views/hosts/_operating_system.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<div class="form-group">
<label class="col-xs-2 control-label"><%= _('Provisioning templates') %></label>
<div class="col-xs-10">
<%= link_to_function icon_text("refresh", _("Resolve")), "template_info('#templates_info','#{template_used_hosts_url}')", :class => "btn btn-default" %>
<%= link_to_function icon_text("refresh", _("Resolve")), "template_info('#templates_info','#{template_used_hosts_url(:id => @host.id)}')", :class => "btn btn-default" %>
<div class="help-block">
<%= _("Display the templates that will be used to provision this host") %>
</div>
Expand Down
14 changes: 10 additions & 4 deletions test/functional/hosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,10 @@ class Host::Valid < Host::Managed; end
@host.setBuild
nic=FactoryGirl.create(:nic_managed, :host => @host)
attrs = @host.attributes
attrs[:interfaces_attributes] = nic.attributes
xhr :put, :template_used, {:provisioning => 'build', :host => attrs }, set_session_user
attrs[:interfaces_attributes] = nic.attributes.except 'updated_at', 'created_at', 'attrs'
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
xhr :put, :template_used, {:provisioning => 'build', :host => attrs, :id => @host.id }, set_session_user
assert_response :success
assert_template :partial => '_provisioning'
end
Expand All @@ -891,15 +893,19 @@ class Host::Valid < Host::Managed; end
@host.setBuild
attrs = @host.attributes
attrs[:host_parameters_attributes] = {'0' => {:name => 'foo', :value => 'bar', :id => '34'}}
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
xhr :put, :template_used, {:provisioning => 'build', :host => attrs }, set_session_user
assert_response :success
assert_template :partial => '_provisioning'
end

test 'process_taxonomy renders a host from the params correctly' do
nic=FactoryGirl.create(:nic_managed, :host => @host)
nic = FactoryGirl.build(:nic_managed, :host => @host)
attrs = @host.attributes
attrs[:interfaces_attributes] = nic.attributes
attrs[:interfaces_attributes] = nic.attributes.except 'updated_at', 'created_at', 'attrs'
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
xhr :put, :process_taxonomy, { :host => attrs }, set_session_user
assert_response :success
assert response.body.include?(nic.attributes["mac"])
Expand Down

0 comments on commit 585329f

Please sign in to comment.