Foreman host's hostgroup update failed via api/hammer with Name has already been taken error

Problem: in Foreman few of the host’s hostgroup update failed via api/hammer. While try to update via api/hammer getting the error “Name has already been taken error”, but there is no duplicate entry found in foreman.

Expected outcome: foreman api / hammer should update the hostgroup for hosts.

Foreman and Proxy versions: Foreman : 3.4.0-rc1, Proxy: 3.4.0~rc1-1

Foreman and Proxy plugin versions:

Distribution and version: Debian 11 (bullseye )

Other relevant data:
[ INFO 2022-08-25T01:51:03 HammerCLIForeman::Host::UpdateCommand] Called with options: {“option_volume_list”=>, “option_interface_list”=>, “option_id”=>852, “option_name”=>“10.200.70.81”, “option_hostgroup_title”=>“Test”, “option_hostgroup_id”=>408}
{“name”=>“10.200.70.81”, “hostgroup_id”=>408, “puppet_attributes”=>{}}
[DEBUG 2022-08-25T01:51:03 HammerCLIForeman::CommandExtensions::UpdateCommon] Called block for HammerCLIForeman::UpdateCommand request params:
#<Proc:0x00005631a99d3038 /usr/lib/ruby/vendor_ruby/hammer_cli_foreman/command_extensions/update_common.rb:8>
[DEBUG 2022-08-25T01:51:03 HammerCLIForemanPuppet::CommandExtensions::HostPuppetProxy] Called block for HammerCLIForeman::Host::UpdateCommand request params:
#<Proc:0x00005631aa0abdd8 /usr/lib/ruby/vendor_ruby/hammer_cli_foreman_puppet/command_extensions/host.rb:37>
[ INFO 2022-08-25T01:51:03 API] Server: https://foreman.example.com
[ INFO 2022-08-25T01:51:03 API] PUT /api/hosts/852
[DEBUG 2022-08-25T01:51:03 API] Params: {
“host” => {
“name” => “10.200.70.81”,
“hostgroup_id” => 408,
“puppet_attributes” => {}
}
}
[DEBUG 2022-08-25T01:51:03 API] Headers: {}
[DEBUG 2022-08-25T01:51:03 API] Using authenticator: HammerCLIForeman::Api::InteractiveBasicAuth
[ERROR 2022-08-25T01:51:03 API] 422 Unprocessable Entity
[DEBUG 2022-08-25T01:51:03 API] {
“error” => {
“id” => 852,
“errors” => {
“name” => [
[0] “has already been taken”
]
},
“full_messages” => [
[0] “Name has already been taken”
]
}
}
[DEBUG 2022-08-25T01:51:03 Exception] Using exception handler HammerCLIForeman::ExceptionHandler#handle_unprocessable_entity
[ERROR 2022-08-25T01:51:03 Exception] Name has already been taken
Could not update the host:
Name has already been taken

any one please look into it

Hi Guys,

We found issue causing scenario in /usr/share/foreman/app/models/host/base.rb file.

Hence we disabled “:uniqueness => true” check.

Before Change:
validates :name, :presence => true, :uniqueness => true, :format => {:with => Net::Validations::HOST_REGEXP, :message => _(Net::Validations::HOST_REGEXP_ERR_MSG)}

After change:
validates :name, :presence => true, :format => {:with => Net::Validations::HOST_REGEXP, :message => _(Net::Validations::HOST_REGEXP_ERR_MSG)}

But we are not sure about the impact of this change, we did this as a work around only.

Someone please check and get back to us on the actual issue.

Thanks & Regards,
Suresh

This is still an issue with foreman-3.6.1 and the above work around does work but is there a better fix for this ?

In our scenario, we were updating just the device owner in Foreman via the API for an existing device which returned the “Name has already been taken”.

I understand the need to enforce this during a name attribute update, but in this case we are only updating the owner and not the name at all.