aws-parameters-store
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -43,17 +43,38 @@ "use strict"; | ||
}; | ||
let nextToken = ''; | ||
let res; | ||
const params = {}; | ||
while (nextToken !== undefined) { | ||
try { | ||
res = yield ssmPaginatedCall(ssm, getParamsByPathConf); | ||
} | ||
catch (err) { | ||
throw (`Failed fetching parameters for region: ${options.region} & namespace: ${ssmNamespace}. Error: ${err}`); | ||
} | ||
nextToken = res.NextToken; | ||
Object.assign(getParamsByPathConf, { NextToken: nextToken }); | ||
for (const parameter of res.Parameters || []) { | ||
splitSetParam(params, parameter); | ||
} | ||
} | ||
return params; | ||
function splitSetParam(params, parameter) { | ||
const paramName = parameter.Name ? parameter.Name.replace(ssmNamespace, '') : null; | ||
const paramValue = parameter.Value; | ||
if (paramName) { | ||
const splitedParamName = paramName.split('/'); | ||
lodash_set_1.default(params, splitedParamName, paramValue); | ||
} | ||
} | ||
}); | ||
} | ||
exports.default = getParams; | ||
function ssmPaginatedCall(ssm, params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise(function (resolve, reject) { | ||
ssm.getParametersByPath(getParamsByPathConf, function (err, data) { | ||
ssm.getParametersByPath(params, function (err, data) { | ||
if (err) | ||
reject(`Failed fetching parameters for region: ${options.region} & namespace: ${ssmNamespace}. Error: ${err}`); | ||
reject(err); | ||
else { | ||
const res = {}; | ||
for (const parameter of data.Parameters || []) { | ||
const paramName = parameter.Name ? parameter.Name.replace(ssmNamespace, '') : null; | ||
const paramValue = parameter.Value; | ||
if (paramName) { | ||
const splitedParamName = paramName.split('/'); | ||
lodash_set_1.default(res, splitedParamName, paramValue); | ||
} | ||
} | ||
resolve(res); | ||
resolve(data); | ||
} | ||
@@ -64,2 +85,1 @@ }); | ||
} | ||
exports.default = getParams; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,4 +14,15 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// import getParams from "."; | ||
const _1 = __importDefault(require(".")); | ||
// let paramsStoreConf = {}; | ||
const region = 'us-east-1'; | ||
_1.default('/iceberg/cp-provisioning-api/apps-panther/feature__param_store_config-e799d64', { region }).then(data => { console.log(data); }); | ||
function fetchConfigFromAws() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield _1.default('/iceberg/cp-config-api/apps-panther/', { region }); | ||
}); | ||
} | ||
fetchConfigFromAws().then((paramsStoreConf) => { | ||
console.log(`Loaded configuration from AWS parameters store: ${JSON.stringify(paramsStoreConf)}`); | ||
}).catch((error) => { | ||
console.log(`Failed to load configuration from aws, Error caught: ${error}`); | ||
}); |
59
index.ts
import set from 'lodash.set'; | ||
import SSM from 'aws-sdk/clients/ssm'; | ||
import SSM, { Parameter } from 'aws-sdk/clients/ssm'; | ||
@@ -34,3 +34,2 @@ class getParamsByPathOptions { | ||
options = new getParamsByPathOptions(options.region, options.recursive, options.withDecryption) | ||
const ssmClientConfiguration: SSM.ClientConfiguration = { | ||
@@ -40,7 +39,4 @@ apiVersion: '2014-11-06', | ||
} | ||
const ssm = new SSM(ssmClientConfiguration); | ||
const ssmNamespace = namespace || '/'; | ||
const getParamsByPathConf = { | ||
@@ -51,23 +47,44 @@ Path: ssmNamespace, | ||
} | ||
let nextToken: string | undefined = ''; | ||
let res; | ||
const params = {} | ||
while (nextToken !== undefined) { | ||
try { | ||
res = await ssmPaginatedCall(ssm, getParamsByPathConf) | ||
} catch(err) { | ||
throw(`Failed fetching parameters for region: ${options.region} & namespace: ${ssmNamespace}. Error: ${err}`) | ||
} | ||
nextToken = res.NextToken | ||
Object.assign(getParamsByPathConf, { NextToken: nextToken }); | ||
for (const parameter of res.Parameters || []) { | ||
setParam(params, parameter); | ||
} | ||
} | ||
return params; | ||
function setParam(params: object, parameter: Parameter) { | ||
const paramName = parameter.Name ? parameter.Name.replace(ssmNamespace, '') : null; | ||
const paramValue = parameter.Value; | ||
if (paramName) { | ||
const splitedParamName = paramName.split('/'); | ||
set(params, splitedParamName, paramValue); | ||
} | ||
} | ||
} | ||
async function ssmPaginatedCall( ssm: SSM, params: SSM.Types.GetParametersByPathRequest): Promise<SSM.Types.GetParametersByPathResult> { | ||
return new Promise(function(resolve, reject) { | ||
ssm.getParametersByPath(getParamsByPathConf, function(err, data) { | ||
if (err) reject(`Failed fetching parameters for region: ${options.region} & namespace: ${ssmNamespace}. Error: ${err}`); | ||
ssm.getParametersByPath(params, function(err, data) { | ||
if (err) reject(err); | ||
else { | ||
const res = {}; | ||
for (const parameter of data.Parameters || []) { | ||
const paramName = parameter.Name ? parameter.Name.replace(ssmNamespace, '') : null; | ||
const paramValue = parameter.Value; | ||
if (paramName) { | ||
const splitedParamName = paramName.split('/'); | ||
set(res, splitedParamName, paramValue); | ||
} | ||
} | ||
resolve(res); | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
{ | ||
"name": "aws-parameters-store", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Loads parameters from aws parameter store to a json object", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
13
test.ts
@@ -0,5 +1,14 @@ | ||
// import getParams from "."; | ||
import getParams from "."; | ||
// let paramsStoreConf = {}; | ||
const region = 'us-east-1' | ||
getParams('/iceberg/cp-provisioning-api/apps-panther/feature__param_store_config-e799d64', { region }).then(data => {console.log(data)}); | ||
async function fetchConfigFromAws() { | ||
return await getParams('/iceberg/cp-config-api/apps-panther/', { region }) | ||
} | ||
fetchConfigFromAws().then((paramsStoreConf) => { | ||
console.log(`Loaded configuration from AWS parameters store: ${JSON.stringify(paramsStoreConf)}`); | ||
}).catch((error) => { | ||
console.log(`Failed to load configuration from aws, Error caught: ${error}`); | ||
}); |
12597
256