1# This code is part of Ansible, but is an independent component.
2# This particular file snippet, and this file snippet only, is BSD licensed.
3# Modules you write using this snippet, which is embedded dynamically by Ansible
4# still belong to the author of the module, and may assign their own license
5# to the complete work.
6#
7# Copyright (c) 2020, Laurent Nicolas <laurentn@netapp.com>
8# All rights reserved.
9#
10# Redistribution and use in source and binary forms, with or without modification,
11# are permitted provided that the following conditions are met:
12#
13#    * Redistributions of source code must retain the above copyright
14#      notice, this list of conditions and the following disclaimer.
15#    * Redistributions in binary form must reproduce the above copyright notice,
16#      this list of conditions and the following disclaimer in the documentation
17#      and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29""" Support functions for NetApp ansible modules
30
31    Provides common processing for responses and errors from REST calls
32"""
33
34from __future__ import (absolute_import, division, print_function)
35__metaclass__ = type
36
37import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh
38
39
40def get_flexcache(rest_api, vserver, name, fields=None):
41    api = 'storage/flexcache/flexcaches'
42    query = dict(name=name)
43    query['svm.name'] = vserver
44    if fields is not None:
45        query['fields'] = fields
46    response, error = rest_api.get(api, query)
47    flexcache, error = rrh.check_for_0_or_1_records(api, response, error, query)
48    return flexcache, error
49
50
51def delete_flexcache(rest_api, uuid, timeout=180, return_timeout=5):
52    api = 'storage/flexcache/flexcaches/%s' % uuid
53    # without return_timeout, REST returns immediately with a 202 and a job link
54    #   but the job status is 'running'
55    # with return_timeout, REST returns quickly with a 200 and a job link
56    #   and the job status is 'success'
57    # I guess that if the operation was taking more than 30 seconds, we'd get a 202 with 'running'.
58    # I tried with a value of 1 second, I got a 202, but the job showed as complete (success) :)
59
60    # There may be a bug in ONTAP.  If return_timeout is >= 15, the call fails with uuid not found!
61    # With 5, a job is queued, and completes with success.  With a big enough value, no job is
62    # queued, and the API returns in around 15 seconds with a not found error.
63    query = dict(return_timeout=return_timeout)
64    response, error = rest_api.delete(api, params=query)
65    response, error = rrh.check_for_error_and_job_results(api, response, error, rest_api, increment=10, timeout=timeout)
66    return response, error
67
68
69def post_flexcache(rest_api, body, query=None, timeout=180):
70    api = 'storage/flexcache/flexcaches'
71    # see delete_flexcache for async and sync operations and status codes
72    params = None
73    if timeout > 0:
74        params = dict(return_timeout=min(30, timeout))
75    if query is not None:
76        params.update(query)
77    response, error = rest_api.post(api, body=body, params=params)
78    response, error = rrh.check_for_error_and_job_results(api, response, error, rest_api, increment=20, timeout=timeout)
79    return response, error
80
81
82def patch_flexcache(rest_api, uuid, body, query=None, timeout=180):
83    api = 'storage/flexcache/flexcaches/%s' % uuid
84    # see delete_flexcache for async and sync operations and status codes
85    params = dict(return_timeout=30)
86    if query is not None:
87        params.update(query)
88    response, error = rest_api.patch(api, body=body, params=params)
89    response, error = rrh.check_for_error_and_job_results(api, response, error, rest_api, increment=20, timeout=timeout)
90    return response, error
91