Skip to content

Commit c9b8d13

Browse files
Optimised logic to use existing immutable list
Signed-off-by: Shiva Shankar Vaddepally <shivashankar.vaddepally@cloud.com>
1 parent 1674f69 commit c9b8d13

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

plugins/module_utils/module_executor.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,7 @@ def is_resource_identical(self):
361361
if not self.module_result["diff_list"]:
362362
del self.module_result["diff_list"]
363363
if immutable_resource_module_params != []:
364-
msg = (
365-
"Cannot change value for the following non-updateable attributes %s"
366-
% immutable_resource_module_params
367-
)
368-
return (False, msg)
364+
return (False, immutable_resource_module_params)
369365

370366
return (False, None) if diff_list else (True, None)
371367

@@ -430,8 +426,12 @@ def create_or_update(self):
430426
self.resource_module_params["sitename"] = sitename
431427

432428
else:
433-
# Update only if resource is not identical (idempotent)
434-
is_identical, msg = self.is_resource_identical()
429+
# Update process will go through 2 iterations of is_resource_identical
430+
# 1. First iteration will check if the resource is identical. If not, it will give the list of non-updatable attributes that exists
431+
# in the user playbook. If non-updatable attributes are present, it will remove them from the module_params and update the resource
432+
# 2. Second iteration will check if the resource is identical. If not, it will update the resource after ignoring the
433+
# non-updatable resources
434+
is_identical, immutable_keys_list = self.is_resource_identical()
435435
if is_identical:
436436
log(
437437
"INFO: Resource `%s:%s` exists and is identical. No change required."
@@ -453,7 +453,7 @@ def create_or_update(self):
453453
self.client, self.resource_name, self.resource_module_params
454454
)
455455

456-
elif msg is None:
456+
elif immutable_keys_list is None:
457457
self.module_result["changed"] = True
458458
log(
459459
"INFO: Resource %s:%s exists and is different. Will be UPDATED."
@@ -465,28 +465,28 @@ def create_or_update(self):
465465
if not ok:
466466
self.return_failure(err)
467467
else:
468-
immutable_keys = [
469-
key for key in self.resource_module_params.keys()
470-
if key in NITRO_RESOURCE_MAP[self.resource_name]["immutable_keys"]
471-
]
472-
for key in immutable_keys:
468+
for key in immutable_keys_list:
473469
self.resource_module_params.pop(key)
474470

475-
is_identical, msg1 = self.is_resource_identical()
471+
is_identical, _ = self.is_resource_identical()
476472

477-
if is_identical and msg1 is None:
478-
self.module.warn(f"DEBUG: Resource not updated because - {msg}")
473+
if is_identical:
474+
msg = (
475+
f"Resource {self.resource_name}/{self.resource_id} not updated because user is trying to "
476+
f"update following non-updatable keys: {immutable_keys_list}"
477+
)
478+
self.module.warn(msg)
479+
log(msg)
479480
self.module_result["changed"] = False
480481
self.module.exit_json(**self.module_result)
481482
else:
482483
self.module_result["changed"] = True
483-
log(
484-
"INFO: Resource %s:%s exists. Will be UPDATED after omitting the immutable keys."
485-
% (
486-
self.resource_name,
487-
self.resource_id,
488-
)
484+
msg = (
485+
f"Resource {self.resource_name}/{self.resource_id} is updated after ignoring following "
486+
f"non-updatable keys: {immutable_keys_list}"
489487
)
488+
self.module.warn(msg)
489+
log(msg)
490490
ok, err = update_resource(
491491
self.client, self.resource_name, self.resource_module_params
492492
)

0 commit comments

Comments
 (0)