david-caro / python-foreman

Small low level python wrapper around Foreman API
GNU General Public License v2.0
57 stars 37 forks source link

`update_hosts` method works from the 2nd time #86

Open miarmak opened 7 years ago

miarmak commented 7 years ago

So here are 2 methods of foreman.client.Foreman(), that work incorrect for me: update_hosts hosts.update

It take 2 arguments: host and id. So I'm trying to invoke any of those methods from my code with these 2 args: self.foreman_client.hosts.update(host=host_update_dict, id=1)

And here is the result from the first call:

(server side log)

2017-06-16T00:53:31 [app] [I] Started PUT "/api/hosts/1" for <foreman_ip> at 2017-06-16 00:53:31 +0000
2017-06-16T00:53:31 [app] [I] Processing by Api::V2::HostsController#update as JSON
2017-06-16T00:53:31 [app] [I]   Parameters: {"apiv"=>"v2", "id"=>"1", :host=>{}}

The host argument was sent empty for some reason.

And if I add the same LOC right after the first one, it works:

(my code) self.foreman_client.hosts.update(host=host_update_dict, id=1) self.foreman_client.hosts.update(host=host_update_dict, id=1)

(server side log)

2017-06-16T00:55:25 [app] [I] Started PUT "/api/hosts/1" for <foreman_ip> at 2017-06-16 00:55:25 +0000
2017-06-16T00:55:25 [app] [I] Processing by Api::V2::HostsController#update as JSON
2017-06-16T00:55:25 [app] [I]   Parameters: {"apiv"=>"v2", "id"=>"1", :host=>{}}
....................
2017-06-16T00:55:26 [app] [I] Started PUT "/api/hosts/1" for <foreman_ip> at 2017-06-16 00:55:26 +0000
2017-06-16T00:55:26 [app] [I] Processing by Api::V2::HostsController#update as JSON
2017-06-16T00:55:26 [app] [I]   Parameters: {"host"=>{"host_parameters_attributes"=>[{"id"=>1, "value"=>"[FILTERED]", "name"=>"option"}]}, "id"=>"1", "apiv"=>"v2"}

And every next the same request will work as well.

Foreman version: 1.14.3 python-foreman: 0.4.19

So in this case, am I doing something wrong or is it a bug?

david-caro commented 6 years ago

That's very interesting... I'll have to take a look. Can you share the code where you use it? even if it's 'anonymized'? That will help troubleshooting :)

miarmak commented 6 years ago

Unfortunately the code is not published anywhere. But I can show here, how I use that part. Simplistically, it looks like this:

    PARAM_NAME = 'param_name'

    def _get_host_param(self, host):
        for param in host['parameters']:
            if param['name'] == self.PARAM_NAME:
                return param

    def set_param(self, param_state, hosts_list):
        for host in hosts_list:
            h = self.foreman_client.hosts.show(host)

            param = self._get_host_param(h)

            if not param:
                param = dict()
                param['name'] = self.PARAM_NAME

            if param.get('value') == param_state:
                continue

            param['value'] = param_state

            host_update = {'host_parameters_attributes': [param]}

            self.foreman_client.hosts.update(host=host_update, id=h['id'])
            self.foreman_client.hosts.update(host=host_update, id=h['id'])

Thanks!