Skip to content
Open
67 changes: 67 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ async function saveParamsFile(config) {
}
}

async function getSharedConfigByService (config) {
const { service, env } = config

console.log(chalk.green(`Getting parameters from Config Table environment: "${env}" service: "${service}"`))

const results = await configWrapper.awsManager.getSharedConfigByService(env, service)

console.log(`Parameters under environment: "${env}" service: "${service}"\n`, JSON.stringify(results, null, 2))
}

async function putSharedConfigFromFile (config) {
const { infile, env, service } = config
console.log(chalk.green(`Reading params from file: ${infile}`))
const data = await fs.readFile(infile, 'utf8')

// Parse the JSON data
const jsonData = JSON.parse(data)

console.log(chalk.green(`Saving parameters to Config Table environment: "${env}" service: "${service}"`))
await configWrapper.awsManager.putSharedConfigRecordsByService(jsonData, env, service)
console.log(chalk.green(`Saved parameters to Config Table "environment ${env}" service: "${service}"`))
}

async function putToAWSFromFile(config) {
const { infile, env, service, overwrite, encrypt } = config
console.log(chalk.green(`Reading params from file: ${infile}`))
Expand Down Expand Up @@ -225,6 +248,50 @@ async function promptForMissingOptions(options) {
}
break
}
case 'getSharedConfigByService': {
commandFunc = getSharedConfigByService

if (!options.env) {
questions.push({
type: 'input',
name: 'env',
message: 'Environment: ',
default: 'develop'
})
}

if (!options.service) {
questions.push({
type: 'input',
name: 'service',
message: 'Service: ',
default: ''
})
}
break
}
case 'putSharedConfigFromFile': {
commandFunc = putSharedConfigFromFile

if (!options.env) {
questions.push({
type: 'input',
name: 'env',
message: 'Environment: ',
default: 'develop'
})
}

if (!options.service) {
questions.push({
type: 'input',
name: 'service',
message: 'Service: ',
default: ''
})
}
break
}
case 'putToAWSFromFile': {
commandFunc = putToAWSFromFile
if (!options.infile) {
Expand Down
90 changes: 89 additions & 1 deletion src/lib/awsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient()

let ssm

let configTable

const BASE_PATH = '/torc'

const cachedParams = {}
Expand Down Expand Up @@ -30,6 +33,20 @@ function restructureParam(param) {
return newParam
}

function mapSharedConfigParam (param) {
const newParam = {
name: param.name,
value: param.value,
service: param.service,
type: typeof param.value
// lastModifiedDate: param.lastModifiedDate
// fullName: param.name,
// version: param.Version,
}

return newParam
}

async function getParameter(env, service, paramName, isEncrypted) {
const Name = constructParamPath(env, service, paramName)
const params = {
Expand Down Expand Up @@ -80,6 +97,75 @@ async function getParametersByService(env, service, isEncrypted) {
return convertedParams
}

async function getSharedConfigByService (env, service) {
const Path = constructParamPath(env, service)
console.log(`Getting SharedConfig from "${service}" service`)

console.log(`Cache path: ${Path}`)
if (cachedParams[Path]) {
console.log('Found parameters in cache. Returning...')
return cachedParams[Path]
}

if (!configTable) {
configTable = await getParameter(env, 'common', 'DYNAMODB_CONFIG_TABLE', true)
}

const params = {
TableName: configTable.value,
IndexName: 'byService',
KeyConditionExpression: 'service = :service',
ExpressionAttributeValues: {
':service': service
}
}

const convertedParams = {}
let nexToken = null

do {
const records = await docClient.query(params).promise()

for (const record of records.Items) {
const param = mapSharedConfigParam(record)
convertedParams[param.name] = param
}

nexToken = records.LastEvaluatedKey || null

params.ExclusiveStartKey = nexToken
} while (nexToken)

cachedParams[Path] = convertedParams

return convertedParams
}

async function putSharedConfigRecordsByService (fileObj, env, service) {
if (!configTable) {
configTable = await getParameter(env, 'common', 'DYNAMODB_CONFIG_TABLE', true)
}

for (const key in fileObj) {
if (!fileObj[key].value) {
console.log(`Skipping ${key} no value provided.`)
continue
}
const params = {
TableName: configTable.value,
Item: {
name: key,
service,
value: fileObj[key].value
}
}

console.log('Create Config record params', JSON.stringify(params, null, 2))

await docClient.put(params).promise()
}
}

// TODO: add param caching
// TODO: add support for labels

Expand Down Expand Up @@ -216,9 +302,11 @@ module.exports = {
constructParamPath,
getParameter,
getParametersByService,
getSharedConfigByService,
putSharedConfigRecordsByService,
setParameter,
setParametersByService,
getEnvironments,
getServicesForEnvironment,
getAllOrgParams
}
}