cloudstack: make tags handling idempotence

Credits to @jeffersongirao, who provided the patch.
This commit is contained in:
Rene Moser 2015-09-22 16:12:35 +02:00
parent efd122c2f0
commit b43939dfd6

View file

@ -90,6 +90,7 @@ class AnsibleCloudStack(object):
self.os_type = None self.os_type = None
self.hypervisor = None self.hypervisor = None
self.capabilities = None self.capabilities = None
self.tags = None
def _connect(self): def _connect(self):
@ -319,47 +320,44 @@ class AnsibleCloudStack(object):
def get_tags(self, resource=None): def get_tags(self, resource=None):
existing_tags = self.cs.listTags(resourceid=resource['id']) if not self.tags:
if existing_tags: args = {}
return existing_tags['tag'] args['projectid'] = self.get_project(key='id')
return [] args['account'] = self.get_account(key='name')
args['domainid'] = self.get_domain(key='id')
args['resourceid'] = resource['id']
response = self.cs.listTags(**args)
self.tags = response.get('tag', [])
existing_tags = []
if self.tags:
for tag in self.tags:
existing_tags.append({'key': tag['key'], 'value': tag['value']})
return existing_tags
def _delete_tags(self, resource, resource_type, tags): def _process_tags(self, resource, resource_type, tags, operation="create"):
existing_tags = resource['tags'] if tags:
tags_to_delete = []
for existing_tag in existing_tags:
if existing_tag['key'] in tags:
if existing_tag['value'] != tags[key]:
tags_to_delete.append(existing_tag)
else:
tags_to_delete.append(existing_tag)
if tags_to_delete:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
args = {} args = {}
args['resourceids'] = resource['id'] args['resourceids'] = resource['id']
args['resourcetype'] = resource_type args['resourcetype'] = resource_type
args['tags'] = tags_to_delete args['tags'] = tags
if operation == "create":
self.cs.createTags(**args)
else:
self.cs.deleteTags(**args) self.cs.deleteTags(**args)
def _create_tags(self, resource, resource_type, tags): def _tags_that_should_exist_or_be_updated(self, resource, tags):
tags_to_create = [] existing_tags = self.get_tags(resource)
for i, tag_entry in enumerate(tags): return [tag for tag in tags if tag not in existing_tags]
tag = {
'key': tag_entry['key'],
'value': tag_entry['value'], def _tags_that_should_not_exist(self, resource, tags):
} existing_tags = self.get_tags(resource)
tags_to_create.append(tag) return [tag for tag in existing_tags if tag not in tags]
if tags_to_create:
self.result['changed'] = True
if not self.module.check_mode:
args = {}
args['resourceids'] = resource['id']
args['resourcetype'] = resource_type
args['tags'] = tags_to_create
self.cs.createTags(**args)
def ensure_tags(self, resource, resource_type=None): def ensure_tags(self, resource, resource_type=None):
@ -369,8 +367,9 @@ class AnsibleCloudStack(object):
if 'tags' in resource: if 'tags' in resource:
tags = self.module.params.get('tags') tags = self.module.params.get('tags')
if tags is not None: if tags is not None:
self._delete_tags(resource, resource_type, tags) self._process_tags(resource, resource_type, self._tags_that_should_exist_or_be_updated(resource, tags))
self._create_tags(resource, resource_type, tags) self._process_tags(resource, resource_type, self._tags_that_should_not_exist(resource, tags), operation="delete")
self.tags = None
resource['tags'] = self.get_tags(resource) resource['tags'] = self.get_tags(resource)
return resource return resource