Bug #13775

Not able to change an HG for a host using API call

Added by Konstantin Orekhov about 1 year ago. Updated 4 months ago.

Status:Closed
Priority:Normal
Assigned To:Amit Karsale
Category:API
Target version:Team Anurag backlog
Difficulty: Bugzilla link:1405992
Found in release:1.10.1 Pull request:https://github.com/theforeman/foreman/pull/4052
Story points-
Velocity based estimate-
Release1.14.1Release relationshipAuto

Description

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,
<snip>
[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.

update_host.png (103 KB) Preview Amit Karsale, 11/11/2016 02:10 AM

Update_host

Related issues

Related to Foreman - Bug #11266: Unnested *_id parameters treated as parent resources in API New 07/31/2015
Related to Foreman - Bug #8343: API resource_scope ignores options Closed 11/11/2014
Related to Foreman - Bug #16403: Updating hostgroups via API with environment_id set fails... Duplicate 09/01/2016

Associated revisions

Revision 8e81effc
Added by Amit Karsale 4 months ago

Fixes #13775 - merge if data set is present

History

#1 Updated by Dominic Cleal about 1 year ago

  • Related to Bug #11266: Unnested *_id parameters treated as parent resources in API added

#2 Updated by Dominic Cleal about 1 year ago

  • Related to Bug #8343: API resource_scope ignores options added

#3 Updated by Dominic Cleal about 1 year ago

  • Release set to 1.10.2

#4 Updated by Dominic Cleal about 1 year ago

  • Release changed from 1.10.2 to 1.10.3

#5 Updated by Dominic Cleal about 1 year ago

  • Release deleted (1.10.3)

#6 Updated by Konstantin Orekhov 12 months ago

Dominic, what release the fix for this is targeted now?

#7 Updated by Dominic Cleal 12 months ago

There is no release target, I'll decide a release if/when a patch is merged.

#8 Updated by Amit Karsale 9 months ago

Hello Konstantine,

the request format for the API is wrong, you are triggering it as below:

curl -kSs -H "Content-type:application/json" -u admin:$FOREMAN_PASSWORD -X PUT https://localhost/api/hosts/`hostname -f` -d '{ "hostgroup_id": 3 }'

instead it should be triggered as :

curl -kSs -H "Content-type:application/json" -u admin:$FOREMAN_PASSWORD -X PUT https://localhost/api/hosts/`hostname -f` -d '{ "host":{"hostgroup_id": 3 }}'

I have verified it with the mentioned request parameter format and it works absolutely fine. Do let know if you find any problem further.

If it works for you we can close the issue.

#9 Updated by Dominic Cleal 9 months ago

It's not wrong, the nested hash is not required per the API documentation, it's optional. This is a regression as the hostgroup_id top-level parameter is being misinterpreted.

#10 Updated by Amit Karsale 9 months ago

okay, in that case the `parent_scope` in the code is forming this misinterpreted query, will look for it and post a fix.

#11 Updated by Dominic Cleal 8 months ago

  • Related to Bug #16403: Updating hostgroups via API with environment_id set fails to find host group added

#12 Updated by Amit Karsale 5 months ago

Hello Dominic, I have viewed the Documentation and the documentation states "host" hash param is "required", Doc url: '/apidoc/v2/hosts/update.html'

PFA

#13 Updated by Dominic Cleal 5 months ago

See https://theforeman.org/manuals/1.13/index.html#5.1API, and the support for unwrapped parameters in both Foreman and Rails. The inline API docs only show the wrapped equivalent, not both.

#14 Updated by The Foreman Bot 5 months ago

  • Status changed from New to Ready For Testing
  • Pull request https://github.com/theforeman/foreman/pull/4052 added

#15 Updated by Amit Karsale 5 months ago

  • Assigned To set to Amit Karsale
  • Target version set to Team Anurag backlog

#16 Updated by Amit Karsale 4 months ago

  • Status changed from Ready For Testing to Closed
  • % Done changed from 0 to 100

#17 Updated by Dominic Cleal 4 months ago

  • Release set to 1.14.1

#18 Updated by Daniel Lobato Garcia 4 months ago

  • Bugzilla link set to 1405992

Also available in: Atom PDF