2015-05-30 22:54:56 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2015, René Moser <mail@renemoser.net>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: cs_project
|
|
|
|
short_description: Manages projects on Apache CloudStack based clouds.
|
|
|
|
description:
|
|
|
|
- Create, update, suspend, activate and remove projects.
|
|
|
|
version_added: '2.0'
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "René Moser (@resmo)"
|
2015-06-07 23:45:33 +02:00
|
|
|
options:
|
2015-05-30 22:54:56 +02:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the project.
|
|
|
|
required: true
|
2015-08-19 21:51:34 +02:00
|
|
|
display_text:
|
2015-05-30 22:54:56 +02:00
|
|
|
description:
|
2015-08-19 21:51:34 +02:00
|
|
|
- Display text of the project.
|
|
|
|
- If not specified, C(name) will be used as C(display_text).
|
2015-05-30 22:54:56 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the project.
|
|
|
|
required: false
|
|
|
|
default: 'present'
|
|
|
|
choices: [ 'present', 'absent', 'active', 'suspended' ]
|
|
|
|
domain:
|
|
|
|
description:
|
|
|
|
- Domain the project is related to.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
account:
|
|
|
|
description:
|
|
|
|
- Account the project is related to.
|
|
|
|
required: false
|
|
|
|
default: null
|
2016-06-08 15:11:03 +02:00
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- List of tags. Tags are a list of dictionaries having keys C(key) and C(value).
|
|
|
|
- "If you want to delete all tags, set a empty list e.g. C(tags: [])."
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.2"
|
2015-05-30 22:54:56 +02:00
|
|
|
poll_async:
|
|
|
|
description:
|
|
|
|
- Poll async jobs until job has finished.
|
|
|
|
required: false
|
|
|
|
default: true
|
|
|
|
extends_documentation_fragment: cloudstack
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a project
|
|
|
|
- local_action:
|
|
|
|
module: cs_project
|
|
|
|
name: web
|
2016-06-08 15:11:03 +02:00
|
|
|
tags:
|
|
|
|
- { key: admin, value: john }
|
|
|
|
- { key: foo, value: bar }
|
2015-05-30 22:54:56 +02:00
|
|
|
|
|
|
|
# Rename a project
|
|
|
|
- local_action:
|
|
|
|
module: cs_project
|
|
|
|
name: web
|
2015-08-19 21:51:34 +02:00
|
|
|
display_text: my web project
|
2015-05-30 22:54:56 +02:00
|
|
|
|
|
|
|
# Suspend an existing project
|
|
|
|
- local_action:
|
|
|
|
module: cs_project
|
|
|
|
name: web
|
|
|
|
state: suspended
|
|
|
|
|
|
|
|
# Activate an existing project
|
|
|
|
- local_action:
|
|
|
|
module: cs_project
|
|
|
|
name: web
|
|
|
|
state: active
|
|
|
|
|
|
|
|
# Remove a project
|
|
|
|
- local_action:
|
|
|
|
module: cs_project
|
|
|
|
name: web
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
---
|
|
|
|
id:
|
2015-08-17 08:30:11 +02:00
|
|
|
description: UUID of the project.
|
2015-05-30 22:54:56 +02:00
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: 04589590-ac63-4ffc-93f5-b698b8ac38b6
|
|
|
|
name:
|
|
|
|
description: Name of the project.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: web project
|
2015-08-19 21:51:34 +02:00
|
|
|
display_text:
|
2015-05-30 22:54:56 +02:00
|
|
|
description: Display text of the project.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: web project
|
|
|
|
state:
|
|
|
|
description: State of the project.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: Active
|
|
|
|
domain:
|
|
|
|
description: Domain the project is related to.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: example domain
|
|
|
|
account:
|
|
|
|
description: Account the project is related to.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: example account
|
|
|
|
tags:
|
|
|
|
description: List of resource tags associated with the project.
|
|
|
|
returned: success
|
|
|
|
type: dict
|
|
|
|
sample: '[ { "key": "foo", "value": "bar" } ]'
|
|
|
|
'''
|
|
|
|
|
|
|
|
# import cloudstack common
|
|
|
|
from ansible.module_utils.cloudstack import *
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleCloudStackProject(AnsibleCloudStack):
|
|
|
|
|
|
|
|
|
|
|
|
def get_project(self):
|
|
|
|
if not self.project:
|
|
|
|
project = self.module.params.get('name')
|
|
|
|
|
|
|
|
args = {}
|
|
|
|
args['account'] = self.get_account(key='name')
|
|
|
|
args['domainid'] = self.get_domain(key='id')
|
|
|
|
|
|
|
|
projects = self.cs.listProjects(**args)
|
|
|
|
if projects:
|
|
|
|
for p in projects['project']:
|
2015-06-09 13:06:24 +02:00
|
|
|
if project.lower() in [ p['name'].lower(), p['id']]:
|
2015-05-30 22:54:56 +02:00
|
|
|
self.project = p
|
|
|
|
break
|
|
|
|
return self.project
|
|
|
|
|
|
|
|
|
|
|
|
def present_project(self):
|
|
|
|
project = self.get_project()
|
|
|
|
if not project:
|
|
|
|
project = self.create_project(project)
|
|
|
|
else:
|
|
|
|
project = self.update_project(project)
|
2016-06-08 15:11:03 +02:00
|
|
|
if project:
|
|
|
|
project = self.ensure_tags(resource=project, resource_type='project')
|
|
|
|
# refresh resource
|
|
|
|
self.project = project
|
2015-05-30 22:54:56 +02:00
|
|
|
return project
|
|
|
|
|
|
|
|
|
|
|
|
def update_project(self, project):
|
|
|
|
args = {}
|
|
|
|
args['id'] = project['id']
|
2015-08-19 21:51:34 +02:00
|
|
|
args['displaytext'] = self.get_or_fallback('display_text', 'name')
|
2015-05-30 22:54:56 +02:00
|
|
|
|
2016-06-09 22:41:32 +02:00
|
|
|
if self.has_changed(args, project):
|
2015-05-30 22:54:56 +02:00
|
|
|
self.result['changed'] = True
|
|
|
|
if not self.module.check_mode:
|
|
|
|
project = self.cs.updateProject(**args)
|
|
|
|
|
|
|
|
if 'errortext' in project:
|
|
|
|
self.module.fail_json(msg="Failed: '%s'" % project['errortext'])
|
|
|
|
|
|
|
|
poll_async = self.module.params.get('poll_async')
|
|
|
|
if project and poll_async:
|
2016-06-09 22:39:03 +02:00
|
|
|
project = self.poll_job(project, 'project')
|
2015-05-30 22:54:56 +02:00
|
|
|
return project
|
|
|
|
|
|
|
|
|
|
|
|
def create_project(self, project):
|
|
|
|
self.result['changed'] = True
|
|
|
|
|
|
|
|
args = {}
|
|
|
|
args['name'] = self.module.params.get('name')
|
2015-08-19 21:51:34 +02:00
|
|
|
args['displaytext'] = self.get_or_fallback('display_text', 'name')
|
2015-05-30 22:54:56 +02:00
|
|
|
args['account'] = self.get_account('name')
|
|
|
|
args['domainid'] = self.get_domain('id')
|
|
|
|
|
|
|
|
if not self.module.check_mode:
|
|
|
|
project = self.cs.createProject(**args)
|
|
|
|
|
|
|
|
if 'errortext' in project:
|
|
|
|
self.module.fail_json(msg="Failed: '%s'" % project['errortext'])
|
|
|
|
|
|
|
|
poll_async = self.module.params.get('poll_async')
|
|
|
|
if project and poll_async:
|
2016-06-09 22:39:03 +02:00
|
|
|
project = self.poll_job(project, 'project')
|
2015-05-30 22:54:56 +02:00
|
|
|
return project
|
|
|
|
|
|
|
|
|
2016-06-08 15:14:06 +02:00
|
|
|
def state_project(self, state='active'):
|
|
|
|
project = self.present_project()
|
2015-05-30 22:54:56 +02:00
|
|
|
|
|
|
|
if project['state'].lower() != state:
|
|
|
|
self.result['changed'] = True
|
|
|
|
|
|
|
|
args = {}
|
|
|
|
args['id'] = project['id']
|
|
|
|
|
|
|
|
if not self.module.check_mode:
|
|
|
|
if state == 'suspended':
|
|
|
|
project = self.cs.suspendProject(**args)
|
|
|
|
else:
|
|
|
|
project = self.cs.activateProject(**args)
|
|
|
|
|
|
|
|
if 'errortext' in project:
|
|
|
|
self.module.fail_json(msg="Failed: '%s'" % project['errortext'])
|
|
|
|
|
|
|
|
poll_async = self.module.params.get('poll_async')
|
|
|
|
if project and poll_async:
|
2016-06-09 22:39:03 +02:00
|
|
|
project = self.poll_job(project, 'project')
|
2015-05-30 22:54:56 +02:00
|
|
|
return project
|
|
|
|
|
|
|
|
|
|
|
|
def absent_project(self):
|
|
|
|
project = self.get_project()
|
|
|
|
if project:
|
|
|
|
self.result['changed'] = True
|
|
|
|
|
|
|
|
args = {}
|
|
|
|
args['id'] = project['id']
|
|
|
|
|
|
|
|
if not self.module.check_mode:
|
|
|
|
res = self.cs.deleteProject(**args)
|
|
|
|
|
|
|
|
if 'errortext' in res:
|
|
|
|
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
|
|
|
|
|
|
|
|
poll_async = self.module.params.get('poll_async')
|
|
|
|
if res and poll_async:
|
2016-06-09 22:39:03 +02:00
|
|
|
res = self.poll_job(res, 'project')
|
2015-05-30 22:54:56 +02:00
|
|
|
return project
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2015-09-22 16:28:14 +02:00
|
|
|
argument_spec = cs_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
name = dict(required=True),
|
|
|
|
display_text = dict(default=None),
|
|
|
|
state = dict(choices=['present', 'absent', 'active', 'suspended' ], default='present'),
|
|
|
|
domain = dict(default=None),
|
|
|
|
account = dict(default=None),
|
2015-12-16 11:00:17 +01:00
|
|
|
poll_async = dict(type='bool', default=True),
|
2016-06-08 15:11:03 +02:00
|
|
|
tags=dict(type='list', aliases=['tag'], default=None),
|
2015-09-22 16:28:14 +02:00
|
|
|
))
|
|
|
|
|
2015-05-30 22:54:56 +02:00
|
|
|
module = AnsibleModule(
|
2015-09-22 16:28:14 +02:00
|
|
|
argument_spec=argument_spec,
|
|
|
|
required_together=cs_required_together(),
|
2015-05-30 22:54:56 +02:00
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
acs_project = AnsibleCloudStackProject(module)
|
|
|
|
|
|
|
|
state = module.params.get('state')
|
|
|
|
if state in ['absent']:
|
|
|
|
project = acs_project.absent_project()
|
|
|
|
|
|
|
|
elif state in ['active', 'suspended']:
|
|
|
|
project = acs_project.state_project(state=state)
|
|
|
|
|
|
|
|
else:
|
|
|
|
project = acs_project.present_project()
|
|
|
|
|
|
|
|
result = acs_project.get_result(project)
|
|
|
|
|
2015-12-16 11:00:17 +01:00
|
|
|
except CloudStackException as e:
|
2015-05-30 22:54:56 +02:00
|
|
|
module.fail_json(msg='CloudStackException: %s' % str(e))
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2015-06-28 12:52:05 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|