Skip to content

Commit

Permalink
Merge 4aa90a0 into 27dcd1b
Browse files Browse the repository at this point in the history
  • Loading branch information
keitakn committed Jan 11, 2019
2 parents 27dcd1b + 4aa90a0 commit 5e61f0a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ dist

# output terraform config files
backend.tf
provider.tf
45 changes: 45 additions & 0 deletions src/createAwsProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { promisify } from "util";
import fs from "fs";

interface IProvider {
version: string;
region: string;
profile: string;
alias?: string;
}

interface ICreateAwsProviderParams {
outputPath: string;
awsProviderParams: IProvider[];
}

const createProvider = (provider: IProvider): string => {
return `provider "aws" {
version = "${provider.version}"
region = "${provider.region}"
profile = "${provider.profile}"
}`;
};

const createProviderWithAlias = (provider: IProvider): string => {
return `provider "aws" {
version = "${provider.version}"
region = "${provider.region}"
profile = "${provider.profile}"
alias = "${provider.alias}"
}`;
};

export const createAwsProvider = async (params: ICreateAwsProviderParams) => {
const writeFile = promisify(fs.writeFile);
let outputString = "";

params.awsProviderParams.map((provider: IProvider) => {
outputString +=
provider.alias === undefined
? `${createProvider(provider)}\n\n`
: `${createProviderWithAlias(provider)}\n\n`;
});

await writeFile(`${params.outputPath}provider.tf`, outputString);
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./createS3Backend";
export * from "./createAwsProvider";
42 changes: 42 additions & 0 deletions test/integration/createAwsProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createAwsProvider } from "../../src/index";
import fs from "fs";

describe("createAwsProvider.integrationTest", () => {
it("should be able to create a provider.tf", async () => {
const params = {
outputPath: "./",
awsProviderParams: [
{
version: "=1.54.0",
region: "ap-northeast-1",
profile: "nekochans-dev"
},
{
version: "=1.54.0",
region: "us-east-1",
profile: "nekochans-dev",
alias: "us_east_1"
}
]
};

const expected = `provider "aws" {
version = "=1.54.0"
region = "ap-northeast-1"
profile = "nekochans-dev"
}
provider "aws" {
version = "=1.54.0"
region = "us-east-1"
profile = "nekochans-dev"
alias = "us_east_1"
}\n\n`;

await createAwsProvider(params);

const resultString = fs.readFileSync("./provider.tf").toString();

expect(resultString).toEqual(expected);
});
});

0 comments on commit 5e61f0a

Please sign in to comment.