Comparing version 1.2.10 to 1.2.12
@@ -13,2 +13,5 @@ 'use strict'; | ||
exports.updateApiPlugin = updateApiPlugin; | ||
exports.addGlobalPlugin = addGlobalPlugin; | ||
exports.removeGlobalPlugin = removeGlobalPlugin; | ||
exports.updateGlobalPlugin = updateGlobalPlugin; | ||
exports.createConsumer = createConsumer; | ||
@@ -78,2 +81,25 @@ exports.removeConsumer = removeConsumer; | ||
function addGlobalPlugin(pluginName, params) { | ||
return { | ||
endpoint: { name: 'plugins', params: { pluginName: pluginName } }, | ||
method: 'POST', | ||
body: (0, _objectAssign2.default)({}, params, { name: pluginName }) | ||
}; | ||
} | ||
function removeGlobalPlugin(pluginId) { | ||
return { | ||
endpoint: { name: 'plugin', params: { pluginId: pluginId } }, | ||
method: 'DELETE' | ||
}; | ||
} | ||
function updateGlobalPlugin(pluginId, params) { | ||
return { | ||
endpoint: { name: 'plugin', params: { pluginId: pluginId } }, | ||
method: 'PATCH', | ||
body: params | ||
}; | ||
} | ||
function createConsumer(username) { | ||
@@ -80,0 +106,0 @@ return { |
@@ -45,2 +45,5 @@ 'use strict'; | ||
}, | ||
fetchGlobalPlugins: function fetchGlobalPlugins() { | ||
return getPaginatedJson(router({ name: 'plugins' })); | ||
}, | ||
fetchPlugins: function fetchPlugins(apiName) { | ||
@@ -47,0 +50,0 @@ return getPaginatedJson(router({ name: 'api-plugins', params: { apiName: apiName } })); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
exports.apis = apis; | ||
exports.globalPlugins = globalPlugins; | ||
exports.plugins = plugins; | ||
@@ -56,3 +57,3 @@ exports.consumers = consumers; | ||
case 0: | ||
actions = [].concat(_toConsumableArray(apis(config.apis)), _toConsumableArray(consumers(config.consumers))); | ||
actions = [].concat(_toConsumableArray(apis(config.apis)), _toConsumableArray(consumers(config.consumers)), _toConsumableArray(globalPlugins(config.plugins))); | ||
return _context.abrupt('return', actions.map(_bindWorldState(adminApi)).reduce(function (promise, action) { | ||
@@ -85,2 +86,10 @@ return promise.then(_executeActionOnApi(action, adminApi)); | ||
function globalPlugins() { | ||
var globalPlugins = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0]; | ||
return globalPlugins.reduce(function (actions, plugin) { | ||
return [].concat(_toConsumableArray(actions), [_globalPlugin(plugin)]); | ||
}, []); | ||
} | ||
function plugins(apiName, plugins) { | ||
@@ -196,2 +205,3 @@ return plugins.reduce(function (actions, plugin) { | ||
var consumers = _ref.consumers; | ||
var plugins = _ref.plugins; | ||
@@ -215,2 +225,13 @@ var world = { | ||
}, | ||
getGlobalPlugin: function getGlobalPlugin(pluginName) { | ||
var plugin = plugins.find(function (plugin) { | ||
return plugin.api_id === undefined && plugin.name === pluginName; | ||
}); | ||
if (!plugin) { | ||
throw new Error('Unable to find global plugin ' + pluginName); | ||
} | ||
return plugin; | ||
}, | ||
getPlugin: function getPlugin(apiName, pluginName) { | ||
@@ -230,5 +251,11 @@ var plugin = world.getApi(apiName).plugins.find(function (plugin) { | ||
}, | ||
getGlobalPluginId: function getGlobalPluginId(pluginName) { | ||
return world.getGlobalPlugin(pluginName).id; | ||
}, | ||
getPluginAttributes: function getPluginAttributes(apiName, pluginName) { | ||
return world.getPlugin(apiName, pluginName).config; | ||
}, | ||
getGlobalPluginAttributes: function getGlobalPluginAttributes(pluginName) { | ||
return world.getGlobalPlugin(pluginName).config; | ||
}, | ||
hasPlugin: function hasPlugin(apiName, pluginName) { | ||
@@ -241,2 +268,7 @@ return Array.isArray(apis) && apis.some(function (api) { | ||
}, | ||
hasGlobalPlugin: function hasGlobalPlugin(pluginName) { | ||
return Array.isArray(plugins) && plugins.some(function (plugin) { | ||
return plugin.api_id === undefined && plugin.name === pluginName; | ||
}); | ||
}, | ||
hasConsumer: function hasConsumer(username) { | ||
@@ -343,2 +375,25 @@ return Array.isArray(consumers) && consumers.some(function (consumer) { | ||
isGlobalPluginUpToDate: function isGlobalPluginUpToDate(plugin) { | ||
if (false == plugin.hasOwnProperty('attributes')) { | ||
// of a plugin has no attributes, and its been added then it is up to date | ||
return true; | ||
} | ||
var diff = function diff(a, b) { | ||
return Object.keys(a).filter(function (key) { | ||
return JSON.stringify(a[key]) !== JSON.stringify(b[key]); | ||
}); | ||
}; | ||
var current = world.getGlobalPlugin(plugin.name); | ||
var _normalizeAttributes2 = (0, _utils.normalize)(plugin.attributes); | ||
var config = _normalizeAttributes2.config; | ||
var rest = _objectWithoutProperties(_normalizeAttributes2, ['config']); | ||
return diff(config, current.config).length === 0 && diff(rest, current).length === 0; | ||
}, | ||
isConsumerCredentialUpToDate: function isConsumerCredentialUpToDate(username, credential) { | ||
@@ -446,2 +501,28 @@ var current = world.getConsumerCredential(username, credential.name, credential.attributes); | ||
function _globalPlugin(plugin) { | ||
validateEnsure(plugin.ensure); | ||
return function (world) { | ||
if (plugin.ensure == 'removed') { | ||
if (world.hasGlobalPlugin(plugin.name)) { | ||
return (0, _actions.removeGlobalPlugin)(world.getGlobalPluginId(plugin.name)); | ||
} | ||
return (0, _actions.noop)(); | ||
} | ||
if (world.hasGlobalPlugin(plugin.name)) { | ||
if (world.isGlobalPluginUpToDate(plugin)) { | ||
console.log(" - global plugin", ('' + plugin.name).bold, "is up-to-date".green); | ||
return (0, _actions.noop)(); | ||
} | ||
return (0, _actions.updateGlobalPlugin)(world.getGlobalPluginId(plugin.name), plugin.attributes); | ||
} | ||
return (0, _actions.addGlobalPlugin)(plugin.name, plugin.attributes); | ||
}; | ||
} | ||
function _consumer(consumer) { | ||
@@ -448,0 +529,0 @@ validateEnsure(consumer.ensure); |
@@ -21,6 +21,7 @@ 'use strict'; | ||
var fetchPlugins = _ref.fetchPlugins; | ||
var fetchGlobalPlugins = _ref.fetchGlobalPlugins; | ||
var fetchConsumers = _ref.fetchConsumers; | ||
var fetchConsumerCredentials = _ref.fetchConsumerCredentials; | ||
var fetchConsumerAcls = _ref.fetchConsumerAcls; | ||
var apis, apisWithPlugins, consumers, consumersWithCredentialsAndAcls; | ||
var apis, apisWithPlugins, consumers, consumersWithCredentialsAndAcls, allPlugins, globalPlugins; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -129,8 +130,17 @@ while (1) { | ||
consumersWithCredentialsAndAcls = _context3.sent; | ||
_context3.next = 14; | ||
return fetchGlobalPlugins(); | ||
case 14: | ||
allPlugins = _context3.sent; | ||
globalPlugins = allPlugins.filter(function (plugin) { | ||
return plugin.api_id === undefined && plugin.consumer_id === undefined; | ||
}); | ||
return _context3.abrupt('return', { | ||
apis: apisWithPlugins, | ||
consumers: consumersWithCredentialsAndAcls | ||
consumers: consumersWithCredentialsAndAcls, | ||
plugins: globalPlugins | ||
}); | ||
case 13: | ||
case 17: | ||
case 'end': | ||
@@ -137,0 +147,0 @@ return _context3.stop(); |
@@ -39,6 +39,10 @@ 'use strict'; | ||
}; | ||
var parsePluginsForSchemes = function parsePluginsForSchemes(plugins) { | ||
return parseGlobalPlugins(plugins, prepareConfig); | ||
}; | ||
return { | ||
apis: parseApis(state.apis, parseApiPluginsForSchemes), | ||
consumers: parseConsumers(state.consumers) | ||
consumers: parseConsumers(state.consumers), | ||
plugins: parsePluginsForSchemes(state.plugins) | ||
}; | ||
@@ -169,2 +173,28 @@ })); | ||
function parseGlobalPlugins(plugins, prepareConfig) { | ||
return plugins.map(function (_ref10) { | ||
var name = _ref10.name; | ||
var enabled = _ref10.enabled; | ||
var config = _ref10.config; | ||
var id = _ref10.id; | ||
var api_id = _ref10.api_id; | ||
var consumer_id = _ref10.consumer_id; | ||
var created_at = _ref10.created_at; | ||
return { | ||
name: name, | ||
attributes: { | ||
enabled: enabled, | ||
config: prepareConfig(name, config) | ||
}, | ||
_info: { | ||
id: id, | ||
api_id: api_id, | ||
consumer_id: consumer_id, | ||
created_at: created_at | ||
} | ||
}; | ||
}); | ||
} | ||
function stripConfig(config, schema) { | ||
@@ -171,0 +201,0 @@ var mutableConfig = _extends({}, config); |
@@ -36,2 +36,6 @@ 'use strict'; | ||
case 'plugins': | ||
return adminApiRoot + '/plugins'; | ||
case 'plugin': | ||
return adminApiRoot + '/plugins/' + params.pluginId; | ||
case 'plugins-enabled': | ||
@@ -38,0 +42,0 @@ return adminApiRoot + '/plugins/enabled'; |
{ | ||
"name": "kongfig", | ||
"version": "1.2.10", | ||
"version": "1.2.12", | ||
"description": "A tool for Kong to allow declarative configuration.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/mybuilder/kongfig", |
@@ -58,3 +58,3 @@ <p align="center"> | ||
For APIs which uses custom consumer credential pluings, specify plugin and id name in <plugin>:<idValue> format with `--credential-schema` option. | ||
For APIs which uses custom consumer credential plugins, specify plugin and id name in <plugin>:<idValue> format with `--credential-schema` option. | ||
@@ -73,2 +73,4 @@ ``` | ||
Note: If you change the name of an API/Plugin/Consumer and want to ensure the old one is removed automatically, do not delete or modify the old API/Plugin/Consumer section, other than to add the `ensure: "removed"` flag. Examples shown below. | ||
Api schema: | ||
@@ -79,2 +81,3 @@ | ||
- name: mockbin # unique api name | ||
ensure: "present" # Set to "removed" to have Kongfig ensure the API is removed. Default is present. | ||
attributes: | ||
@@ -96,2 +99,3 @@ request_host: | ||
- name: rate-limiting # kong plugin name | ||
ensure: "present" # Set to "removed" to have Kongfig ensure the plugin is removed. Default is present. | ||
attributes: # the plugin attributes | ||
@@ -103,2 +107,15 @@ consumer_id: | ||
Global plugin schema: | ||
```yaml | ||
plugins: | ||
- name: cors | ||
attributes: | ||
enabled: true | ||
config: | ||
credentials: false | ||
preflight_continue: false | ||
max_age: 7000 | ||
``` | ||
All of the kong plugins should be supported if you find one that doesn't work please [add an issue](https://github.com/mybuilder/kongfig/issues/new). | ||
@@ -105,0 +122,0 @@ |
Sorry, the diff of this file is not supported yet
165996
3114
324