Debian Codegraph @

ansible_collections/f5networks/f5_modules/plugins/module_utils/compare.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2017 F5 Networks Inc.
4 # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 from ansible.module_utils.six import iteritems
10
11
12 def cmp_simple_list(want, have):
13 if want is None:
14 return None
15 if have is None and want in ['', 'none']:
16 return None
17 if have is not None and want in ['', 'none']:
18 return []
19 if have is None:
20 return want
21 if set(want) != set(have):
22 return want
23 return None
24
25
26 def cmp_str_with_none(want, have):
27 if want is None:
28 return None
29 if have is None and want == '':
30 return None
31 if want != have:
32 return want
33
34
35 def compare_complex_list(want, have):
36 """Performs a complex list comparison
37
38 A complex list is a list of dictionaries
39
40 Args:
41 want (list): List of dictionaries to compare with second parameter.
42 have (list): List of dictionaries to compare with first parameter.
43
44 Returns:
45 bool:
46 """
47 if want == [] and have is None:
48 return None
49 if want is None:
50 return None
51 w = []
52 h = []
53 for x in want:
54 tmp = [(str(k), str(v)) for k, v in iteritems(x)]
55 w += tmp
56 for x in have:
57 tmp = [(str(k), str(v)) for k, v in iteritems(x)]
58 h += tmp
59 if set(w) == set(h):
60 return None
61 else:
62 return want
63
64
65 def compare_dictionary(want, have):
66 """Performs a dictionary comparison
67
68 Args:
69 want (dict): Dictionary to compare with second parameter.
70 have (dict): Dictionary to compare with first parameter.
71
72 Returns:
73 bool:
74 """
75 if want == {} and have is None:
76 return None
77 if want is None:
78 return None
79 w = [(str(k), str(v)) for k, v in iteritems(want)]
80 h = [(str(k), str(v)) for k, v in iteritems(have)]
81 if set(w) == set(h):
82 return None
83 else:
84 return want