Aggregates setting zone changes to server

This commit is contained in:
Akshay Nair
2020-10-11 18:08:37 +05:30
parent 49e8dd7fd0
commit c63e8c4562
3 changed files with 67 additions and 24 deletions
+32 -6
View File
@@ -2,8 +2,8 @@ const R = require('ramda');
const { getDomainService } = require('../utils/domain-service');
const getCpanel = ({ zone, redir, setZone, setRedir } = {}) => ({
addZoneRecord: (host) => setZone(host),
addRedirection: (_) => setRedir(),
addZoneRecord: (rec) => setZone(rec),
addRedirection: (rec) => setRedir(rec),
fetchZoneRecords: (_) => zone(),
fetchRedirections: (_) => redir(),
});
@@ -50,21 +50,47 @@ describe('Domain service', () => {
});
});
return;
describe('setHosts', () => {
it('should resolve with a list of hosts', async () => {
const records = [ { x: 'y' }, { z: 'a' } ];
const records = [
{ name: 'xx', type: 'CNAME', address: 'fck.com' },
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
{ name: 'foo', type: 'URL', address: 'https://google.com' },
{ name: 'foo1', type: 'URL', address: 'https://duck.com' },
];
const setZone = jest.fn(async () => {});
const setRedir = jest.fn(async () => {});
const mockDomainService = getDomainService({ cpanel: getCpanel({ setZone }) });
const mockDomainService = getDomainService({ cpanel: getCpanel({ setZone, setRedir }) });
await mockDomainService.setHosts(records);
expect(setZone).toBeCalledTimes(2);
expect(setZone.mock.calls.map(R.head)).toEqual([ { x: 'y' }, { z: 'a' } ]);
expect(setRedir).toBeCalledTimes(2);
expect(setZone.mock.calls.map(R.head)).toEqual([
{ name: 'xx', type: 'CNAME', address: 'fck.com' },
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
]);
expect(setRedir.mock.calls.map(R.head)).toEqual([
{
domain: 'foo.booboo.xyz',
redirect: 'https://google.com',
redirect_wildcard: 1,
redirect_www: 0,
type: 'permanent',
},
{
domain: 'foo1.booboo.xyz',
redirect: 'https://duck.com',
redirect_wildcard: 1,
redirect_www: 0,
type: 'permanent',
},
]);
});
});
return;
describe('updateHosts', () => {
it('should append new hosts with existing ones and set it', async () => {
+29 -12
View File
@@ -4,21 +4,35 @@ const {DOMAIN_DOMAIN} = require('./constants');
const flattenPromise = xs => Promise.all(xs);
const recordToRedirection = ({ name, address }) => ({
domain: `${name}.${DOMAIN_DOMAIN}`,
redirect: address,
type: 'permanent',
redirect_wildcard: 1,
redirect_www: 0,
});
const recordToZone = R.identity;
const zoneToRecord = ({ name, type, cname, address, ...host }) => ({
...host,
name: `${name}`,
type: `${type}`,
address: `${cname || address}`.replace(/\.$/g, ''),
});
const redirectionToRecord = ({ domain, destination }) => ({
name: `${domain}`.replace('.' + DOMAIN_DOMAIN, ''),
type: 'URL',
address: `${destination}`,
});
const getDomainService = ({ cpanel }) => {
let hostList = [];
const fetchZoneRecords = () => cpanel.fetchZoneRecords().then(R.map(host => ({
...host,
name: `${host.name}`,
type: `${host.type}`,
address: `${host.cname || host.address}`.replace(/\.$/g, ''),
})));
const fetchZoneRecords = () => cpanel.fetchZoneRecords().then(R.map(zoneToRecord));
const fetchRedirections = () => cpanel.fetchRedirections().then(R.map(redirectionToRecord));
const fetchRedirections = () => cpanel.fetchRedirections().then(R.map(host => ({
name: `${host.domain}`.replace('.' + DOMAIN_DOMAIN, ''),
type: 'URL',
address: `${host.destination}`,
})));
const addZoneRecord = R.compose(cpanel.addZoneRecord, recordToZone);
const addRedirection = R.compose(cpanel.addRedirection, recordToRedirection);
const getHosts = async () => {
if (hostList.length) return hostList;
@@ -29,7 +43,10 @@ const getDomainService = ({ cpanel }) => {
return list;
};
const setHosts = R.compose(flattenPromise, R.map(cpanel.addZoneRecord));
const setHosts = R.compose(flattenPromise, R.map(R.cond([
[ R.propEq('type', 'URL'), addRedirection ],
[ R.T, addZoneRecord ],
])));
const getHostKey = host => `${host.HostName}--${host.RecordType}`;
const toHostMap = hosts => hosts.reduce((acc, host) => {
+6 -6
View File
@@ -42,19 +42,19 @@ const CpanelClient = (options) => {
),
// { domain, name, type(A|CNAME), cname, address, ttl }
// -> { result: { status } }
// -> {}
addZoneRecord: api2('ZoneEdit', 'add_zone_record', { domain: options.domain }),
// { domain, redirect, type(permanent|tmp), redirect_wildcard(0|1), redirect(0|1|2) }
// -> {}
addRedirection: uapi('Mime', 'add_redirect'),
// {}
// -> { }
// -> { domain, destination }
fetchRedirections: R.compose(
p => p.then(R.pathOr([], ['data'])),
uapi('Mime', 'list_redirects'),
),
// { domain, redirect, type(permanent|tmp), redirect_wildcard(0|1), redirect(0|1|2) }
// -> {}
addRedirection: uapi('Mime', 'add_redirect'),
};
};