Skip to content

Commit

Permalink
Fix client MX setup (#436)
Browse files Browse the repository at this point in the history
* Avoid swallowing Cloudflare exceptions

* Fix Cloudflare MX record creation error
  • Loading branch information
c-w authored May 8, 2020
1 parent af6f8f2 commit 5bf0b94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion opwen_email_server/services/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ def _run(self, client_name: str, zone: Zone) -> None:
name=client_name,
type=RecordType.MX,
data=MX_RECORD,
extra={'priority': 0},
)
except LibcloudError:
except LibcloudError as ex:
if 'record already exists' not in ex.value:
raise ex
self.log_warning('MX records for client %s.%s already exist', client_name, zone.domain)
else:
self.log_info('Set up MX records for client %s.%s', client_name, zone.domain)
16 changes: 15 additions & 1 deletion tests/opwen_email_server/services/test_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,23 @@ def test_returns_when_record_already_exists(self):
Zone(id='1', domain='foo.com', type='master', ttl=1, driver=mock_driver),
Zone(id='2', domain='my-zone', type='master', ttl=1, driver=mock_driver),
]
mock_driver.create_record.side_effect = throw(LibcloudError(None, mock_driver))
mock_driver.create_record.side_effect = throw(
LibcloudError('81057: The record already exists.', mock_driver))

action('my-domain.my-zone')

self.assertEqual(mock_driver.iterate_zones.call_count, 1)
self.assertEqual(mock_driver.create_record.call_count, 1)

def test_throws_when_error_is_unknown(self):
action = SetupMxRecords('my-user', 'my-key', provider='CLOUDFLARE')

with patch.object(action, '_driver', new_callable=PropertyMock) as mock_driver:
mock_driver.iterate_zones.return_value = [
Zone(id='1', domain='foo.com', type='master', ttl=1, driver=mock_driver),
Zone(id='2', domain='my-zone', type='master', ttl=1, driver=mock_driver),
]
mock_driver.create_record.side_effect = throw(LibcloudError('Some unknown error', mock_driver))

with self.assertRaises(LibcloudError):
action('my-domain.my-zone')

0 comments on commit 5bf0b94

Please sign in to comment.