|
|
@ -34,6 +34,18 @@ try:
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
has_lib_cs = False
|
|
|
|
has_lib_cs = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cs_argument_spec():
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
|
|
|
|
api_key = dict(default=None),
|
|
|
|
|
|
|
|
api_secret = dict(default=None, no_log=True),
|
|
|
|
|
|
|
|
api_url = dict(default=None),
|
|
|
|
|
|
|
|
api_http_method = dict(choices=['get', 'post'], default='get'),
|
|
|
|
|
|
|
|
api_timeout = dict(type='int', default=10),
|
|
|
|
|
|
|
|
api_region = dict(default='cloudstack'),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cs_required_together():
|
|
|
|
|
|
|
|
return [['api_key', 'api_secret', 'api_url']]
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleCloudStack(object):
|
|
|
|
class AnsibleCloudStack(object):
|
|
|
|
|
|
|
|
|
|
|
@ -63,6 +75,8 @@ class AnsibleCloudStack(object):
|
|
|
|
|
|
|
|
|
|
|
|
# Init returns dict for use in subclasses
|
|
|
|
# Init returns dict for use in subclasses
|
|
|
|
self.returns = {}
|
|
|
|
self.returns = {}
|
|
|
|
|
|
|
|
# these values will be casted to int
|
|
|
|
|
|
|
|
self.returns_to_int = {}
|
|
|
|
|
|
|
|
|
|
|
|
self.module = module
|
|
|
|
self.module = module
|
|
|
|
self._connect()
|
|
|
|
self._connect()
|
|
|
@ -76,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):
|
|
|
@ -305,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']
|
|
|
|
def _delete_tags(self, resource, resource_type, tags):
|
|
|
|
response = self.cs.listTags(**args)
|
|
|
|
existing_tags = resource['tags']
|
|
|
|
self.tags = response.get('tag', [])
|
|
|
|
tags_to_delete = []
|
|
|
|
|
|
|
|
for existing_tag in existing_tags:
|
|
|
|
existing_tags = []
|
|
|
|
if existing_tag['key'] in tags:
|
|
|
|
if self.tags:
|
|
|
|
if existing_tag['value'] != tags[key]:
|
|
|
|
for tag in self.tags:
|
|
|
|
tags_to_delete.append(existing_tag)
|
|
|
|
existing_tags.append({'key': tag['key'], 'value': tag['value']})
|
|
|
|
else:
|
|
|
|
return existing_tags
|
|
|
|
tags_to_delete.append(existing_tag)
|
|
|
|
|
|
|
|
if tags_to_delete:
|
|
|
|
|
|
|
|
|
|
|
|
def _process_tags(self, resource, resource_type, tags, operation="create"):
|
|
|
|
|
|
|
|
if tags:
|
|
|
|
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
|
|
|
|
self.cs.deleteTags(**args)
|
|
|
|
if operation == "create":
|
|
|
|
|
|
|
|
self.cs.createTags(**args)
|
|
|
|
|
|
|
|
else:
|
|
|
|
def _create_tags(self, resource, resource_type, tags):
|
|
|
|
self.cs.deleteTags(**args)
|
|
|
|
tags_to_create = []
|
|
|
|
|
|
|
|
for i, tag_entry in enumerate(tags):
|
|
|
|
|
|
|
|
tag = {
|
|
|
|
def _tags_that_should_exist_or_be_updated(self, resource, tags):
|
|
|
|
'key': tag_entry['key'],
|
|
|
|
existing_tags = self.get_tags(resource)
|
|
|
|
'value': tag_entry['value'],
|
|
|
|
return [tag for tag in tags if tag not in existing_tags]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags_to_create.append(tag)
|
|
|
|
|
|
|
|
if tags_to_create:
|
|
|
|
def _tags_that_should_not_exist(self, resource, tags):
|
|
|
|
self.result['changed'] = True
|
|
|
|
existing_tags = self.get_tags(resource)
|
|
|
|
if not self.module.check_mode:
|
|
|
|
return [tag for tag in existing_tags if tag not in tags]
|
|
|
|
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):
|
|
|
@ -355,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
|
|
|
|
|
|
|
|
|
|
|
@ -396,6 +409,11 @@ class AnsibleCloudStack(object):
|
|
|
|
if search_key in resource:
|
|
|
|
if search_key in resource:
|
|
|
|
self.result[return_key] = resource[search_key]
|
|
|
|
self.result[return_key] = resource[search_key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Bad bad API does not always return int when it should.
|
|
|
|
|
|
|
|
for search_key, return_key in self.returns_to_int.iteritems():
|
|
|
|
|
|
|
|
if search_key in resource:
|
|
|
|
|
|
|
|
self.result[return_key] = int(resource[search_key])
|
|
|
|
|
|
|
|
|
|
|
|
# Special handling for tags
|
|
|
|
# Special handling for tags
|
|
|
|
if 'tags' in resource:
|
|
|
|
if 'tags' in resource:
|
|
|
|
self.result['tags'] = []
|
|
|
|
self.result['tags'] = []
|
|
|
|