Skip to content

Commit 73c6f73

Browse files
authored
refactor: evaluate variables during parsing insead of iacStack (#16)
refactor: evaluate variables during parsing instead of iacStack - [x] tags - [x] events - [x] functions - [x] databases - [x] service - [x] version - [x] provider - [x] stages Refs: --------- Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent 1753652 commit 73c6f73

File tree

16 files changed

+349
-274
lines changed

16 files changed

+349
-274
lines changed

src/commands/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const deploy = async (
88
) => {
99
const context = constructActionContext({ ...options, stackName });
1010
logger.info('Validating yaml...');
11-
const iac = parseYaml(context.iacLocation);
11+
const iac = parseYaml(context);
1212
logger.info('Yaml is valid! 🎉');
1313

1414
logger.info('Deploying stack...');

src/commands/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const template = (
99
options: { format: TemplateFormat; location: string; stage: string | undefined },
1010
) => {
1111
const context = constructActionContext({ ...options, stackName });
12-
const iac = parseYaml(context.iacLocation);
12+
const iac = parseYaml(context);
1313
const { template } = generateStackTemplate(stackName, iac, context);
1414

1515
const output =

src/commands/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { parseYaml } from '../parser';
33

44
export const validate = (location: string | undefined, stage: string | undefined) => {
55
const context = constructActionContext({ location, stage });
6-
parseYaml(context.iacLocation);
6+
parseYaml(context);
77
logger.info('Yaml is valid! 🎉');
88
logger.debug('Yaml is valid! debug🎉');
99
};

src/parser/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync, readFileSync } from 'node:fs';
2-
import { ServerlessIac, ServerlessIacRaw } from '../types';
2+
import { ActionContext, ServerlessIac, ServerlessIacRaw } from '../types';
33
import { parseFunction } from './functionParser';
44
import { parseEvent } from './eventParser';
55
import { parseDatabase } from './databaseParser';
@@ -27,10 +27,10 @@ const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
2727
};
2828
};
2929

30-
export const parseYaml = (yamlPath: string): ServerlessIac => {
31-
validateExistence(yamlPath);
30+
export const parseYaml = (context: ActionContext): ServerlessIac => {
31+
validateExistence(context.iacLocation);
3232

33-
const yamlContent = readFileSync(yamlPath, 'utf8');
33+
const yamlContent = readFileSync(context.iacLocation, 'utf8');
3434
const iacJson = parse(yamlContent) as ServerlessIacRaw;
3535

3636
validateYaml(iacJson);

src/parser/tagParser.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { TagDomain, Tags } from '../types';
2-
import { isEmpty } from 'lodash';
32

4-
export const parseTag = (tags?: Tags): Array<TagDomain> => {
5-
const tagList = [{ key: 'iac-provider', value: 'ServerlessInsight' }];
6-
if (isEmpty(tags)) return tagList;
7-
8-
return [...tagList, ...Object.entries(tags).map(([key, value]) => ({ key, value }))];
3+
export const parseTag = (tags: Tags | undefined): Array<TagDomain> => {
4+
return [
5+
{ key: 'iac-provider', value: 'ServerlessInsight' },
6+
...Object.entries(tags ?? {}).map(([key, value]) => ({ key, value })),
7+
];
98
};

src/stack/deploy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as ros from '@alicloud/ros-cdk-core';
22
import { ActionContext, ServerlessIac } from '../types';
33
import { logger, rosStackDeploy } from '../common';
4-
import { IacStack } from './iacStack';
4+
import { RosStack } from './rosStack/rosStack';
55

66
export const generateStackTemplate = (
77
stackName: string,
88
iac: ServerlessIac,
99
context: ActionContext,
1010
) => {
1111
const app = new ros.App();
12-
new IacStack(app, iac, context);
12+
new RosStack(app, iac, context);
1313

1414
const assembly = app.synth();
1515
const stackArtifact = assembly.getStackByName(stackName);

src/stack/iacStack.ts

Lines changed: 0 additions & 255 deletions
This file was deleted.

src/stack/rosStack/database.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as ros from '@alicloud/ros-cdk-core';
2+
import { replaceReference } from '../../common';
3+
import { ActionContext, DatabaseDomain, DatabaseEngineMode, DatabaseEnum } from '../../types';
4+
import { isEmpty } from 'lodash';
5+
import * as esServerless from '@alicloud/ros-cdk-elasticsearchserverless';
6+
7+
export const resolveDatabases = (
8+
scope: ros.Construct,
9+
databases: Array<DatabaseDomain> | undefined,
10+
context: ActionContext,
11+
) => {
12+
if (isEmpty(databases)) {
13+
return undefined;
14+
}
15+
databases!.forEach((db) => {
16+
if ([DatabaseEnum.ELASTICSEARCH_SERVERLESS].includes(db.type)) {
17+
new esServerless.App(
18+
scope,
19+
replaceReference(db.key, context),
20+
{
21+
appName: replaceReference(db.name, context),
22+
appVersion: db.version,
23+
authentication: {
24+
basicAuth: [
25+
{
26+
password: replaceReference(db.security.basicAuth.password, context),
27+
},
28+
],
29+
},
30+
quotaInfo: {
31+
cu: db.cu,
32+
storage: db.storageSize,
33+
appType: db.engineMode === DatabaseEngineMode.TIMESERIES ? 'TRIAL' : 'STANDARD',
34+
},
35+
// network: [
36+
// {
37+
// type: 'PUBLIC_KIBANA',
38+
// enabled: true,
39+
// whiteIpGroup: [{ groupName: 'default', ips: ['0.0.0.0/24'] }],
40+
// },
41+
// {
42+
// type: 'PUBLIC_ES',
43+
// enabled: true,
44+
// whiteIpGroup: [{ groupName: 'default', ips: ['0.0.0.0/24'] }],
45+
// },
46+
// ],
47+
},
48+
true,
49+
);
50+
}
51+
});
52+
};

0 commit comments

Comments
 (0)