@@ -28,11 +28,11 @@ def get(isamAppliance, server, resourceUri, check_mode=False, force=False):
2828 check_mode = check_mode ,
2929 force = force ,
3030 )
31- resource_id = ret_obj [ "data" ]
31+ resource_id = ret_obj . get ( "data" , {})
3232
3333 if resource_id == {}:
3434 logger .info (
35- f"Resource { server } / { resourceUri } had no match, skipping retrieval."
35+ f"Resource { server } { resourceUri } had no match, skipping retrieval."
3636 )
3737 return isamAppliance .create_return_object ()
3838 else :
@@ -57,7 +57,7 @@ def get_attachments(isamAppliance, server, resourceUri, check_mode=False, force=
5757
5858 if resource_id == {}:
5959 logger .info (
60- f"Resource { server } / { resourceUri } had no match, skipping retrieval."
60+ f"Resource { server } { resourceUri } had no match, skipping retrieval."
6161 )
6262 return isamAppliance .create_return_object ()
6363 else :
@@ -77,10 +77,11 @@ def search(isamAppliance, server, resourceUri, force=False, check_mode=False):
7777 for obj in ret_obj ["data" ]:
7878 if obj ["resourceUri" ] == resourceUri and obj ["server" ] == server :
7979 logger .info (
80- f"Found server/resourceUri { server } / { resourceUri } id: { obj ['id' ]} "
80+ f"Found server/resourceUri { server } { resourceUri } id: { obj ['id' ]} "
8181 )
8282 return_obj ["data" ] = obj ["id" ]
8383 return_obj ["rc" ] = 0
84+ return return_obj
8485
8586 return return_obj
8687
@@ -144,38 +145,55 @@ def config(
144145 {'name': '<definition name>', 'type': 'definition'}]
145146 """
146147 warnings = []
147- if force is False :
148- ret_obj = search (isamAppliance , server , resourceUri )
148+ ret_obj = search (isamAppliance , server , resourceUri )
149149
150- if force is True or ret_obj ["data" ] == {}:
151- json_data = {"server" : server , "resourceUri" : resourceUri }
152- if policyType is not None :
153- if tools .version_compare (isamAppliance .facts ["version" ], "9.0.6.0" ) < 0 :
154- warnings .append (
155- f"Appliance at version: { isamAppliance .facts ['version' ]} , policyType: { cache } is not supported. Needs 9.0.6.0 or higher. Ignoring policyType for this call."
156- )
157- else :
158- json_data ["type" ] = policyType
150+ json_data = {"server" : server , "resourceUri" : resourceUri }
151+ if policyType is not None :
152+ if tools .version_compare (isamAppliance .facts ["version" ], "9.0.6.0" ) < 0 :
153+ warnings .append (
154+ f"Appliance at version: { isamAppliance .facts ['version' ]} , policyType: { policyType } is not supported. Needs 9.0.6.0 or higher. Ignoring policyType for this call."
155+ )
156+ else :
157+ json_data ["type" ] = policyType
158+
159+ json_data ["policies" ] = _convert_policy_name_to_id (isamAppliance , policies )
160+ if policyCombiningAlgorithm is not None :
161+ json_data ["policyCombiningAlgorithm" ] = policyCombiningAlgorithm
162+ if cache is not None :
163+ if tools .version_compare (isamAppliance .facts ["version" ], "9.0.3.0" ) < 0 :
164+ warnings .append (
165+ f"Appliance at version: { isamAppliance .facts ['version' ]} , cache: { cache } is not supported. Needs 9.0.3.0 or higher. Ignoring cache for this call."
166+ )
167+ else :
168+ json_data ["cache" ] = int (cache )
159169
160- json_data ["policies" ] = _convert_policy_name_to_id (isamAppliance , policies )
161- if policyCombiningAlgorithm is not None :
162- json_data ["policyCombiningAlgorithm" ] = policyCombiningAlgorithm
163- if cache is not None :
164- if tools .version_compare (isamAppliance .facts ["version" ], "9.0.3.0" ) < 0 :
165- warnings .append (
166- f"Appliance at version: { isamAppliance .facts ['version' ]} , cache: { cache } is not supported. Needs 9.0.3.0 or higher. Ignoring cache for this call."
167- )
168- else :
169- json_data ["cache" ] = int (cache )
170- if check_mode is True :
170+ if force or ret_obj .get ("data" , {}) == {}:
171+ if check_mode :
171172 return isamAppliance .create_return_object (changed = True , warnings = warnings )
172173 else :
173174 return isamAppliance .invoke_post (
174175 "Configure a resource" , uri , json_data , warnings = warnings
175176 )
176-
177- return isamAppliance .create_return_object ()
178-
177+ else :
178+ # Idempotency check
179+ logger .debug (f"Idempotency check for { server } { resourceUri } " )
180+ curConfig = get (isamAppliance , server , resourceUri )
181+ curConfig = curConfig .get ("data" , {})
182+ # Need to get rid of lastmodified and name in each policy
183+ _policies = list (map (lambda item : item .pop ('lastmodified' , None ), curConfig .get ("policies" , [])))
184+ _policies = list (map (lambda item : item .pop ('name' , None ), curConfig .get ("policies" , [])))
185+ if tools .json_equals (curConfig , json_data ):
186+ # No updates needed
187+ return isamAppliance .create_return_object ()
188+ else :
189+ if check_mode :
190+ return isamAppliance .create_return_object (changed = True , warnings = warnings )
191+ else :
192+ # delete and add again
193+ delete (isamAppliance , server , resourceUri )
194+ return isamAppliance .invoke_post (
195+ "Reconfigure a resource" , uri , json_data , warnings = warnings
196+ )
179197
180198def update (
181199 isamAppliance ,
@@ -306,17 +324,22 @@ def publish(isamAppliance, server, resourceUri, check_mode=False, force=False):
306324 """
307325 ret_obj = get (isamAppliance , server , resourceUri )
308326
309- if force or (
310- ret_obj ["data" ] != {} and (
311- ret_obj ["data" ]["deployrequired" ] or not ret_obj ["data" ]["deployed" ]
312- ) and ret_obj ["data" ]["policies" ] != []
313- ):
327+ deploy_required = False
328+ if ret_obj .get ("data" , {}).get ("deployrequired" , False ) or not ret_obj .get ("data" , {}).get ("deployed" , False ):
329+ deploy_required = True
330+
331+ logger .debug (f"\n \n Deploy required: { deploy_required } \n \n " )
332+
333+ resource_id = ret_obj .get ('data' , {}).get ('id' , None )
334+ logger .debug (f"\n \n ID: { resource_id } \n \n " )
335+ # if (force or deploy_required) and ret_obj.get("data", {}).get("policies", []) != []:
336+ if (force or deploy_required ) and resource_id is not None :
314337 if check_mode :
315338 return isamAppliance .create_return_object (changed = True )
316339 else :
317340 return isamAppliance .invoke_put (
318341 "Publish the policy attachments for a resource" ,
319- f"{ uri } /deployment/{ ret_obj [ 'data' ][ 'id' ] } " ,
342+ f"{ uri } /deployment/{ resource_id } " ,
320343 {},
321344 )
322345
@@ -333,7 +356,7 @@ def publish_list(isamAppliance, attachments, check_mode=False, force=False):
333356 id_list = []
334357 for attach in attachments :
335358 ret_obj = get (isamAppliance , attach ["server" ], attach ["resourceUri" ])
336- if force is True or ret_obj ["data" ]["deployrequired" ] is True :
359+ if force or ret_obj ["data" ]["deployrequired" ]:
337360 id_list .append (ret_obj ["data" ]["id" ])
338361 logger .debug (f"Attachments: { id_list } " )
339362
0 commit comments