diff --git a/library/decort_osimage.py b/library/decort_osimage.py index 8aef066..cbe84e5 100644 --- a/library/decort_osimage.py +++ b/library/decort_osimage.py @@ -313,9 +313,17 @@ class decort_osimage(DecortController): amodule.fail_json(**self.result) - if amodule.params['image_id'] != 0 and amodule.params['image_name']: - self.validated_image_id = amodule.params['image_id'] - if 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']): decort_osimage.decort_image_rename(self,amodule) self.result['msg'] = ("Image renamed successfully") @@ -350,7 +358,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'], @@ -389,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 @@ -507,38 +521,15 @@ 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) + 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'] 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 +538,36 @@ 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 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 diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index 7eb3bd1..0be52ea 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, @@ -1439,7 +1438,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 @@ -1448,7 +1447,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,