1919from ansible_collections .check_point .mgmt .plugins .module_utils .checkpoint import (
2020 CheckPointRequest ,
2121 map_params_to_obj ,
22+ map_obj_to_params ,
2223 sync_show_params_with_add_params ,
2324 remove_unwanted_key ,
2425 contains_show_identifier_param ,
@@ -72,42 +73,79 @@ def search_for_existing_rules(
7273
7374 def search_for_resource_name (self , conn_request , payload ):
7475 search_result = []
76+ round_trip = False
7577 search_payload = utils .remove_empties (payload )
78+ if search_payload .get ("round_trip" ):
79+ round_trip = True
80+ if search_payload .get ("round_trip" ) is not None :
81+ del search_payload ["round_trip" ]
82+
83+ search_payload = map_params_to_obj (search_payload , self .key_transform )
7684 if not contains_show_identifier_param (search_payload ):
7785 search_result = self .search_for_existing_rules (
7886 conn_request ,
7987 self .api_call_object_plural_version ,
8088 search_payload ,
8189 "gathered" ,
8290 )
91+ if search_result .get ("code" ) == 200 :
92+ search_result = search_result ["response" ][
93+ self .api_call_object_plural_version
94+ ]
95+ return search_result
8396 else :
8497 search_result = self .search_for_existing_rules (
8598 conn_request , self .api_call_object , search_payload , "gathered"
8699 )
87- search_result = sync_show_params_with_add_params (
88- search_result ["response" ], self .key_transform
89- )
90- if (
91- search_result .get ("code" )
92- and "object_not_found" in search_result .get ("code" )
93- and "not found" in search_result .get ("message" )
94- ):
95- search_result = {}
100+ if round_trip :
101+ search_result = sync_show_params_with_add_params (
102+ search_result ["response" ], self .key_transform
103+ )
104+ elif search_result .get ("code" ) and search_result ["code" ] == 200 :
105+ search_result = search_result ["response" ]
106+ search_result = map_obj_to_params (
107+ search_result ,
108+ self .key_transform ,
109+ self .module_return ,
110+ )
111+ if search_result .get ("code" ) and search_result ["code" ] != 200 :
112+ if (
113+ search_result .get ("response" )
114+ and "object_not_found" in search_result ["response" ]["code" ]
115+ and "not found" in search_result ["response" ]["message" ]
116+ ):
117+ search_result = {}
118+ elif "object_not_found" in search_result .get (
119+ "code"
120+ ) and "not found" in search_result .get ("message" ):
121+ search_result = {}
96122 return search_result
97123
98124 def delete_module_api_config (self , conn_request , module_config_params ):
99125 config = {}
100126 before = {}
101127 after = {}
102- changed = False
103128 result = {}
129+ changed = False
130+ round_trip = False
131+ ckp_session_uid = None
104132 payload = utils .remove_empties (module_config_params )
133+ if payload .get ("round_trip" ):
134+ round_trip = True
135+ del payload ["round_trip" ]
105136 remove_from_response = ["uid" , "read-only" , "domain" ]
106- search_result = self .search_for_resource_name (conn_request , payload )
137+ if round_trip :
138+ search_payload = {"name" : payload ["name" ], "round_trip" : True }
139+ else :
140+ search_payload = {"name" : payload ["name" ]}
141+ search_result = self .search_for_resource_name (
142+ conn_request , search_payload
143+ )
107144 if search_result :
108- search_result = remove_unwanted_key (
109- search_result , remove_from_response
110- )
145+ if round_trip :
146+ search_result = remove_unwanted_key (
147+ search_result , remove_from_response
148+ )
111149 before = search_result
112150 result = conn_request .post (
113151 self .api_call_object , self ._task .args ["state" ], data = payload
@@ -118,29 +156,39 @@ def delete_module_api_config(self, conn_request, module_config_params):
118156 config .update ({"before" : before })
119157 if result .get ("changed" ):
120158 changed = True
121- return config , changed
159+ ckp_session_uid = result ["checkpoint_session_uid" ]
160+ return config , changed , ckp_session_uid
122161
123162 def configure_module_api (self , conn_request , module_config_params ):
124163 config = {}
125164 before = {}
126165 after = {}
127- changed = False
128166 result = {}
167+ changed = False
168+ round_trip = False
169+ ckp_session_uid = None
129170 # Add to the THIS list for the value which needs to be excluded
130171 # from HAVE params when compared to WANT param like 'ID' can be
131172 # part of HAVE param but may not be part of your WANT param
132173 remove_from_response = ["uid" , "read-only" , "domain" ]
133174 remove_from_set = ["add-default-rule" ]
134175 payload = utils .remove_empties (module_config_params )
176+ if payload .get ("round_trip" ):
177+ round_trip = True
178+ del payload ["round_trip" ]
135179 if payload .get ("name" ):
136- search_payload = {"name" : payload ["name" ]}
180+ if round_trip :
181+ search_payload = {"name" : payload ["name" ], "round_trip" : True }
182+ else :
183+ search_payload = {"name" : payload ["name" ]}
137184 search_result = self .search_for_resource_name (
138185 conn_request , search_payload
139186 )
140187 if search_result :
141- search_result = remove_unwanted_key (
142- search_result , remove_from_response
143- )
188+ if round_trip :
189+ search_result = remove_unwanted_key (
190+ search_result , remove_from_response
191+ )
144192 before = search_result
145193 payload = map_params_to_obj (payload , self .key_transform )
146194 delete_params = {
@@ -154,22 +202,32 @@ def configure_module_api(self, conn_request, module_config_params):
154202 delete_params = delete_params ,
155203 )
156204 if result .get ("changed" ):
157- search_result = sync_show_params_with_add_params (
158- result ["response" ], self .key_transform
159- )
160- search_result = remove_unwanted_key (
161- search_result , remove_from_response
162- )
205+ if round_trip :
206+ search_result = sync_show_params_with_add_params (
207+ result ["response" ], self .key_transform
208+ )
209+ else :
210+ search_result = map_obj_to_params (
211+ result ["response" ],
212+ self .key_transform ,
213+ self .module_return ,
214+ )
215+ if round_trip :
216+ search_result = remove_unwanted_key (
217+ search_result , remove_from_response
218+ )
163219 after = search_result
220+ ckp_session_uid = result ["checkpoint_session_uid" ]
164221 changed = True
165222 config .update ({"before" : before , "after" : after })
166223
167- return config , changed
224+ return config , changed , ckp_session_uid
168225
169226 def run (self , tmp = None , task_vars = None ):
170227 self ._supports_check_mode = True
171228 self ._result = super (ActionModule , self ).run (tmp , task_vars )
172229 self ._check_argspec ()
230+ self ._result ["checkpoint_session_uid" ] = None
173231 if self ._result .get ("failed" ):
174232 return self ._result
175233 conn = Connection (self ._connection .socket_path )
@@ -191,6 +249,7 @@ def run(self, tmp=None, task_vars=None):
191249 (
192250 self ._result [self .module_return ],
193251 self ._result ["changed" ],
252+ self ._result ["checkpoint_session_uid" ],
194253 ) = self .configure_module_api (
195254 conn_request , self ._task .args ["config" ]
196255 )
@@ -199,8 +258,11 @@ def run(self, tmp=None, task_vars=None):
199258 (
200259 self ._result [self .module_return ],
201260 self ._result ["changed" ],
261+ self ._result ["checkpoint_session_uid" ],
202262 ) = self .delete_module_api_config (
203263 conn_request , self ._task .args ["config" ]
204264 )
265+ if self ._result .get ("checkpoint_session_uid" ) is None :
266+ del self ._result ["checkpoint_session_uid" ]
205267
206268 return self ._result
0 commit comments