Skip to content

Commit

Permalink
add support for operating on part of objects, not all
Browse files Browse the repository at this point in the history
  • Loading branch information
Maples7 committed Aug 8, 2017
1 parent 9e2e613 commit 299835b
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ typings/
kcm-cli
main
kcm-config.json
sec_test
sec*test
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,19 @@ For example, `kcm-config.json` in the current working directory:
```json
{
"main": "http://192.168.99.100:8001",
"sec_test": "https://localhost:8444"
"sec_test": "https://localhost:8444",
"third_test": {
// `host` is a required field
"host": "http://localhost:8001",
// specify which objects are your real concerns
// this can be used to avoid too many `consumers` here
// see ./enums/index.js to get valid objects
// `targets` are bound up with `upstreams`, so use `upstreams` rather than `targets`
"objects": ["apis", "plugins", "certificates", "snis", "upstreams"]
}
}
```

The key would be also used as folder name with all configs of corresponding Kong instance in it, so do **NOT** use any illegal characters for a folder name.

**NOTE**: the protocol like `http` or `https` can NOT be omitted.

#### Commands
Expand Down Expand Up @@ -132,10 +139,10 @@ Finally, run `npm test`.
### TODO

- [x] use more readable field as filename, remember to filenamify
- [ ] auto-generate a CLI config file template `kcm-config.json` at the beginning
- [ ] add support for operating on part of objects, not all. (consumers may be too many)
- [ ] auto-generate a CLI config file template `kcm-config.json` at the beginning or with `kcm init`
- [x] add support for operating on part of objects, not all. (consumers may be too many)
- [ ] add test mode for `apply`. Just show diff, don't operate truly
- [ ] * omit some unimportant fields such as `created_at`
- [ ] * ~~omit some unimportant fields such as `created_at`~~ NO NEED TO DO THIS
- [x] * support node version 4 (need more consideration)

## Relatives
Expand Down
9 changes: 6 additions & 3 deletions bin/kcm-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ const exit = require('../utils/exit');
const getAbsolutePath = require('../utils/get_absolute_path');
const getConfigs = require('../utils/get_configs');
const makeProgram = require('../utils/make_program');
const validateConfig = require('../utils/validate_config');

const program = makeProgram();

let retPromise = null;

function initApply(instance, host) {
function initApply(instance, config) {
config = validateConfig(config, instance);

const instancePath = getAbsolutePath(filenameConverter.serialize(instance));
host = _.trimEnd(host, '/');
const host = _.trimEnd(config.host, '/');
if (fs.existsSync(instancePath)) {
console.log(chalk.green(`Ready to apply configs for ${instance}...`));
return apply(instancePath, host).then(() => {
Expand Down Expand Up @@ -54,5 +57,5 @@ if (program.host) {
}

retPromise
.catch(err => exit(`Error: ${err}`))
.catch(err => exit(`Error: ${err.stack}`))
.finally(() => console.log(chalk.green('All Finished!')));
2 changes: 1 addition & 1 deletion bin/kcm-dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ if (program.host) {
}

retPromise
.catch(err => exit(`Error: ${err}`))
.catch(err => exit(`Error: ${err.stack}`))
.finally(() => console.log(chalk.green('All Finished!')));
23 changes: 19 additions & 4 deletions lib/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const rp = require('request-promise');
const ENUMS = require('../enums/');
const exit = require('../utils/exit');
const makeDir = require('../utils/make_dir');
const validateConfig = require('../utils/validate_config');

module.exports = function dump(host, name) {
module.exports = function dump(config, name) {
function dumpItems(url, currentDir, items, obj) {
debug(`Url waits to be dumped is ${url}`);
return rp({
Expand Down Expand Up @@ -55,12 +56,26 @@ module.exports = function dump(host, name) {
});
}

console.log(chalk.green(`Ready to dump configs of kong instance ${name}...`));
host = _.trimEnd(host, '/');
config = validateConfig(config, name);
const host = _.trimEnd(config.host, '/');
let objects = ENUMS.OBJECTS;
if (config.objects) {
if (!_.isArray(config.objects)) {
exit(`'objects' field of '${name}' must be an array of string`);
} else {
objects = config.objects;
const wrongObjs = _.difference(objects, ENUMS.OBJECTS);
if (wrongObjs.length > 0) {
exit(`invalid or unsupported objects found: ${wrongObjs}`);
}
}
}

console.log(chalk.green(`Ready to dump configs of kong instance ${name}...`));
makeDir(filenameConverter.serialize(name));

// handle dump
return Promise.map(ENUMS.OBJECTS, obj => {
return Promise.map(objects, obj => {
const currentDir = `${filenameConverter.serialize(name)}/${obj}`;
makeDir(currentDir);
debug(`Ready to dump ${obj} object of ${name}...`);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
"fs-extra": "^4.0.0",
"lodash": "^4.17.4",
"request": "^2.81.0",
"request-promise": "^4.2.1"
"request-promise": "^4.2.1",
"shelljs": "^0.7.8"
},
"devDependencies": {
"ava": "^0.21.0",
"coveralls": "^2.13.1",
"nyc": "^11.1.0",
"pm2": "^2.6.1",
"shelljs": "^0.7.8"
"pm2": "^2.6.1"
},
"engines": {
"node": ">=4.4.0"
Expand Down
72 changes: 68 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,71 @@ test.before(t => {
fse.writeJsonSync(
'./kcm-config.json',
{
main: 'http://localhost:3001',
'sec:test': 'http://localhost:3001'
main: {
host: 'http://localhost:3001'
},
'sec:test': {
host: 'http://localhost:3001'
},
wrong1: [],
wrong2: {
host: []
},
wrong3: {},
wrong4: {
host: 'str',
objects: ['wrongobj']
},
wrong5: {
host: 'str',
objects: 'strtoo'
}
},
{
spaces: 2
}
);
});

test.serial('kcm dump -i wrong1', t => {
t.plan(1);
const ret = shell.exec('kcm dump -i wrong1');
t.is(ret.code, 1);
});

test.serial('kcm dump -i wrong2', t => {
t.plan(1);
const ret = shell.exec('kcm dump -i wrong2');
t.is(ret.code, 1);
});

test.serial('kcm dump -i wrong4', t => {
t.plan(1);
const ret = shell.exec('kcm dump -i wrong4');
t.is(ret.code, 1);
});

test.serial('kcm dump -i wrong5', t => {
t.plan(1);
const ret = shell.exec('kcm dump -i wrong5');
t.is(ret.code, 1);
});

test.serial('kcm dump -i wrong3', t => {
t.plan(1);
const ret = shell.exec('kcm dump -i wrong3');
t.is(ret.code, 1);

fse.writeJsonSync(
'./kcm-config.json',
{
main: {
host: 'http://localhost:3001'
},
'sec:test': {
host: 'http://localhost:3001',
objects: ['apis', 'plugins', 'certificates', 'snis', 'upstreams']
}
},
{
spaces: 2
Expand All @@ -25,7 +88,7 @@ test.before(t => {
});

test.serial('DEBUG=kcm:dump kcm dump --all', t => {
t.plan(5);
t.plan(7);
const ret = shell.exec('DEBUG=kcm:dump kcm dump --all');

t.is(ret.code, 0);
Expand All @@ -37,6 +100,8 @@ test.serial('DEBUG=kcm:dump kcm dump --all', t => {
const target1 = require('./main/upstreams_service.v1.xyz_targets/4661f55e-95c2-4011-8fd6-c5c56df1c9db.json');
t.is(target1.id, '4661f55e-95c2-4011-8fd6-c5c56df1c9db');
t.is(target1.weight, 15);
t.true(!fs.existsSync(`./${filenameConverter.serialize('sec:test')}/consumers`));
t.true(!fs.existsSync(`./${filenameConverter.serialize('sec:test')}/cluster`));
});

test.serial('kcm dump --host http://localhost:3001', t => {
Expand Down Expand Up @@ -64,7 +129,6 @@ test.serial('kcm dump --file ./kcm-config.json', t => {
test.serial('kcm dump --instance wrongins', t => {
t.plan(1);
const ret = shell.exec('kcm dump --instance wrongins');

t.is(ret.code, 1);
});

Expand Down
22 changes: 22 additions & 0 deletions utils/validate_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const _ = require('lodash');
const exit = require('./exit');

module.exports = function validateConfig(conf, name) {
if (_.isString(conf)) {
conf = { host: conf };
}
if (!_.isPlainObject(conf)) {
exit(`value of ${name} field in config file must be a plain object or a string`);
}
if (!conf.host) {
exit(`required 'host' field of '${name}' is NOT found in config file`);
}
if (!_.isString(conf.host)) {
exit(
`'host' field of '${name}' must be a string, e.g. https://localhost:8444`
);
}
return conf;
};

0 comments on commit 299835b

Please sign in to comment.