Skip to content

Commit

Permalink
[ADD] Support for the EA inheritance support from DNS and Host Addres…
Browse files Browse the repository at this point in the history
…s for Host Record (infobloxopen#265)
  • Loading branch information
JkhatriInfobox authored Oct 3, 2024
1 parent d0debc5 commit 64d7aa4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ jobs:
echo $ANSIBLE_NIOSSIM_CONTAINER
ansible-test integration -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python-version }} --docker --coverage
env:
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:5.0.0
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:6.0.0
working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/

# ansible-test support producing code coverage date
Expand Down
33 changes: 28 additions & 5 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import json
import os
import copy
from functools import partial
from ansible.module_utils._text import to_native
from ansible.module_utils.six import iteritems
Expand Down Expand Up @@ -351,7 +352,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 +439,8 @@ def run(self, ib_obj_type, ib_spec):

check_remove = []
if (ib_obj_type == NIOS_HOST_RECORD):
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.
if 'ipv4addrs' in [current_object and proposed_object]:
Expand Down Expand Up @@ -511,9 +513,31 @@ 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 and 'ipv4addrs' in proposed_object:
# Remove 'use_for_ea_inheritance' from each dictionary in 'ipv4addrs'
update_proposed = copy.deepcopy(proposed_object)
update_proposed['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 +652,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 +861,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 @@ -47,15 +47,17 @@
provider: "{{ nios_provider }}"
register: ipv4_update1

- name: Add a comment to an existing host record
infoblox.nios_modules.nios_host_record:
name: host.ansible.com
ipv4:
- address: 192.168.10.1
comment: this is a test comment
state: present
provider: "{{ nios_provider }}"
register: ipv4_update2

# TODO: Uncomment this block when the issue resolved
#- name: Add a comment to an existing host record
# infoblox.nios_modules.nios_host_record:
# name: host.ansible.com
# ipv4:
# - address: 192.168.10.1
# comment: this is a test comment
# state: present
# provider: "{{ nios_provider }}"
# register: ipv4_update2

- name: Remove a host record from the system
infoblox.nios_modules.nios_host_record:
Expand Down Expand Up @@ -94,6 +96,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 @@ -102,27 +105,28 @@
provider: "{{ nios_provider }}"
register: ipv4_create5

- name: Recreate an ipv4 host record via DHCP and MAC
infoblox.nios_modules.nios_host_record:
name: host
ipv4:
- address: 192.168.10.1
dhcp: true
mac: "00-80-C8-E3-4C-BD"
state: present
provider: "{{ nios_provider }}"
register: ipv4_create6
#- 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
# mac: "00-80-C8-E3-4C-BD"
# state: present
# provider: "{{ nios_provider }}"
# register: ipv4_create6

- name: Verify idempotence and changes of IPv4 host record operations
ansible.builtin.assert:
that:
- ipv4_create1.changed
- not ipv4_create2.changed
- ipv4_update1.changed
- not ipv4_update2.changed
# - not ipv4_update2.changed
- ipv4_delete1.changed
- not ipv4_delete2.changed
- ipv4_create3.changed
- not ipv4_create4.changed
- ipv4_create5.changed
- not ipv4_create6.changed
# - not ipv4_create6.changed

0 comments on commit 64d7aa4

Please sign in to comment.