summaryrefslogtreecommitdiffstats
path: root/collections-debian-merged/ansible_collections/openstack/cloud/plugins/modules/volume_backup.py
blob: 43cacc72b2d0a869b795b92005779a90f6e7464f (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
# coding: utf-8 -*-
#
# Copyright (c) 2020 by Open Telekom Cloud, operated by T-Systems International GmbH
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


DOCUMENTATION = '''
---
module: volume_backup
short_description: Add/Delete Volume backup
extends_documentation_fragment: openstack.cloud.openstack
author: OpenStack Ansible SIG
description:
  - Add or Remove Volume Backup in OTC.
options:
  display_name:
    description:
      - Name that has to be given to the backup
    required: true
    type: str
    aliases: ['name']
  display_description:
    description:
      - String describing the backup
    required: false
    type: str
    aliases: ['description']
  state:
    description:
      - Should the resource be present or absent.
    choices: [present, absent]
    default: present
    type: str
  volume:
    description:
      - Name or ID of the volume. Required when state is True.
    type: str
    required: False
  snapshot:
    description: Name or ID of the Snapshot to take backup of
    type: str
  force:
    description:
      - Indicates whether to backup, even if the volume is attached.
    type: bool
    default: False
  metadata:
    description: Metadata for the backup
    type: dict
  incremental:
    description: The backup mode
    type: bool
    default: False
requirements: ["openstacksdk"]
'''

RETURN = '''
id:
    description: The Volume backup ID.
    returned: On success when C(state=present)
    type: str
    sample: "39007a7e-ee4f-4d13-8283-b4da2e037c69"
backup:
    description: Dictionary describing the Cluster.
    returned: On success when C(state=present)
    type: complex
    contains:
        id:
            description: Unique UUID.
            type: str
            sample: "39007a7e-ee4f-4d13-8283-b4da2e037c69"
        name:
            description: Name given to the load balancer.
            type: str
            sample: "elb_test"
'''

EXAMPLES = '''
- name: Create backup
  openstack.cloud.volume_backup:
    display_name: test_volume_backup
    volume: "test_volume"

- name: Create backup from snapshot
  openstack.cloud.volume_backup:
    display_name: test_volume_backup
    volume: "test_volume"
    snapshot: "test_snapshot"

- name: Delete volume backup
  openstack.cloud.volume_backup:
    display_name: test_volume_backup
    state: absent
'''

from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule


class VolumeBackupModule(OpenStackModule):
    module_min_sdk_version = '0.49.0'

    argument_spec = dict(
        display_name=dict(required=True, aliases=['name'], type='str'),
        display_description=dict(required=False, aliases=['description'],
                                 type='str'),
        volume=dict(required=False, type='str'),
        snapshot=dict(required=False, type='str'),
        state=dict(default='present', type='str', choices=['absent', 'present']),
        force=dict(default=False, type='bool'),
        metadata=dict(required=False, type='dict'),
        incremental=dict(required=False, default=False, type='bool')
    )
    module_kwargs = dict(
        required_if=[
            ('state', 'present', ['volume'])
        ],
        supports_check_mode=True
    )

    def _create_backup(self):
        if self.ansible.check_mode:
            self.exit_json(changed=True)

        name = self.params['display_name']
        description = self.params['display_description']
        volume = self.params['volume']
        snapshot = self.params['snapshot']
        force = self.params['force']
        is_incremental = self.params['incremental']
        metadata = self.params['metadata']

        changed = False

        cloud_volume = self.conn.block_storage.find_volume(volume)
        cloud_snapshot_id = None

        attrs = {
            'name': name,
            'volume_id': cloud_volume.id,
            'force': force,
            'is_incremental': is_incremental
        }

        if snapshot:
            cloud_snapshot_id = self.conn.block_storage.find_snapshot(
                snapshot, ignore_missing=False).id
            attrs['snapshot_id'] = cloud_snapshot_id

        if metadata:
            attrs['metadata'] = metadata

        if description:
            attrs['description'] = description

        backup = self.conn.block_storage.create_backup(**attrs)
        changed = True

        if self.params['wait']:
            try:
                backup = self.conn.block_storage.wait_for_status(
                    backup,
                    status='available',
                    wait=self.params['timeout'])
                self.exit_json(
                    changed=True, volume_backup=backup.to_dict(), id=backup.id
                )
            except self.sdk.exceptions.ResourceTimeout:
                self.fail_json(
                    msg='Timeout failure waiting for backup '
                        'to complete'
                )

        self.exit_json(
            changed=changed, volume_backup=backup.to_dict(), id=backup.id
        )

    def _delete_backup(self, backup):
        if self.ansible.check_mode:
            self.exit_json(changed=True)

        if backup:
            self.conn.block_storage.delete_backup(backup)
            if self.params['wait']:
                try:
                    self.conn.block_storage.wait_for_delete(
                        backup,
                        interval=2,
                        wait=self.params['timeout'])
                except self.sdk.exceptions.ResourceTimeout:
                    self.fail_json(
                        msg='Timeout failure waiting for backup '
                            'to be deleted'
                    )

        self.exit_json(changed=True)

    def run(self):
        name = self.params['display_name']

        backup = self.conn.block_storage.find_backup(name)

        if self.params['state'] == 'present':
            if not backup:
                self._create_backup()
            else:
                # For the moment we do not support backup update, since SDK
                # doesn't support it either => do nothing
                self.exit_json(changed=False)

        elif self.params['state'] == 'absent':
            self._delete_backup(backup)


def main():
    module = VolumeBackupModule()
    module()


if __name__ == '__main__':
    main()