-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,3 +69,4 @@ dist | |
|
||
# output terraform config files | ||
backend.tf | ||
provider.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./createS3Backend"; | ||
export * from "./createAwsProvider"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |