From 90334b1dddcd5a18adb49b9f7cafbd1e6a71f9bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ne=C4=8Das?= Date: Fri, 7 Aug 2020 20:00:42 +0200 Subject: [PATCH] Backport: ovirt_vm fix cd attachment (#70932) * Backport: ovirt_vm fix cd attachment * add changelog * fix formats * Update changelogs/fragments/70932-ovirt_vm-fix-cd_iso-search.yml Co-authored-by: Rick Elrod --- .../70932-ovirt_vm-fix-cd_iso-search.yml | 2 ++ lib/ansible/modules/cloud/ovirt/ovirt_vm.py | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/70932-ovirt_vm-fix-cd_iso-search.yml diff --git a/changelogs/fragments/70932-ovirt_vm-fix-cd_iso-search.yml b/changelogs/fragments/70932-ovirt_vm-fix-cd_iso-search.yml new file mode 100644 index 00000000000000..b1d8c116d99acf --- /dev/null +++ b/changelogs/fragments/70932-ovirt_vm-fix-cd_iso-search.yml @@ -0,0 +1,2 @@ +bugfixes: +- ovirt_vm - fix cd_iso search diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_vm.py b/lib/ansible/modules/cloud/ovirt/ovirt_vm.py index 2edc476aecf381..0c96ae26faac2e 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_vm.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_vm.py @@ -306,7 +306,7 @@ cd_iso: description: - ISO file from ISO storage domain which should be attached to Virtual Machine. - - If you have multiple ISO disks with the same name use disk ID to specify which should be used. + - If you have multiple ISO disks with the same name use disk ID to specify which should be used or use C(storage_domain) to filter disks. - If you pass empty string the CD will be ejected from VM. - If used with C(state) I(running) or I(present) and VM is running the CD will be attached to VM. - If used with C(state) I(running) or I(present) and VM is down the CD will be attached to VM persistently. @@ -1677,15 +1677,29 @@ def _post_start_action(self, entity): self._wait_for_UP(vm_service) self._attach_cd(vm_service.get()) + def __get_cds_from_sds(self, sds): + for sd in sds: + if sd.type == otypes.StorageDomainType.ISO: + disks = sd.files + elif sd.type == otypes.StorageDomainType.DATA: + disks = sd.disks + else: + continue + disks = list(filter(lambda x: (x.name == self.param('cd_iso') or x.id == self.param('cd_iso')) and + (sd.type == otypes.StorageDomainType.ISO or x.content_type == otypes.DiskContentType.ISO), + self._connection.follow_link(disks))) + if disks: + return disks + def __get_cd_id(self): - disks_service = self._connection.system_service().disks_service() - disks = disks_service.list(search='name="{0}"'.format(self.param('cd_iso'))) + sds_service = self._connection.system_service().storage_domains_service() + sds = sds_service.list(search='name="{0}"'.format(self.param('storage_domain') if self.param('storage_domain') else "*")) + disks = self.__get_cds_from_sds(sds) + if not disks: + raise ValueError('Was not able to find disk with name or id "{0}".'.format(self.param('cd_iso'))) if len(disks) > 1: raise ValueError('Found mutiple disks with same name "{0}" please use \ disk ID in "cd_iso" to specify which disk should be used.'.format(self.param('cd_iso'))) - if not disks: - # The `cd_iso` is valid disk ID returning to _attach_cd - return disks_service.disk_service(self.param('cd_iso')).get().id return disks[0].id def _attach_cd(self, entity):