fix(mx-remove): fixes diffing and removal of mx records

This commit is contained in:
Akshay Nair
2021-09-02 18:13:48 +05:30
parent be828568e6
commit a06538e57e
3 changed files with 36 additions and 9 deletions
+6 -2
View File
@@ -6,6 +6,10 @@
"email": "phenax5@gmail.com"
},
"record": {
"CNAME": "phenax.github.io"
"CNAME": "phenax.github.io",
"MX": [
"mx1.improvmx.com",
"mx2.improvmx.com"
]
}
}
}
+17 -5
View File
@@ -24,11 +24,12 @@ const recordToZone = ({ name, type, address, id, priority }) => ({
const cleanName = name => name === DOMAIN_DOMAIN ? '@' : `${name}`.replace(new RegExp(`\\.${DOMAIN_DOMAIN}\\.?$`), '').toLowerCase();
const zoneToRecord = ({ name, type, cname, address, record, line: id }) => ({
const zoneToRecord = ({ name, type, cname, address, priority, preference, exchange, record, line: id }) => ({
id,
name: cleanName(name),
type: `${type}`,
address: `${cname || address || record}`.replace(/\.$/g, '').toLowerCase(),
address: `${exchange || cname || address || record}`.replace(/\.$/g, '').toLowerCase(),
priority: priority || preference,
});
const redirectionToRecord = ({ domain, destination }) => ({
id: domain,
@@ -37,6 +38,12 @@ const redirectionToRecord = ({ domain, destination }) => ({
address: `${destination}`.replace(/\/$/g, ''),
});
const recordToEmailMx = ({ name, address, priority }) => ({
domain: `${name}.is-a.dev`,
exchanger: address,
priority,
})
const getHostKey = host => `${host.name}##${host.type}##${host.address}`;
const diffRecords = (oldRecords, newRecords) => {
@@ -70,13 +77,18 @@ const getDomainService = ({ cpanel }) => {
const fetchRedirections = R.compose(then(R.map(redirectionToRecord)), cpanel.redirection.fetch);
const addZoneRecord = lazyTask(R.compose(
cpanel.zone.add,
R.ifElse(R.propEq('type', 'MX'),
R.compose(cpanel.email.add, recordToEmailMx),
cpanel.zone.add
),
recordToZone,
print(({ name }) => `Adding zone for ${name}...`),
));
const removeZoneRecord = lazyTask(R.compose(
cpanel.zone.remove,
R.pick(['line']),
R.ifElse(R.propEq('type', 'MX'),
R.compose(cpanel.email.remove, recordToEmailMx),
R.compose(cpanel.zone.remove, R.pick(['line']))
),
recordToZone,
print(({ name }) => `Deleting zone for ${name}...`),
));
+13 -2
View File
@@ -35,7 +35,7 @@ const CpanelClient = (options) => {
return {
zone: {
// { customonly, domain }
// -> { cpanelresult: { data[{ class, ttl, name, line, Line, cname, type, record }] } }
// -> [{ class, ttl, name, line, Line, cname, type, record }]
fetch: R.compose(
p => p.then(R.pathOr([], ['cpanelresult', 'data'])),
api2('ZoneEdit', 'fetchzone_records', { customonly: 1, domain: options.domain })
@@ -44,13 +44,14 @@ const CpanelClient = (options) => {
// { name, type(A|CNAME), cname, address, ttl }
// -> {}
add: api2('ZoneEdit', 'add_zone_record', { domain: options.domain }),
// { line }
// -> {}
remove: api2('ZoneEdit', 'remove_zone_record', { domain: options.domain }),
},
redirection: {
// {}
// -> { domain, destination }
// -> [{ domain, destination }]
fetch: R.compose(
p => p.then(R.pathOr([], ['data'])),
uapi('Mime', 'list_redirects'),
@@ -59,6 +60,7 @@ const CpanelClient = (options) => {
// { domain, redirect, type(permanent|tmp), redirect_wildcard(0|1), redirect(0|1|2) }
// -> {}
add: uapi('Mime', 'add_redirect'),
// { domain }
// -> {}
remove: uapi('Mime', 'delete_redirect'),
@@ -66,6 +68,15 @@ const CpanelClient = (options) => {
file: {
write: uapi('Fileman', 'save_file_content', { from_charset: 'UTF-8', to_charset: 'UTF-8', fallback: 1 }),
},
email: {
// { domain, exchanger, priority }
// -> {}
add: uapi('Email', 'add_mx', { alwaysaccept: 'auto' }),
// { domain, exchanger, priority }
// -> {}
remove: uapi('Email', 'delete_mx', { alwaysaccept: 'auto' }),
},
};
};