Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_hosts method works from the 2nd time #86

Open
miarmak opened this issue Jun 16, 2017 · 2 comments
Open

update_hosts method works from the 2nd time #86

miarmak opened this issue Jun 16, 2017 · 2 comments
Assignees

Comments

@miarmak
Copy link

miarmak commented Jun 16, 2017

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
Copy link
Owner

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 :)

@david-caro david-caro self-assigned this Aug 15, 2017
@miarmak
Copy link
Author

miarmak commented Aug 15, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants