Skip to content

Commit

Permalink
Backport: ovirt_vm fix cd attachment (ansible#70932)
Browse files Browse the repository at this point in the history
* 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 <rick@elrod.me>
  • Loading branch information
mnecas and relrod committed Aug 7, 2020
1 parent c632d74 commit 90334b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/70932-ovirt_vm-fix-cd_iso-search.yml
@@ -0,0 +1,2 @@
bugfixes:
- ovirt_vm - fix cd_iso search
26 changes: 20 additions & 6 deletions lib/ansible/modules/cloud/ovirt/ovirt_vm.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 90334b1

Please sign in to comment.