-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdump.js
105 lines (99 loc) · 3.28 KB
/
dump.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
const Promise = require('bluebird');
const debug = require('debug')('kcm:dump');
const filenameConverter = require('filename-converter');
const _ = require('lodash');
const rp = require('request-promise');
const ENUMS = require('../enums/');
const getObjects = require('../tools/get_objects');
const handleWrongStatusCode = require('../tools/non2xx_handler');
const logger = require('../utils/logger');
const makeDir = require('../utils/make_dir');
const validateConfig = require('../utils/validate_config');
const writeJsonSync = require('../utils/write_json_sync');
module.exports = function dump(config, name, ssl) {
debug(`Dumping with [ssl checks=${ssl}]`);
config = validateConfig(config, name);
const host = _.trimEnd(config.host, '/');
function dumpItems(url, currentDir, items, obj) {
debug(`Url waits to be dumped is ${url}`);
return rp({
method: 'GET',
uri: url,
insecure: !ssl,
rejectUnauthorized: ssl,
timeout: ENUMS.REQUEST_TIMEOUT
})
.then(body => {
// debug(`for ${obj}, after request, we got: ${body}`);
const res = JSON.parse(body);
if (_.isArray(res.data)) {
items = _.concat(items, res.data);
}
if (res.next) {
let nextPageURL = res.next;
if (!/^http(s)?:\/\//.test(nextPageURL)) {
nextPageURL = `${host}${nextPageURL}`;
}
return dumpItems(nextPageURL, currentDir, items, obj);
}
return Promise.map(items, item => {
const id = item[ENUMS.IDENTIFIRES[obj] || 'id'];
if (id) {
try {
writeJsonSync(
`./${currentDir}/${filenameConverter.serialize(id)}.json`,
item,
{
spaces: 2
}
);
} catch (e) {
logger.error(
`fail to save json for ${id}.json in ${currentDir}: ${e}`
);
}
debug(`Success to save JSON ./${currentDir}/${id}.json`);
if (obj === 'upstreams') {
const targetDir = `${currentDir}_${filenameConverter.serialize(
id
)}_targets`;
makeDir(targetDir);
debug(`Ready to dump targets of upstream ${id}`);
return dumpItems(
`${url}${id}/targets/`,
targetDir,
[],
'targets'
);
}
}
});
})
.catch(err => handleWrongStatusCode(err, url));
}
return getObjects(host, ssl).then(OBJECTS => {
let objects = OBJECTS;
if (config.objects) {
if (!_.isArray(config.objects)) {
logger.error(`'objects' field of '${name}' must be an array of string`);
} else {
objects = config.objects;
}
}
logger.info(`Ready to dump configs of kong instance ${name}...`);
makeDir(filenameConverter.serialize(name));
// handle dump
return Promise.map(objects, obj => {
const currentDir = `${filenameConverter.serialize(name)}/${obj}`;
makeDir(currentDir);
debug(`Ready to dump ${obj} object of ${name}...`);
return dumpItems(
`${host}/${_.replace(obj, /_/g, '/')}/`,
currentDir,
[],
obj
);
});
});
};