<commit_before>'use strict';
/*
	Automatically configure microservice in AWS
	Copyright (C) 2017 <NAME>
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

var archiver = require('archiver');
var aws = require('aws-sdk');
var exec = require('child_process').exec;
var fs = require('fs-extra');
var path = require('path');
var os = require('os');
var uuid = require('uuid');
var Api = require('openapi-factory');
var http = require('http');
var _ = require('lodash');

var Server = require('./lib/server');
var ApiGatewayManager = require('./lib/ApiGatewayManager');
var DynamoDbManager = require('./lib/DynamoDbManager');
var LambdaManager = require('./lib/LambdaManager');
var ApiConfiguration = require('./lib/ApiConfiguration');
var BucketManager = require('./lib/BucketManager');
var IamManager = require('./lib/IamManager');
let CloudFormationDeployer = require('./lib/CloudFormationDeployer');
let LockFinder = require('./lib/lockFinder');

function AwsArchitect(packageMetadata, apiOptions, contentOptions) {
	this.PackageMetadata = packageMetadata;
	this.ContentOptions = contentOptions || {};
	this.SourceDirectory = (apiOptions || {}).sourceDirectory;
	this.UseCloudFormation = (apiOptions || {}).useCloudFormation;

	this.GetApi = () => {
		var indexPath = path.join(this.SourceDirectory, 'index.js');
		try {
			fs.accessSync(indexPath);
		} catch (innerException) {
			return new Api();
		}

		try {
			return require(indexPath);
		} catch (exception) {
			throw { title: 'Failed to expand index.js.', error: exception };
		}
	};
	this.Configuration = new ApiConfiguration(apiOptions, 'index.js', aws.config.region || 'us-east-1');

	if(this.Configuration.Regions.length === 0) { throw new Error('A single region must be defined in the apiOptions.'); }
	if(this.Configuration.Regions.length > 1) { throw new Error('Only deployments to a single region are allowed at this time.'); }
	this.Region = this.Configuration.Regions[0];

	var apiGatewayFactory = new aws.APIGateway({region: this.Region});
	this.ApiGatewayManager = new ApiGatewayManager(this.PackageMetadata.name, this.PackageMetadata.version, apiGatewayFactory);

	var lambdaFactory = new aws.Lambda({region: this.Region});
	this.LambdaManager = new LambdaManager(this.PackageMetadata.name, lambdaFactory, this.Configuration);

	var dynamoDbFactory = new aws.DynamoDB({region: this.Region});
	this.DynamoDbManager = new DynamoDbManager(this.PackageMetadata.name, dynamoDbFactory);

	var s3Factory = new aws.S3({region: this.Region});
	this.BucketManager = new BucketManager(s3Factory, this.ContentOptions.bucket);

	var iamFactory = new aws.IAM({region: this.Region})
	this.IamManager = new IamManager(iamFactory, null, this.UseCloudFormation);

	let cloudFormationClient = new aws.CloudFormation({ region: this.Region });
	this.CloudFormationDeployer = new CloudFormationDeployer(cloudFormationClient);<commit_msg>Default fallback region to one in aws configuration.<commit_after>'use strict';
/*
	Automatically configure microservice in AWS
	Copyright (C) 2017 <NAME>
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

var archiver = require('archiver');
var aws = require('aws-sdk');
var exec = require('child_process').exec;
var fs = require('fs-extra');
var path = require('path');
var os = require('os');
var uuid = require('uuid');
var Api = require('openapi-factory');
var http = require('http');
var _ = require('lodash');

var Server = require('./lib/server');
var ApiGatewayManager = require('./lib/ApiGatewayManager');
var DynamoDbManager = require('./lib/DynamoDbManager');
var LambdaManager = require('./lib/LambdaManager');
var ApiConfiguration = require('./lib/ApiConfiguration');
var BucketManager = require('./lib/BucketManager');
var IamManager = require('./lib/IamManager');
let CloudFormationDeployer = require('./lib/CloudFormationDeployer');
let LockFinder = require('./lib/lockFinder');

function AwsArchitect(packageMetadata, apiOptions, contentOptions) {
	this.PackageMetadata = packageMetadata;
	this.ContentOptions = contentOptions || {};
	this.SourceDirectory = (apiOptions || {}).sourceDirectory;
	this.UseCloudFormation = (apiOptions || {}).useCloudFormation;

	this.GetApi = () => {
		var indexPath = path.join(this.SourceDirectory, 'index.js');
		try {
			fs.accessSync(indexPath);
		} catch (innerException) {
			return new Api();
		}

		try {
			return require(indexPath);
		} catch (exception) {
			throw { title: 'Failed to expand index.js.', error: exception };
		}
	};
	if (aws.config.region == null && apiOptions.regions && apiOptions.regions[0]) {
		aws.config.update({ region: apiOptions.regions[0] });
	}
	this.Configuration = new ApiConfiguration(apiOptions, 'index.js', aws.config.region || 'us-east-1');

	if(this.Configuration.Regions.length === 0) { throw new Error('A single region must be defined in the apiOptions.'); }
	if(this.Configuration.Regions.length > 1) { throw new Error('Only deployments to a single region are allowed at this time.'); }
	this.Region = this.Configuration.Regions[0];

	var apiGatewayFactory = new aws.APIGateway({region: this.Region});
	this.ApiGatewayManager = new ApiGatewayManager(this.PackageMetadata.name, this.PackageMetadata.version, apiGatewayFactory);

	var lambdaFactory = new aws.Lambda({region: this.Region});
	this.LambdaManager = new LambdaManager(this.PackageMetadata.name, lambdaFactory, this.Configuration);

	var dynamoDbFactory = new aws.DynamoDB({region: this.Region});
	this.DynamoDbManager = new DynamoDbManager(this.PackageMetadata.name, dynamoDbFactory);

	var s3Factory = new aws.S3({region: this.Region});
	this.BucketManager = new BucketManager(s3Factory, this.ContentOptions.bucket);

	var iamFactory = new aws.IAM({region: this.Region})
	this.IamManager = new IamManager(iamFactory, null, this.UseCloudFormation);

	let cloudFormationClient = new aws.CloudFormation({ region: this.Region });
	this.CloudFormationDeployer = new CloudFormationDeployer(cloudFormationClient);
{'fraction_non_alphanumeric': 0.12375193362396288, 'fraction_numerical': 0.003656307129798903, 'mean_word_length': 5.26056338028169, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2662735', 'n_tokens_mistral': 2085, 'n_tokens_neox': 2043, 'n_words': 610}