From 3616c4f3faa3a20ea151d355968de11881c3f0d9 Mon Sep 17 00:00:00 2001 From: Prem Karat Date: Sat, 5 Feb 2022 18:12:47 +0530 Subject: [PATCH 1/2] code cleanup: fix github issue#59 Signed-off-by: Prem Karat --- plugins/module_utils/entity.py | 10 ++++++++++ plugins/module_utils/prism/vms.py | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/module_utils/entity.py b/plugins/module_utils/entity.py index 3b6fe298d..0ea256eb8 100644 --- a/plugins/module_utils/entity.py +++ b/plugins/module_utils/entity.py @@ -66,6 +66,16 @@ def list(self, data=None, endpoint=None, use_base_url=False, timeout=30): url = url + "/{0}".format(endpoint) return self._fetch_url(url, method="POST", data=data, timeout=timeout) + def get_spec(self): + spec = self._get_default_spec() + for ansible_param, ansible_value in self.module.params.items(): + build_spec_method = self.build_spec_methods.get(ansible_param) + if build_spec_method and ansible_value: + spec, error = build_spec_method(spec, ansible_value) + if error: + return None, error + return spec, None + def get_uuid(self, name): data = {"filter": "name=={0}".format(name), "length": 1} resp, status = self.list(data) diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index 91bfd5a30..3b371bc93 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -36,16 +36,6 @@ def __init__(self, module): "categories": self._build_spec_categories, } - def get_spec(self): - spec = self._get_default_spec() - for ansible_param, ansible_value in self.module.params.items(): - build_spec_method = self.build_spec_methods.get(ansible_param) - if build_spec_method and ansible_value: - spec, error = build_spec_method(spec, ansible_value) - if error: - return None, error - return spec, None - def _get_default_spec(self): return deepcopy( { From 8b7c471dc13cf9ae2c72e118025deeb7642b50ec Mon Sep 17 00:00:00 2001 From: alaa-bish Date: Sun, 6 Feb 2022 08:50:15 +0000 Subject: [PATCH 2/2] Add unit test cases: Gihub issue 59 --- .../unit/plugins/module_utils/test_entity.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/plugins/module_utils/test_entity.py b/tests/unit/plugins/module_utils/test_entity.py index e0e35d043..ea921e80c 100644 --- a/tests/unit/plugins/module_utils/test_entity.py +++ b/tests/unit/plugins/module_utils/test_entity.py @@ -105,6 +105,16 @@ def test_list_action(self): result = self.entity.list(data) self.assertEqual(result["request"], req) + def test_negative_list_action(self): + data = {} + self.module.params = {} + entity = Entity(self.module, resource_type="") + + req = {"method": "POST", "url": "https://None//list", "data": data} + result = entity.list(data) + self.assertEqual(result["request"], req) + self.assertEqual(entity.headers.get("Authorization"), None) + def test_delete_action(self): uuid = "test_uuid" req = { @@ -115,6 +125,16 @@ def test_delete_action(self): result = self.entity.delete(uuid=uuid) self.assertEqual(result["request"], req) + def test_negative_delete_action(self): + data = None + self.module.params = {} + entity = Entity(self.module, resource_type="") + + req = {"method": "DELETE", "url": "https://None/", "data": data} + result = entity.delete(data) + self.assertEqual(result["request"], req) + self.assertEqual(entity.headers.get("Authorization"), None) + def test_build_url(self): module_name = "test" actual = self.entity._build_url(self.module, "https", "/test")