From b22b43448147727b8a6374c0bc4b5695929411d4 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Fri, 24 Mar 2023 14:59:44 +1100 Subject: [PATCH 1/6] Added call method to select proper response from xo server --- plugins/inventory/xen_orchestra.py | 39 ++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index 5a466a6ab0c..93f8a122ed4 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -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 @@ -138,21 +139,43 @@ 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: str, params: dict) -> dict: + """Calls a method on the XO server with the provided parameters.""" + id = int(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( + f'Method call {method} timed out after {self.CALL_TIMEOUT / 10} seconds.') + + 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( From 860d1611677459dab96d41530cfc9c492841a78f Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Fri, 24 Mar 2023 15:29:25 +1100 Subject: [PATCH 2/6] Added changelog fragment --- changelogs/fragments/6227-xen-orchestra-check-response-id.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/6227-xen-orchestra-check-response-id.yml diff --git a/changelogs/fragments/6227-xen-orchestra-check-response-id.yml b/changelogs/fragments/6227-xen-orchestra-check-response-id.yml new file mode 100644 index 00000000000..71ebc6b3684 --- /dev/null +++ b/changelogs/fragments/6227-xen-orchestra-check-response-id.yml @@ -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) From 3c3fb99286f18189539fbed0d3f1dc640f888275 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Fri, 24 Mar 2023 15:53:28 +1100 Subject: [PATCH 3/6] Removed excess blank lines --- plugins/inventory/xen_orchestra.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index 93f8a122ed4..5926e8d6a95 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -139,7 +139,6 @@ 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: str, params: dict) -> dict: @@ -164,7 +163,6 @@ def call(self, method: str, params: dict) -> dict: raise AnsibleError( f'Method call {method} timed out after {self.CALL_TIMEOUT / 10} seconds.') - def login(self, user, password): result = self.call('session.signIn', { 'username': user, 'password': password From 7eb979ecc249022c395c07908ea36bdf32846617 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Fri, 24 Mar 2023 23:38:58 +1100 Subject: [PATCH 4/6] Moved period in changelog fragment --- changelogs/fragments/6227-xen-orchestra-check-response-id.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/6227-xen-orchestra-check-response-id.yml b/changelogs/fragments/6227-xen-orchestra-check-response-id.yml index 71ebc6b3684..972caa7d602 100644 --- a/changelogs/fragments/6227-xen-orchestra-check-response-id.yml +++ b/changelogs/fragments/6227-xen-orchestra-check-response-id.yml @@ -1,2 +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) + - 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). From 415c5ebe45c5f491601255ca30afd64cc04c5652 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Fri, 24 Mar 2023 23:39:53 +1100 Subject: [PATCH 5/6] Made suggested changes --- plugins/inventory/xen_orchestra.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index 5926e8d6a95..a60265b7448 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -141,9 +141,10 @@ def create_connection(self, xoa_api_host): CALL_TIMEOUT = 100 """Number of 1/10ths of a second to wait before method call times out.""" - def call(self, method: str, params: dict) -> dict: + + def call(self, method, params): """Calls a method on the XO server with the provided parameters.""" - id = int(self.pointer) + id = self.pointer self.conn.send(json.dumps({ 'id': id, 'jsonrpc': '2.0', From 77a9b69c1d6b591899ff708eed6decbf58c03b6f Mon Sep 17 00:00:00 2001 From: lirkwood Date: Sun, 26 Mar 2023 18:04:13 +1100 Subject: [PATCH 6/6] Remove f-strings for Python 2.7 compatibility Co-authored-by: Felix Fontein --- plugins/inventory/xen_orchestra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index a60265b7448..ddbdd9bb04b 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -162,7 +162,7 @@ def call(self, method, params): waited += 1 raise AnsibleError( - f'Method call {method} timed out after {self.CALL_TIMEOUT / 10} seconds.') + 'Method call {method} timed out after {timeout} seconds.'.format(method=method, timeout=self.CALL_TIMEOUT / 10)) def login(self, user, password): result = self.call('session.signIn', {