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

Fixed XenOrchestra inventory plugin failing due to not checking response ID. #6227

Merged
merged 6 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6227-xen-orchestra-check-response-id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- xenorchestra inventory plugin - fix failure to receive objects from server due to not checking the id of the response (https://github.com/ansible-collections/community.general/pull/6227).
38 changes: 30 additions & 8 deletions plugins/inventory/xen_orchestra.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

import json
import ssl
from time import sleep

from ansible.errors import AnsibleError
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
Expand Down Expand Up @@ -138,21 +139,42 @@ def create_connection(self, xoa_api_host):
self.conn = create_connection(
'{0}://{1}/api/'.format(proto, xoa_api_host), sslopt=sslopt)

CALL_TIMEOUT = 100
"""Number of 1/10ths of a second to wait before method call times out."""

def call(self, method, params):
"""Calls a method on the XO server with the provided parameters."""
id = self.pointer
self.conn.send(json.dumps({
'id': id,
'jsonrpc': '2.0',
'method': method,
'params': params
}))

waited = 0
while waited < self.CALL_TIMEOUT:
response = json.loads(self.conn.recv())
if 'id' in response and response['id'] == id:
return response
else:
sleep(0.1)
waited += 1

raise AnsibleError(
'Method call {method} timed out after {timeout} seconds.'.format(method=method, timeout=self.CALL_TIMEOUT / 10))

def login(self, user, password):
payload = {'id': self.pointer, 'jsonrpc': '2.0', 'method': 'session.signIn', 'params': {
'username': user, 'password': password}}
self.conn.send(json.dumps(payload))
result = json.loads(self.conn.recv())
result = self.call('session.signIn', {
'username': user, 'password': password
})

if 'error' in result:
raise AnsibleError(
'Could not connect: {0}'.format(result['error']))

def get_object(self, name):
payload = {'id': self.pointer, 'jsonrpc': '2.0',
'method': 'xo.getAllObjects', 'params': {'filter': {'type': name}}}
self.conn.send(json.dumps(payload))
answer = json.loads(self.conn.recv())
answer = self.call('xo.getAllObjects', {'filter': {'type': name}})

if 'error' in answer:
raise AnsibleError(
Expand Down