Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#274] Add project name to the IAM group names #275

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/commands/generate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('Generator command', () => {
describe('given valid options', () => {
describe('given provider is AWS', () => {
describe('given infrastructure type is blank', () => {
const projectDir = 'aws-blank-test';
const originalDirectoryName = 'AWS blank test';
const processedDirectoryName = 'aws-blank-test';
const stdoutSpy = jest.spyOn(process.stdout, 'write');

beforeAll(async () => {
Expand All @@ -23,18 +24,18 @@ describe('Generator command', () => {
terraformCloudEnabled: false,
});

await Generator.run([projectDir]);
await Generator.run([originalDirectoryName]);
});

afterAll(() => {
jest.clearAllMocks();
remove('/', projectDir);
remove('/', processedDirectoryName);
});

it('creates expected directories', () => {
const expectedDirectories = ['core/', 'shared/'];

expect(projectDir).toHaveDirectories(expectedDirectories);
expect(processedDirectoryName).toHaveDirectories(expectedDirectories);
});

it('creates expected files', () => {
Expand All @@ -51,7 +52,7 @@ describe('Generator command', () => {
'shared/outputs.tf',
];

expect(projectDir).toHaveFiles(expectedFiles);
expect(processedDirectoryName).toHaveFiles(expectedFiles);
});

it('displays the success message', () => {
Expand All @@ -63,6 +64,17 @@ describe('Generator command', () => {
it('calls postProcess hook', () => {
expect(postProcess).toHaveBeenCalledTimes(1);
});

it('contains processed project name in main files', () => {
const mainFiles = ['shared/main.tf', 'core/main.tf'];
mainFiles.forEach((fileName) => {
expect(processedDirectoryName).toHaveContentInFile(
fileName,
`project_name = "${processedDirectoryName}"`,
{ ignoreSpaces: true }
);
});
});
});

describe('given infrastructure type is advanced', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export default class Generator extends Command {
const { args } = await this.parse(Generator);

const generalPrompt = await prompt<GeneralOptions>([...providerChoices]);

const projectName = args.projectName.toLowerCase().replace(/\s/g, '-');
const generalOptions: GeneralOptions = {
projectName: args.projectName,
projectName: projectName,
provider: generalPrompt.provider,
};

Expand Down
2 changes: 2 additions & 0 deletions src/generators/addons/aws/modules/core/iamUserAndGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const iamVariablesContent = dedent`
const iamGroupsModuleContent = dedent`
module "iam_groups" {
source = "../modules/iam_groups"

project_name = local.project_name
}`;

const iamUsersModuleContent = dedent`
Expand Down
4 changes: 2 additions & 2 deletions src/generators/terraform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const applyTerraformCore = async (generalOptions: GeneralOptions) => {

copy('terraform/', '.', projectName);

// Use projectName to append the Namespace local in the main.tf file
const coreLocalsContent = dedent`
locals {
env_namespace = "${projectName}-\${var.environment}"
project_name = "${projectName}"
env_namespace = "\${local.project_name}-\${var.environment}"
}`;

appendToFile(INFRA_CORE_MAIN_PATH, coreLocalsContent, projectName);
Expand Down
6 changes: 3 additions & 3 deletions templates/addons/aws/modules/iam_groups/main.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#tfsec:ignore:aws-iam-enforce-group-mfa
resource "aws_iam_group" "admin" {
name = "Admin-group"
name = "${var.project_name}-admin-group"
}

#tfsec:ignore:aws-iam-enforce-group-mfa
resource "aws_iam_group" "infra-service-account" {
name = "Infra-service-account-group"
name = "${var.project_name}-infra-service-account-group"
}

#tfsec:ignore:aws-iam-enforce-group-mfa
resource "aws_iam_group" "developer" {
name = "Developer-group"
name = "${var.project_name}-developer-group"
}

resource "aws_iam_group_policy_attachment" "admin_access" {
Expand Down
4 changes: 4 additions & 0 deletions templates/addons/aws/modules/iam_groups/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "project_name" {
description = "The name of the project"
type = string
}
Loading