@@ -555,6 +555,20 @@ def spec_handling(self, spec, module):
555
555
"""
556
556
pass
557
557
558
+ def object_handling (self , obj , module ):
559
+ """Override to provide custom functionality for newly created/replaced objects.
560
+
561
+ This method is run for newly created objects with merged state or
562
+ created/replaced objects with present state.
563
+
564
+ By default it will handle default values for objects.
565
+ It's advised to call `super().object_handling(obj, module)` if overriden
566
+ in the modules.
567
+ """
568
+ for key , obj_value in obj .about ().items ():
569
+ if obj_value is None :
570
+ setattr (obj , key , self ._get_default_value (obj , key ))
571
+
558
572
def pre_state_handling (self , obj , result , module ):
559
573
"""Override to provide custom pre-state handling functionality."""
560
574
pass
@@ -694,6 +708,8 @@ def apply_state(
694
708
continue
695
709
other_children .append (x )
696
710
item .remove (x )
711
+ # object_handling need to be before equal comparison for evaluating defaults
712
+ self .object_handling (obj , module )
697
713
if not item .equal (obj , compare_children = True ):
698
714
result ["changed" ] = True
699
715
obj .extend (other_children )
@@ -703,10 +719,6 @@ def apply_state(
703
719
# NOTE checking defaults for with_update_in_apply_state doesnot have
704
720
# a use for now as template, stack and device group dont have
705
721
# defaults in the SDK
706
- # it also breaks panos_template as SDK has `mode` attribute set
707
- # to "normal" by default, but there is no xpath for this.
708
- # if obj_value is None:
709
- # setattr(obj, key, self._get_default_value(obj, key))
710
722
if getattr (item , key ) != getattr (obj , key ):
711
723
try :
712
724
obj .update (key )
@@ -717,9 +729,6 @@ def apply_state(
717
729
result ["after" ] = self .describe (obj )
718
730
result ["diff" ]["after" ] = eltostr (obj )
719
731
else :
720
- for key , obj_value in obj .about ().items ():
721
- if obj_value is None :
722
- setattr (obj , key , self ._get_default_value (obj , key ))
723
732
result ["after" ] = self .describe (obj )
724
733
result ["diff" ]["after" ] = eltostr (obj )
725
734
try :
@@ -728,9 +737,7 @@ def apply_state(
728
737
module .fail_json (msg = "Failed apply: {0}" .format (e ))
729
738
break
730
739
else :
731
- for key , obj_value in obj .about ().items ():
732
- if obj_value is None :
733
- setattr (obj , key , self ._get_default_value (obj , key ))
740
+ self .object_handling (obj , module )
734
741
result ["changed" ] = True
735
742
result ["before" ] = None
736
743
result ["after" ] = self .describe (obj )
@@ -889,9 +896,7 @@ def apply_state(
889
896
)
890
897
break
891
898
else : # create new record with merge
892
- for key , obj_value in obj .about ().items ():
893
- if obj_value is None :
894
- setattr (obj , key , self ._get_default_value (obj , key ))
899
+ self .object_handling (obj , module )
895
900
result ["before" ] = None
896
901
result ["after" ] = self .describe (obj )
897
902
result ["diff" ] = {
0 commit comments