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

feat: Account detection by environment #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/project/project-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Account } from './interfaces';
import { Project } from './project';
import { addError } from '../error';
import { get } from '../utils/get';
import { isNonEmptyString } from '../utils/isNonEmptyString';

export class ProjectContext {
/**
Expand Down Expand Up @@ -66,6 +67,14 @@ export class ProjectContext {
}

static getEnvironment(scope: Construct): string {
const environmentType = ProjectContext.tryGetEnvironment(scope);
const tryAccountType: string = ProjectContext.getAccountType(scope);
if (isNonEmptyString(environmentType) && tryAccountType == '') {
const accountType = ProjectContext._getAccountTypeByEnvironment(scope, environmentType);
if (isNonEmptyString(accountType)) {
AccountType.set(scope, accountType);
}
}
const allowedEnvironments = ProjectContext.getAllowedEnvironments(scope);
return EnvironmentType.get(scope, allowedEnvironments);
}
Expand Down
8 changes: 7 additions & 1 deletion test/helpers/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as route53 from 'aws-cdk-lib/aws-route53';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import { Construct } from 'constructs';
import { Project, EnvironmentWrapper, SmartStack, EC, Name, PathName, UrlName, ProjectProps, AC } from '../../src';
import { Project, EnvironmentWrapper, SmartStack, EC, Name, PathName, UrlName, ProjectProps, AC, AccountWrapper } from '../../src';


export class MyStack extends SmartStack {
Expand Down Expand Up @@ -52,6 +52,12 @@ export class MyStack extends SmartStack {
}
}

export class Account extends AccountWrapper {
constructor(scope: Construct) {
super(scope);
}
}

export class Environment extends EnvironmentWrapper {
constructor(scope: Construct) {
super(scope);
Expand Down
83 changes: 83 additions & 0 deletions test/wrapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Account, Environment } from './helpers/app';
import { AccountStrategy, PC, Project } from '../src';


describe('Account and Environment wrappers', () => {

const props = {
name: 'my-cool-project',
author: {
organization: 'Acme Corp',
name: 'Mad Scientists',
email: '[email protected]',
},
defaultRegion: 'eu-west-1',
accounts: AccountStrategy.two({
dev: {
id: '111111111111',
},
prod: {
id: '222222222222',
},
}),
};

describe('AccountWrapper', () => {

test('dev/account', () => {
const project = new Project(props);
project.node.setContext('account-type', 'dev');
const account = new Account(project);
expect(PC.getAccountType(account)).toBe('dev');
});

test('prod/account', () => {
const project = new Project(props);
project.node.setContext('account-type', 'prod');
const account = new Account(project);
expect(PC.getAccountType(account)).toBe('prod');
});

});

describe('EnvironmentWrapper', () => {

['development', 'feature/foo-bar', 'test', 'staging'].forEach((env) => {
test('dev/environment/with-account', () => {
const project = new Project(props);
project.node.setContext('account-type', 'dev');
project.node.setContext('environment-type', env);
const environment = new Environment(project);
expect(PC.getAccountType(environment)).toBe('dev');
expect(PC.getEnvironment(environment)).toBe(env);
});

test('dev/environment/no-account', () => {
const project = new Project(props);
project.node.setContext('environment-type', env);
const environment = new Environment(project);
expect(PC.getAccountType(environment)).toBe('dev');
expect(PC.getEnvironment(environment)).toBe(env);
});
});

['preproduction', 'production'].forEach((env) => {
test('prod/environment/with-account', () => {
const project = new Project(props);
project.node.setContext('account-type', 'prod');
project.node.setContext('environment-type', env);
const environment = new Environment(project);
expect(PC.getAccountType(environment)).toBe('prod');
expect(PC.getEnvironment(environment)).toBe(env);
});

test('prod/environment/no-account', () => {
const project = new Project(props);
project.node.setContext('environment-type', env);
const environment = new Environment(project);
expect(PC.getAccountType(environment)).toBe('prod');
expect(PC.getEnvironment(environment)).toBe(env);
});
});
});
});
Loading