2015-03-31 20:37:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
|
|
|
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
|
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-03-31 20:37:07 +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:05 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_object
|
|
|
|
short_description: Create or Delete objects and containers from OpenStack
|
2016-12-08 02:33:38 +00:00
|
|
|
version_added: "2.0"
|
|
|
|
author: "Monty Taylor (@emonty)"
|
2015-03-31 20:37:07 +00:00
|
|
|
extends_documentation_fragment: openstack
|
|
|
|
description:
|
|
|
|
- Create or Delete objects and containers from OpenStack
|
|
|
|
options:
|
|
|
|
container:
|
|
|
|
description:
|
|
|
|
- The name of the container in which to create the object
|
|
|
|
required: true
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name to be give to the object. If omitted, operations will be on
|
|
|
|
the entire container
|
|
|
|
required: false
|
2015-12-29 07:31:14 +00:00
|
|
|
filename:
|
2015-03-31 20:37:07 +00:00
|
|
|
description:
|
|
|
|
- Path to local file to be uploaded.
|
|
|
|
required: false
|
|
|
|
container_access:
|
|
|
|
description:
|
|
|
|
- desired container access level.
|
|
|
|
required: false
|
|
|
|
choices: ['private', 'public']
|
|
|
|
default: private
|
|
|
|
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
|
2017-02-17 20:49:03 +00:00
|
|
|
required: false
|
2015-03-31 20:37:07 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2016-11-16 17:24:21 +00:00
|
|
|
- name: "Create a object named 'fstab' in the 'config' container"
|
|
|
|
os_object:
|
|
|
|
cloud: mordred
|
|
|
|
state: present
|
|
|
|
name: fstab
|
|
|
|
container: config
|
|
|
|
filename: /etc/fstab
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2016-11-16 17:24:21 +00:00
|
|
|
- name: Delete a container called config and all of its contents
|
|
|
|
os_object:
|
|
|
|
cloud: rax-iad
|
|
|
|
state: absent
|
|
|
|
container: config
|
2015-03-31 20:37:07 +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-10-09 16:56:26 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
def process_object(
|
|
|
|
cloud_obj, container, name, filename, container_access, **kwargs):
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
container_obj = cloud_obj.get_container(container)
|
|
|
|
if kwargs['state'] == 'present':
|
|
|
|
if not container_obj:
|
|
|
|
container_obj = cloud_obj.create_container(container)
|
|
|
|
changed = True
|
|
|
|
if cloud_obj.get_container_access(container) != container_access:
|
|
|
|
cloud_obj.set_container_access(container, container_access)
|
|
|
|
changed = True
|
|
|
|
if name:
|
|
|
|
if cloud_obj.is_object_stale(container, name, filename):
|
|
|
|
cloud_obj.create_object(container, name, filename)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if container_obj:
|
|
|
|
if name:
|
|
|
|
if cloud_obj.get_object_metadata(container, name):
|
2017-01-30 23:01:47 +00:00
|
|
|
cloud_obj.delete_object(container, name)
|
2017-10-09 16:56:26 +00:00
|
|
|
changed = True
|
2015-03-31 20:37:07 +00:00
|
|
|
else:
|
|
|
|
cloud_obj.delete_container(container)
|
2017-10-09 16:56:26 +00:00
|
|
|
changed = True
|
2015-03-31 20:37:07 +00:00
|
|
|
return changed
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = openstack_full_argument_spec(
|
|
|
|
name=dict(required=False, default=None),
|
|
|
|
container=dict(required=True),
|
|
|
|
filename=dict(required=False, default=None),
|
|
|
|
container_access=dict(default='private', choices=['private', 'public']),
|
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
|
|
)
|
|
|
|
module_kwargs = openstack_module_kwargs()
|
|
|
|
module = AnsibleModule(argument_spec, **module_kwargs)
|
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
sdk, cloud = openstack_cloud_from_module(module)
|
2015-03-31 20:37:07 +00:00
|
|
|
try:
|
|
|
|
changed = process_object(cloud, **module.params)
|
|
|
|
|
2015-06-17 09:24:08 +00:00
|
|
|
module.exit_json(changed=changed)
|
2018-05-26 01:40:39 +00:00
|
|
|
except sdk.exceptions.OpenStackCloudException as e:
|
2016-01-13 16:00:16 +00:00
|
|
|
module.fail_json(msg=str(e))
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2016-04-06 19:18:35 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|