-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide option to force kong to sync with config
This adds the ability to use the --force argument to meet the needs of the feature requested in: #52 Removes the need for `ensure: "removed"` in configuration files.
- Loading branch information
Showing
5 changed files
with
560 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict'; | ||
|
||
import clone from 'clone'; | ||
import { getSchema } from './consumerCredentials' | ||
|
||
const rootKeys = ['apis', 'consumers', 'plugins', 'upstreams']; | ||
|
||
const ensureRemoved = (obj) => { | ||
obj.ensure = 'removed'; | ||
}; | ||
|
||
const markPluginsForRemoval = (local, remote) => { | ||
remote.forEach(plugin => { | ||
if (!local.find(p => p.name === plugin.name)) { | ||
ensureRemoved(plugin); | ||
local.unshift(plugin); | ||
} | ||
}); | ||
}; | ||
|
||
const markApisForRemoval = (local, remote) => { | ||
remote.forEach(api => { | ||
let found = local.find(a => a.name === api.name); | ||
if (found && api.plugins) { | ||
found.plugins = found.plugins ? found.plugins : []; | ||
markPluginsForRemoval(found.plugins, api.plugins); | ||
} else { | ||
ensureRemoved(api); | ||
local.unshift(api); | ||
} | ||
}) | ||
}; | ||
|
||
const markCredentialsForRemoval = (local, remote) => { | ||
remote.forEach(cred => { | ||
let key = getSchema(cred.name).id; | ||
if (!local.find(c=> c.attributes[key] === cred.attributes[key])) { | ||
ensureRemoved(cred); | ||
local.unshift(cred); | ||
} | ||
}); | ||
}; | ||
|
||
const markConsumersForRemoval = (local, remote) => { | ||
remote.forEach(consumer => { | ||
let found = local.find(c => c.username === consumer.username); | ||
if (found) { | ||
found.credentials = found.credentials ? found.credentials : []; | ||
markCredentialsForRemoval(found.credentials, consumer.credentials); | ||
} else { | ||
ensureRemoved(consumer); | ||
local.unshift(consumer); | ||
} | ||
}); | ||
}; | ||
|
||
const markTargetsForRemoval = (local, remote) => { | ||
remote.forEach(target => { | ||
if (!local.find(t => t.target === target.target)) { | ||
ensureRemoved(target); | ||
local.unshift(target); | ||
} | ||
}); | ||
}; | ||
|
||
const markUpstreamsForRemoval = (local, remote) => { | ||
remote.forEach(upstream => { | ||
let found = local.find(u => u.name === upstream.name); | ||
if (found) { | ||
found.targets = found.targets ? found.targets : []; | ||
markTargetsForRemoval(found.targets, upstream.targets); | ||
} else { | ||
ensureRemoved(upstream); | ||
local.unshift(upstream); | ||
} | ||
}); | ||
}; | ||
|
||
export default (local, remote, ignoreConsumers) => { | ||
const localKeys = Object.keys(local); | ||
const remoteKeys = Object.keys(remote); | ||
|
||
local = clone(local); | ||
remote = clone(remote); | ||
|
||
rootKeys.forEach(key => { | ||
local[key] = localKeys.includes(key) ? local[key] : []; | ||
remote[key] = remoteKeys.includes(key) ? remote[key] : []; | ||
}); | ||
|
||
markApisForRemoval(local.apis, remote.apis); | ||
markPluginsForRemoval(local.plugins, remote.plugins); | ||
if (!ignoreConsumers) { | ||
markConsumersForRemoval(local.consumers, remote.consumers); | ||
} | ||
markUpstreamsForRemoval(local.upstreams, remote.upstreams); | ||
|
||
return local; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
{ | ||
"apis": [ | ||
{ | ||
"name": "placement-service", | ||
"plugins": [] | ||
}, | ||
{ | ||
"name": "testAPI1", | ||
"plugins": [ | ||
{ | ||
"name": "request-termination" | ||
}, | ||
{ | ||
"name": "key-auth" | ||
} | ||
] | ||
},{ | ||
"name": "lookup-service", | ||
"plugins": [] | ||
}, | ||
{ | ||
"name": "testAPI2", | ||
"plugins": [ | ||
{ | ||
"name": "key-auth" | ||
} | ||
] | ||
} | ||
], | ||
"consumers": [ | ||
{ | ||
"username": "testConsumer1", | ||
"credentials": [ | ||
{ | ||
"name": "key-auth", | ||
"attributes": { | ||
"key": "1234A" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"username": "testConsumer2", | ||
"credentials": [ | ||
{ | ||
"name": "basic-auth", | ||
"attributes": { | ||
"username": "jdoe" | ||
} | ||
}, | ||
{ | ||
"name": "key-auth", | ||
"attributes": { | ||
"key": "5678B" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"username": "anonymous", | ||
"credentials": [ | ||
{ | ||
"name": "key-auth", | ||
"attributes": { | ||
"key": "9012C" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"username": "iosapp", | ||
"credentials": [ | ||
{ | ||
"name": "key-auth", | ||
"attributes": { | ||
"key": "3456D" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"username": "android", | ||
"credentials": [ | ||
{ | ||
"name": "key-auth", | ||
"attributes": { | ||
"key": "7890E" | ||
} | ||
}, | ||
{ | ||
"name": "key-auth", | ||
"attributes": { | ||
"key": "1234F" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"username": "testConsumer3", | ||
"credentials": [ | ||
{ | ||
"name": "oauth2", | ||
"attributes": { | ||
"client_id": "5678G" | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"plugins": [ | ||
{ | ||
"name": "syslog" | ||
}, | ||
{ | ||
"name": "basic-auth" | ||
}, | ||
{ | ||
"name": "key-auth" | ||
}, | ||
{ | ||
"name": "response-transformer" | ||
}, | ||
{ | ||
"name": "oauth2" | ||
} | ||
], | ||
"upstreams": [ | ||
{ | ||
"name": "lookupService", | ||
"targets": [ | ||
{ | ||
"target": "app.lookup.service.mydomain.io:8080" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "testUpstream1", | ||
"targets": [ | ||
{ | ||
"target": "test.target1.com:8080" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "testUpstream2", | ||
"targets": [ | ||
{ | ||
"target": "test.target2a.com:8080" | ||
}, | ||
{ | ||
"target": "test.target2b.com:8080" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.