Skip to content

Commit 227cc82

Browse files
authored
Merge pull request #179 from devsapp/handler-role
Handler role
2 parents 3bf91db + 1633732 commit 227cc82

File tree

10 files changed

+272
-52
lines changed

10 files changed

+272
-52
lines changed

dist/build/Release/cpufeatures.node

0 Bytes
Binary file not shown.

dist/index.js

Lines changed: 159 additions & 46 deletions
Large diffs are not rendered by default.

dist/lib/fc/trigger.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,6 @@ export declare class FcTrigger extends FcDeploy<TriggerConfig> {
9595
isTimerTrigger(): boolean;
9696
makeInvocationRole(): Promise<string>;
9797
generateSystemDomain(): Promise<string>;
98+
handlerEbSlr(): Promise<void>;
9899
makeTrigger(): Promise<TriggerConfig>;
99100
}
Binary file not shown.

dist/lib/resource/ram.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export declare function isAutoGeneratedRole(role: any): boolean;
1919
export declare class AlicloudRam extends AlicloudClient {
2020
genRamComponentProp(roleName: string, resourceName?: string, assumeRolePolicy?: any, attachedPolicies?: Array<string | CustomPolicyConfig>, description?: string): any;
2121
makeRole(roleName: string, serviceName: string, args?: string, description?: string, resourceName?: string, assumeRolePolicy?: any, attachedPolicies?: Array<string | CustomPolicyConfig>): Promise<string>;
22+
checkRoleExist({ arn }: {
23+
arn: any;
24+
}): Promise<any>;
25+
createServiceLinkedRole({ serviceName }: {
26+
serviceName: any;
27+
}): Promise<any>;
2228
}
2329
export declare function extractRoleNameFromArn(roleArn: string): string;
2430
export declare function checkRoleArnFormat(roleArn: string): void;

dist/setup-sandbox.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Object.defineProperties(global, {
6767
global: {value: global, writable: true, configurable: true, enumerable: true},
6868
globalThis: {value: global, writable: true, configurable: true},
6969
GLOBAL: {value: global, writable: true, configurable: true},
70-
root: {value: global, writable: true, configurable: true}
70+
root: {value: global, writable: true, configurable: true},
71+
Error: {value: LocalError}
7172
});
7273

7374
if (!localReflectDefineProperty(global, 'VMError', {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"dependencies": {
66
"@alicloud/fc2": "^2.3.0",
77
"@alicloud/pop-core": "^1.7.10",
8-
"@alicloud/ram": "^1.0.0",
98
"@serverless-devs/core": "latest",
109
"ali-oss": "^6.1.1",
1110
"deep-object-diff": "^1.1.0",

publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Component
22
Name: fc-deploy
33
Provider:
44
- 阿里云
5-
Version: 0.0.90
5+
Version: 0.0.91
66
Description: 阿里云函数计算基础组件
77
HomePage: https://github.com/devsapp/fc-base
88
Tags:

src/lib/fc/trigger.ts

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'lodash';
1+
import { lodash as _, CatchableError } from '@serverless-devs/core';
22
import { generateRamResourceName, CustomPolicyConfig, AlicloudRam } from '../resource/ram';
33
import { DESCRIPTION } from '../static';
44
import { ServerlessProfile, ICredentials, getFcEndpoint } from '../profile';
@@ -355,7 +355,7 @@ export class FcTrigger extends FcDeploy<TriggerConfig> {
355355
assumeRolePolicy,
356356
null,
357357
' ',
358-
)}, policy: ${policyConf}`,
358+
)}, policy: ${JSON.stringify(policyConf)}`,
359359
);
360360
const alicloudRam = new AlicloudRam(
361361
this.serverlessProfile,
@@ -381,6 +381,42 @@ export class FcTrigger extends FcDeploy<TriggerConfig> {
381381
return `${endpoint}/2016-08-15/proxy/${this.serviceName}/${this.functionName}/`;
382382
}
383383

384+
async handlerEbSlr() {
385+
const eventSourceType = _.get(this.localConfig, 'config.eventSourceConfig.eventSourceType');
386+
if (_.isEmpty(eventSourceType) || !_.isString(eventSourceType)) {
387+
throw new CatchableError('Eventbridge trigger config.eventSourceConfig.eventSourceType (required)');
388+
}
389+
const alicloudRam = new AlicloudRam(
390+
this.serverlessProfile,
391+
this.credentials,
392+
this.region,
393+
this.curPath,
394+
);
395+
try {
396+
await alicloudRam.createServiceLinkedRole({ serviceName: 'sendevent-fc.eventbridge.aliyuncs.com' });
397+
this.logger.debug('createServiceLinkedRole: SendToFC success');
398+
} catch (ex) {
399+
this.logger.debug(`handler SendToFC error: ${ex}`);
400+
}
401+
402+
try {
403+
const SOURCE_TYPE = {
404+
Default: ['source-cms.eventbridge.aliyuncs.com', 'source-actiontrail.eventbridge.aliyuncs.com'],
405+
MNS: ['sendevent-mns.eventbridge.aliyuncs.com'],
406+
RocketMQ: ['source-rocketmq.eventbridge.aliyuncs.com'],
407+
RabbitMQ: ['source-rabbitmq.eventbridge.aliyuncs.com'],
408+
Kafka: ['source-kafka.eventbridge.aliyuncs.com'],
409+
};
410+
const sourceType = _.get(SOURCE_TYPE, eventSourceType, []);
411+
for (const serviceName of sourceType) {
412+
await alicloudRam.createServiceLinkedRole({ serviceName });
413+
this.logger.debug(`createServiceLinkedRole ${serviceName} success`);
414+
}
415+
} catch (ex) {
416+
this.logger.debug(`handler Eb Slr error: ${ex}`);
417+
}
418+
}
419+
384420
async makeTrigger(): Promise<TriggerConfig> {
385421
if (this.useRemote) {
386422
this.statefulConfig = _.cloneDeep(this.remoteConfig);
@@ -409,9 +445,37 @@ export class FcTrigger extends FcDeploy<TriggerConfig> {
409445
delete remoteConfig.lastModifiedTime;
410446
}
411447

412-
if (!_.isNil(this.localConfig.role) || this.isEBTrigger() || this.isHttpTrigger() || this.isTimerTrigger()) {
448+
if (this.isEBTrigger()) {
449+
// TODO: https://github.com/devsapp/fc/issues/827
450+
await this.handlerEbSlr();
451+
return resolvedTriggerConf;
452+
}
453+
454+
if (this.isHttpTrigger() || this.isTimerTrigger()) {
413455
return resolvedTriggerConf;
414456
}
457+
458+
// check role ( https://github.com/devsapp/fc/issues/805 )
459+
if (!_.isNil(this.localConfig.role)) {
460+
try {
461+
const arn = this.localConfig.role;
462+
const alicloudRam = new AlicloudRam(
463+
this.serverlessProfile,
464+
this.credentials,
465+
this.region,
466+
this.curPath,
467+
);
468+
const roleExist = await alicloudRam.checkRoleExist({ arn });
469+
if (!roleExist) {
470+
this.logger.log('');
471+
this.logger.warn(`It is detected that the role(${arn}) configured by the trigger does not exist. Please go to the ram console(https://ram.console.aliyun.com/roles) reconfirm`);
472+
}
473+
} catch (ex) {
474+
this.logger.debug(`Check role exist error: ${ex}`);
475+
}
476+
return resolvedTriggerConf;
477+
}
478+
415479
const role = await this.makeInvocationRole();
416480
Object.assign(resolvedTriggerConf, {
417481
role,

src/lib/resource/ram.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,42 @@ export class AlicloudRam extends AlicloudClient {
111111
const roleArn = await ramComponentIns.deploy(ramComponentInputs);
112112
return roleArn;
113113
}
114+
115+
async checkRoleExist({ arn }): Promise<any> {
116+
const roleName = extractRoleNameFromArn(arn);
117+
const ramComponent = new RamComponent(
118+
this.serverlessProfile,
119+
{ roleName } as any,
120+
this.region,
121+
this.credentials,
122+
this.curPath,
123+
);
124+
const ramComponentInputs = ramComponent.genComponentInputs('ram', '');
125+
logger.spinner?.stop();
126+
const ramComponentIns = await core.loadComponent('devsapp/ram');
127+
logger.spinner?.start();
128+
const roleExist = await ramComponentIns.check(ramComponentInputs);
129+
this.logger.debug(`roleExist: ${roleExist}`);
130+
return roleExist;
131+
}
132+
133+
// 此 serviceName 不是fc的服务,而是ram的服务关联角色的云服务的服务 说明链接 https://help.aliyun.com/document_detail/160674.htm?spm=a2c4g.11186623.0.0.6a0c315chezXDu
134+
async createServiceLinkedRole({ serviceName }): Promise<any> {
135+
const ramComponent = new RamComponent(
136+
this.serverlessProfile,
137+
{ serviceName } as any,
138+
this.region,
139+
this.credentials,
140+
this.curPath,
141+
);
142+
const ramComponentInputs = ramComponent.genComponentInputs('ram', '');
143+
logger.spinner?.stop();
144+
const ramComponentIns = await core.loadComponent('devsapp/ram');
145+
logger.spinner?.start();
146+
const roleExist = await ramComponentIns.createServiceLinkedRole(ramComponentInputs);
147+
this.logger.debug(`roleExist: ${roleExist}`);
148+
return roleExist;
149+
}
114150
}
115151

116152
export function extractRoleNameFromArn(roleArn: string): string {

0 commit comments

Comments
 (0)