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

[ADD] Support for the EA inheritance support from DNS and Host Address for Host Record #265

Merged
29 changes: 24 additions & 5 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ def run(self, ib_obj_type, ib_spec):
if not proposed_object.get('configure_for_dns') and proposed_object.get('view') == 'default' \
and ib_obj_type == NIOS_HOST_RECORD:
del proposed_object['view']

if ib_obj_ref:
if len(ib_obj_ref) > 1:
for each in ib_obj_ref:
Expand Down Expand Up @@ -439,6 +438,8 @@ def run(self, ib_obj_type, ib_spec):

check_remove = []
if (ib_obj_type == NIOS_HOST_RECORD):
if 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.
if 'ipv4addrs' in [current_object and proposed_object]:
Expand Down Expand Up @@ -511,9 +512,28 @@ def run(self, ib_obj_type, ib_spec):
result['changed'] = True
if not self.module.check_mode and res is None:
proposed_object = self.on_update(proposed_object, ib_spec)
self.update_object(ref, proposed_object)
if ib_obj_type == NIOS_HOST_RECORD:
# Remove 'use_for_ea_inheritance' from each dictionary in 'ipv4addrs'
update_proposed = {**proposed_object, 'ipv4addrs': [
{k: v for k, v in addr.items() if k != 'use_for_ea_inheritance'} for addr in proposed_object['ipv4addrs']]}
res = self.update_object(ref, update_proposed)
else:
res = self.update_object(ref, proposed_object)
result['changed'] = True

if ib_obj_type == NIOS_HOST_RECORD and res:
# WAPI always reset the use_for_ea_inheritance for each update operation
# Handle use_for_ea_inheritance flag changes for IPv4addr in a host record
# Fetch the updated reference of host to avoid drift.
host_ref = self.connector.get_object(obj_type=str(res), return_fields=['ipv4addrs'])
if host_ref:
# Create a dictionary for quick lookups
ref_dict = {obj['ipv4addr']: obj['_ref'] for obj in host_ref['ipv4addrs']}
sorted_ipv4addrs = sorted(proposed_object['ipv4addrs'], key=lambda x: x.get('use_for_ea_inheritance', False))
for proposed in sorted_ipv4addrs:
ipv4addr = proposed['ipv4addr']
if ipv4addr in ref_dict and 'use_for_ea_inheritance' in proposed:
self.update_object(ref_dict[ipv4addr], {'use_for_ea_inheritance': proposed['use_for_ea_inheritance']})
elif state == 'absent':
if ref is not None:
if 'ipv4addrs' in proposed_object:
Expand Down Expand Up @@ -628,7 +648,7 @@ def issubset(self, item, objects):
'''
for obj in objects:
if isinstance(item, dict):
# Normalize MAC address for comparission
# Normalize MAC address for comparison
if 'mac' in item:
item['mac'] = item['mac'].replace('-', ':').lower()
if all(entry in obj.items() for entry in item.items()):
Expand Down Expand Up @@ -837,15 +857,14 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
if ib_obj_type == NIOS_HOST_RECORD:
ipv4addrs_return = [
'ipv4addrs.ipv4addr', 'ipv4addrs.mac', 'ipv4addrs.configure_for_dhcp', 'ipv4addrs.host',
'ipv4addrs.nextserver', 'ipv4addrs.use_nextserver'
'ipv4addrs.nextserver', 'ipv4addrs.use_nextserver', 'ipv4addrs.use_for_ea_inheritance'
]
ipv6addrs_return = [
'ipv6addrs.ipv6addr', 'ipv6addrs.duid', 'ipv6addrs.configure_for_dhcp', 'ipv6addrs.host',
'ipv6addrs.use_nextserver', 'ipv6addrs.nextserver'
]
return_fields.extend(ipv4addrs_return)
return_fields.extend(ipv6addrs_return)

ib_obj = self.get_object(ib_obj_type, test_obj_filter.copy(), return_fields=return_fields)

# prevents creation of a new A record with 'new_ipv4addr' when A record with a particular 'old_ipv4addr' is not found
Expand Down
15 changes: 15 additions & 0 deletions plugins/modules/nios_host_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
default: true
aliases:
- dns
use_dns_ea_inheritance:
version_added: "1.7.0"
description:
- When use_dns_ea_inheritance is True, the EA is inherited from associated zone. The default value is False.
type: bool
default: false
ipv4addrs:
description:
- Configures the IPv4 addresses for this host record. This argument
Expand All @@ -55,6 +61,13 @@
aliases:
- ipv4
suboptions:
use_for_ea_inheritance:
version_added: "1.7.0"
description:
- When use_for_ea_inheritance is True, the EA is inherited from Host address. The default value is False.
type: bool
default: false
required: false
ipv4addr:
description:
- Configures the IPv4 address for the host record. Users can dynamically
Expand Down Expand Up @@ -365,6 +378,7 @@ def main():
add=dict(type='bool', required=False),
use_nextserver=dict(type='bool', required=False, aliases=['use_pxe']),
nextserver=dict(required=False, aliases=['pxe']),
use_for_ea_inheritance=dict(type='bool', required=False, default=False),
remove=dict(type='bool', required=False)
)

Expand All @@ -381,6 +395,7 @@ def main():
ipv4addrs=dict(type='list', aliases=['ipv4'], elements='dict', options=ipv4addr_spec, transform=ipv4addrs),
ipv6addrs=dict(type='list', aliases=['ipv6'], elements='dict', options=ipv6addr_spec, transform=ipv6addrs),
configure_for_dns=dict(type='bool', default=True, required=False, aliases=['dns'], ib_req=True),
use_dns_ea_inheritance=dict(type='bool', default=False, required=False),
aliases=dict(type='list', elements='str'),

ttl=dict(type='int'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
- name: Create an ipv4 host record via DHCP and MAC
infoblox.nios_modules.nios_host_record:
name: host
configure_for_dns: false
ipv4:
- address: 192.168.10.1
dhcp: true
Expand All @@ -105,6 +106,7 @@
- name: Recreate an ipv4 host record via DHCP and MAC
infoblox.nios_modules.nios_host_record:
name: host
configure_for_dns: false
ipv4:
- address: 192.168.10.1
dhcp: true
Expand Down
Loading