Skip to content

Commit 1fd4c2d

Browse files
authored
feat: enable support to define elasticsearch serverless (#14)
feat: enable support to define Elasticsearch serverless Refs: #12 --------- Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent 8c925cf commit 1fd4c2d

File tree

11 files changed

+347
-27
lines changed

11 files changed

+347
-27
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@
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",
5859
"@alicloud/ros-cdk-ram": "^1.4.0",
59-
"@alicloud/ros20190910": "^3.5.0",
60+
"@alicloud/ros20190910": "^3.5.2",
6061
"ajv": "^8.17.1",
6162
"chalk": "^5.3.0",
6263
"commander": "^12.1.0",

src/stack/iacSchema.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,43 @@ 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+
cu: { type: 'number' },
125+
storage_size: { type: 'number' },
126+
security: {
127+
type: 'object',
128+
properties: {
129+
basic_auth: {
130+
type: 'object',
131+
properties: {
132+
password: { type: 'string' },
133+
},
134+
required: ['password'],
135+
},
136+
},
137+
required: ['basic_auth'],
138+
},
139+
network: {
140+
type: 'object',
141+
properties: {
142+
public: { type: 'boolean' },
143+
},
144+
},
145+
},
146+
required: ['name', 'type', 'version', 'security', 'cu', 'storage_size'],
147+
additionalProperties: false,
148+
},
149+
},
150+
},
114151
},
115152
required: ['version', 'provider', 'service'],
116153
additionalProperties: false,

src/stack/iacStack.ts

Lines changed: 51 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,42 @@ 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+
// network: [
236+
// {
237+
// type: 'PUBLIC_KIBANA',
238+
// enabled: true,
239+
// whiteIpGroup: [{ groupName: 'default', ips: ['0.0.0.0/24'] }],
240+
// },
241+
// {
242+
// type: 'PUBLIC_ES',
243+
// enabled: true,
244+
// whiteIpGroup: [{ groupName: 'default', ips: ['0.0.0.0/24'] }],
245+
// },
246+
// ],
247+
},
248+
true,
249+
);
250+
}
251+
});
207252
}
208253
}

src/stack/parse.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { parse } from 'yaml';
22
import { existsSync, readFileSync } from 'node:fs';
3-
import { Event, IacFunction, RawServerlessIac, ServerlessIac } from '../types';
3+
import {
4+
DatabaseEnum,
5+
Event,
6+
IacDatabase,
7+
IacFunction,
8+
RawIacDatabase,
9+
RawServerlessIac,
10+
ServerlessIac,
11+
} from '../types';
412
import { validateYaml } from './iacSchema';
13+
import { get, isEmpty } from 'lodash';
514

615
const mapToArr = (obj: Record<string, Record<string, unknown> | string | null | undefined>) => {
716
if (!obj) {
@@ -24,7 +33,30 @@ const validateExistence = (path: string) => {
2433
throw new Error(`File does not exist at path: ${path}`);
2534
}
2635
};
27-
36+
const transformDatabase = (databases?: {
37+
[key: string]: RawIacDatabase;
38+
}): Array<IacDatabase> | undefined => {
39+
if (isEmpty(databases)) {
40+
return undefined;
41+
}
42+
return Object.entries(databases)?.map(([key, database]) => ({
43+
key: key,
44+
name: database.name,
45+
type: database.type as DatabaseEnum,
46+
version: database.version,
47+
engineMode: database.engine_mode,
48+
security: {
49+
basicAuth: {
50+
password: get(database, 'security.basic_auth.password'),
51+
},
52+
},
53+
cu: database.cu,
54+
storageSize: database.storage_size,
55+
network: database.network && {
56+
public: database.network?.public as boolean,
57+
},
58+
}));
59+
};
2860
const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
2961
return {
3062
service: iacJson.service,
@@ -38,6 +70,7 @@ const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
3870
{ key: 'iac-provider', value: 'ServerlessInsight' },
3971
...mapToKvArr(iacJson.tags),
4072
] as unknown as Array<{ key: string; value: string }>,
73+
databases: transformDatabase(iacJson.databases),
4174
};
4275
};
4376

src/types.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ type Events = {
4545
type Tags = {
4646
[key: string]: string;
4747
};
48+
export type RawIacDatabase = {
49+
name: string;
50+
type: DatabaseEnum;
51+
version: string;
52+
engine_mode: DatabaseEngineMode;
53+
security: {
54+
basic_auth: {
55+
password: string;
56+
};
57+
};
58+
network?: {
59+
public: boolean;
60+
};
61+
cu: number;
62+
storage_size: number;
63+
};
4864

4965
export type RawServerlessIac = {
5066
version: string;
@@ -55,6 +71,34 @@ export type RawServerlessIac = {
5571
tags: Tags;
5672
functions: { [key: string]: IacFunction };
5773
events: Events;
74+
databases: { [key: string]: RawIacDatabase };
75+
};
76+
77+
export enum DatabaseEnum {
78+
ELASTICSEARCH_SERVERLESS = 'ELASTICSEARCH_SERVERLESS',
79+
}
80+
81+
export enum DatabaseEngineMode {
82+
SEARCH = 'SEARCH',
83+
TIMESERIES = 'TIMESERIES',
84+
}
85+
86+
export type IacDatabase = {
87+
key: string;
88+
name: string;
89+
type: DatabaseEnum;
90+
version: string;
91+
engineMode: string;
92+
security: {
93+
basicAuth: {
94+
password: string;
95+
};
96+
};
97+
network?: {
98+
public: boolean;
99+
};
100+
cu: number;
101+
storageSize: number;
58102
};
59103

60104
export type ServerlessIac = {
@@ -64,8 +108,9 @@ export type ServerlessIac = {
64108
vars?: Vars;
65109
stages?: Stages;
66110
tags?: Array<{ key: string; value: string }>;
67-
functions: Array<IacFunction>;
111+
functions?: Array<IacFunction>;
68112
events?: Array<Event>;
113+
databases?: Array<IacDatabase>;
69114
};
70115

71116
export type ActionContext = {

0 commit comments

Comments
 (0)