Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ program
.option('-f, --file <path>', 'specify the yaml file')
.option('-s, --stage <stage>', 'specify the stage')
.option('-r, --region <region>', 'specify the region')
.option('-pr, --provider <provider>', 'specify the provider')
.option('-ak, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
.option('-as, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
.option('-at, --securityToken <securityToken>', 'specify the SecurityToken')
.option('-v, --provider <provider>', 'specify the provider')
.option('-k, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
.option('-x, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
.option('-n, --securityToken <securityToken>', 'specify the SecurityToken')
.option(
'-p, --parameter <key=value>',
'override parameters',
Expand Down Expand Up @@ -72,7 +72,7 @@ program

program
.command('template <stackName>')
.description('print ROS template')
.description('print platform specific infrastructure as code template')
.option('-f, --file <path>', 'specify the yaml file')
.option('-s, --stage <stage>', 'specify the stage')
.option('-t, --format <type>', 'output content type (JSON or YAML)', 'JSON')
Expand All @@ -84,10 +84,10 @@ program
.command('destroy <stackName>')
.option('-f, --file <path>', 'specify the yaml file')
.option('-r, --region <region>', 'specify the region')
.option('-pr, --provider <provider>', 'specify the provider')
.option('-ak, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
.option('-as, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
.option('-at, --securityToken <securityToken>', 'specify the SecurityToken')
.option('-v, --provider <provider>', 'specify the provider')
.option('-k, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
.option('-x, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
.option('-n, --securityToken <securityToken>', 'specify the SecurityToken')
.description('destroy serverless stack')
.action(
async (stackName, { file, region, provider, accessKeyId, accessKeySecret, securityToken }) => {
Expand Down
20 changes: 19 additions & 1 deletion src/common/iacHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const getParam = (key: string, records?: Array<{ key: string; value: string }>)
return records?.find((param) => param.key === key)?.value as string;
};

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

return value as T;
};
export const formatRosId = (id: string): string => {
// Insert underscore before uppercase letters, but only when they follow a lowercase letter
let result = id.replace(/([a-z])([A-Z])/g, '$1_$2');

// Convert to lowercase
result = result.toLowerCase();

// Replace special characters with underscores
result = result.replace(/[/#,-]/g, '_');

// Remove any number of underscores to single one
result = result.replace(/_+/g, '_');

// Remove leading underscores
result = result.replace(/^_/, '');

return result;
};
4 changes: 2 additions & 2 deletions src/stack/rosStack/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Context } from '../../types';

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

const template = {
Description: 'ServerlessInsight Bootstrap Stack',
Expand All @@ -14,7 +14,7 @@ const getBootstrapTemplate = async (context: Context) => {
},
ROSTemplateFormatVersion: '2015-09-01',
Resources: {
ServerlessInsight_artifacts_bucket: {
si_artifacts_bucket: {
Type: 'ALIYUN::OSS::Bucket',
Properties: {
BucketName: {
Expand Down
47 changes: 20 additions & 27 deletions src/stack/rosStack/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { BucketAccessEnum, BucketDomain, Context } from '../../types';
import * as oss from '@alicloud/ros-cdk-oss';
import * as ros from '@alicloud/ros-cdk-core';
import {
encodeBase64ForRosId,
calcRefs,
calcValue,
formatRosId,
getAssets,
OSS_DEPLOYMENT_TIMEOUT,
calcRefs,
splitDomain,
} from '../../common';
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
Expand Down Expand Up @@ -58,7 +59,7 @@ export const resolveBuckets = (
}

buckets.forEach((bucket) => {
const ossBucket = new oss.Bucket(scope, calcRefs(bucket.key, context), {
const ossBucket = new oss.Bucket(scope, bucket.key, {
bucketName: calcRefs(bucket.name, context),
accessControl: aclMap.get(
calcRefs(bucket.security?.acl, context) ?? ('' as BucketAccessEnum),
Expand All @@ -78,10 +79,10 @@ export const resolveBuckets = (
: undefined,
});
if (bucket.website?.code) {
const filePath = path.resolve(process.cwd(), calcRefs(bucket.website.code, context));
const filePath = path.resolve(process.cwd(), calcValue(bucket.website.code, context));
new ossDeployment.BucketDeployment(
scope,
`si_auto_${bucket.key}_bucket_code_deployment`,
formatRosId(`si_auto_${bucket.key}_bucket_code_deployment`),
{
sources: getAssets(filePath),
destinationBucket: ossBucket.attrName,
Expand All @@ -95,29 +96,21 @@ export const resolveBuckets = (
}
if (bucket.website?.domain) {
const { rr, domainName } = splitDomain(bucket.website.domain);
new oss.Domain(
scope,
`${bucket.key}_custom_domain_${encodeBase64ForRosId(bucket.website.domain)}`,
{
bucketName: ossBucket.attrName,
domainName: calcRefs(bucket.website.domain, context),
},
);
new oss.Domain(scope, formatRosId(`${bucket.key}_custom_domain`), {
bucketName: ossBucket.attrName,
domainName: calcRefs(bucket.website.domain, context),
});

new dns.DomainRecord(
scope,
`${bucket.key}_custom_domain_record_${encodeBase64ForRosId(bucket.website.domain)}`,
{
domainName: domainName,
rr,
type: 'CNAME',
value: [BucketAccessEnum.PUBLIC_READ, BucketAccessEnum.PUBLIC_READ_WRITE].includes(
bucket.security?.acl ?? ('' as BucketAccessEnum),
)
? ossBucket.attrDomainName
: ossBucket.attrInternalDomainName,
},
);
new dns.DomainRecord(scope, formatRosId(`${bucket.key}_custom_domain_record`), {
domainName: domainName,
rr,
type: 'CNAME',
value: [BucketAccessEnum.PUBLIC_READ, BucketAccessEnum.PUBLIC_READ_WRITE].includes(
bucket.security?.acl ?? ('' as BucketAccessEnum),
)
? ossBucket.attrDomainName
: ossBucket.attrInternalDomainName,
});
}
});
};
4 changes: 2 additions & 2 deletions src/stack/rosStack/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export const resolveDatabases = (
if ([DatabaseEnum.ELASTICSEARCH_SERVERLESS].includes(db.type)) {
new esServerless.App(
scope,
calcRefs(db.key, context),
db.key,
{
appName: calcRefs(db.name, context),
appVersion: version,
Expand Down Expand Up @@ -249,7 +249,7 @@ export const resolveDatabases = (
) {
new rds.DBInstance(
scope,
calcRefs(db.key, context),
db.key,
{
engine: engine as string,
/**
Expand Down
32 changes: 17 additions & 15 deletions src/stack/rosStack/event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ros from '@alicloud/ros-cdk-core';
import { Context, EventDomain, EventTypes, ServerlessIac } from '../../types';
import * as ram from '@alicloud/ros-cdk-ram';
import { encodeBase64ForRosId, calcRefs, splitDomain } from '../../common';
import { calcRefs, calcValue, formatRosId, splitDomain } from '../../common';
import * as agw from '@alicloud/ros-cdk-apigateway';
import { isEmpty } from 'lodash';
import * as dns from '@alicloud/ros-cdk-dns';
Expand All @@ -22,9 +22,9 @@ export const resolveEvents = (
apiGateway.forEach((event) => {
const gatewayAccessRole = new ram.RosRole(
scope,
calcRefs(`${event.key}_role`, context),
formatRosId(`${event.key}_agw_role`),
{
roleName: calcRefs(`${service}-${event.name}-agw-access-role`, context),
roleName: calcRefs(`${event.name}-agw-access-role`, context),
description: calcRefs(`${service} role`, context),
assumeRolePolicyDocument: {
version: '1',
Expand Down Expand Up @@ -60,20 +60,20 @@ export const resolveEvents = (

const apiGatewayGroup = new agw.RosGroup(
scope,
calcRefs(`${service}_apigroup`, context),
formatRosId(`${event.key}_agw_group`),
{
groupName: calcRefs(`${service}_apigroup`, context),
groupName: calcRefs(`${service}-agw-group`, context),
tags: calcRefs(tags, context),
passthroughHeaders: 'host',
},
true,
);

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

new dns.DomainRecord(scope, dnsRecordRosId, {
new dns.DomainRecord(scope, dnsRecordId, {
domainName,
rr,
type: 'CNAME',
Expand All @@ -82,7 +82,7 @@ export const resolveEvents = (

const agwCustomDomain = new agw.RosCustomDomain(
scope,
`${event.key}_custom_domain_${encodeBase64ForRosId(event.domain.domain_name)}`,
formatRosId(`${event.key}_agw_custom_domain`),
{
groupId: apiGatewayGroup.attrGroupId,
domainName: event.domain.domain_name,
Expand All @@ -92,17 +92,16 @@ export const resolveEvents = (
},
true,
);
agwCustomDomain.addRosDependency(dnsRecordRosId);
agwCustomDomain.addRosDependency(dnsRecordId);
}

event.triggers.forEach((trigger) => {
const key = encodeBase64ForRosId(calcRefs(`${trigger.method}_${trigger.path}`, context));

const key = formatRosId(calcValue(`${trigger.method}_${trigger.path}`, context));
const api = new agw.RosApi(
scope,
`${event.key}_api_${key}`,
formatRosId(`${event.key}_agw_api_${key}`),
{
apiName: calcRefs(`${event.name}_api_${key}`, context),
apiName: calcRefs(`${event.name}-agw-api-${key.replace(/_/g, '-')}`, context),
groupId: apiGatewayGroup.attrGroupId,
visibility: 'PRIVATE',
authType: 'ANONYMOUS',
Expand Down Expand Up @@ -130,11 +129,14 @@ export const resolveEvents = (
);
api.addDependsOn(apiGatewayGroup);

new agw.Deployment(scope, `${service}_deployment`, {
new agw.Deployment(scope, formatRosId(`${event.key}_agw_api_deployment_${key}`), {
apiId: api.attrApiId,
groupId: apiGatewayGroup.attrGroupId,
stageName: 'RELEASE',
description: `${service} Api Gateway deployment`,
description: calcRefs(
`${service} Api Gateway deployment for api: ${trigger.method} ${trigger.path}`,
context,
),
});
});
});
Expand Down
Loading
Loading