ovirt: Add support to diff (#20698)

* cloud: ovirt: Add diff support to

* cloud: ovirt: Add ability to print nested list of objects

* cloud: ovirt: Fix waiting while reinstalling host

* cloud: ovirt: fix ovirt_vms 404 error

* cloud: ovirt: add check_mode support to ovirt_auth module
This commit is contained in:
Ondra Machacek 2017-02-02 19:50:52 +01:00 committed by Ryan Brown
commit ee7f1cde0e
5 changed files with 118 additions and 56 deletions

View file

@ -190,6 +190,7 @@ def main():
('state', 'absent', ['ovirt_auth']),
('state', 'present', ['username', 'password', 'url']),
],
supports_check_mode=True,
)
check_sdk(module)

View file

@ -19,6 +19,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
import time
import traceback
try:
@ -152,6 +153,12 @@ EXAMPLES = '''
state: upgraded
name: myhost
# Reinstall host using public key
- ovirt_hosts:
state: reinstalled
name: myhost
public_key: true
# Remove host
- ovirt_hosts:
state: absent
@ -184,7 +191,7 @@ class HostsModule(BaseModule):
address=self._module.params['address'],
root_password=self._module.params['password'],
ssh=otypes.Ssh(
authentication_method='publickey',
authentication_method=otypes.SshAuthenticationMethod.PUBLICKEY,
) if self._module.params['public_key'] else None,
kdump_status=otypes.KdumpStatus(
self._module.params['kdump_integration']
@ -228,6 +235,15 @@ class HostsModule(BaseModule):
self._service.host_service(entity.id).activate()
self.changed = True
def post_reinstall(self, host):
wait(
service=self._service.service(host.id),
condition=lambda h: h.status != hoststate.MAINTENANCE,
fail_condition=failed_state,
wait=self._module.params['wait'],
timeout=self._module.params['timeout'],
)
def failed_state(host):
return host.status in [
@ -315,23 +331,23 @@ def main():
state = module.params['state']
control_state(hosts_module)
if state == 'present':
ret = hosts_module.create()
ret['changed'] = hosts_module.action(
hosts_module.create()
ret = hosts_module.action(
action='activate',
action_condition=lambda h: h.status == hoststate.MAINTENANCE,
wait_condition=lambda h: h.status == hoststate.UP,
fail_condition=failed_state,
)['changed'] or ret['changed']
)
elif state == 'absent':
ret = hosts_module.remove()
elif state == 'maintenance':
ret = hosts_module.action(
hosts_module.action(
action='deactivate',
action_condition=lambda h: h.status != hoststate.MAINTENANCE,
wait_condition=lambda h: h.status == hoststate.MAINTENANCE,
fail_condition=failed_state,
)
ret['changed'] = hosts_module.create()['changed'] or ret['changed']
ret = hosts_module.create()
elif state == 'upgraded':
ret = hosts_module.action(
action='upgrade',
@ -348,19 +364,19 @@ def main():
fence_type='start',
)
elif state == 'stopped':
ret = hosts_module.action(
hosts_module.action(
action='deactivate',
action_condition=lambda h: h.status not in [hoststate.MAINTENANCE, hoststate.DOWN],
wait_condition=lambda h: h.status in [hoststate.MAINTENANCE, hoststate.DOWN],
fail_condition=failed_state,
)
ret['changed'] = hosts_module.action(
ret = hosts_module.action(
action='fence',
action_condition=lambda h: h.status != hoststate.DOWN,
wait_condition=lambda h: h.status == hoststate.DOWN,
wait_condition=lambda h: h.status == hoststate.DOWN if module.params['wait'] else True,
fail_condition=failed_state,
fence_type='stop',
)['changed'] or ret['changed']
)
elif state == 'restarted':
ret = hosts_module.action(
action='fence',
@ -370,17 +386,18 @@ def main():
)
elif state == 'reinstalled':
# Deactivate host if not in maintanence:
deactivate_changed = hosts_module.action(
hosts_module.action(
action='deactivate',
action_condition=lambda h: h.status not in [hoststate.MAINTENANCE, hoststate.DOWN],
wait_condition=lambda h: h.status in [hoststate.MAINTENANCE, hoststate.DOWN],
fail_condition=failed_state,
)['changed']
)
# Reinstall host:
install_changed = hosts_module.action(
hosts_module.action(
action='install',
action_condition=lambda h: h.status == hoststate.MAINTENANCE,
post_action=hosts_module.post_reinstall,
wait_condition=lambda h: h.status == hoststate.MAINTENANCE,
fail_condition=failed_state,
host=otypes.Host(
@ -388,9 +405,9 @@ def main():
) if module.params['override_iptables'] else None,
root_password=module.params['password'],
ssh=otypes.Ssh(
authentication_method='publickey',
authentication_method=otypes.SshAuthenticationMethod.PUBLICKEY,
) if module.params['public_key'] else None,
)['changed']
)
# Activate host after reinstall:
ret = hosts_module.action(
@ -399,8 +416,6 @@ def main():
wait_condition=lambda h: h.status == hoststate.UP,
fail_condition=failed_state,
)
ret['changed'] = install_changed or deactivate_changed or ret['changed']
module.exit_json(**ret)
except Exception as e:

View file

@ -34,6 +34,7 @@ from ansible.module_utils.ovirt import (
convert_to_bytes,
create_connection,
equal,
get_entity,
get_link_name,
ovirt_full_argument_spec,
search_by_name,
@ -627,7 +628,7 @@ class VmsModule(BaseModule):
# Attach disk to VM:
disk_attachments_service = self._service.service(entity.id).disk_attachments_service()
if disk_attachments_service.attachment_service(disk_id).get() is None:
if get_entity(disk_attachments_service.attachment_service(disk_id)) is None:
if not self._module.check_mode:
disk_attachments_service.add(
otypes.DiskAttachment(