Skip to content

Commit d6285bf

Browse files
authored
refactor: standlize all ros id and ros resources name converntion (#64)
Refactor: standardize all ros id and ros resources name convention Refs: #38 --------- Signed-off-by: seven <[email protected]>
1 parent 02ac9df commit d6285bf

27 files changed

+2071
-1992
lines changed

src/commands/index.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ program
3838
.option('-f, --file <path>', 'specify the yaml file')
3939
.option('-s, --stage <stage>', 'specify the stage')
4040
.option('-r, --region <region>', 'specify the region')
41-
.option('-pr, --provider <provider>', 'specify the provider')
42-
.option('-ak, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
43-
.option('-as, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
44-
.option('-at, --securityToken <securityToken>', 'specify the SecurityToken')
41+
.option('-v, --provider <provider>', 'specify the provider')
42+
.option('-k, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
43+
.option('-x, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
44+
.option('-n, --securityToken <securityToken>', 'specify the SecurityToken')
4545
.option(
4646
'-p, --parameter <key=value>',
4747
'override parameters',
@@ -72,7 +72,7 @@ program
7272

7373
program
7474
.command('template <stackName>')
75-
.description('print ROS template')
75+
.description('print platform specific infrastructure as code template')
7676
.option('-f, --file <path>', 'specify the yaml file')
7777
.option('-s, --stage <stage>', 'specify the stage')
7878
.option('-t, --format <type>', 'output content type (JSON or YAML)', 'JSON')
@@ -84,10 +84,10 @@ program
8484
.command('destroy <stackName>')
8585
.option('-f, --file <path>', 'specify the yaml file')
8686
.option('-r, --region <region>', 'specify the region')
87-
.option('-pr, --provider <provider>', 'specify the provider')
88-
.option('-ak, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
89-
.option('-as, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
90-
.option('-at, --securityToken <securityToken>', 'specify the SecurityToken')
87+
.option('-v, --provider <provider>', 'specify the provider')
88+
.option('-k, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
89+
.option('-x, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
90+
.option('-n, --securityToken <securityToken>', 'specify the SecurityToken')
9191
.description('destroy serverless stack')
9292
.action(
9393
async (stackName, { file, region, provider, accessKeyId, accessKeySecret, securityToken }) => {

src/common/iacHelper.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const getParam = (key: string, records?: Array<{ key: string; value: string }>)
8484
return records?.find((param) => param.key === key)?.value as string;
8585
};
8686

87-
export const realValue = <T>(rawValue: string, ctx: Context): T => {
87+
export const calcValue = <T>(rawValue: string, ctx: Context): T => {
8888
const containsStage = rawValue.match(/\$\{ctx.stage}/);
8989
const containsVar = rawValue.match(/\$\{vars.\w+}/);
9090
const containsMap = rawValue.match(/\$\{stages\.(\w+)}/);
@@ -107,3 +107,21 @@ export const realValue = <T>(rawValue: string, ctx: Context): T => {
107107

108108
return value as T;
109109
};
110+
export const formatRosId = (id: string): string => {
111+
// Insert underscore before uppercase letters, but only when they follow a lowercase letter
112+
let result = id.replace(/([a-z])([A-Z])/g, '$1_$2');
113+
114+
// Convert to lowercase
115+
result = result.toLowerCase();
116+
117+
// Replace special characters with underscores
118+
result = result.replace(/[/#,-]/g, '_');
119+
120+
// Remove any number of underscores to single one
121+
result = result.replace(/_+/g, '_');
122+
123+
// Remove leading underscores
124+
result = result.replace(/^_/, '');
125+
126+
return result;
127+
};

src/stack/rosStack/bootstrap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Context } from '../../types';
33

44
const getBootstrapTemplate = async (context: Context) => {
55
const iamInfo = await getIamInfo(context);
6-
const stackName = `serverlessInsight-bootstrap-${iamInfo?.accountId}-${context.region}`;
6+
const stackName = `si-bootstrap-${iamInfo?.accountId}-${context.region}`;
77

88
const template = {
99
Description: 'ServerlessInsight Bootstrap Stack',
@@ -14,7 +14,7 @@ const getBootstrapTemplate = async (context: Context) => {
1414
},
1515
ROSTemplateFormatVersion: '2015-09-01',
1616
Resources: {
17-
ServerlessInsight_artifacts_bucket: {
17+
si_artifacts_bucket: {
1818
Type: 'ALIYUN::OSS::Bucket',
1919
Properties: {
2020
BucketName: {

src/stack/rosStack/bucket.ts

+20-27
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { BucketAccessEnum, BucketDomain, Context } from '../../types';
22
import * as oss from '@alicloud/ros-cdk-oss';
33
import * as ros from '@alicloud/ros-cdk-core';
44
import {
5-
encodeBase64ForRosId,
5+
calcRefs,
6+
calcValue,
7+
formatRosId,
68
getAssets,
79
OSS_DEPLOYMENT_TIMEOUT,
8-
calcRefs,
910
splitDomain,
1011
} from '../../common';
1112
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
@@ -58,7 +59,7 @@ export const resolveBuckets = (
5859
}
5960

6061
buckets.forEach((bucket) => {
61-
const ossBucket = new oss.Bucket(scope, calcRefs(bucket.key, context), {
62+
const ossBucket = new oss.Bucket(scope, bucket.key, {
6263
bucketName: calcRefs(bucket.name, context),
6364
accessControl: aclMap.get(
6465
calcRefs(bucket.security?.acl, context) ?? ('' as BucketAccessEnum),
@@ -78,10 +79,10 @@ export const resolveBuckets = (
7879
: undefined,
7980
});
8081
if (bucket.website?.code) {
81-
const filePath = path.resolve(process.cwd(), calcRefs(bucket.website.code, context));
82+
const filePath = path.resolve(process.cwd(), calcValue(bucket.website.code, context));
8283
new ossDeployment.BucketDeployment(
8384
scope,
84-
`si_auto_${bucket.key}_bucket_code_deployment`,
85+
formatRosId(`si_auto_${bucket.key}_bucket_code_deployment`),
8586
{
8687
sources: getAssets(filePath),
8788
destinationBucket: ossBucket.attrName,
@@ -95,29 +96,21 @@ export const resolveBuckets = (
9596
}
9697
if (bucket.website?.domain) {
9798
const { rr, domainName } = splitDomain(bucket.website.domain);
98-
new oss.Domain(
99-
scope,
100-
`${bucket.key}_custom_domain_${encodeBase64ForRosId(bucket.website.domain)}`,
101-
{
102-
bucketName: ossBucket.attrName,
103-
domainName: calcRefs(bucket.website.domain, context),
104-
},
105-
);
99+
new oss.Domain(scope, formatRosId(`${bucket.key}_custom_domain`), {
100+
bucketName: ossBucket.attrName,
101+
domainName: calcRefs(bucket.website.domain, context),
102+
});
106103

107-
new dns.DomainRecord(
108-
scope,
109-
`${bucket.key}_custom_domain_record_${encodeBase64ForRosId(bucket.website.domain)}`,
110-
{
111-
domainName: domainName,
112-
rr,
113-
type: 'CNAME',
114-
value: [BucketAccessEnum.PUBLIC_READ, BucketAccessEnum.PUBLIC_READ_WRITE].includes(
115-
bucket.security?.acl ?? ('' as BucketAccessEnum),
116-
)
117-
? ossBucket.attrDomainName
118-
: ossBucket.attrInternalDomainName,
119-
},
120-
);
104+
new dns.DomainRecord(scope, formatRosId(`${bucket.key}_custom_domain_record`), {
105+
domainName: domainName,
106+
rr,
107+
type: 'CNAME',
108+
value: [BucketAccessEnum.PUBLIC_READ, BucketAccessEnum.PUBLIC_READ_WRITE].includes(
109+
bucket.security?.acl ?? ('' as BucketAccessEnum),
110+
)
111+
? ossBucket.attrDomainName
112+
: ossBucket.attrInternalDomainName,
113+
});
121114
}
122115
});
123116
};

src/stack/rosStack/database.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export const resolveDatabases = (
208208
if ([DatabaseEnum.ELASTICSEARCH_SERVERLESS].includes(db.type)) {
209209
new esServerless.App(
210210
scope,
211-
calcRefs(db.key, context),
211+
db.key,
212212
{
213213
appName: calcRefs(db.name, context),
214214
appVersion: version,
@@ -249,7 +249,7 @@ export const resolveDatabases = (
249249
) {
250250
new rds.DBInstance(
251251
scope,
252-
calcRefs(db.key, context),
252+
db.key,
253253
{
254254
engine: engine as string,
255255
/**

src/stack/rosStack/event.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ros from '@alicloud/ros-cdk-core';
22
import { Context, EventDomain, EventTypes, ServerlessIac } from '../../types';
33
import * as ram from '@alicloud/ros-cdk-ram';
4-
import { encodeBase64ForRosId, calcRefs, splitDomain } from '../../common';
4+
import { calcRefs, calcValue, formatRosId, splitDomain } from '../../common';
55
import * as agw from '@alicloud/ros-cdk-apigateway';
66
import { isEmpty } from 'lodash';
77
import * as dns from '@alicloud/ros-cdk-dns';
@@ -22,9 +22,9 @@ export const resolveEvents = (
2222
apiGateway.forEach((event) => {
2323
const gatewayAccessRole = new ram.RosRole(
2424
scope,
25-
calcRefs(`${event.key}_role`, context),
25+
formatRosId(`${event.key}_agw_role`),
2626
{
27-
roleName: calcRefs(`${service}-${event.name}-agw-access-role`, context),
27+
roleName: calcRefs(`${event.name}-agw-access-role`, context),
2828
description: calcRefs(`${service} role`, context),
2929
assumeRolePolicyDocument: {
3030
version: '1',
@@ -60,20 +60,20 @@ export const resolveEvents = (
6060

6161
const apiGatewayGroup = new agw.RosGroup(
6262
scope,
63-
calcRefs(`${service}_apigroup`, context),
63+
formatRosId(`${event.key}_agw_group`),
6464
{
65-
groupName: calcRefs(`${service}_apigroup`, context),
65+
groupName: calcRefs(`${service}-agw-group`, context),
6666
tags: calcRefs(tags, context),
6767
passthroughHeaders: 'host',
6868
},
6969
true,
7070
);
7171

7272
if (event.domain) {
73-
const dnsRecordRosId = `${event.key}_custom_domain_record_${encodeBase64ForRosId(event.domain.domain_name)}`;
73+
const dnsRecordId = formatRosId(`${event.key}_agw_custom_domain_record`);
7474
const { domainName, rr } = splitDomain(event.domain?.domain_name);
7575

76-
new dns.DomainRecord(scope, dnsRecordRosId, {
76+
new dns.DomainRecord(scope, dnsRecordId, {
7777
domainName,
7878
rr,
7979
type: 'CNAME',
@@ -82,7 +82,7 @@ export const resolveEvents = (
8282

8383
const agwCustomDomain = new agw.RosCustomDomain(
8484
scope,
85-
`${event.key}_custom_domain_${encodeBase64ForRosId(event.domain.domain_name)}`,
85+
formatRosId(`${event.key}_agw_custom_domain`),
8686
{
8787
groupId: apiGatewayGroup.attrGroupId,
8888
domainName: event.domain.domain_name,
@@ -92,17 +92,16 @@ export const resolveEvents = (
9292
},
9393
true,
9494
);
95-
agwCustomDomain.addRosDependency(dnsRecordRosId);
95+
agwCustomDomain.addRosDependency(dnsRecordId);
9696
}
9797

9898
event.triggers.forEach((trigger) => {
99-
const key = encodeBase64ForRosId(calcRefs(`${trigger.method}_${trigger.path}`, context));
100-
99+
const key = formatRosId(calcValue(`${trigger.method}_${trigger.path}`, context));
101100
const api = new agw.RosApi(
102101
scope,
103-
`${event.key}_api_${key}`,
102+
formatRosId(`${event.key}_agw_api_${key}`),
104103
{
105-
apiName: calcRefs(`${event.name}_api_${key}`, context),
104+
apiName: calcRefs(`${event.name}-agw-api-${key.replace(/_/g, '-')}`, context),
106105
groupId: apiGatewayGroup.attrGroupId,
107106
visibility: 'PRIVATE',
108107
authType: 'ANONYMOUS',
@@ -130,11 +129,14 @@ export const resolveEvents = (
130129
);
131130
api.addDependsOn(apiGatewayGroup);
132131

133-
new agw.Deployment(scope, `${service}_deployment`, {
132+
new agw.Deployment(scope, formatRosId(`${event.key}_agw_api_deployment_${key}`), {
134133
apiId: api.attrApiId,
135134
groupId: apiGatewayGroup.attrGroupId,
136135
stageName: 'RELEASE',
137-
description: `${service} Api Gateway deployment`,
136+
description: calcRefs(
137+
`${service} Api Gateway deployment for api: ${trigger.method} ${trigger.path}`,
138+
context,
139+
),
138140
});
139141
});
140142
});

0 commit comments

Comments
 (0)