API behavior change or a bug?

Hi, all!

I'm running into interesting issue with 1.10.1 - I can't change a hostgroup
of a host through API anymore (the same API used to work in 1.7 through 1.9
for sure). UI works fine as well.

So, I have a host that is in common/puppet-master (id: 5) HG currently:

[root@spc01 ~]# curl -kSs -H "Content-type:application/json" -u
admin:$FOREMAN_PASSWORD https://localhost/api/hosts/hostname -f | jq '.'
> egrep "name|id" | grep -v null
"environment_id": 1,
"environment_name": "production",
"domain_id": 1,
"domain_name": "test.domain.com",
"architecture_id": 1,
"architecture_name": "x86_64",
"operatingsystem_id": 1,
"operatingsystem_name": "CentOS 7.2",
"model_id": 1,
"model_name": "Standard PC (i440FX + PIIX, 1996)",
"hostgroup_id": 5,
"hostgroup_name": "common",
"owner_id": 3,
"certname": "spc01.test.domain.com",
"name": "spc01.test.domain.com",
"id": 1,
"id": 1,
"name": "spc01.test.domain.com",
"identifier": "eth0",
"id": 7,
"name": "puppet_ca",
"id": 6,
"name": "puppetmaster",
"id": 8,
"name": "mtu",
"id": 10,
"name": "dns_secondary",
"id": 9,
"name": "dns_primary",
[root@spc01 ~]#

If I try to change its HG into some other one, I get this error:

[root@spc01 ~]# curl -kSs -H "Content-type:application/json" -u
admin:$FOREMAN_PASSWORD -X PUT https://localhost/api/hosts/hostname -f -d
'{ "hostgroup_id": 3 }' | jq '.'
{
"error": {
"message": "Resource host not found by id 'spc01.test.domain.com'"
}
}

production log shows me this:

> Started PUT "/api/hosts/spc01.test.domain.com" for 127.0.0.1 at
2016-02-17 01:21:45 +0000
2016-02-17 01:21:45 [app] [I] Processing by Api::V2::HostsController#update
as JSON
2016-02-17 01:21:45 [app] [I] Parameters: {"hostgroup_id"=>3,
"apiv"=>"v2", "id"=>"spc01.test.domain.com", :host=>{"hostgroup_id"=>3}}
2016-02-17 01:21:45 [app] [I] Authorized user admin(Admin User)
2016-02-17 01:21:45 [app] [I] Couldn't find Host::Managed with
id=spc01.test.domain.com [WHERE hosts.type IN ('Host::Managed') AND
((hostgroups.id = 3 OR hostgroups.title = 3))
]
(ActiveRecord::RecordNotFound)
2016-02-17 01:21:45 [app] [I] Rendered api/v2/errors/not_found.json.rabl
within api/v2/layouts/error_layout (1.2ms)
2016-02-17 01:21:45 [app] [I] Completed 404 Not Found in 32ms (Views: 2.2ms
> ActiveRecord: 5.3ms)

As you can see, the reason things fail is that API tries to find my host AS
if it is already in a new HG, which is obviously can't be true unless a new
HG is the same as a current one. The latter works, but obviously useless:

[root@spc01 ~]# curl -kSs -H "Content-type:application/json" -u
admin:$FOREMAN_PASSWORD -X PUT https://localhost/api/hosts/hostname -f -d
'{ "hostgroup_id": 5 }' | jq '.' | grep hostgroup
"hostgroup_id": 5,
"hostgroup_name": "common/puppet-master",
[root@spc01 ~]#

Is my API call is screwed up and I'm missing something in it that wasn't
required in previous versions of Foreman? Or is this a bug in API
processing?

Ideas, suggestions?

Thanks!

It's a bug, please report it in Redmine:
http://projects.theforeman.org/projects/foreman/issues/new

··· On 17/02/16 01:43, 'Konstantin Orekhov' via Foreman users wrote: > production log shows me this: > > > Started PUT "/api/hosts/spc01.test.domain.com" for 127.0.0.1 at > 2016-02-17 01:21:45 +0000 > 2016-02-17 01:21:45 [app] [I] Processing by > Api::V2::HostsController#update as JSON > 2016-02-17 01:21:45 [app] [I] Parameters: {*"hostgroup_id"=>3*, > "apiv"=>"v2", "id"=>"spc01.test.domain.com", :host=>{*"hostgroup_id"=>3*}} > 2016-02-17 01:21:45 [app] [I] Authorized user admin(Admin User) > 2016-02-17 01:21:45 [app] [I] Couldn't find Host::Managed with > id=spc01.test.domain.com [WHERE `hosts`.`type` IN ('Host::Managed') *AND > ((`hostgroups`.`id` = 3 OR `hostgroups`.`title` = 3))*] > (ActiveRecord::RecordNotFound) > 2016-02-17 01:21:45 [app] [I] Rendered > api/v2/errors/not_found.json.rabl within api/v2/layouts/error_layout (1.2ms) > 2016-02-17 01:21:45 [app] [I] Completed 404 Not Found in 32ms (Views: > 2.2ms | ActiveRecord: 5.3ms) > > As you can see, the reason things fail is that API tries to find my host > AS if it is already in a new HG, which is obviously can't be true unless > a new HG is the same as a current one. The latter works, but obviously > useless: > > [root@spc01 ~]# curl -kSs -H "Content-type:application/json" -u > admin:$FOREMAN_PASSWORD -X PUT https://localhost/api/hosts/`hostname -f` > -d '{ "hostgroup_id": 5 }' | jq '.' | grep hostgroup > "hostgroup_id": 5, > "hostgroup_name": "common/puppet-master", > [root@spc01 ~]# > > Is my API call is screwed up and I'm missing something in it that wasn't > required in previous versions of Foreman? Or is this a bug in API > processing?


Dominic Cleal
dominic@cleal.org

>
> It's a bug, please report it in Redmine:
> Foreman
>
>
Here you - Bug #13775: Not able to change an HG for a host using API call - Foreman
Thanks!