Skip to content

Commit 0441a7b

Browse files
committed
feat: implement elasitcsearch serverless for aliyun resources
Signed-off-by: seven <[email protected]>
1 parent 56d367d commit 0441a7b

File tree

9 files changed

+137
-18
lines changed

9 files changed

+137
-18
lines changed

package-lock.json

Lines changed: 7 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"@alicloud/ros-cdk-oss": "^1.4.0",
5858
"@alicloud/ros-cdk-ossdeployment": "^1.4.0",
5959
"@alicloud/ros-cdk-ram": "^1.4.0",
60-
"@alicloud/ros20190910": "^3.5.0",
60+
"@alicloud/ros20190910": "^3.5.2",
6161
"ajv": "^8.17.1",
6262
"chalk": "^5.3.0",
6363
"commander": "^12.1.0",

src/stack/iacSchema.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ const schema = {
121121
type: { type: 'string', enum: ['ELASTICSEARCH_SERVERLESS'] },
122122
version: { type: 'string' },
123123
engine_mode: { type: 'string', enum: ['SEARCH', 'TIMESERIES'] },
124+
cu: { type: 'number' },
125+
storage_size: { type: 'number' },
124126
security: {
125127
type: 'object',
126128
properties: {
@@ -134,8 +136,12 @@ const schema = {
134136
},
135137
required: ['basic_auth'],
136138
},
137-
cu: { type: 'number' },
138-
storage_size: { type: 'number' },
139+
network: {
140+
type: 'object',
141+
properties: {
142+
public: { type: 'boolean' },
143+
},
144+
},
139145
},
140146
required: ['name', 'type', 'version', 'security', 'cu', 'storage_size'],
141147
additionalProperties: false,

src/stack/iacStack.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ export class IacStack extends ros.Stack {
232232
storage: db.storageSize,
233233
appType: db.engineMode === DatabaseEngineMode.TIMESERIES ? 'TRIAL' : 'STANDARD',
234234
},
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+
// ],
235247
},
236248
true,
237249
);

src/stack/parse.ts

Lines changed: 35 additions & 3 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, IacDatabase, 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,7 +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 }>,
41-
databases: mapToArr(iacJson.databases) as unknown as Array<IacDatabase>,
73+
databases: transformDatabase(iacJson.databases),
4274
};
4375
};
4476

src/types.ts

Lines changed: 20 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,7 +71,7 @@ export type RawServerlessIac = {
5571
tags: Tags;
5672
functions: { [key: string]: IacFunction };
5773
events: Events;
58-
databases: { [key: string]: IacDatabase };
74+
databases: { [key: string]: RawIacDatabase };
5975
};
6076

6177
export enum DatabaseEnum {
@@ -78,6 +94,9 @@ export type IacDatabase = {
7894
password: string;
7995
};
8096
};
97+
network?: {
98+
public: boolean;
99+
};
81100
cu: number;
82101
storageSize: number;
83102
};

tests/fixtures/deployFixture.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,16 @@ export const esServerlessMinimumRos = {
791791
Cu: 1,
792792
Storage: 20,
793793
},
794+
// Network: [
795+
// {
796+
// Enabled: true,
797+
// Type: 'PUBLIC_KIBANA',
798+
// },
799+
// {
800+
// Enabled: true,
801+
// Type: 'PUBLIC_ES',
802+
// },
803+
// ],
794804
},
795805
Type: 'ALIYUN::ElasticSearchServerless::App',
796806
},

tests/fixtures/serverless-insight-es.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ databases:
1111
insight_es_db:
1212
name: insight-poc-es
1313
type: ELASTICSEARCH_SERVERLESS
14-
version: 7.10
14+
version: '7.10'
1515
engine_mode: SEARCH
16-
security:
17-
basic_auth:
18-
password: 123456
1916
cu: 1
2017
storage_size: 20
18+
security:
19+
basic_auth:
20+
password: 'U34I6InQ8elseTgqTWT2t2oFXpoqFg'
21+

tests/stack/parse.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { parseYaml } from '../../src/stack';
2+
import path from 'node:path';
3+
4+
describe('unit test for parse', () => {
5+
describe('domain - databases', () => {
6+
const yamlPath = path.resolve(__dirname, '../fixtures/serverless-insight-es.yml');
7+
8+
it('should pass databases from yaml to domain instance when the yaml is valid', () => {
9+
const databaseDomain = parseYaml(yamlPath);
10+
expect(databaseDomain).toEqual({
11+
service: 'insight-es-poc',
12+
version: '0.0.1',
13+
provider: 'aliyun',
14+
tags: [
15+
{ key: 'iac-provider', value: 'ServerlessInsight' },
16+
{ key: 'owner', value: 'geek-fun' },
17+
],
18+
events: [],
19+
functions: [],
20+
databases: [
21+
{
22+
key: 'insight_es_db',
23+
type: 'ELASTICSEARCH_SERVERLESS',
24+
name: 'insight-poc-es',
25+
engineMode: 'SEARCH',
26+
version: '7.10',
27+
security: {
28+
basicAuth: {
29+
password: 'U34I6InQ8elseTgqTWT2t2oFXpoqFg',
30+
},
31+
},
32+
cu: 1,
33+
storageSize: 20,
34+
},
35+
],
36+
});
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)