Bug #6299
openPOST to /api/hosts fails when host has interfaces aray
Description
Using Foreman 1.5.1, if I POST to /api/hosts with a JSON body that contains an interfaces array, it fails with the following message in the log file. My goal is to create a host and its dependent IPMI interface entry in a single query.
I see a nearly identical message with 0 replies in the mailing list from April, that fellow was using Ruby 1.9. So this is a current bug and independent of the Ruby version.
My hunch is that something about the nested resources is not declared right in the Hosts model, or not working right at the ActiveRecord level.
Completed 500 Internal Server Error in 26.2ms (Views: 1.9ms | ActiveRecord: 3.2ms)
Started POST "/api/hosts" for 127.0.0.1 at Thu Jun 19 18:54:41 +0000 2014
Processing by Api::V2::HostsController#create as JSON
Parameters: {"apiv"=>"v2", "host"=>{"build"=>"false", "name"=>"hostname", "interfaces"=>{"name"=>"", "domain_id"=>"1", "ip"=>"1.2.3.4", "type"=>"Nic::BMC", "username"=>"ipmiuser", "mac"=>"AA:BB:CC:DD:EE:FF", "subnet_id"=>"26", "provider"=>"IPMI", "password"=>"[FILTERED]"},
"domain_id"=>"1", "model_id"=>"4", "environment_id"=>"1", "ip"=>"2.3.4.5", "mac"=>"FF:EE:DD:CC:BB:AA", "ptable_id"=>"11", "subnet_id"=>
"10", "hostgroup_id"=>"3", "operatingsystem_id"=>"1"}}
Expire fragment views/tabs_and_title_records-1 0.2ms
Authorized user astone(Aaron Stone)
Nic::Base(#70287117238000) expected, got Array(#70287225667100) (ActiveRecord::AssociationTypeMismatch)
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/association.rb:210:in `raise_on_type_mismatch'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/collection_association.rb:310:in `replace'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/collection_association.rb:310:in `each'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/collection_association.rb:310:in `replace'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/collection_association.rb:41:in `writer'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/builder/association.rb:51:in `interfaces='
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/attribute_assignment.rb:94:in `send'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/attribute_assignment.rb:94:in `assign_attributes'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/attribute_assignment.rb:93:in `each'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
/usr/share/foreman/vendor/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/base.rb:498:in `initialize'
/usr/share/foreman/vendor/ruby/1.8/gems/actionpack-3.2.18/lib/action_dispatch/routing/url_for.rb:103:in `initialize'
/usr/share/foreman/app/models/concerns/foreman/sti.rb:22:in `new_without_cast'
/usr/share/foreman/app/models/concerns/foreman/sti.rb:22:in `new'
/usr/share/foreman/app/models/host.rb:16:in `send'
/usr/share/foreman/app/models/host.rb:16:in `method_missing'
/usr/share/foreman/app/controllers/api/v2/hosts_controller.rb:70:in `create'