summaryrefslogtreecommitdiffstats
path: root/collections-debian-merged/ansible_collections/openstack/cloud/plugins/modules/volume_snapshot.py
blob: 7c27fcc2513dc297fe40a58249980237bc40ab62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python
# coding: utf-8 -*-

# Copyright (c) 2016, Mario Santos <mario.rf.santos@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

DOCUMENTATION = '''
---
module: volume_snapshot
short_description: Create/Delete Cinder Volume Snapshots
author: OpenStack Ansible SIG
description:
   - Create or Delete cinder block storage volume snapshots
options:
   display_name:
     description:
        - Name of the snapshot
     required: true
     aliases: ['name']
     type: str
   display_description:
     description:
       - String describing the snapshot
     aliases: ['description']
     type: str
   volume:
     description:
       - The volume name or id to create/delete the snapshot
     required: True
     type: str
   force:
     description:
       - Allows or disallows snapshot of a volume to be created when the volume
         is attached to an instance.
     type: bool
     default: 'no'
   state:
     description:
       - Should the resource be present or absent.
     choices: [present, absent]
     default: present
     type: str
requirements:
    - "python >= 3.6"
    - "openstacksdk"

extends_documentation_fragment:
- openstack.cloud.openstack
'''

EXAMPLES = '''
# Creates a snapshot on volume 'test_volume'
- name: create and delete snapshot
  hosts: localhost
  tasks:
  - name: create snapshot
    openstack.cloud.volume_snapshot:
      state: present
      cloud: mordred
      availability_zone: az2
      display_name: test_snapshot
      volume: test_volume
  - name: delete snapshot
    openstack.cloud.volume_snapshot:
      state: absent
      cloud: mordred
      availability_zone: az2
      display_name: test_snapshot
      volume: test_volume
'''

RETURN = '''
snapshot:
    description: The snapshot instance after the change
    returned: success
    type: dict
    sample:
      id: 837aca54-c0ee-47a2-bf9a-35e1b4fdac0c
      name: test_snapshot
      volume_id: ec646a7c-6a35-4857-b38b-808105a24be6
      size: 2
      status: available
      display_name: test_snapshot
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (
    openstack_full_argument_spec,
    openstack_module_kwargs,
    openstack_cloud_from_module,
)


def _present_volume_snapshot(module, cloud):
    volume = cloud.get_volume(module.params['volume'])
    snapshot = cloud.get_volume_snapshot(module.params['display_name'],
                                         filters={'volume_id': volume.id})
    if not snapshot:
        snapshot = cloud.create_volume_snapshot(volume.id,
                                                force=module.params['force'],
                                                wait=module.params['wait'],
                                                timeout=module.params[
                                                    'timeout'],
                                                name=module.params['display_name'],
                                                description=module.params.get(
                                                    'display_description')
                                                )
        module.exit_json(changed=True, snapshot=snapshot)
    else:
        module.exit_json(changed=False, snapshot=snapshot)


def _absent_volume_snapshot(module, cloud):
    volume = cloud.get_volume(module.params['volume'])
    snapshot = cloud.get_volume_snapshot(module.params['display_name'],
                                         filters={'volume_id': volume.id})
    if not snapshot:
        module.exit_json(changed=False)
    else:
        cloud.delete_volume_snapshot(name_or_id=snapshot.id,
                                     wait=module.params['wait'],
                                     timeout=module.params['timeout'],
                                     )
        module.exit_json(changed=True, snapshot_id=snapshot.id)


def _system_state_change(module, cloud):
    volume = cloud.get_volume(module.params['volume'])
    snapshot = cloud.get_volume_snapshot(module.params['display_name'],
                                         filters={'volume_id': volume.id})
    state = module.params['state']

    if state == 'present':
        return snapshot is None
    if state == 'absent':
        return snapshot is not None


def main():
    argument_spec = openstack_full_argument_spec(
        display_name=dict(required=True, aliases=['name']),
        display_description=dict(default=None, aliases=['description']),
        volume=dict(required=True),
        force=dict(required=False, default=False, type='bool'),
        state=dict(default='present', choices=['absent', 'present']),
    )

    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(argument_spec,
                           supports_check_mode=True,
                           **module_kwargs)

    sdk, cloud = openstack_cloud_from_module(module)

    state = module.params['state']

    try:
        if cloud.volume_exists(module.params['volume']):
            if module.check_mode:
                module.exit_json(changed=_system_state_change(module, cloud))
            if state == 'present':
                _present_volume_snapshot(module, cloud)
            if state == 'absent':
                _absent_volume_snapshot(module, cloud)
        else:
            module.fail_json(
                msg="No volume with name or id '{0}' was found.".format(
                    module.params['volume']))
    except (sdk.exceptions.OpenStackCloudException, sdk.exceptions.ResourceTimeout) as e:
        module.fail_json(msg=e.message)


if __name__ == '__main__':
    main()