-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDED: Import and export command, no tests provided (#9)
* ADDED: Import and export command, no tests provided * ADDED: Tests * FIXED: Dependency * ADDED: Change message * RELEASE: 2.3.1 * FIXED: Dependency
- Loading branch information
Showing
8 changed files
with
281 additions
and
3 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,36 @@ | ||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const get = require('lodash.get'); | ||
const fs = require('fs'); | ||
|
||
const { log } = require('../../utils/logger'); | ||
|
||
const makeExport = ({ settingsService, parameterStore }) => { | ||
return (filePath) => settingsService.getSettings() | ||
.tap(() => log(chalk.white(`Getting parameters..`))) | ||
.then(settings => { | ||
const secretsPromise = parameterStore.getParameters({ | ||
parameterNames: get(settings, 'secretParameters') | ||
}); | ||
|
||
const configsPromise = parameterStore.getParameters({ | ||
parameterNames: get(settings, 'configParameters') | ||
}); | ||
|
||
return Promise.all([ secretsPromise, configsPromise ]); | ||
}) | ||
.tap(() => log(chalk.white(`Saving parameters to: ${filePath}`))) | ||
.then(([ secrets, configs ]) => { | ||
const writeOutput = { | ||
configs, | ||
secrets, | ||
}; | ||
|
||
fs.writeFileSync(filePath, JSON.stringify(writeOutput, null, 2)); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
makeExport, | ||
}; |
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,62 @@ | ||
const { makeExport } = require('./make-export'); | ||
|
||
const Bluebird = require('bluebird'); | ||
|
||
const fs = require('fs'); | ||
|
||
jest.mock('fs', () => ({ | ||
writeFileSync: jest.fn(), | ||
})); | ||
|
||
describe('export', () => { | ||
let exportFunction; | ||
const getSettingsMock = jest.fn(); | ||
const getParametersMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
exportFunction = makeExport({ | ||
settingsService: { | ||
getSettings: getSettingsMock, | ||
}, | ||
parameterStore: { | ||
getParameters: getParametersMock, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should write to the specified file path', () => { | ||
getSettingsMock.mockImplementation(() => Bluebird.resolve({ | ||
secretParameters: [ | ||
'path/to/secret1', | ||
], | ||
configParameters: [ | ||
'path/to/config1', | ||
] | ||
})); | ||
|
||
getParametersMock | ||
.mockImplementationOnce(() => Bluebird.resolve({ | ||
'path/to/secret1': 'secret1Value', | ||
})) | ||
.mockImplementationOnce(() => Bluebird.resolve({ | ||
'path/to/config1': 'config1Value', | ||
})); | ||
|
||
const filePath = './test.json'; | ||
|
||
return exportFunction(filePath) | ||
.then(() => { | ||
const writeOutput = { | ||
configs: { | ||
'path/to/config1': 'config1Value', | ||
}, | ||
secrets: { | ||
'path/to/secret1': 'secret1Value', | ||
}, | ||
}; | ||
|
||
expect(fs.writeFileSync) | ||
.toHaveBeenCalledWith(filePath, JSON.stringify(writeOutput, null, 2)) | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const get = require('lodash.get'); | ||
const fs = require('fs'); | ||
const mapKeys = require('lodash.mapkeys'); | ||
|
||
const { log } = require('../../utils/logger') | ||
|
||
const configurationImport = (parameterStore, settings, importedConfig) => { | ||
const path = get(settings, 'config.path') | ||
|
||
const configurationParameters = mapKeys(importedConfig, (_, key) => { | ||
return `${path}/${key}`; | ||
}); | ||
|
||
return parameterStore.updateConfigs({ | ||
parameters: configurationParameters | ||
}); | ||
}; | ||
|
||
const secretImport = (parameterStore, settings, importedSecrets) => { | ||
const path = get(settings, 'secret.path'); | ||
|
||
const secretParameters = mapKeys(importedSecrets, (_, key) => { | ||
return `${path}/${key}`; | ||
}); | ||
|
||
return parameterStore.updateSecrets({ | ||
parameters: secretParameters | ||
}); | ||
}; | ||
|
||
const makeImport = ({ settingsService, parameterStore }) => { | ||
return (filePath) => settingsService.getSettings() | ||
.tap(() => log(chalk.white(`Getting parameters from: ${filePath}`))) | ||
.then(settings => { | ||
const importedExport = JSON.parse(fs.readFileSync(filePath)); | ||
|
||
return { settings, importedExport }; | ||
}) | ||
.then(({ settings, importedExport }) => { | ||
const importedConfig = get(importedExport, 'configs', {}); | ||
const importedSecrets = get(importedExport, 'secrets', {}); | ||
|
||
return Promise.all([ | ||
configurationImport(parameterStore, settings, importedConfig), | ||
secretImport(parameterStore, settings, importedSecrets), | ||
]); | ||
}) | ||
.tap(() => log(chalk.white(`Saved parameters to provider`))) | ||
}; | ||
|
||
module.exports = { | ||
makeImport, | ||
}; |
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,86 @@ | ||
const { makeImport } = require('./make-import'); | ||
|
||
const Bluebird = require('bluebird'); | ||
|
||
const fs = require('fs'); | ||
|
||
jest.mock('fs', () => ({ | ||
readFileSync: jest.fn(), | ||
})); | ||
|
||
describe('import', () => { | ||
let importFunction; | ||
const getSettingsMock = jest.fn(); | ||
const updateConfigsMock = jest.fn(); | ||
const updateSecretsMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
importFunction = makeImport({ | ||
settingsService: { | ||
getSettings: getSettingsMock, | ||
}, | ||
parameterStore: { | ||
updateConfigs: updateConfigsMock, | ||
updateSecrets: updateSecretsMock, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should write to the specified file path', () => { | ||
const readResult = { | ||
configs: { | ||
'path/to/config1': 'config1Value', | ||
}, | ||
secrets: { | ||
'path/to/secret1': 'secret1Value', | ||
}, | ||
}; | ||
|
||
getSettingsMock.mockImplementation(() => Bluebird.resolve({ | ||
config: { | ||
path: 'service/path', | ||
}, | ||
secret: { | ||
path: 'service/path', | ||
}, | ||
secretParameters: [ | ||
'path/to/secret1', | ||
], | ||
configParameters: [ | ||
'path/to/config1', | ||
] | ||
})); | ||
|
||
updateConfigsMock | ||
.mockImplementation(() => Bluebird.resolve()); | ||
|
||
updateSecretsMock | ||
.mockImplementation(() => Bluebird.resolve()); | ||
|
||
fs | ||
.readFileSync | ||
.mockImplementation(() => JSON.stringify(readResult, null, 2)); | ||
|
||
const filePath = './test.json'; | ||
|
||
return importFunction(filePath) | ||
.then(() => { | ||
expect(fs.readFileSync) | ||
.toHaveBeenCalledWith(filePath); | ||
|
||
expect(updateConfigsMock) | ||
.toHaveBeenCalledWith({ | ||
parameters: { | ||
'service/path/path/to/config1': 'config1Value' | ||
} | ||
}) | ||
|
||
expect(updateSecretsMock) | ||
.toHaveBeenCalledWith({ | ||
parameters: { | ||
'service/path/path/to/secret1': 'secret1Value', | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "oprah", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "Package to deploy parameters to AWS", | ||
"repository": "https://github.com/ACloudGuru/oprah.git", | ||
"author": "subash adhikari <[email protected]>", | ||
|