Skip to content

Commit

Permalink
feat: enable support to define elasticsearch serverless (#14)
Browse files Browse the repository at this point in the history
feat: enable support to define Elasticsearch serverless




Refs: #12

---------

Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll authored Dec 8, 2024
1 parent 8c925cf commit 1fd4c2d
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 27 deletions.
37 changes: 30 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
"@alicloud/openapi-client": "^0.4.12",
"@alicloud/ros-cdk-apigateway": "^1.4.0",
"@alicloud/ros-cdk-core": "^1.4.0",
"@alicloud/ros-cdk-elasticsearchserverless": "^1.4.0",
"@alicloud/ros-cdk-fc3": "^1.4.0",
"@alicloud/ros-cdk-oss": "^1.4.0",
"@alicloud/ros-cdk-ossdeployment": "^1.4.0",
"@alicloud/ros-cdk-ram": "^1.4.0",
"@alicloud/ros20190910": "^3.5.0",
"@alicloud/ros20190910": "^3.5.2",
"ajv": "^8.17.1",
"chalk": "^5.3.0",
"commander": "^12.1.0",
Expand Down
37 changes: 37 additions & 0 deletions src/stack/iacSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ const schema = {
},
},
},
databases: {
type: 'object',
patternProperties: {
'.*': {
type: 'object',
properties: {
name: { type: 'string' },
type: { type: 'string', enum: ['ELASTICSEARCH_SERVERLESS'] },
version: { type: 'string' },
engine_mode: { type: 'string', enum: ['SEARCH', 'TIMESERIES'] },
cu: { type: 'number' },
storage_size: { type: 'number' },
security: {
type: 'object',
properties: {
basic_auth: {
type: 'object',
properties: {
password: { type: 'string' },
},
required: ['password'],
},
},
required: ['basic_auth'],
},
network: {
type: 'object',
properties: {
public: { type: 'boolean' },
},
},
},
required: ['name', 'type', 'version', 'security', 'cu', 'storage_size'],
additionalProperties: false,
},
},
},
},
required: ['version', 'provider', 'service'],
additionalProperties: false,
Expand Down
57 changes: 51 additions & 6 deletions src/stack/iacStack.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import * as ros from '@alicloud/ros-cdk-core';
import { RosParameterType } from '@alicloud/ros-cdk-core';
import { ActionContext, EventTypes, ServerlessIac } from '../types';
import {
ActionContext,
DatabaseEngineMode,
DatabaseEnum,
EventTypes,
ServerlessIac,
} from '../types';
import * as fc from '@alicloud/ros-cdk-fc3';
import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';
import * as ram from '@alicloud/ros-cdk-ram';
import * as agw from '@alicloud/ros-cdk-apigateway';
import * as oss from '@alicloud/ros-cdk-oss';
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
import * as esServerless from '@alicloud/ros-cdk-elasticsearchserverless';
import {
CODE_ZIP_SIZE_LIMIT,
getFileSource,
readCodeSize,
replaceReference,
resolveCode,
} from '../common';
import { isEmpty } from 'lodash';

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

const fileSources = iac.functions
.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
.map(({ code, name }) => {
const fcName = replaceReference(name, context);

return { fcName, ...getFileSource(fcName, code) };
});

let destinationBucket: oss.Bucket;
if (fileSources.length > 0) {
if (!isEmpty(fileSources)) {
// creat oss to store code
destinationBucket = new oss.Bucket(
this,
Expand All @@ -70,7 +78,7 @@ export class IacStack extends ros.Stack {
this,
`${this.service}_artifacts_code_deployment`,
{
sources: fileSources.map(({ source }) => source),
sources: fileSources!.map(({ source }) => source),
destinationBucket,
timeout: 300,
logMonitoring: false, // 是否开启日志监控,设为false则不开启
Expand All @@ -79,14 +87,14 @@ export class IacStack extends ros.Stack {
);
}

iac.functions.forEach((fnc) => {
iac.functions?.forEach((fnc) => {
let code: RosFunction.CodeProperty = {
zipFile: resolveCode(fnc.code),
};
if (readCodeSize(fnc.code) > CODE_ZIP_SIZE_LIMIT) {
code = {
ossBucketName: destinationBucket.attrName,
ossObjectName: fileSources.find(({ fcName }) => fcName === fnc.name)?.objectKey,
ossObjectName: fileSources?.find(({ fcName }) => fcName === fnc.name)?.objectKey,
};
}
new fc.RosFunction(
Expand Down Expand Up @@ -204,5 +212,42 @@ export class IacStack extends ros.Stack {
});
});
}
iac.databases?.forEach((db) => {
if ([DatabaseEnum.ELASTICSEARCH_SERVERLESS].includes(db.type)) {
new esServerless.App(
this,
replaceReference(db.key, context),
{
appName: replaceReference(db.name, context),
appVersion: db.version,
authentication: {
basicAuth: [
{
password: replaceReference(db.security.basicAuth.password, context),
},
],
},
quotaInfo: {
cu: db.cu,
storage: db.storageSize,
appType: db.engineMode === DatabaseEngineMode.TIMESERIES ? 'TRIAL' : 'STANDARD',
},
// network: [
// {
// type: 'PUBLIC_KIBANA',
// enabled: true,
// whiteIpGroup: [{ groupName: 'default', ips: ['0.0.0.0/24'] }],
// },
// {
// type: 'PUBLIC_ES',
// enabled: true,
// whiteIpGroup: [{ groupName: 'default', ips: ['0.0.0.0/24'] }],
// },
// ],
},
true,
);
}
});
}
}
37 changes: 35 additions & 2 deletions src/stack/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { parse } from 'yaml';
import { existsSync, readFileSync } from 'node:fs';
import { Event, IacFunction, RawServerlessIac, ServerlessIac } from '../types';
import {
DatabaseEnum,
Event,
IacDatabase,
IacFunction,
RawIacDatabase,
RawServerlessIac,
ServerlessIac,
} from '../types';
import { validateYaml } from './iacSchema';
import { get, isEmpty } from 'lodash';

const mapToArr = (obj: Record<string, Record<string, unknown> | string | null | undefined>) => {
if (!obj) {
Expand All @@ -24,7 +33,30 @@ const validateExistence = (path: string) => {
throw new Error(`File does not exist at path: ${path}`);
}
};

const transformDatabase = (databases?: {
[key: string]: RawIacDatabase;
}): Array<IacDatabase> | undefined => {
if (isEmpty(databases)) {
return undefined;
}
return Object.entries(databases)?.map(([key, database]) => ({
key: key,
name: database.name,
type: database.type as DatabaseEnum,
version: database.version,
engineMode: database.engine_mode,
security: {
basicAuth: {
password: get(database, 'security.basic_auth.password'),
},
},
cu: database.cu,
storageSize: database.storage_size,
network: database.network && {
public: database.network?.public as boolean,
},
}));
};
const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
return {
service: iacJson.service,
Expand All @@ -38,6 +70,7 @@ const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
{ key: 'iac-provider', value: 'ServerlessInsight' },
...mapToKvArr(iacJson.tags),
] as unknown as Array<{ key: string; value: string }>,
databases: transformDatabase(iacJson.databases),
};
};

Expand Down
47 changes: 46 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ type Events = {
type Tags = {
[key: string]: string;
};
export type RawIacDatabase = {
name: string;
type: DatabaseEnum;
version: string;
engine_mode: DatabaseEngineMode;
security: {
basic_auth: {
password: string;
};
};
network?: {
public: boolean;
};
cu: number;
storage_size: number;
};

export type RawServerlessIac = {
version: string;
Expand All @@ -55,6 +71,34 @@ export type RawServerlessIac = {
tags: Tags;
functions: { [key: string]: IacFunction };
events: Events;
databases: { [key: string]: RawIacDatabase };
};

export enum DatabaseEnum {
ELASTICSEARCH_SERVERLESS = 'ELASTICSEARCH_SERVERLESS',
}

export enum DatabaseEngineMode {
SEARCH = 'SEARCH',
TIMESERIES = 'TIMESERIES',
}

export type IacDatabase = {
key: string;
name: string;
type: DatabaseEnum;
version: string;
engineMode: string;
security: {
basicAuth: {
password: string;
};
};
network?: {
public: boolean;
};
cu: number;
storageSize: number;
};

export type ServerlessIac = {
Expand All @@ -64,8 +108,9 @@ export type ServerlessIac = {
vars?: Vars;
stages?: Stages;
tags?: Array<{ key: string; value: string }>;
functions: Array<IacFunction>;
functions?: Array<IacFunction>;
events?: Array<Event>;
databases?: Array<IacDatabase>;
};

export type ActionContext = {
Expand Down
Loading

0 comments on commit 1fd4c2d

Please sign in to comment.