From ff32b509f83b3743070404fafa766e9384ffeb3c Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Tue, 28 May 2024 16:41:17 +0300 Subject: [PATCH 1/9] Exclude failed setting in case when image not found in DecortController.image_find method --- module_utils/decort_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index 7eb3bd1..ea19522 100644 --- a/module_utils/decort_utils.py +++ b/module_utils/decort_utils.py @@ -1386,7 +1386,6 @@ class DecortController(object): return image_record['id'], image_record self.result['failed'] = False - self.result['failed'] = True self.result['msg'] = ("Failed to find OS image by name '{}', SEP ID {}, pool '{}' for " "account ID '{}'.").format(image_name, sepid, pool, From 9dc3a5e780f90d2f953ae7d3a7c26d616a1c8dd9 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Tue, 28 May 2024 17:31:44 +0300 Subject: [PATCH 2/9] Fix account_Id parameter value for call of decort_osimage.decort_image_create method --- library/decort_osimage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/decort_osimage.py b/library/decort_osimage.py index b78c89e..22f0a49 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -350,7 +350,7 @@ class decort_osimage(DecortController): hotresize=amodule.params['hotresize'], username=amodule.params['image_username'], password=amodule.params['image_password'], - account_Id=amodule.params['account_Id'], + account_Id=self.validated_account_id, usernameDL=amodule.params['usernameDL'], passwordDL=amodule.params['passwordDL'], sepId=amodule.params['sepId'], From 76a1ff1788709b9f751da1951a6c2974e1cb0abf Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Wed, 29 May 2024 13:18:12 +0300 Subject: [PATCH 3/9] Fix image renaming logic in decort_osimage.__init__ method --- library/decort_osimage.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/decort_osimage.py b/library/decort_osimage.py index b78c89e..0b95c39 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -314,8 +314,9 @@ class decort_osimage(DecortController): if amodule.params['image_id'] != 0 and amodule.params['image_name']: - self.validated_image_id = amodule.params['image_id'] - if amodule.params['image_name']: + self.validated_image_id, image_facts = self.decort_image_find(amodule) + if (self.validated_image_id and + amodule.params['image_name'] != image_facts['name']): decort_osimage.decort_image_rename(self,amodule) self.result['msg'] = ("Image renamed successfully") From a39ab95c1f710ce908c378558601d68b2bb13b49 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Wed, 29 May 2024 14:55:47 +0300 Subject: [PATCH 4/9] Fix access to API response image list in DecortController.virt_image_find method --- module_utils/decort_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index 7eb3bd1..851d6a1 100644 --- a/module_utils/decort_utils.py +++ b/module_utils/decort_utils.py @@ -1439,7 +1439,7 @@ class DecortController(object): api_resp = self.decort_api_call(requests.post, "/restmachine/cloudapi/image/list", api_params) # On success the above call will return here. On error it will abort execution by calling fail_json. images_list = json.loads(api_resp.content.decode('utf8')) - for image_record in images_list: + for image_record in images_list['data']: if image_record['name'] == virt_name and image_record['status'] == "CREATED" and image_record['type'] == "virtual": if sepid == 0 and pool == "": # if no filtering by SEP ID or pool name is requested, return the first match From aa96af1455e3c502d95baf9cf2977688536388c0 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Wed, 29 May 2024 18:11:05 +0300 Subject: [PATCH 5/9] Exclude 'failed' setting in case when image not found in DecortController.virt_image_find method --- module_utils/decort_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index 7eb3bd1..0d481fb 100644 --- a/module_utils/decort_utils.py +++ b/module_utils/decort_utils.py @@ -1448,7 +1448,6 @@ class DecortController(object): if full_match: return image_record['id'], image_record - self.result['failed'] = True self.result['msg'] = ("Failed to find virtual OS image by name '{}', SEP ID {}, pool '{}' for " "account ID '{}'.").format(virt_name, sepid, pool, From 2eb43815e79cce2ad87d4157c72b12e51a0cebfb Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Wed, 29 May 2024 19:02:27 +0300 Subject: [PATCH 6/9] Fix if-condition logic in main function in decort_osimage.py --- library/decort_osimage.py | 45 +++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/library/decort_osimage.py b/library/decort_osimage.py index 8aef066..c08fba8 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -507,29 +507,6 @@ def main(): decon = decort_osimage(amodule) - if amodule.params['image_name'] or amodule.params['image_id']: - image_id, image_facts = decort_osimage.decort_image_find(decon, amodule) - decon.validated_image_id = decort_osimage.decort_osimage_package_facts(image_facts)['id'] - if decort_osimage.decort_osimage_package_facts(image_facts)['id'] > 0: - decon.result['facts'] = decort_osimage.decort_osimage_package_facts(image_facts, amodule.check_mode) - - if amodule.params['state'] == "present" and decon.validated_image_id == 0 and amodule.params['image_name'] and amodule.params['url']: - decort_osimage.decort_image_create(decon,amodule) - decon.result['changed'] = True - image_id, image_facts = decort_osimage.decort_image_find(decon, amodule) - decon.result['msg'] = ("OS image '{}' created").format(decort_osimage.decort_osimage_package_facts(image_facts)['id']) - decon.result['facts'] = decort_osimage.decort_osimage_package_facts(image_facts, amodule.check_mode) - decon.validated_image_id = decort_osimage.decort_osimage_package_facts(image_facts)['id'] - - - elif amodule.params['state'] == "absent" and decon.validated_image_id: - if amodule.params['image_name'] or amodule.params['image_id'] and\ - decort_osimage.decort_osimage_package_facts(image_facts)['accountId'] == amodule.params['account_Id']: - amodule.image_id_delete = decon.validated_image_id - decort_osimage.decort_image_delete(decon,amodule) - - - if amodule.params['virt_name'] or amodule.params['virt_id']: image_id, image_facts = decort_osimage.decort_virt_image_find(decon, amodule) @@ -538,7 +515,6 @@ def main(): decon.validated_virt_image_id = decort_osimage.decort_osimage_package_facts(image_facts)['id'] decon.validated_virt_image_name = decort_osimage.decort_osimage_package_facts(image_facts)['name'] - if decort_osimage.decort_osimage_package_facts(image_facts)['id'] == 0 and amodule.params['state'] == "present" and decon.validated_image_id > 0: image_id, image_facts = decort_osimage.decort_virt_image_create(decon,amodule) decon.result['msg'] = ("Virtual image '{}' created").format(decort_osimage.decort_osimage_package_facts(image_facts)['id']) @@ -547,19 +523,36 @@ def main(): decon.result['msg'] = ("Cannot find OS image") amodule.fail_json(**decon.result) - if decon.validated_image_id: if decort_osimage.decort_osimage_package_facts(image_facts)['linkto'] != decon.validated_image_id: decort_osimage.decort_virt_image_link(decon,amodule) decon.result['changed'] = True amodule.exit_json(**decon.result) - if decon.validated_virt_image_id > 0 and amodule.params['state'] == "absent": decon.result['msg'] = ("Osimage module cannot delete virtual images.") decon.result['failed'] = True amodule.exit_json(**decon.result) + elif amodule.params['image_name'] or amodule.params['image_id']: + image_id, image_facts = decort_osimage.decort_image_find(decon, amodule) + decon.validated_image_id = decort_osimage.decort_osimage_package_facts(image_facts)['id'] + if decort_osimage.decort_osimage_package_facts(image_facts)['id'] > 0: + decon.result['facts'] = decort_osimage.decort_osimage_package_facts(image_facts, amodule.check_mode) + + if amodule.params['state'] == "present" and decon.validated_image_id == 0 and amodule.params['image_name'] and amodule.params['url']: + decort_osimage.decort_image_create(decon,amodule) + decon.result['changed'] = True + image_id, image_facts = decort_osimage.decort_image_find(decon, amodule) + decon.result['msg'] = ("OS image '{}' created").format(decort_osimage.decort_osimage_package_facts(image_facts)['id']) + decon.result['facts'] = decort_osimage.decort_osimage_package_facts(image_facts, amodule.check_mode) + decon.validated_image_id = decort_osimage.decort_osimage_package_facts(image_facts)['id'] + + elif amodule.params['state'] == "absent" and decon.validated_image_id: + if amodule.params['image_name'] or amodule.params['image_id'] and\ + decort_osimage.decort_osimage_package_facts(image_facts)['accountId'] == amodule.params['account_Id']: + amodule.image_id_delete = decon.validated_image_id + decort_osimage.decort_image_delete(decon,amodule) if decon.result['failed'] == True: # we failed to find the specified image - fail the module From e5504b3ac9ca1223336c43c0c0925527ac5c1d88 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Thu, 30 May 2024 14:30:10 +0300 Subject: [PATCH 7/9] Add if-condition logic correction in main function in decort_osimage.py --- library/decort_osimage.py | 1 + 1 file changed, 1 insertion(+) diff --git a/library/decort_osimage.py b/library/decort_osimage.py index c08fba8..b75470a 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -510,6 +510,7 @@ def main(): if amodule.params['virt_name'] or amodule.params['virt_id']: image_id, image_facts = decort_osimage.decort_virt_image_find(decon, amodule) + decon.validated_image_id, _ = decort_osimage.decort_image_find(decon, amodule) if decort_osimage.decort_osimage_package_facts(image_facts)['id'] > 0: decon.result['facts'] = decort_osimage.decort_osimage_package_facts(image_facts, amodule.check_mode) decon.validated_virt_image_id = decort_osimage.decort_osimage_package_facts(image_facts)['id'] From 2777059b6bf217f4a1b0ae296f0f8ae6e4922453 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Thu, 30 May 2024 16:06:25 +0300 Subject: [PATCH 8/9] Add logic for virtual image renaming in decort_osimage class --- library/decort_osimage.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/library/decort_osimage.py b/library/decort_osimage.py index e93009c..f607769 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -313,7 +313,14 @@ class decort_osimage(DecortController): amodule.fail_json(**self.result) - if amodule.params['image_id'] != 0 and amodule.params['image_name']: + if amodule.params['virt_id'] != 0 and amodule.params['virt_name']: + self.validated_virt_image_id, image_facts =\ + self.decort_virt_image_find(amodule) + if (self.validated_virt_image_id and + amodule.params['virt_name'] != image_facts['name']): + self.decort_virt_image_rename(amodule) + self.result['msg'] = 'Virtual image renamed successfully' + elif amodule.params['image_id'] != 0 and amodule.params['image_name']: self.validated_image_id, image_facts = self.decort_image_find(amodule) if (self.validated_image_id and amodule.params['image_name'] != image_facts['name']): @@ -390,6 +397,12 @@ class decort_osimage(DecortController): image_id, image_facts = decort_osimage.decort_image_find(self, amodule) return image_id, image_facts + def decort_virt_image_rename(self, amodule): + image_facts = self.image_rename(imageId=self.validated_virt_image_id, + name=amodule.params['virt_name']) + self.result['msg'] = ("Virtual image renamed successfully") + image_id, image_facts = self.decort_virt_image_find(amodule) + return image_id, image_facts def decort_osimage_package_facts(arg_osimage_facts, arg_check_mode=False): """Package a dictionary of OS image according to the decort_osimage module specification. This From 1e994410fe5b8bbfafbd19ba40ff71a02dbb9193 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Thu, 30 May 2024 16:24:28 +0300 Subject: [PATCH 9/9] Fix if-condition for executing logic of virtual image link changing --- library/decort_osimage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/decort_osimage.py b/library/decort_osimage.py index f607769..cbe84e5 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -538,7 +538,7 @@ def main(): decon.result['msg'] = ("Cannot find OS image") amodule.fail_json(**decon.result) - if decon.validated_image_id: + if decon.validated_virt_image_id: if decort_osimage.decort_osimage_package_facts(image_facts)['linkto'] != decon.validated_image_id: decort_osimage.decort_virt_image_link(decon,amodule) decon.result['changed'] = True