18
18
from ansible .module_utils .basic import json
19
19
from ansible .module_utils .basic import env_fallback
20
20
from ansible .module_utils .six import PY3
21
- from ansible .module_utils .six .moves import filterfalse
22
21
from ansible .module_utils .six .moves .urllib .parse import urlencode
23
22
from ansible .module_utils ._text import to_native , to_text
24
23
from ansible .module_utils .connection import Connection
@@ -73,53 +72,27 @@ def cmp(a, b):
73
72
74
73
75
74
def issubset (subset , superset ):
76
- """Recurse through nested dictionary and compare entries """
75
+ """Recurse through a nested dictionary and check if it is a subset of another. """
77
76
78
- # Both objects are the same object
79
- if subset is superset :
80
- return True
81
-
82
- # Both objects are identical
83
- if subset == superset :
84
- return True
85
-
86
- # Both objects have a different type
87
- if isinstance (subset ) is not isinstance (superset ):
77
+ if type (subset ) is not type (superset ):
88
78
return False
89
79
80
+ if not isinstance (subset , dict ):
81
+ if isinstance (subset , list ):
82
+ return all (item in superset for item in subset )
83
+ return subset == superset
84
+
90
85
for key , value in subset .items ():
91
- # Ignore empty values
92
86
if value is None :
93
- return True
87
+ continue
94
88
95
- # Item from subset is missing from superset
96
89
if key not in superset :
97
90
return False
98
91
99
- # Item has different types in subset and superset
100
- if isinstance (superset .get (key )) is not isinstance (value ):
101
- return False
92
+ superset_value = superset .get (key )
102
93
103
- # Compare if item values are subset
104
- if isinstance (value , dict ):
105
- if not issubset (superset .get (key ), value ):
106
- return False
107
- elif isinstance (value , list ):
108
- try :
109
- # NOTE: Fails for lists of dicts
110
- if not set (value ) <= set (superset .get (key )):
111
- return False
112
- except TypeError :
113
- # Fall back to exact comparison for lists of dicts
114
- diff = list (filterfalse (lambda i : i in value , superset .get (key ))) + list (filterfalse (lambda j : j in superset .get (key ), value ))
115
- if diff :
116
- return False
117
- elif isinstance (value , set ):
118
- if not value <= superset .get (key ):
119
- return False
120
- else :
121
- if not value == superset .get (key ):
122
- return False
94
+ if not issubset (value , superset_value ):
95
+ return False
123
96
124
97
return True
125
98
@@ -210,6 +183,9 @@ def __init__(self, module):
210
183
211
184
# info output
212
185
self .previous = dict ()
186
+ self .before = []
187
+ self .commands = []
188
+ self .after = []
213
189
self .proposed = dict ()
214
190
self .sent = dict ()
215
191
self .stdout = None
@@ -433,6 +409,7 @@ def exit_json(self, **kwargs):
433
409
if self .params .get ("state" ) in ALLOWED_STATES_TO_APPEND_SENT_AND_PROPOSED :
434
410
if self .params .get ("output_level" ) in ("debug" , "info" ):
435
411
self .result ["previous" ] = self .previous
412
+ self .result ["before" ] = self .before
436
413
# FIXME: Modified header only works for PATCH
437
414
if not self .has_modified and self .previous != self .existing :
438
415
self .result ["changed" ] = True
@@ -450,8 +427,10 @@ def exit_json(self, **kwargs):
450
427
if self .params .get ("state" ) in ALLOWED_STATES_TO_APPEND_SENT_AND_PROPOSED :
451
428
self .result ["sent" ] = self .sent
452
429
self .result ["proposed" ] = self .proposed
430
+ self .result ["commands" ] = self .commands
453
431
454
432
self .result ["current" ] = self .existing
433
+ self .result ["after" ] = self .after
455
434
456
435
if self .module ._diff and self .result .get ("changed" ) is True :
457
436
self .result ["diff" ] = dict (
@@ -468,6 +447,7 @@ def fail_json(self, msg, **kwargs):
468
447
if self .params .get ("state" ) in ALLOWED_STATES_TO_APPEND_SENT_AND_PROPOSED :
469
448
if self .params .get ("output_level" ) in ("debug" , "info" ):
470
449
self .result ["previous" ] = self .previous
450
+ self .result ["before" ] = self .before
471
451
# FIXME: Modified header only works for PATCH
472
452
if not self .has_modified and self .previous != self .existing :
473
453
self .result ["changed" ] = True
@@ -486,8 +466,10 @@ def fail_json(self, msg, **kwargs):
486
466
if self .params .get ("state" ) in ALLOWED_STATES_TO_APPEND_SENT_AND_PROPOSED :
487
467
self .result ["sent" ] = self .sent
488
468
self .result ["proposed" ] = self .proposed
469
+ self .result ["commands" ] = self .commands
489
470
490
471
self .result ["current" ] = self .existing
472
+ self .result ["after" ] = self .after
491
473
492
474
self .result .update (** kwargs )
493
475
self .module .fail_json (msg = msg , ** self .result )
@@ -499,23 +481,30 @@ def check_changed(self):
499
481
existing ["password" ] = self .sent .get ("password" )
500
482
return not issubset (self .sent , existing )
501
483
502
- def get_diff (self , unwanted = None ):
484
+ def get_diff (self , unwanted = None , previous = None , payload = None ):
503
485
"""Check if existing payload and sent payload and removing keys that are not required"""
504
486
if unwanted is None :
505
487
unwanted = []
506
- if not self .existing and self .sent :
507
- return True
488
+
489
+ if previous is None and payload is None :
490
+ if not self .existing and self .sent :
491
+ return True
508
492
509
493
existing = self .existing
510
494
sent = self .sent
511
495
496
+ if previous and payload :
497
+ existing = previous
498
+ sent = payload
499
+
512
500
for key in unwanted :
513
501
if isinstance (key , str ):
514
502
if key in existing :
515
503
try :
516
504
del existing [key ]
517
505
except KeyError :
518
506
pass
507
+ if key in sent :
519
508
try :
520
509
del sent [key ]
521
510
except KeyError :
@@ -524,12 +513,14 @@ def get_diff(self, unwanted=None):
524
513
key_path , last = key [:- 1 ], key [- 1 ]
525
514
try :
526
515
existing_parent = reduce (dict .get , key_path , existing )
527
- del existing_parent [last ]
516
+ if existing_parent is not None :
517
+ del existing_parent [last ]
528
518
except KeyError :
529
519
pass
530
520
try :
531
521
sent_parent = reduce (dict .get , key_path , sent )
532
- del sent_parent [last ]
522
+ if sent_parent is not None :
523
+ del sent_parent [last ]
533
524
except KeyError :
534
525
pass
535
526
return not issubset (sent , existing )
@@ -567,3 +558,8 @@ def get_object_by_nested_key_value(self, path, nested_key_path, value, data_key=
567
558
return obj
568
559
569
560
return None
561
+
562
+ def delete (self , check_mode , path ):
563
+ if not check_mode :
564
+ self .request (path , method = "DELETE" )
565
+ return {"path" : path , "method" : "DELETE" }
0 commit comments