All files / Nodejs/lib generateProfile.js

0% Statements 0/51
0% Branches 0/33
0% Functions 0/6
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                                 
"use strict";
 
let homeDir = require('os').homedir();
let path = require("path");
let fs = require("fs");
let extend = require("extend");
var aws = require("./leo-aws");
var async = require('async');
var crypto = require("crypto");
var moment = require("moment");
 
 
module.exports = function(stack, options, dir = null, callback) {
	let configPath;
	if (dir == null) {
		dir = homeDir;
		configPath = path.resolve(dir, ".leo/config.json");
	} else {
		configPath = path.resolve(dir, "leo_config.json");
	}
	let configDir = path.dirname(configPath);
 
	var credentials;
	if (options.awsprofile) {
		console.log("USING AWS PROFILE", options.awsprofile);
		credentials = new aws.SharedIniFileCredentials({
			profile: options.awsprofile
		});
	}
 
	var cloudformation = new aws.CloudFormation({
		region: options.region,
		credentials: credentials
	});
 
	cloudformation.listStackResources({
		StackName: stack
	}, function(err, data) {
		if (err) {
			console.log(err);
			return;
		}
		if (data.NextToken) {
			console.log("We need to deal with next token");
		}
		var resources = {};
		data.StackResourceSummaries.map((resource) => {
			resources[resource.LogicalResourceId] = {
				type: resource.ResourceType,
				id: resource.PhysicalResourceId,
				name: resource.LogicalResourceId
			};
		});
 
		let config = get();
		let profile = config[options.leoprofile || stack] = Object.assign({
			region: options.region,
			resources: {
				"Region": options.region
			},
			profile: options.awsprofile || undefined
		}, config[options.leoprofile || stack] || {});
		Object.keys(resources).forEach((id) => {
			if (id == "LeoKinesisStream") {
				profile.kinesis = resources[id].id;
			} else if (id == "LeoFirehoseStream") {
				profile.firehose = resources[id].id;
			} else if (id == "LeoS3") {
				profile.s3 = resources[id].id;
			}
			if (resources[id].type.match(/Table|Bucket|DeliveryStream|Stream/)) {
				profile.resources[id] = resources[id].id;
			}
		});
 
		//if (!config.default) {
		//	config.default = profile;
		//}
		fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
		console.log("Profile Saved");
		callback(null);
	});
 
	function get() {
		createPath(configDir);
		let config = {};
 
		if (fs.existsSync(configPath)) {
			config = require(configPath) || {};
		}
		return config;
	}
 
	function createPath(dir) {
		if (!fs.existsSync(dir)) {
			var parent = path.dirname(dir);
			if (parent) {
				createPath(parent);
			}
			fs.mkdirSync(dir);
		}
	}
 
}