feat: support custom fc-endpoint when create fc client

This commit is contained in:
wss-git 2021-07-29 09:40:29 +08:00
parent b3da0be0ba
commit a56064f35c
3 changed files with 45 additions and 32 deletions

View File

@ -5,16 +5,45 @@ import HELP from './common/help';
import { InputProps, isProperties, IProperties } from './interface/entity';
// import StdoutFormatter from './common/stdout-formatter';
import RemoteInvoke from './lib/remote-invoke';
import Client from './lib/client';
export default class FcRemoteInvoke {
async report(componentName: string, command: string, accountID: string): Promise<void> {
/**
* event
* @param inputs
* @returns
*/
async invoke(inputs: InputProps): Promise<any> {
const {
props,
eventPayload,
credentials,
isHelp,
invocationType,
} = await this.handlerInputs(inputs);
await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID);
if (isHelp) {
core.help(HELP);
return;
}
let fcClient;
if (!props.domainName) {
fcClient = await Client.buildFcClient(props.region, credentials);
}
const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID);
await remoteInvoke.invoke(props, eventPayload, { invocationType });
}
private async report(componentName: string, command: string, accountID: string): Promise<void> {
core.reportComponent(componentName, {
command,
uid: accountID,
});
}
async handlerInputs(inputs: InputProps): Promise<any> {
private async handlerInputs(inputs: InputProps): Promise<any> {
// 去除 args 的行首以及行尾的空格
const args: string = (inputs?.args || '').replace(/(^\s*)|(\s*$)/g, '');
logger.debug(`input args: ${args}`);
@ -85,27 +114,4 @@ export default class FcRemoteInvoke {
};
}
/**
* event
* @param inputs
* @returns
*/
public async invoke(inputs: InputProps): Promise<any> {
const {
props,
eventPayload,
credentials,
isHelp,
invocationType,
} = await this.handlerInputs(inputs);
await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID);
if (isHelp) {
core.help(HELP);
return;
}
const remoteInvoke = new RemoteInvoke(props.region, credentials, props.domainName);
await remoteInvoke.invoke(props, eventPayload, { invocationType });
}
}

View File

@ -1,14 +1,24 @@
import FC from '@alicloud/fc2';
import * as core from '@serverless-devs/core';
import { ICredentials } from '../interface/entity';
export default class Client {
static buildFcClient(region: string, credentials: ICredentials) {
static async buildFcClient(region: string, credentials: ICredentials) {
return new FC(credentials.AccountID, {
accessKeyID: credentials.AccessKeyID,
accessKeySecret: credentials.AccessKeySecret,
securityToken: credentials.SecurityToken,
region,
endpoint: await this.getFcEndpoint(),
timeout: 6000000,
})
}
private static async getFcEndpoint(): Promise<string | undefined> {
const fcDefault = await core.loadComponent('devsapp/fc-default');
const fcEndpoint: string = await fcDefault.get({ args: 'fc-endpoint' });
if (!fcEndpoint) { return undefined; }
const enableFcEndpoint: any = await fcDefault.get({ args: 'enable-fc-endpoint' });
return (enableFcEndpoint === true || enableFcEndpoint === 'true') ? fcEndpoint : undefined;
}
}

View File

@ -1,6 +1,5 @@
import _ from 'lodash';
import got from 'got';
import Client from './client';
import { IProperties, IEventPayload } from '../interface/entity';
import Event from './event';
import logger from '../common/logger';
@ -9,11 +8,9 @@ export default class RemoteInvoke {
fcClient: any;
accountId: string;
constructor(region: string, credentials, domainName) {
if (!domainName) {
this.accountId = credentials.AccountID;
this.fcClient = Client.buildFcClient(region, credentials);
}
constructor(fcClient: any, accountId: string) {
this.fcClient = fcClient;
this.accountId = accountId;
}
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType }) {