class TestHover(unittest.TestCase): def setUp(self): self.DNS_ID = 12345 with patch('requests.post') as patched_post, patch( 'requests.request') as patched_request: type( patched_post.return_value).ok = PropertyMock(return_value=True) type(patched_post.return_value).cookies = PropertyMock( return_value={ "hoverauth": "foo", "domains": [] }) type(patched_request.return_value).ok = PropertyMock( return_value=True) type(patched_request.return_value).json = Mock( return_value={ "succeeded": True, "domains": [{ "domain_name": "worldofchris.com", "id": self.DNS_ID }] }) username = '******' password = '******' domain_name = 'worldofchris.com' self.client = HoverClient(username=username, password=password, domain_name=domain_name) def testInitClient(self): """ Initalise the client """ self.assertEqual(self.client.dns_id, self.DNS_ID) def testAddCname(self): """ Add a CNAME """ with patch('requests.request') as patched_request: type(patched_request.return_value).json = Mock( return_value={"succeeded": True}) expected = {"succeeded": True} actual = self.client.add_record(type="CNAME", name="megatron", content="crazyland.aws.com") self.assertEqual(actual, expected) def testGetRecord(self): """ Get a record so we can check if it exists and has the expected value """ with patch('requests.request') as patched_request: type(patched_request.return_value).json = Mock(side_effect=[{ "succeeded": True, "domains": [{ "entries": [{ "type": "CNAME", "name": "megatron", "content": "crazyland.aws.com", "id": "dns1234" }] }] }]) expected = { "name": "megatron", "type": "CNAME", "content": "crazyland.aws.com", "id": "dns1234" } actual = self.client.get_record(type="CNAME", name="megatron") self.assertEqual(actual, expected) def testUpdateCname(self): """ Update content for an existing record """ with patch('requests.request') as patched_request: type( patched_request.return_value).json = Mock( side_effect=[{ "succeeded": True, "domains": [{ "entries": [{ "type": "CNAME", "name": "megatron", "content": "blah", "id": "dns1234" }] }] }, { "succeeded": True }]) expected = {"succeeded": True} actual = self.client.update_record(type="CNAME", name="megatron", content="foo.aws.com") self.assertEqual(actual, expected) def testRemoveCname(self): """ Remove a CNAME """ with patch('requests.request') as patched_request: type( patched_request.return_value).json = Mock( side_effect=[{ "succeeded": True, "domains": [{ "entries": [{ "type": "CNAME", "name": "megatron", "content": "blah", "id": "dns1234" }] }] }, { "succeeded": True }]) expected = {"succeeded": True} actual = self.client.remove_record(type="CNAME", name="megatron") self.assertEqual(actual, expected)
def main(): module = AnsibleModule( argument_spec=dict( domain=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), name=dict(required=True), value=dict(required=False, default=None), state=dict(required=False, choices=['present', 'absent'], default='present'), type=dict(requiured=False, choices=['A', 'ALIAS', 'CNAME', 'MX', 'SPF', 'URL', 'TXT', 'NS', 'SRV', 'NAPTR', 'PTR', 'AAAA', 'SSHFP', 'HINFO', 'POOL'], default='A'), ), add_file_common_args=True, supports_check_mode=True ) if not HAS_LIB: module.fail_json(msg="Requires hover module") params = module.params changed = False try: hc = HoverClient(username=params['username'], password=params['password'], domain_name=params['domain']) record = hc.get_record(name=params['name'], type=params['type']) if params['state'] == 'present': if record is None: if not module.check_mode: hc.add_record(name=params['name'], type=params['type'], content=params['value']) changed = True else: if record['content'] != params['value']: if not module.check_mode: hc.update_record(name=params['name'], type=params['type'], content=params['value']) changed = True else: if record is not None: if not module.check_mode: hc.remove_record(name=params['name'], type=params['type']) changed = True except HoverException as e: module.fail_json(msg=str(e)) module.exit_json(changed=changed)
class TestHover(unittest.TestCase): def setUp(self): self.DNS_ID = 12345 with patch("requests.post") as patched_post, patch("requests.request") as patched_request: type(patched_post.return_value).ok = PropertyMock(return_value=True) type(patched_post.return_value).cookies = PropertyMock(return_value={"hoverauth": "foo", "domains": []}) type(patched_request.return_value).ok = PropertyMock(return_value=True) type(patched_request.return_value).json = Mock( return_value={"succeeded": True, "domains": [{"domain_name": "worldofchris.com", "id": self.DNS_ID}]} ) username = "******" password = "******" domain_name = "worldofchris.com" self.client = HoverClient(username=username, password=password, domain_name=domain_name) def testInitClient(self): """ Initalise the client """ self.assertEqual(self.client.dns_id, self.DNS_ID) def testAddCname(self): """ Add a CNAME """ with patch("requests.request") as patched_request: type(patched_request.return_value).json = Mock(return_value={"succeeded": True}) expected = {"succeeded": True} actual = self.client.add_record(type="CNAME", name="megatron", content="crazyland.aws.com") self.assertEqual(actual, expected) def testGetRecord(self): """ Get a record so we can check if it exists and has the expected value """ with patch("requests.request") as patched_request: type(patched_request.return_value).json = Mock( side_effect=[ { "succeeded": True, "domains": [ { "entries": [ { "type": "CNAME", "name": "megatron", "content": "crazyland.aws.com", "id": "dns1234", } ] } ], } ] ) expected = {"name": "megatron", "type": "CNAME", "content": "crazyland.aws.com", "id": "dns1234"} actual = self.client.get_record(type="CNAME", name="megatron") self.assertEqual(actual, expected) def testUpdateCname(self): """ Update content for an existing record """ with patch("requests.request") as patched_request: type(patched_request.return_value).json = Mock( side_effect=[ { "succeeded": True, "domains": [ {"entries": [{"type": "CNAME", "name": "megatron", "content": "blah", "id": "dns1234"}]} ], }, {"succeeded": True}, ] ) expected = {"succeeded": True} actual = self.client.update_record(type="CNAME", name="megatron", content="foo.aws.com") self.assertEqual(actual, expected) def testRemoveCname(self): """ Remove a CNAME """ with patch("requests.request") as patched_request: type(patched_request.return_value).json = Mock( side_effect=[ { "succeeded": True, "domains": [ {"entries": [{"type": "CNAME", "name": "megatron", "content": "blah", "id": "dns1234"}]} ], }, {"succeeded": True}, ] ) expected = {"succeeded": True} actual = self.client.remove_record(type="CNAME", name="megatron") self.assertEqual(actual, expected)