summaryrefslogtreecommitdiffstats
path: root/ansible_collections/hpe/nimble/plugins/modules/hpe_nimble_snapshot.py
blob: aaf8915509d447567140b746a1997b937e87ac59 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/python

# Copyright 2020 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
# file except in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

# author Alok Ranjan (alok.ranjan2@hpe.com)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = r'''
---
author:
  - HPE Nimble Storage Ansible Team (@ar-india) <nimble-dcs-storage-automation-eng@hpe.com>
description: Manage the snapshots on an HPE Nimble Storage group.
module: hpe_nimble_snapshot
options:
  agent_type:
    required: False
    choices:
    - none
    - smis
    - vvol
    - openstack
    - openstackv2
    type: str
    description:
    - External management agent type.
  app_uuid:
    required: False
    type: str
    description:
    - Application identifier of snapshot.
  change_name:
    required: False
    type: str
    description:
    - Change name of the existing snapshot.
  description:
    required: False
    type: str
    description:
    - Text description of snapshot.
  expiry_after:
    required: False
    type: int
    description:
    - Number of seconds after which this snapshot is considered expired by snapshot TTL. A value of 0 indicates that snapshot never expires.
  force:
    required: False
    type: bool
    description:
    - Forcibly delete the specified snapshot even if it is the last replicated collection. Doing so could lead to full re-seeding at the next replication.
  metadata:
    required: False
    type: dict
    description:
    - Key-value pairs that augment a snapshot's attributes. List of key-value pairs. Keys must be unique and non-empty.
  name:
    required: True
    type: str
    description:
    - Name of the snapshot.
  online:
    required: False
    type: bool
    description:
    - Online state for a snapshot means it could be mounted for data restore.
  state:
    required: True
    choices:
    - present
    - absent
    - create
    type: str
    description:
    - The snapshot state.
  volume:
    required: True
    type: str
    description:
    - Parent volume name.
  writable:
    required: False
    type: bool
    description:
    - Allow snapshot to be writable. Mandatory and must be set to 'true' for VSS application synchronized snapshots.
extends_documentation_fragment: hpe.nimble.hpe_nimble
short_description: Manage the HPE Nimble Storage snapshots
version_added: "1.0.0"
notes:
  - This module does not support C(check_mode).
'''

EXAMPLES = r'''

# if state is create , then create a snapshot if not present. Fails if already present.
# if state is present, then create a snapshot if not present. Succeeds if it already exists.
- name: Create snapshot if not present
  hpe.nimble.hpe_nimble_snapshot:
    host: "{{ host }}"
    username: "{{ username }}"
    password: "{{ password }}"
    state: "{{ state | default('present') }}"
    volume: "{{ volume }}"
    name: "{{ name }}"
    online: "{{ online | default(true) }}"
    writable: "{{ writable | default(false) }}"

- name: Delete snapshot  (must be offline)
  hpe.nimble.hpe_nimble_snapshot:
    host: "{{ host }}"
    username: "{{ username }}"
    password: "{{ password }}"
    volume: "{{ volume }}"
    name: "{{ name }}"
    state: absent

'''
RETURN = r'''
'''

from ansible.module_utils.basic import AnsibleModule
try:
    from nimbleclient.v1 import client
except ImportError:
    client = None
from ansible_collections.hpe.nimble.plugins.module_utils.hpe_nimble import __version__ as NIMBLE_ANSIBLE_VERSION
import ansible_collections.hpe.nimble.plugins.module_utils.hpe_nimble as utils


def create_snapshot(
        client_obj,
        vol_name,
        snapshot_name,
        **kwargs):

    if utils.is_null_or_empty(snapshot_name):
        return (False, False, "Create snapshot failed as snapshot is not present.", {}, {})
    if utils.is_null_or_empty(vol_name):
        return (False, False, "Create snapshot failed as volume is not present.", {}, {})

    try:
        vol_resp = client_obj.volumes.get(id=None, name=vol_name)
        if utils.is_null_or_empty(vol_resp):
            return (False, False, f"Volume '{vol_name}' not present on array for taking snapshot.", {}, {})
        snap_resp = client_obj.snapshots.get(id=None, vol_name=vol_name, name=snapshot_name)
        if utils.is_null_or_empty(snap_resp):
            params = utils.remove_null_args(**kwargs)
            snap_resp = client_obj.snapshots.create(name=snapshot_name,
                                                    vol_id=vol_resp.attrs.get("id"),
                                                    **params)
            if snap_resp is not None:
                return (True, True, f"Snapshot '{snapshot_name}' created successfully.", {}, snap_resp.attrs)
        else:
            return (False, False, f"Snapshot '{snapshot_name}' cannot be created as it is already present in given state.", {}, {})
    except Exception as ex:
        return (False, False, f"Snapshot creation failed | {ex}", {}, {})


def update_snapshot(
        client_obj,
        snap_resp,
        **kwargs):

    if utils.is_null_or_empty(snap_resp):
        return (False, False, "Update snapshot failed as snapshot is not present.", {}, {})

    try:
        snapshot_name = snap_resp.attrs.get("name")
        changed_attrs_dict, params = utils.remove_unchanged_or_null_args(snap_resp, **kwargs)
        if changed_attrs_dict.__len__() > 0:
            snap_resp = client_obj.snapshots.update(id=snap_resp.attrs.get("id"), **params)
            return (True, True, f"Snapshot '{snapshot_name}' already present. Modified the following attributes '{changed_attrs_dict}'",
                    changed_attrs_dict, snap_resp.attrs)
        else:
            return (True, False, f"Snapshot '{snapshot_name}' already present in given state.", {}, snap_resp.attrs)

    except Exception as ex:
        return (False, False, f"Snapshot update failed | {ex}", {}, {})


def delete_snapshot(
        client_obj,
        vol_name,
        snapshot_name):

    if utils.is_null_or_empty(snapshot_name):
        return (False, False, "Delete snapshot failed as snapshot is not present.", {})
    if utils.is_null_or_empty(vol_name):
        return (False, False, "Delete snapshot failed. Volume is not present.", {})

    try:
        vol_resp = client_obj.volumes.get(id=None, name=vol_name)
        if utils.is_null_or_empty(vol_resp):
            return (False, False, f"Volume '{vol_name}' is not present on Array for deleting snapshot.", {})
        snap_resp = client_obj.snapshots.get(id=None, vol_name=vol_name, name=snapshot_name)
        if utils.is_null_or_empty(snap_resp):
            return (False, False, f"Snapshot '{snapshot_name}' cannot be deleted as it is not present in given volume '{vol_name}'.", {})
        else:
            client_obj.snapshots.delete(id=snap_resp.attrs.get("id"))
            return (True, True, f"Deleted snapshot '{snapshot_name}' successfully.", {})
    except Exception as ex:
        return (False, False, f"Snapshot deletion failed | {ex}", {})


def main():

    fields = {
        "state": {
            "required": True,
            "choices": ['present',
                        'absent',
                        'create'
                        ],
            "type": "str"
        },
        "change_name": {
            "required": False,
            "type": "str"
        },
        "name": {
            "required": True,
            "type": "str"
        },
        "description": {
            "required": False,
            "type": "str"
        },
        "volume": {
            "required": True,
            "type": "str"
        },
        "online": {
            "required": False,
            "type": "bool"
        },
        "writable": {
            "required": False,
            "type": "bool"
        },
        "app_uuid": {
            "required": False,
            "type": "str"
        },
        "metadata": {
            "required": False,
            "type": "dict"
        },
        "agent_type": {
            "required": False,
            "choices": ['none', 'smis', 'vvol', 'openstack', 'openstackv2'],
            "type": "str"
        },
        "expiry_after": {
            "required": False,
            "type": "int"
        },
        "force": {
            "required": False,
            "type": "bool"
        }
    }
    default_fields = utils.basic_auth_arg_fields()
    fields.update(default_fields)
    required_if = [('state', 'create', ['volume'])]

    module = AnsibleModule(argument_spec=fields, required_if=required_if)
    if client is None:
        module.fail_json(msg='Python nimble-sdk could not be found.')

    hostname = module.params["host"]
    username = module.params["username"]
    password = module.params["password"]
    state = module.params["state"]
    snapshot_name = module.params["name"]
    change_name = module.params["change_name"]
    description = module.params["description"]
    vol_name = module.params["volume"]
    online = module.params["online"]
    writable = module.params["writable"]
    app_uuid = module.params["app_uuid"]
    metadata = module.params["metadata"]
    agent_type = module.params["agent_type"]
    expiry_after = module.params["expiry_after"]
    force = module.params["force"]

    if (username is None or password is None or hostname is None or snapshot_name is None):
        module.fail_json(
            msg="Storage system IP or username or password is null or snapshot name is null.")

    # defaults
    return_status = changed = False
    msg = "No task to run."
    resp = None
    try:
        client_obj = client.NimOSClient(
            hostname,
            username,
            password,
            f"HPE Nimble Ansible Modules v{NIMBLE_ANSIBLE_VERSION}"
        )

        # States
        if state == "create" or state == "present":
            snap_resp = client_obj.snapshots.get(id=None, vol_name=vol_name, name=snapshot_name)
            if utils.is_null_or_empty(snap_resp) or state == "create":
                return_status, changed, msg, changed_attrs_dict, resp = create_snapshot(
                    client_obj,
                    vol_name,
                    snapshot_name,
                    description=description,
                    online=online,
                    writable=writable,
                    app_uuid=app_uuid,
                    metadata=metadata,
                    agent_type=agent_type)
            else:
                # update op
                return_status, changed, msg, changed_attrs_dict, resp = update_snapshot(
                    client_obj,
                    snap_resp,
                    name=change_name,
                    description=description,
                    online=online,
                    expiry_after=expiry_after,
                    app_uuid=app_uuid,
                    metadata=metadata,
                    force=force)

        elif state == "absent":
            return_status, changed, msg, changed_attrs_dict = delete_snapshot(
                client_obj,
                vol_name,
                snapshot_name)

    except Exception as ex:
        # failed for some reason.
        msg = str(ex)

    if return_status:
        if utils.is_null_or_empty(resp):
            module.exit_json(return_status=return_status, changed=changed, msg=msg)
        else:
            module.exit_json(return_status=return_status, changed=changed, msg=msg, attrs=resp)
    else:
        module.fail_json(return_status=return_status, changed=changed, msg=msg)


if __name__ == '__main__':
    main()