Adds redirection api for cpanel (via uapi) + refactors cpanel

This commit is contained in:
Akshay Nair committed 2020-10-11 17:33:26 +05:30
1 parent 28c0f7301a
commit fa9ad8baef
2 files changed
+32 -9

No files matched your search

+3 -3
View File
@@ -12,7 +12,7 @@ describe('Cpanel client', () => {
describe('fetchzonerecords', () => { describe('fetchzonerecords', () => {
it('should make the correct request', async () => { it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => { const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000//json-api/cpanel?customonly=1&domain=a.b&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2'); expect(url).toBe('https://example.com:2000/json-api/cpanel?customonly=1&domain=a.b&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({ expect(request).toEqual({
headers: { headers: {
Authorization: 'cpanel boy:boybyebye', Authorization: 'cpanel boy:boybyebye',
@@ -35,7 +35,7 @@ describe('Cpanel client', () => {
it('should make the correct request with query', async () => { it('should make the correct request with query', async () => {
const fetch = mockFetch((url, request) => { const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000//json-api/cpanel?customonly=1&domain=foobar.boeey&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2'); expect(url).toBe('https://example.com:2000/json-api/cpanel?customonly=1&domain=foobar.boeey&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({ expect(request).toEqual({
headers: { headers: {
Authorization: 'cpanel boy:boybyebye', Authorization: 'cpanel boy:boybyebye',
@@ -60,7 +60,7 @@ describe('Cpanel client', () => {
describe('addzonerecord', () => { describe('addzonerecord', () => {
it('should make the correct request', async () => { it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => { const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000//json-api/cpanel?domain=a.b&name=googo&type=CNAME&cname=beey&ttl=2020&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=add_zone_record&cpanel_jsonapi_apiversion=2'); expect(url).toBe('https://example.com:2000/json-api/cpanel?domain=a.b&name=googo&type=CNAME&cname=beey&ttl=2020&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=add_zone_record&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({ expect(request).toEqual({
headers: { headers: {
Authorization: 'cpanel boy:boybyebye', Authorization: 'cpanel boy:boybyebye',
+29 -6
View File
@@ -5,7 +5,7 @@ const { DOMAIN_API_HOST, DOMAIN_API_PORT, DOMAIN_USER, DOMAIN_API_KEY, DOMAIN_DO
const CpanelClient = (options) => { const CpanelClient = (options) => {
// TODO: Make defaultQuery functional // TODO: Make defaultQuery functional
const api = (module, func, defaultQuery = {}) => (q = {}) => { const api = ({ basePath = '', action = '' }) => (module, func, defaultQuery = {}) => (q = {}) => {
const query = { const query = {
...defaultQuery, ...defaultQuery,
...q, ...q,
@@ -22,23 +22,36 @@ const CpanelClient = (options) => {
rejectUnauthorized: false, rejectUnauthorized: false,
}; };
const path = `${options.path || '/json-api'}/cpanel?${qs.stringify(query)}`; const path = `${basePath}/${action}?${qs.stringify(query)}`;
const reqUrl = `https://${options.host}:${options.port}/${path}`; const reqUrl = `https://${options.host}:${options.port}/${path}`;
const { fetch } = options.dependencies; const { fetch } = options.dependencies;
return fetch(reqUrl, request).then(res => res.json()); return fetch(reqUrl, request).then(res => res.json());
}; };
const api2 = api({ basePath: 'json-api', action: 'cpanel' });
const uapi = (module, func, defaultQuery) =>
api({ basePath: 'execute', action: `${module}/${func}` })(module, func, defaultQuery);
return { return {
// { customonly, domain } // { customonly, domain }
// -> { cpanelresult: { data[{ class, ttl, name, line, Line, cname, type, record }] } } // -> { cpanelresult: { data[{ class, ttl, name, line, Line, cname, type, record }] } }
fetchZoneRecords: R.compose( fetchZoneRecords: R.compose(
p => p.then(R.pathOr([], ['cpanelresult', 'data'])), p => p.then(R.pathOr([], ['cpanelresult', 'data'])),
api('ZoneEdit', 'fetchzone_records', { customonly: 1, domain: options.domain }) api2('ZoneEdit', 'fetchzone_records', { customonly: 1, domain: options.domain })
), ),
// { domain, name, type, cname, address, ttl }
// { domain, name, type(A|CNAME), cname, address, ttl }
// -> { result: { status } } // -> { result: { status } }
addZoneRecord: api('ZoneEdit', 'add_zone_record', { domain: options.domain }), 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'),
// {}
// -> { }
fetchRedirections: uapi('Mime', 'list_redirects'),
}; };
}; };
@@ -56,7 +69,17 @@ const cpanel = CpanelClient({
dependencies: { fetch }, dependencies: { fetch },
}); });
// cpanel.fetchZoneRecords().then(hosts => console.log(JSON.stringify(hosts, null, 2))); //cpanel.fetchZoneRecords()
//cpanel.addRedirection({
//domain: 'hello.is-a.dev',
//redirect: 'https://googole.com',
//type: 'permanent',
//redirect_wildcard: 1,
//redirect_www: 0,
//})
//cpanel.fetchRedirections()
//.then(d => console.log(JSON.stringify(d, null, 2)))
//.catch(console.error);
module.exports = { module.exports = {
cpanel, cpanel,