Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX VLAN module and VLAN field under network #284

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plugins/lookup/nios_next_vlan_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def run(self, terms, variables=None, **kwargs):
provider = kwargs.pop('provider', {})
wapi = WapiLookup(provider)
num = kwargs.get('num', 1)
exclude_ip = kwargs.get('exclude', [])
exclude_vlan_id = kwargs.get('exclude', [])
parent = kwargs.get('parent', 'default')

try:
Expand All @@ -84,9 +84,9 @@ def run(self, terms, variables=None, **kwargs):
elif parent_obj_vlanview:
parent_ref = parent_obj_vlanview[0]['_ref']
else:
raise AnsibleError(msg='VLAN View/Range \'%s\' cannot be found.' % parent)
raise AnsibleError(message='VLAN View/Range \'%s\' cannot be found.' % parent)

avail_ids = wapi.call_func('next_available_vlan_id', parent_ref, {'num': num, 'exclude': exclude_ip})
avail_ids = wapi.call_func('next_available_vlan_id', parent_ref, {'num': num, 'exclude': exclude_vlan_id})
return [avail_ids['vlan_ids']]

except Exception as exc:
Expand Down
45 changes: 39 additions & 6 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,33 @@ 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 performans 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: a results dict
:returns: result dict
'''

update = new_name = None
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 Expand Up @@ -991,6 +1023,7 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
# del key 'members' as nios_network get_object fails with the key present
# Don't reinstate the field after as it is not valid for network containers
del ib_spec['members']
del ib_spec['vlans']

ib_obj = self.get_object(ib_obj_type, obj_filter.copy(), return_fields=list(ib_spec.keys()))
# reinstate the 'template' and 'members' key
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/nios_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def vlans(module):
if obj_vlan:
vlans_list.append({'vlan': obj_vlan[0]['_ref']})
else:
module.fail_json(msg='VLAN `%s` cannot be found.' % vlan['name'])
module.fail_json(msg='VLAN `%s` cannot be found.' % vlan)

return vlans_list

Expand Down
Loading