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 to create txt record with an equals sign in the text field and update the text field #128

Merged
merged 6 commits into from
Jun 29, 2022
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
50 changes: 43 additions & 7 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
#


import json
import os
from functools import partial
from ansible.module_utils._text import to_native
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.common.validation import check_type_dict
from ansible.module_utils.common.validation import check_type_dict, safe_eval
from ansible.module_utils.six import string_types

try:
from infoblox_client.connector import Connector
Expand Down Expand Up @@ -313,6 +315,20 @@ def run(self, ib_obj_type, ib_spec):
if (ib_obj_type == NIOS_MEMBER):
proposed_object = member_normalize(proposed_object)

# checks if the 'text' field has to be updated for the TXT Record
if (ib_obj_type == NIOS_TXT_RECORD):
text_obj = proposed_object["text"]
if text_obj.startswith("{"):
try:
text_obj = json.loads(text_obj)
txt = text_obj['new_text']
except Exception:
(result, exc) = safe_eval(text_obj, dict(), include_exceptions=True)
if exc is not None:
raise TypeError('unable to evaluate string as dictionary')
txt = result['new_text']
proposed_object['text'] = txt

# checks if the name's field has been updated
if update and new_name:
proposed_object['name'] = new_name
Expand Down Expand Up @@ -514,8 +530,8 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
# gets and returns the current object based on name/old_name passed
try:
name_obj = check_type_dict(obj_filter['name'])
old_name = name_obj['old_name']
new_name = name_obj['new_name']
old_name = name_obj['old_name'].lower()
new_name = name_obj['new_name'].lower()
except TypeError:
name = obj_filter['name']

Expand Down Expand Up @@ -560,8 +576,18 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
# resolves issue where multiple txt_records with same name and different text
test_obj_filter = obj_filter
try:
text_obj = check_type_dict(obj_filter['text'])
txt = text_obj['old_text']
text_obj = obj_filter['text']
if text_obj.startswith("{"):
try:
text_obj = json.loads(text_obj)
txt = text_obj['old_text']
except Exception:
(result, exc) = safe_eval(text_obj, dict(), include_exceptions=True)
if exc is not None:
raise TypeError('unable to evaluate string as dictionary')
txt = result['old_text']
else:
txt = text_obj
except TypeError:
txt = obj_filter['text']
test_obj_filter['text'] = txt
Expand All @@ -583,8 +609,18 @@ def get_object_ref(self, module, ib_obj_type, obj_filter, ib_spec):
# resolves issue where multiple txt_records with same name and different text
test_obj_filter = obj_filter
try:
text_obj = check_type_dict(obj_filter['text'])
txt = text_obj['old_text']
text_obj = obj_filter(['text'])
if text_obj.startswith("{"):
try:
text_obj = json.loads(text_obj)
txt = text_obj['old_text']
except Exception:
(result, exc) = safe_eval(text_obj, dict(), include_exceptions=True)
if exc is not None:
raise TypeError('unable to evaluate string as dictionary')
txt = result['old_text']
else:
txt = text_obj
except TypeError:
txt = obj_filter['text']
test_obj_filter['text'] = txt
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/plugins/module_utils/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_wapi_no_change(self):
{
"comment": "test comment",
"_ref": "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true",
"name": self.mock_check_type_dict_obj().__getitem__(),
"name": self.mock_check_type_dict_obj().__getitem__().lower(),
"extattrs": {}
}
]
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_wapi_extattrs_change(self):

kwargs = copy.deepcopy(test_object[0])
kwargs['extattrs']['Site']['value'] = 'update'
kwargs['name'] = self.mock_check_type_dict_obj().__getitem__()
kwargs['name'] = self.mock_check_type_dict_obj().__getitem__().lower()
del kwargs['_ref']

wapi = self._get_wapi(test_object)
Expand All @@ -162,7 +162,7 @@ def test_wapi_extattrs_nochange(self):
test_object = [{
"comment": "test comment",
"_ref": "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true",
"name": self.mock_check_type_dict_obj().__getitem__(),
"name": self.mock_check_type_dict_obj().__getitem__().lower(),
"extattrs": {'Site': {'value': 'test'}}
}]

Expand Down Expand Up @@ -193,7 +193,7 @@ def test_wapi_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__()})
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower()})

def test_wapi_delete(self):
self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible',
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_wapi_strip_network_view(self):
kwargs = test_object[0].copy()
ref = kwargs.pop('_ref')
kwargs['comment'] = 'updated comment'
kwargs['name'] = self.mock_check_type_dict_obj().__getitem__()
kwargs['name'] = self.mock_check_type_dict_obj().__getitem__().lower()
del kwargs['network_view']
del kwargs['extattrs']

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_a_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_nios_a_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__(),
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower(),
'ipv4': '192.168.10.1'})

def test_nios_a_record_update_comment(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_aaaa_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_nios_aaaa_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__(),
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower(),
'ipv6': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'})

def test_nios_aaaa_record_update_comment(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_cname_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_nios_a_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__(),
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower(),
'canonical': 'realhost.ansible.com'})

def test_nios_a_record_update_comment(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_dns_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_nios_dns_view_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__()})
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower()})

def test_nios_dns_view_update_comment(self):
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible-dns',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_host_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_nios_host_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__()})
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower()})

def test_nios_host_record_remove(self):
self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_mx_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_nios_mx_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__(),
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower(),
'mx': 'mailhost.ansible.com', 'preference': 0})

def test_nios_mx_record_update_comment(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_naptr_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_nios_naptr_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__(),
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower(),
'order': '1000', 'preference': '10',
'replacement': 'replacement1.network.ansiblezone.com'})

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_network_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_nios_network_view_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__()})
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower()})

def test_nios_network_view_update_comment(self):
self.module.params = {'provider': None, 'state': 'present', 'name': 'default',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_nsgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_nios_nsgroup_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__()})
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower()})

def test_nios_nsgroup_remove(self):
self.module.params = {'provider': None, 'state': 'absent', 'name': 'my-simple-group',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_nios_srv_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_nios_srv_record_create(self):
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__(),
wapi.create_object.assert_called_once_with('testobject', {'name': self.mock_check_type_dict_obj().__getitem__().lower(),
'port': 5080, 'target': 'service1.ansible.com', 'priority': 10, 'weight': 10})

def test_nios_srv_record_update_comment(self):
Expand Down