Skip to content

Commit 42c6125

Browse files
committed
feat: deploy action create apigateway OK
Signed-off-by: seven <[email protected]>
1 parent 533f1c0 commit 42c6125

File tree

7 files changed

+169
-20
lines changed

7 files changed

+169
-20
lines changed

package-lock.json

Lines changed: 46 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
],
5151
"dependencies": {
5252
"@alicloud/openapi-client": "^0.4.11",
53+
"@alicloud/ros-cdk-apigateway": "^1.2.0",
5354
"@alicloud/ros-cdk-core": "^1.2.0",
5455
"@alicloud/ros-cdk-fc": "^1.2.0",
56+
"@alicloud/ros-cdk-ram": "^1.2.0",
5557
"@alicloud/ros20190910": "^3.4.3",
5658
"ajv": "^8.17.1",
5759
"chalk": "^4.1.2",

src/common/rosClient.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ROS20190910, {
44
CreateStackRequestParameters,
55
CreateStackRequestTags,
66
ListStacksRequest,
7+
UpdateStackRequest,
78
} from '@alicloud/ros20190910';
89
import { Config } from '@alicloud/openapi-client';
910
import { ActionContext } from '../types';
@@ -42,7 +43,7 @@ const createStack = async (stackName: string, templateBody: unknown, context: Ac
4243
return response.body?.stackId;
4344
};
4445

45-
const updateStack = async (stackName: string, templateBody: unknown, context: ActionContext) => {
46+
const updateStack = async (stackId: string, templateBody: unknown, context: ActionContext) => {
4647
const parameters = context.parameters?.map(
4748
(parameter) =>
4849
new CreateStackRequestParameters({
@@ -51,10 +52,12 @@ const updateStack = async (stackName: string, templateBody: unknown, context: Ac
5152
}),
5253
);
5354

54-
const createStackRequest = new CreateStackRequest({
55-
stackName,
56-
templateBody,
55+
const createStackRequest = new UpdateStackRequest({
56+
regionId: context.region,
57+
stackId,
58+
templateBody: JSON.stringify(templateBody),
5759
parameters,
60+
tags: context.tags?.map((tag) => new CreateStackRequestTags(tag)),
5861
});
5962

6063
const response = await client.updateStack(createStackRequest);
@@ -93,7 +96,7 @@ export const rosStackDeploy = async (
9396
}
9497

9598
printer.info(`Update stack: ${stackName} deploying... `);
96-
return await updateStack(stackName, templateBody, context);
99+
return await updateStack(stackInfo.stackId as string, templateBody, context);
97100
} else {
98101
// create stack
99102
printer.info(`Create stack: ${stackName} deploying... `);

src/iac/iacSchema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const schema = {
7979
'.*': {
8080
type: 'object',
8181
properties: {
82+
name: { type: 'string' },
8283
type: { type: 'string', enum: ['API_GATEWAY'] },
8384
triggers: {
8485
type: 'array',
@@ -90,7 +91,7 @@ const schema = {
9091
},
9192
},
9293
},
93-
required: ['type', 'triggers'],
94+
required: ['name', 'type', 'triggers'],
9495
},
9596
},
9697
},

src/stack/deploy.ts

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as ros from '@alicloud/ros-cdk-core';
22
import * as fc from '@alicloud/ros-cdk-fc';
3-
import { ActionContext, ServerlessIac } from '../types';
3+
import * as agw from '@alicloud/ros-cdk-apigateway';
4+
import * as ram from '@alicloud/ros-cdk-ram';
5+
6+
import { ActionContext, EventTypes, ServerlessIac } from '../types';
47
import { printer, rosStackDeploy } from '../common';
58
import path from 'node:path';
69
import * as fs from 'node:fs';
@@ -13,9 +16,10 @@ const resolveCode = (location: string): string => {
1316
};
1417

1518
export class IacStack extends ros.Stack {
16-
constructor(scope: ros.Construct, iac: ServerlessIac, props?: ros.StackProps) {
17-
super(scope, iac.service, props);
18-
new ros.RosInfo(this, ros.RosInfo.description, 'This is the simple ros cdk app example.');
19+
constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) {
20+
super(scope, iac.service);
21+
new ros.RosInfo(this, ros.RosInfo.description, `${iac.service} stack`);
22+
1923
const service = new fc.RosService(
2024
this,
2125
`${iac.service}-service`,
@@ -45,12 +49,98 @@ export class IacStack extends ros.Stack {
4549
);
4650
func.addDependsOn(service);
4751
});
52+
53+
const apiGateway = iac.events.find((event) => event.type === EventTypes.API_GATEWAY);
54+
if (apiGateway) {
55+
const gatewayAccessRole = new ram.RosRole(
56+
this,
57+
`${iac.service}_role`,
58+
{
59+
roleName: `${iac.service}-gateway-access-role`,
60+
description: `${iac.service} role`,
61+
assumeRolePolicyDocument: {
62+
version: '1',
63+
statement: [
64+
{
65+
action: 'sts:AssumeRole',
66+
effect: 'Allow',
67+
principal: {
68+
service: ['apigateway.aliyuncs.com'],
69+
},
70+
},
71+
],
72+
},
73+
policies: [
74+
{
75+
policyName: `${iac.service}-policy`,
76+
policyDocument: {
77+
version: '1',
78+
statement: [
79+
{
80+
action: ['fc:InvokeFunction'],
81+
effect: 'Allow',
82+
// @TODO implement at least permission granting
83+
resource: ['*'],
84+
},
85+
],
86+
},
87+
},
88+
],
89+
},
90+
true,
91+
);
92+
93+
const apiGatewayGroup = new agw.RosGroup(
94+
this,
95+
`${iac.service}_apigroup`,
96+
{
97+
groupName: `${iac.service}_apigroup`,
98+
},
99+
true,
100+
);
101+
102+
iac.events
103+
.filter((event) => event.type === EventTypes.API_GATEWAY)
104+
.forEach((event) => {
105+
event.triggers.forEach((trigger) => {
106+
const key = `${trigger.method}_${trigger.path}`.toLowerCase().replace(/\//g, '_');
107+
const api = new agw.RosApi(
108+
this,
109+
`${event.key}_api_${key}`,
110+
{
111+
apiName: `${event.name}_api_${key}`,
112+
groupId: apiGatewayGroup.attrGroupId,
113+
visibility: 'PRIVATE',
114+
requestConfig: {
115+
requestProtocol: 'HTTP',
116+
requestHttpMethod: trigger.method,
117+
requestPath: trigger.path,
118+
requestMode: 'PASSTHROUGH',
119+
},
120+
serviceConfig: {
121+
serviceProtocol: 'FunctionCompute',
122+
functionComputeConfig: {
123+
fcRegionId: context.region,
124+
serviceName: service.serviceName,
125+
functionName: trigger.backend,
126+
roleArn: gatewayAccessRole.attrArn,
127+
},
128+
},
129+
resultSample: 'ServerlessInsight resultSample',
130+
resultType: 'JSON',
131+
},
132+
true,
133+
);
134+
api.addDependsOn(apiGatewayGroup);
135+
});
136+
});
137+
}
48138
}
49139
}
50140

51-
const generateStackTemplate = (stackName: string, iac: ServerlessIac) => {
141+
const generateStackTemplate = (stackName: string, iac: ServerlessIac, context: ActionContext) => {
52142
const app = new ros.App();
53-
new IacStack(app, iac);
143+
new IacStack(app, iac, context);
54144

55145
const assembly = app.synth();
56146
const stackArtifact = assembly.getStackByName(stackName);
@@ -69,7 +159,7 @@ export const deployStack = async (
69159
) => {
70160
printer.info(`Deploying stack... ${JSON.stringify(iac)}`);
71161

72-
const { template, parameters } = generateStackTemplate(stackName, iac);
162+
const { template, parameters } = generateStackTemplate(stackName, iac, context);
73163
console.log('Generated ROS YAML:', JSON.stringify({ template, parameters }));
74164
await rosStackDeploy(stackName, template, { ...context, parameters });
75165
printer.info(`Stack deployed! 🎉`);

src/types.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type Stages = {
1010
[key: string]: Stage;
1111
};
1212

13+
export enum EventTypes {
14+
API_GATEWAY = 'API_GATEWAY',
15+
}
16+
1317
export type IacFunction = {
1418
name: string;
1519
key: string;
@@ -24,12 +28,14 @@ export type IacFunction = {
2428
};
2529

2630
export type Event = {
27-
type: string;
28-
source: string;
29-
function: string;
30-
batch_size?: number;
31-
enabled?: boolean;
32-
target: string;
31+
key: string;
32+
name: string;
33+
type: EventTypes;
34+
triggers: Array<{
35+
method: string;
36+
path: string;
37+
backend: string;
38+
}>;
3339
};
3440

3541
type Events = {

tests/fixtures/serverless-insignt.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tags:
1818
functions:
1919
insight_poc_fn:
2020
name: insight-poc-fn
21-
runtime: nodejs14
21+
runtime: nodejs18
2222
handler: index.handler
2323
code: artifacts/artifact.zip
2424
memory: 512
@@ -30,6 +30,7 @@ functions:
3030
events:
3131
gateway_event:
3232
type: API_GATEWAY
33+
name: insight-poc-gateway
3334
triggers:
3435
- method: GET
3536
path: /api/hello

0 commit comments

Comments
 (0)