mx: adds automatic priority for mx record lists + publishing

This commit is contained in:
Akshay Nair
2021-09-02 15:24:17 +05:30
parent 8d4788c61b
commit eb484fe163
5 changed files with 36 additions and 25 deletions
+5 -4
View File
@@ -10,18 +10,19 @@ const toHostList = R.chain(data => {
const rs = getRecords(data.record);
return R.chain(([recordType, urls]) =>
(Array.isArray(urls) ? urls : [urls]).map(url => ({
(Array.isArray(urls) ? urls : [urls]).map((url, index) => ({
name: data.name,
type: recordType,
address: (recordType === 'CNAME' ? `${url}`.toLowerCase() : `${url}`).replace(/\/$/g, ''),
ttl: TTL,
...(recordType === 'MX' ? { priority: index + 20 } : {})
}))
, rs);
, rs);
});
const registerDomains = async ({ domainService, getDomains, log = () => {} }) => {
const registerDomains = async ({ domainService, getDomains, log = () => { } }) => {
const domains = await getDomains().then(toHostList);
if (domains.length === 0)
return Promise.reject(new Error('Nothing to register'));
+15 -11
View File
@@ -1,6 +1,6 @@
const R = require('ramda');
const { getDomainService, diffRecords } = require('../utils/domain-service');
const {DOMAIN_DOMAIN} = require('../utils/constants');
const { DOMAIN_DOMAIN } = require('../utils/constants');
const getCpanel = ({ zone, addZone, removeZone, redir, addRedir, removeRedir } = {}) => ({
zone: {
@@ -115,16 +115,18 @@ describe('Domain service', () => {
const addRedir = jest.fn(async () => ({}));
const removeRedir = jest.fn(async () => ({}));
const mockDS = ({ zones, redirections }) => getDomainService({ cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addRedir,
removeZone,
removeRedir,
}) });
const mockDS = ({ zones, redirections }) => getDomainService({
cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addRedir,
removeZone,
removeRedir,
})
});
const getRecordCalls = recfn => recfn.mock.calls.map(R.head).map(R.pick(['name', 'type', 'address', 'redirect', 'domain', 'line']));
const getRecordCalls = recfn => recfn.mock.calls.map(R.head).map(R.pick(['name', 'type', 'address', 'redirect', 'domain', 'line', 'priority']));
beforeEach(() => {
addZone.mockClear();
@@ -187,11 +189,13 @@ describe('Domain service', () => {
{ name: 'a', type: 'CNAME', address: 'boo' },
{ name: 'b', type: 'CNAME', address: 'goo' },
{ name: 'c', type: 'A', address: '12.131321.213' },
{ name: 'c', type: 'MX', address: 'foobar.com', priority: 2 },
]);
expect(addZone).toBeCalledTimes(1);
expect(addZone).toBeCalledTimes(2);
expect(getRecordCalls(addZone)).toEqual([
{ name: 'c', type: 'A', address: '12.131321.213' },
{ name: 'c', type: 'MX', address: 'foobar.com', priority: 2 },
]);
expect(removeZone).toBeCalledTimes(0);
});
+14 -8
View File
@@ -22,6 +22,7 @@ describe('toHostList', () => {
{ name: 'akshay', record: { CNAME: 'phenax.github.io' } },
{ name: 'foobar', record: { CNAME: 'v.io' } },
{ name: 'xx', record: { A: ['1.2.3.4', '5.6.3.2', '1.2.31.1'] } },
{ name: 'xx', record: { CNAME: 'foobar.com', MX: ['as.com', 'f.com'] } },
]);
expect(res).toEqual([
@@ -30,6 +31,9 @@ describe('toHostList', () => {
{ name: 'xx', type: 'A', address: '1.2.3.4', ttl: TTL },
{ name: 'xx', type: 'A', address: '5.6.3.2', ttl: TTL },
{ name: 'xx', type: 'A', address: '1.2.31.1', ttl: TTL },
{ name: 'xx', type: 'CNAME', address: 'foobar.com', ttl: TTL },
{ name: 'xx', type: 'MX', address: 'as.com', priority: 20, ttl: TTL },
{ name: 'xx', type: 'MX', address: 'f.com', priority: 21, ttl: TTL },
]);
});
});
@@ -40,14 +44,16 @@ describe('registerDomains', () => {
const addRedir = jest.fn(async () => ({}));
const removeRedir = jest.fn(async () => ({}));
const mockDS = ({ zones, redirections }) => getDomainService({ cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addRedir,
removeZone,
removeRedir,
}) });
const mockDS = ({ zones, redirections }) => getDomainService({
cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addRedir,
removeZone,
removeRedir,
})
});
beforeEach(() => {
addZone.mockClear();
+2 -1
View File
@@ -12,11 +12,12 @@ const recordToRedirection = ({ name, address }) => ({
redirect_wildcard: 1,
redirect_www: 1,
});
const recordToZone = ({ name, type, address, id }) => ({
const recordToZone = ({ name, type, address, id, priority }) => ({
line: id,
name,
type,
address,
...(type === 'MX' ? { priority } : {}),
...(type === 'CNAME' ? { cname: address } : {}),
...(type === 'TXT' ? { txtdata: address } : {}),
});
-1
View File
@@ -4,7 +4,6 @@ const qs = require('qs');
const { DOMAIN_API_HOST, DOMAIN_API_PORT, DOMAIN_USER, DOMAIN_API_KEY, DOMAIN_DOMAIN } = require('../constants');
const CpanelClient = (options) => {
// TODO: Make defaultQuery functional
const api = ({ basePath = '', action = '' }) => (module, func, defaultQuery = {}) => (q = {}) => {
const query = {
...defaultQuery,