Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/module_utils/tools.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) 2020, NetApp Ansible Team <[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 from __future__ import absolute_import, division, print_function
30
31 __metaclass__ = type
32
33
34 def first_inside_second_dict_or_list(d1, d2):
35 """
36 This method is used for PUT operations, where a desired state (d1) is compared against the current state (d2).
37 Evaluates whether a update is needed or not.
38 None values in the desired state are skipped and ignored.
39 This method checks, whether d1 is a subset of d2, it does not prove equality.
40 """
41 if not isinstance(d1, type(d2)):
42 raise AssertionError("Inputs are not of same type. Got %s and %s." % (type(d1), type(d2)))
43 answer = True
44
45 if isinstance(d1, dict):
46 for key, value in d1.items():
47 # key only exists in d1
48 if value is None:
49 continue
50 if key not in d2.keys():
51 return False
52 else:
53 value2 = d2[key]
54 # keys exist in both, but values are different
55 if value != value2:
56 # both values can be compared
57 if isinstance(value, type(value2)):
58 if isinstance(value, (str, int, float, bool)):
59 return False
60 # recursion! dict or list inside here
61 elif isinstance(value, (dict, list)):
62 # takes care that a answer="false" is not overwritten with answer="true". Once answer is false, it should ever be.
63 # Do not return at this point, as we are in a loop and have to check all elements.
64 if answer:
65 answer = first_inside_second_dict_or_list(value, value2)
66 else:
67 return False
68 else:
69 raise Exception("Unknown type in dictionary: %s.") % (type(value))
70 # values are different
71 else:
72 return False
73
74 elif isinstance(d1, list):
75 if len(d1) == 0:
76 return answer
77 else:
78 if not all(isinstance(x, type(d1[0])) for x in d1):
79 raise AssertionError("all elements in a list must be of same type. Not the case for: %s." % (d1))
80 # case if list elements are "simple": strings, integers, bools, none
81 if isinstance(d1[0], (str, int, float, bool, type(None))):
82 if not set(d1).issubset(set(d2)):
83 return False
84 # case if list elements are "complex": recursion! dict or list inside here
85 elif isinstance(d1[0], dict):
86 for item in d1:
87 if any(first_inside_second_dict_or_list(item, item2) for item2 in d2):
88 continue
89 else:
90 return False
91 elif isinstance(d1[0], list):
92 raise Exception("Feature missing: nested lists are not supported yet.")
93 else:
94 raise Exception("Unsupported type inside dictionary or list: %s.") % (type(d1[0]))
95
96 else:
97 raise Exception("Unsupported input type %s.") % (type(d1))
98
99 return answer