Debian Codegraph @

ansible_collections/netapp/ontap/plugins/module_utils/rest_generic.py

raw
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) 2021, Laurent Nicolas <[email protected]>
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
34 from __future__ import (absolute_import, division, print_function)
35 __metaclass__ = type
36
37 import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh
38
39
40 def build_query_with_fields(query, fields):
41 ''' for GET requests'''
42 if fields is not None and query is None:
43 query = {}
44 if fields is not None:
45 query['fields'] = fields
46 return query
47
48
49 def build_query_with_timeout(query, timeout):
50 ''' for POST, PATCH, DELETE requests'''
51 params = {} if query else None
52 if timeout > 0:
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 params = dict(return_timeout=timeout)
58 if query is not None:
59 params.update(query)
60 return params
61
62
63 def get_one_record(rest_api, api, query=None, fields=None):
64 query = build_query_with_fields(query, fields)
65 response, error = rest_api.get(api, query)
66 record, error = rrh.check_for_0_or_1_records(api, response, error, query)
67 return record, error
68
69
70 def get_0_or_more_records(rest_api, api, query=None, fields=None):
71 query = build_query_with_fields(query, fields)
72 response, error = rest_api.get(api, query)
73 records, error = rrh.check_for_0_or_more_records(api, response, error)
74 return records, error
75
76
77 def post_async(rest_api, api, body, query=None, timeout=30, job_timeout=30, headers=None, raw_error=False, files=None):
78 # see delete_async for async and sync operations and status codes
79 response, error = rest_api.post(api, body=body, params=build_query_with_timeout(query, timeout), headers=headers, files=files)
80 # limit the polling interval to something between 5 seconds and 60 seconds
81 increment = min(max(job_timeout / 6, 5), 60)
82 response, error = rrh.check_for_error_and_job_results(api, response, error, rest_api, increment=increment, timeout=job_timeout, raw_error=raw_error)
83 return response, error
84
85
86 def patch_async(rest_api, api, uuid_or_name, body, query=None, timeout=30, job_timeout=30, headers=None, raw_error=False, files=None):
87 # cluster does not use uuid or name, and query based PATCH does not use UUID (for restit)
88 api = '%s/%s' % (api, uuid_or_name) if uuid_or_name is not None else api
89 response, error = rest_api.patch(api, body=body, params=build_query_with_timeout(query, timeout), headers=headers, files=files)
90 increment = min(max(job_timeout / 6, 5), 60)
91 response, error = rrh.check_for_error_and_job_results(api, response, error, rest_api, increment=increment, timeout=job_timeout, raw_error=raw_error)
92 return response, error
93
94
95 def delete_async(rest_api, api, uuid, query=None, body=None, timeout=30, job_timeout=30, headers=None, raw_error=False):
96 # query based DELETE does not use UUID (for restit)
97 api = '%s/%s' % (api, uuid) if uuid is not None else api
98 response, error = rest_api.delete(api, body=body, params=build_query_with_timeout(query, timeout), headers=headers)
99 increment = min(max(job_timeout / 6, 5), 60)
100 response, error = rrh.check_for_error_and_job_results(api, response, error, rest_api, increment=increment, timeout=job_timeout, raw_error=raw_error)
101 return response, error