2015-11-07 17:48:46 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Copyright (c) 2015 IBM Corporation
|
2017-10-09 16:56:26 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-11-07 17:48:46 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2015-11-07 17:48:46 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_project
|
|
|
|
short_description: Manage OpenStack Projects
|
|
|
|
extends_documentation_fragment: openstack
|
|
|
|
version_added: "2.0"
|
|
|
|
author: "Alberto Gireud (@agireud)"
|
|
|
|
description:
|
|
|
|
- Manage OpenStack Projects. Projects can be created,
|
|
|
|
updated or deleted using this module. A project will be updated
|
|
|
|
if I(name) matches an existing project and I(state) is present.
|
|
|
|
The value for I(name) cannot be updated without deleting and
|
|
|
|
re-creating the project.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name for the project
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Description for the project
|
|
|
|
domain_id:
|
|
|
|
description:
|
2016-08-31 19:41:12 +00:00
|
|
|
- Domain id to create the project in if the cloud supports domains.
|
2016-01-01 06:52:52 +00:00
|
|
|
aliases: ['domain']
|
2015-11-07 17:48:46 +00:00
|
|
|
enabled:
|
|
|
|
description:
|
|
|
|
- Is the project enabled
|
2018-03-15 21:15:24 +00:00
|
|
|
type: bool
|
|
|
|
default: 'yes'
|
2015-11-07 17:48:46 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
default: present
|
2017-02-17 20:49:03 +00:00
|
|
|
availability_zone:
|
|
|
|
description:
|
2017-06-12 06:55:19 +00:00
|
|
|
- Ignored. Present for backwards compatibility
|
2015-11-07 17:48:46 +00:00
|
|
|
requirements:
|
2018-05-29 12:51:58 +00:00
|
|
|
- "python >= 2.7"
|
2018-05-26 01:40:39 +00:00
|
|
|
- "openstacksdk"
|
2015-11-07 17:48:46 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a project
|
|
|
|
- os_project:
|
|
|
|
cloud: mycloud
|
2018-06-27 12:26:03 +00:00
|
|
|
endpoint_type: admin
|
2015-11-07 17:48:46 +00:00
|
|
|
state: present
|
|
|
|
name: demoproject
|
|
|
|
description: demodescription
|
|
|
|
domain_id: demoid
|
|
|
|
enabled: True
|
|
|
|
|
|
|
|
# Delete a project
|
|
|
|
- os_project:
|
|
|
|
cloud: mycloud
|
2018-06-27 12:26:03 +00:00
|
|
|
endpoint_type: admin
|
2015-11-07 17:48:46 +00:00
|
|
|
state: absent
|
|
|
|
name: demoproject
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
project:
|
|
|
|
description: Dictionary describing the project.
|
|
|
|
returned: On success when I(state) is 'present'
|
2017-04-27 11:01:11 +00:00
|
|
|
type: complex
|
2015-11-07 17:48:46 +00:00
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Project ID
|
|
|
|
type: string
|
|
|
|
sample: "f59382db809c43139982ca4189404650"
|
|
|
|
name:
|
|
|
|
description: Project name
|
|
|
|
type: string
|
|
|
|
sample: "demoproject"
|
2015-11-16 23:31:53 +00:00
|
|
|
description:
|
|
|
|
description: Project description
|
|
|
|
type: string
|
|
|
|
sample: "demodescription"
|
|
|
|
enabled:
|
|
|
|
description: Boolean to indicate if project is enabled
|
|
|
|
type: bool
|
|
|
|
sample: True
|
2015-11-07 17:48:46 +00:00
|
|
|
'''
|
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-02-15 14:20:49 +00:00
|
|
|
from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs, openstack_cloud_from_module
|
2017-02-02 19:45:22 +00:00
|
|
|
|
|
|
|
|
2015-11-07 17:48:46 +00:00
|
|
|
def _needs_update(module, project):
|
|
|
|
keys = ('description', 'enabled')
|
|
|
|
for key in keys:
|
|
|
|
if module.params[key] is not None and module.params[key] != project.get(key):
|
2016-07-25 18:05:58 +00:00
|
|
|
return True
|
2015-11-07 17:48:46 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
|
2015-11-07 17:48:46 +00:00
|
|
|
def _system_state_change(module, project):
|
|
|
|
state = module.params['state']
|
|
|
|
if state == 'present':
|
|
|
|
if project is None:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if _needs_update(module, project):
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
if project is None:
|
2017-10-09 16:56:26 +00:00
|
|
|
changed = False
|
2015-11-07 17:48:46 +00:00
|
|
|
else:
|
2017-10-09 16:56:26 +00:00
|
|
|
changed = True
|
2015-11-07 17:48:46 +00:00
|
|
|
|
2017-01-28 08:12:11 +00:00
|
|
|
return changed
|
2015-11-07 17:48:46 +00:00
|
|
|
|
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
def main():
|
2015-11-07 17:48:46 +00:00
|
|
|
argument_spec = openstack_full_argument_spec(
|
|
|
|
name=dict(required=True),
|
|
|
|
description=dict(required=False, default=None),
|
2016-01-01 06:52:52 +00:00
|
|
|
domain_id=dict(required=False, default=None, aliases=['domain']),
|
2015-11-07 17:48:46 +00:00
|
|
|
enabled=dict(default=True, type='bool'),
|
|
|
|
state=dict(default='present', choices=['absent', 'present'])
|
|
|
|
)
|
|
|
|
|
|
|
|
module_kwargs = openstack_module_kwargs()
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
**module_kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
description = module.params['description']
|
2018-02-15 14:20:49 +00:00
|
|
|
domain = module.params.get('domain_id')
|
2015-11-07 17:48:46 +00:00
|
|
|
enabled = module.params['enabled']
|
|
|
|
state = module.params['state']
|
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
sdk, cloud = openstack_cloud_from_module(module)
|
2015-11-07 17:48:46 +00:00
|
|
|
try:
|
2016-03-22 11:25:57 +00:00
|
|
|
if domain:
|
|
|
|
try:
|
|
|
|
# We assume admin is passing domain id
|
2018-02-15 14:20:49 +00:00
|
|
|
dom = cloud.get_domain(domain)['id']
|
2016-03-22 11:25:57 +00:00
|
|
|
domain = dom
|
|
|
|
except:
|
|
|
|
# If we fail, maybe admin is passing a domain name.
|
|
|
|
# Note that domains have unique names, just like id.
|
|
|
|
try:
|
2018-02-15 14:20:49 +00:00
|
|
|
dom = cloud.search_domains(filters={'name': domain})[0]['id']
|
2016-03-22 11:25:57 +00:00
|
|
|
domain = dom
|
|
|
|
except:
|
|
|
|
# Ok, let's hope the user is non-admin and passing a sane id
|
|
|
|
pass
|
2016-07-25 18:05:58 +00:00
|
|
|
|
|
|
|
if domain:
|
|
|
|
project = cloud.get_project(name, domain_id=domain)
|
|
|
|
else:
|
|
|
|
project = cloud.get_project(name)
|
2015-11-07 17:48:46 +00:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=_system_state_change(module, project))
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if project is None:
|
|
|
|
project = cloud.create_project(
|
|
|
|
name=name, description=description,
|
|
|
|
domain_id=domain,
|
|
|
|
enabled=enabled)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if _needs_update(module, project):
|
|
|
|
project = cloud.update_project(
|
|
|
|
project['id'], description=description,
|
|
|
|
enabled=enabled)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
module.exit_json(changed=changed, project=project)
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
if project is None:
|
2017-10-09 16:56:26 +00:00
|
|
|
changed = False
|
2015-11-07 17:48:46 +00:00
|
|
|
else:
|
|
|
|
cloud.delete_project(project['id'])
|
2017-10-09 16:56:26 +00:00
|
|
|
changed = True
|
2015-11-07 17:48:46 +00:00
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
except sdk.exceptions.OpenStackCloudException as e:
|
2015-11-07 17:48:46 +00:00
|
|
|
module.fail_json(msg=e.message, extra_data=e.extra_data)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|