Skip to content

Commit

Permalink
Modified api.py
Browse files Browse the repository at this point in the history
- Add method to clean empty keys from proposed_object
- FIX VLAN Name update.
- Fix unsetting of vlan optional fields.
  • Loading branch information
JkhatriInfobox committed Jan 27, 2025
1 parent 5e37916 commit 5605c9c
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,30 @@ def handle_exception(self, method_name, exc):
else:
self.module.fail_json(msg=to_native(exc))

def clean_empty_keys(self, current_object, proposed_object):
"""
Removes keys from the proposed_object that are empty and do not exist in current_object.
:param current_object: The current object to compare with.
:param proposed_object: The proposed object to clean up.
:return: Cleaned a proposed_object.
"""
keys_to_remove = []

for key, proposed_item in iteritems(proposed_object):
# Check if the key is empty (None, empty string, empty list, etc.)
if proposed_item in [None, '', [], {}, set()]: # Add more empty checks if needed
# If the key doesn't exist in current_object, mark it for removal
if key not in current_object:
keys_to_remove.append(key)

# Remove the identified keys from proposed_object
for key in keys_to_remove:
del proposed_object[key]

return proposed_object

def run(self, ib_obj_type, ib_spec):
''' Runs the module and performance configuration tasks
''' Runs the module and perform configuration tasks
:args ib_obj_type: the WAPI object type to operate against
:args ib_spec: the specification for the WAPI object as a dict
:returns: result dict
Expand All @@ -328,7 +350,6 @@ def run(self, ib_obj_type, ib_spec):
result = {'changed': False}

obj_filter = dict([(k, self.module.params[k]) for k, v in iteritems(ib_spec) if v.get('ib_req')])

# get object reference
ib_obj_ref, update, new_name = self.get_object_ref(self.module, ib_obj_type, obj_filter, ib_spec)

Expand Down Expand Up @@ -418,7 +439,7 @@ def run(self, ib_obj_type, ib_spec):
if 'default_value' in obj:
obj['default_value'] = str(obj['default_value'])

if (ib_obj_type == NIOS_VLAN):
if ib_obj_type == NIOS_VLAN and ib_obj_ref:
if 'parent' in current_object:
current_object['parent'] = current_object['parent']['_ref']

Expand Down Expand Up @@ -448,7 +469,7 @@ def run(self, ib_obj_type, ib_spec):
if 'ipv4addrs' in proposed_object and sum(addr.get('use_for_ea_inheritance', False) for addr in proposed_object['ipv4addrs']) > 1:
raise AnsibleError('Only one address allowed to be used for extensible attributes inheritance')
# this check is for idempotency, as if the same ip address shall be passed
# add param will be removed, and same exists true for remove case as well.
# add param will be removed, and the same exists true for the remove case as well.
if 'ipv4addrs' in [current_object and proposed_object]:
for each in current_object['ipv4addrs']:
if each['ipv4addr'] == proposed_object['ipv4addrs'][0]['ipv4addr']:
Expand All @@ -464,6 +485,10 @@ def run(self, ib_obj_type, ib_spec):
proposed_object = self.check_for_new_ipv4addr(proposed_object)

res = None
if ib_obj_type == NIOS_VLAN:
# Removes keys from the proposed_object that are empty and do not exist in current_object.
# Fix the issue to update the optional fields of the object with default empty values
proposed_object = self.clean_empty_keys(current_object, proposed_object)
modified = not self.compare_objects(current_object, proposed_object)
if 'extattrs' in proposed_object:
proposed_object['extattrs'] = normalize_extattrs(proposed_object['extattrs'])
Expand Down Expand Up @@ -581,9 +606,9 @@ def get_network_view(self, proposed_object):
network_view_ref = self.get_object('view', {"name": proposed_object['view']}, return_fields=['network_view'])
if network_view_ref:
network_view = network_view_ref[0].get('network_view')
return network_view
except Exception:
raise Exception("object with dns_view: %s not found" % (proposed_object['view']))
return network_view

def check_if_nios_next_ip_exists(self, proposed_object):
''' Check if nios_next_ip argument is passed in ipaddr while creating
Expand Down Expand Up @@ -770,6 +795,10 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
old_name = new_name = None
old_ipv4addr_exists = old_text_exists = False
next_ip_exists = False

if ib_obj_type == NIOS_VLAN:
obj_filter.update({'parent': ib_spec['parent']['transform'](self.module)})

if ('name' in obj_filter):
# gets and returns the current object based on name/old_name passed
try:
Expand Down Expand Up @@ -810,6 +839,9 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
test_obj_filter['ipv4addr'] = ipaddr
else:
del test_obj_filter['ipv4addr']
elif ib_obj_type == NIOS_VLAN:
test_obj_filter = dict([
('name', old_name), ('id', obj_filter['id']), ('parent', obj_filter['parent'])])
else:
test_obj_filter = dict([('name', old_name)])
# get the object reference
Expand Down

0 comments on commit 5605c9c

Please sign in to comment.