cloud: ovirt: support add/remove tag from vms/hosts (#25900)

This commit is contained in:
Ondra Machacek 2017-06-21 13:11:10 +02:00 committed by Ryan Brown
commit 5f1da6809a

View file

@ -40,8 +40,9 @@ options:
required: true required: true
state: state:
description: description:
- "Should the tag be present or absent." - "Should the tag be present/absent/attached/detached."
choices: ['present', 'absent'] - "C(Note): I(attached) and I(detached) states are supported since version 2.4."
choices: ['present', 'absent', 'attached', 'detached']
default: present default: present
description: description:
description: description:
@ -69,6 +70,20 @@ EXAMPLES = '''
- vm1 - vm1
- vm2 - vm2
# Attach a tag to VM 'vm1', keeping the rest already attached tags on VM:
- ovirt_tags:
name: mytag
state: attached
vms:
- vm3
# Detach a tag from VM 'vm1', keeping the rest already attached tags on VM:
- ovirt_tags:
name: mytag
state: detached
vms:
- vm3
# To detach all VMs from tag: # To detach all VMs from tag:
- ovirt_tags: - ovirt_tags:
name: mytag name: mytag
@ -106,8 +121,8 @@ from ansible.module_utils.ovirt import (
check_sdk, check_sdk,
create_connection, create_connection,
equal, equal,
get_id_by_name,
ovirt_full_argument_spec, ovirt_full_argument_spec,
search_by_name,
) )
@ -129,34 +144,45 @@ class TagsModule(BaseModule):
if self._module.params[name] is None: if self._module.params[name] is None:
return return
state = self.param('state')
entities_service = getattr(self._connection.system_service(), '%s_service' % name)() entities_service = getattr(self._connection.system_service(), '%s_service' % name)()
current_vms = [ current_vms = [
vm.name vm.name
for vm in entities_service.list(search='tag=%s' % self._module.params['name']) for vm in entities_service.list(search='tag=%s' % self._module.params['name'])
] ]
# Assign tags: # Assign tags:
for entity_name in self._module.params[name]: if state in ['present', 'attached', 'detached']:
entity = search_by_name(entities_service, entity_name) for entity_name in self._module.params[name]:
tags_service = entities_service.service(entity.id).tags_service() entity_id = get_id_by_name(entities_service, entity_name)
current_tags = [tag.name for tag in tags_service.list()] tags_service = entities_service.service(entity_id).tags_service()
# Assign the tag: current_tags = [tag.name for tag in tags_service.list()]
if self._module.params['name'] not in current_tags: # Assign the tag:
if not self._module.check_mode: if state in ['attached', 'present']:
tags_service.add( if self._module.params['name'] not in current_tags:
tag=otypes.Tag( if not self._module.check_mode:
name=self._module.params['name'], tags_service.add(
), tag=otypes.Tag(
) name=self._module.params['name'],
self.changed = True ),
)
self.changed = True
# Detach the tag:
elif state == 'detached':
if self._module.params['name'] in current_tags:
tag_id = get_id_by_name(tags_service, self.param('name'))
if not self._module.check_mode:
tags_service.tag_service(tag_id).remove()
self.changed = True
# Unassign tags: # Unassign tags:
for entity_name in [e for e in current_vms if e not in self._module.params[name]]: if state == 'present':
if not self._module.check_mode: for entity_name in [e for e in current_vms if e not in self._module.params[name]]:
entity = search_by_name(entities_service, entity_name) if not self._module.check_mode:
tags_service = entities_service.service(entity.id).tags_service() entity_id = get_id_by_name(entities_service, entity_name)
tag_id = [tag.id for tag in tags_service.list() if tag.name == self._module.params['name']][0] tags_service = entities_service.service(entity_id).tags_service()
tags_service.tag_service(tag_id).remove() tag_id = get_id_by_name(tags_service, self.param('name'))
self.changed = True tags_service.tag_service(tag_id).remove()
self.changed = True
def _get_parent(self, entity): def _get_parent(self, entity):
parent = None parent = None
@ -176,7 +202,7 @@ class TagsModule(BaseModule):
def main(): def main():
argument_spec = ovirt_full_argument_spec( argument_spec = ovirt_full_argument_spec(
state=dict( state=dict(
choices=['present', 'absent'], choices=['present', 'absent', 'attached', 'detached'],
default='present', default='present',
), ),
name=dict(default=None, required=True), name=dict(default=None, required=True),
@ -202,7 +228,7 @@ def main():
) )
state = module.params['state'] state = module.params['state']
if state == 'present': if state in ['present', 'attached', 'detached']:
ret = tags_module.create() ret = tags_module.create()
elif state == 'absent': elif state == 'absent':
ret = tags_module.remove() ret = tags_module.remove()