Skip to content

Commit 56d367d

Browse files
committed
feat: setup es serverless schema
Signed-off-by: seven <[email protected]>
1 parent 8c925cf commit 56d367d

File tree

10 files changed

+219
-18
lines changed

10 files changed

+219
-18
lines changed

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@alicloud/openapi-client": "^0.4.12",
5353
"@alicloud/ros-cdk-apigateway": "^1.4.0",
5454
"@alicloud/ros-cdk-core": "^1.4.0",
55+
"@alicloud/ros-cdk-elasticsearchserverless": "^1.4.0",
5556
"@alicloud/ros-cdk-fc3": "^1.4.0",
5657
"@alicloud/ros-cdk-oss": "^1.4.0",
5758
"@alicloud/ros-cdk-ossdeployment": "^1.4.0",

src/stack/iacSchema.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@ const schema = {
111111
},
112112
},
113113
},
114+
databases: {
115+
type: 'object',
116+
patternProperties: {
117+
'.*': {
118+
type: 'object',
119+
properties: {
120+
name: { type: 'string' },
121+
type: { type: 'string', enum: ['ELASTICSEARCH_SERVERLESS'] },
122+
version: { type: 'string' },
123+
engine_mode: { type: 'string', enum: ['SEARCH', 'TIMESERIES'] },
124+
security: {
125+
type: 'object',
126+
properties: {
127+
basic_auth: {
128+
type: 'object',
129+
properties: {
130+
password: { type: 'string' },
131+
},
132+
required: ['password'],
133+
},
134+
},
135+
required: ['basic_auth'],
136+
},
137+
cu: { type: 'number' },
138+
storage_size: { type: 'number' },
139+
},
140+
required: ['name', 'type', 'version', 'security', 'cu', 'storage_size'],
141+
additionalProperties: false,
142+
},
143+
},
144+
},
114145
},
115146
required: ['version', 'provider', 'service'],
116147
additionalProperties: false,

src/stack/iacStack.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import * as ros from '@alicloud/ros-cdk-core';
22
import { RosParameterType } from '@alicloud/ros-cdk-core';
3-
import { ActionContext, EventTypes, ServerlessIac } from '../types';
3+
import {
4+
ActionContext,
5+
DatabaseEngineMode,
6+
DatabaseEnum,
7+
EventTypes,
8+
ServerlessIac,
9+
} from '../types';
410
import * as fc from '@alicloud/ros-cdk-fc3';
511
import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';
612
import * as ram from '@alicloud/ros-cdk-ram';
713
import * as agw from '@alicloud/ros-cdk-apigateway';
814
import * as oss from '@alicloud/ros-cdk-oss';
915
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
16+
import * as esServerless from '@alicloud/ros-cdk-elasticsearchserverless';
1017
import {
1118
CODE_ZIP_SIZE_LIMIT,
1219
getFileSource,
1320
readCodeSize,
1421
replaceReference,
1522
resolveCode,
1623
} from '../common';
24+
import { isEmpty } from 'lodash';
1725

1826
export class IacStack extends ros.Stack {
1927
private readonly service: string;
@@ -47,15 +55,15 @@ export class IacStack extends ros.Stack {
4755
new ros.RosInfo(this, ros.RosInfo.description, `${this.service} stack`);
4856

4957
const fileSources = iac.functions
50-
.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
58+
?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
5159
.map(({ code, name }) => {
5260
const fcName = replaceReference(name, context);
5361

5462
return { fcName, ...getFileSource(fcName, code) };
5563
});
5664

5765
let destinationBucket: oss.Bucket;
58-
if (fileSources.length > 0) {
66+
if (!isEmpty(fileSources)) {
5967
// creat oss to store code
6068
destinationBucket = new oss.Bucket(
6169
this,
@@ -70,7 +78,7 @@ export class IacStack extends ros.Stack {
7078
this,
7179
`${this.service}_artifacts_code_deployment`,
7280
{
73-
sources: fileSources.map(({ source }) => source),
81+
sources: fileSources!.map(({ source }) => source),
7482
destinationBucket,
7583
timeout: 300,
7684
logMonitoring: false, // 是否开启日志监控,设为false则不开启
@@ -79,14 +87,14 @@ export class IacStack extends ros.Stack {
7987
);
8088
}
8189

82-
iac.functions.forEach((fnc) => {
90+
iac.functions?.forEach((fnc) => {
8391
let code: RosFunction.CodeProperty = {
8492
zipFile: resolveCode(fnc.code),
8593
};
8694
if (readCodeSize(fnc.code) > CODE_ZIP_SIZE_LIMIT) {
8795
code = {
8896
ossBucketName: destinationBucket.attrName,
89-
ossObjectName: fileSources.find(({ fcName }) => fcName === fnc.name)?.objectKey,
97+
ossObjectName: fileSources?.find(({ fcName }) => fcName === fnc.name)?.objectKey,
9098
};
9199
}
92100
new fc.RosFunction(
@@ -204,5 +212,30 @@ export class IacStack extends ros.Stack {
204212
});
205213
});
206214
}
215+
iac.databases?.forEach((db) => {
216+
if ([DatabaseEnum.ELASTICSEARCH_SERVERLESS].includes(db.type)) {
217+
new esServerless.App(
218+
this,
219+
replaceReference(db.key, context),
220+
{
221+
appName: replaceReference(db.name, context),
222+
appVersion: db.version,
223+
authentication: {
224+
basicAuth: [
225+
{
226+
password: replaceReference(db.security.basicAuth.password, context),
227+
},
228+
],
229+
},
230+
quotaInfo: {
231+
cu: db.cu,
232+
storage: db.storageSize,
233+
appType: db.engineMode === DatabaseEngineMode.TIMESERIES ? 'TRIAL' : 'STANDARD',
234+
},
235+
},
236+
true,
237+
);
238+
}
239+
});
207240
}
208241
}

src/stack/parse.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse } from 'yaml';
22
import { existsSync, readFileSync } from 'node:fs';
3-
import { Event, IacFunction, RawServerlessIac, ServerlessIac } from '../types';
3+
import { Event, IacDatabase, IacFunction, RawServerlessIac, ServerlessIac } from '../types';
44
import { validateYaml } from './iacSchema';
55

66
const mapToArr = (obj: Record<string, Record<string, unknown> | string | null | undefined>) => {
@@ -38,6 +38,7 @@ const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
3838
{ key: 'iac-provider', value: 'ServerlessInsight' },
3939
...mapToKvArr(iacJson.tags),
4040
] as unknown as Array<{ key: string; value: string }>,
41+
databases: mapToArr(iacJson.databases) as unknown as Array<IacDatabase>,
4142
};
4243
};
4344

src/types.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ export type RawServerlessIac = {
5555
tags: Tags;
5656
functions: { [key: string]: IacFunction };
5757
events: Events;
58+
databases: { [key: string]: IacDatabase };
59+
};
60+
61+
export enum DatabaseEnum {
62+
ELASTICSEARCH_SERVERLESS = 'ELASTICSEARCH_SERVERLESS',
63+
}
64+
65+
export enum DatabaseEngineMode {
66+
SEARCH = 'SEARCH',
67+
TIMESERIES = 'TIMESERIES',
68+
}
69+
70+
export type IacDatabase = {
71+
key: string;
72+
name: string;
73+
type: DatabaseEnum;
74+
version: string;
75+
engineMode: string;
76+
security: {
77+
basicAuth: {
78+
password: string;
79+
};
80+
};
81+
cu: number;
82+
storageSize: number;
5883
};
5984

6085
export type ServerlessIac = {
@@ -64,8 +89,9 @@ export type ServerlessIac = {
6489
vars?: Vars;
6590
stages?: Stages;
6691
tags?: Array<{ key: string; value: string }>;
67-
functions: Array<IacFunction>;
92+
functions?: Array<IacFunction>;
6893
events?: Array<Event>;
94+
databases?: Array<IacDatabase>;
6995
};
7096

7197
export type ActionContext = {

tests/fixtures/deployFixture.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServerlessIac } from '../../src/types';
1+
import { DatabaseEnum, ServerlessIac } from '../../src/types';
22
import { cloneDeep, set } from 'lodash';
33

44
export const oneFcOneGatewayIac = {
@@ -747,3 +747,52 @@ export const defaultContext = {
747747
stackName: 'my-demo-stack',
748748
stage: 'default',
749749
};
750+
751+
export const esServerlessMinimumIac: ServerlessIac = {
752+
service: 'my-demo-es-serverless-service',
753+
version: '0.0.1',
754+
provider: 'aliyun',
755+
databases: [
756+
{
757+
key: 'insight_es_db_test',
758+
name: 'insight-poc-es-test',
759+
type: DatabaseEnum.ELASTICSEARCH_SERVERLESS,
760+
version: '7.10',
761+
engineMode: 'SEARCH',
762+
security: {
763+
basicAuth: {
764+
password: 'test-password',
765+
},
766+
},
767+
cu: 1,
768+
storageSize: 20,
769+
},
770+
],
771+
};
772+
773+
export const esServerlessMinimumRos = {
774+
Description: 'my-demo-es-serverless-service stack',
775+
Metadata: { 'ALIYUN::ROS::Interface': { TemplateTags: ['Create by ROS CDK'] } },
776+
ROSTemplateFormatVersion: '2015-09-01',
777+
Resources: {
778+
insight_es_db_test: {
779+
Properties: {
780+
AppName: 'insight-poc-es-test',
781+
AppVersion: '7.10',
782+
Authentication: {
783+
BasicAuth: [
784+
{
785+
Password: 'test-password',
786+
},
787+
],
788+
},
789+
QuotaInfo: {
790+
AppType: 'STANDARD',
791+
Cu: 1,
792+
Storage: 20,
793+
},
794+
},
795+
Type: 'ALIYUN::ElasticSearchServerless::App',
796+
},
797+
},
798+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: 0.0.1
2+
provider: aliyun
3+
4+
5+
service: insight-es-poc
6+
7+
tags:
8+
owner: geek-fun
9+
10+
databases:
11+
insight_es_db:
12+
name: insight-poc-es
13+
type: ELASTICSEARCH_SERVERLESS
14+
version: 7.10
15+
engine_mode: SEARCH
16+
security:
17+
basic_auth:
18+
password: 123456
19+
cu: 1
20+
storage_size: 20

tests/fixtures/serverless-insight.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ functions:
3535
TEST_VAR_EXTRA: abcds-${vars.testv}-andyou
3636

3737
#databases:
38-
# insight_poc_db:
39-
# name: insight-poc-db
40-
# type: RDS
41-
# region: ${vars.region}
42-
# instance_class: rds.mysql.t5.xlarge
38+
# insight_es_db:
39+
# name: insight-poc-es
40+
# type: ELASTICSEARCH_SERVERLESS
41+
# version: 7.10
42+
# engine_mode: SEARCH
43+
# security:
44+
# basic_auth:
45+
# password: 123456
46+
# cu: 1
4347
# storage_size: 20
44-
# username: root
45-
# password: 123456
46-
# database: insight_poc_db
47-
#
48+
4849
events:
4950
gateway_event:
5051
type: API_GATEWAY

tests/stack/deploy.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { deployStack } from '../../src/stack';
22
import { ActionContext } from '../../src/types';
33
import {
4+
esServerlessMinimumIac,
5+
esServerlessMinimumRos,
46
largeCodeRos,
57
minimumIac,
68
minimumRos,
@@ -124,4 +126,18 @@ describe('Unit tests for stack deployment', () => {
124126
expect(mockedResolveCode).toHaveBeenCalledTimes(1);
125127
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, largeCodeRos, { stackName });
126128
});
129+
130+
describe('unit test for deploy of databases', () => {
131+
it('should deploy elasticsearch serverless when database minimum fields provided', () => {
132+
const stackName = 'my-demo-es-serverless-stack';
133+
mockedRosStackDeploy.mockResolvedValueOnce(stackName);
134+
135+
deployStack(stackName, esServerlessMinimumIac, { stackName } as ActionContext);
136+
137+
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
138+
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, esServerlessMinimumRos, {
139+
stackName,
140+
});
141+
});
142+
});
127143
});

0 commit comments

Comments
 (0)