Skip to content

Commit

Permalink
patch(feat): octo-aws-cdk | ini based account module.
Browse files Browse the repository at this point in the history
  • Loading branch information
rash805115 committed Nov 29, 2024
1 parent 84ea238 commit dcf0a04
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 21 deletions.
1 change: 1 addition & 0 deletions dictionary.dic
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ iam
idx
igw
infima
ini
intra
Ipv
ispec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jest } from '@jest/globals';
import { App, type Container, TestContainer, TestModuleContainer, TestStateProvider } from '@quadnix/octo';
import { type Container, TestContainer, TestModuleContainer, TestStateProvider } from '@quadnix/octo';
import { AwsAccountModule } from './aws-account.module.js';
import { AddAccountModelAction } from './models/account/actions/add-account.model.action.js';

Expand Down Expand Up @@ -32,7 +32,7 @@ describe('AwsAccountModule UT', () => {
await testModuleContainer.runModule<AwsAccountModule>({
inputs: {
accountId: '1234',
app: '${testModule.model.app}',
app: '${{testModule.model.app}}',
},
moduleId: 'account',
type: AwsAccountModule,
Expand All @@ -41,10 +41,28 @@ describe('AwsAccountModule UT', () => {
await testModuleContainer.commit(app, { enableResourceCapture: true });

expect(addAccountModelActionSpy).toHaveBeenCalledTimes(1);

const actionInputs = addAccountModelActionSpy.mock.calls[0][1];
expect(actionInputs['account.input.accountId']).toBe('1234');
expect(actionInputs['account.input.app'] instanceof App).toBeTruthy();
expect(addAccountModelActionSpy.mock.calls[0][1]).toMatchInlineSnapshot(`
{
"inputs": {
"accountId": "1234",
"app": {
"context": "app=test-app",
"name": "test-app",
},
"iniProfile": "default",
},
"models": {
"account": {
"accountId": "1234",
"accountType": "aws",
"context": "account=1234,app=test-app",
"iniProfile": "default",
},
},
"overlays": {},
"resources": {},
}
`);
});

it('should create a new account', async () => {
Expand All @@ -57,7 +75,7 @@ describe('AwsAccountModule UT', () => {
await testModuleContainer.runModule<AwsAccountModule>({
inputs: {
accountId: '1234',
app: '${testModule.model.app}',
app: '${{testModule.model.app}}',
},
moduleId: 'account',
type: AwsAccountModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export class AwsAccountModuleSchema {
accountId = Schema<string>();

app = Schema<App>();

iniProfile? = Schema<string>('default');
}

@Module<AwsAccountModule>('@octo', AwsAccountModuleSchema)
Expand All @@ -13,7 +15,7 @@ export class AwsAccountModule extends AModule<AwsAccountModuleSchema, AwsAccount
const app = inputs.app;

// Create a new account.
const account = new AwsAccount(inputs.accountId);
const account = new AwsAccount(inputs.accountId, inputs.iniProfile!);
app.addAccount(account);

return account;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Action, ActionOutputs, Diff, DiffAction, Factory, IModelAction } from '@quadnix/octo';
import { AwsAccountModule, AwsAccountModuleSchema } from '../../../index.js';
import {
Action,
type ActionOutputs,
type Diff,
DiffAction,
type EnhancedModuleSchema,
Factory,
type IModelAction,
} from '@quadnix/octo';
import { AwsAccountModule } from '../../../aws-account.module.js';
import { AwsAccount } from '../aws.account.model.js';

@Action(AwsAccount)
Expand All @@ -15,7 +23,7 @@ export class AddAccountModelAction implements IModelAction<AwsAccountModule> {

async handle(
_diff: Diff,
_actionInputs: AwsAccountModuleSchema,
_actionInputs: EnhancedModuleSchema<AwsAccountModule>,
actionOutputs: ActionOutputs,
): Promise<ActionOutputs> {
return actionOutputs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { Account, Model } from '@quadnix/octo';
import { fromIni } from '@aws-sdk/credential-providers';
import { Account, AccountType, Model } from '@quadnix/octo';
import type { AwsCredentialIdentityProvider } from '@smithy/types';
import { AwsAccountSchema } from './aws.account.schema.js';

@Model<AwsAccount>('@octo', 'account', AwsAccountSchema)
export class AwsAccount extends Account {
constructor(accountId: string) {
super(accountId);
readonly iniProfile: string;

constructor(accountId: string, iniProfile: string) {
super(AccountType.AWS, accountId);

this.iniProfile = iniProfile;
}

override getCredentials(): AwsCredentialIdentityProvider {
return fromIni({ profile: this.iniProfile });
}

override synth(): AwsAccountSchema {
return {
accountId: this.accountId,
accountType: this.accountType,
iniProfile: this.iniProfile,
};
}

static override async unSynth(account: AwsAccountSchema): Promise<AwsAccount> {
return new AwsAccount(account.accountId);
return new AwsAccount(account.accountId, account.iniProfile);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AccountSchema } from '@quadnix/octo';
import { AccountSchema, Schema } from '@quadnix/octo';

export class AwsAccountSchema extends AccountSchema {}
export class AwsAccountSchema extends AccountSchema {
iniProfile = Schema<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ describe('AppModule UT', () => {
await testModuleContainer.commit(app);

expect(addAppModelActionSpy).toHaveBeenCalledTimes(1);
expect(addAppModelActionSpy.mock.calls[0][1]).toEqual({ 'app.input.name': 'test-app' });
expect(addAppModelActionSpy.mock.calls[0][1]).toMatchInlineSnapshot(`
{
"inputs": {
"name": "test-app",
},
"models": {
"app": {
"context": "app=test-app",
"name": "test-app",
},
},
"overlays": {},
"resources": {},
}
`);
});

it('should be able to add a new app', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { Action, type ActionOutputs, App, type Diff, DiffAction, Factory, type IModelAction } from '@quadnix/octo';
import type { AppModule, AppModuleSchema } from '../../../app.module.js';
import {
Action,
type ActionOutputs,
App,
type Diff,
DiffAction,
type EnhancedModuleSchema,
Factory,
type IModelAction,
} from '@quadnix/octo';
import type { AppModule } from '../../../app.module.js';

@Action(App)
export class AddAppModelAction implements IModelAction<AppModule> {
filter(diff: Diff): boolean {
return diff.action === DiffAction.ADD && (diff.node.constructor as typeof App).NODE_NAME === 'app';
}

async handle(_diff: Diff, _actionInputs: AppModuleSchema, actionOutputs: ActionOutputs): Promise<ActionOutputs> {
async handle(
_diff: Diff,
_actionInputs: EnhancedModuleSchema<AppModule>,
actionOutputs: ActionOutputs,
): Promise<ActionOutputs> {
return actionOutputs;
}
}
Expand Down

0 comments on commit dcf0a04

Please sign in to comment.