Skip to content

Commit

Permalink
Merge pull request #376 from devsapp/integrated/fc-common
Browse files Browse the repository at this point in the history
feat: integrated fc common
  • Loading branch information
wss-git authored Oct 26, 2021
2 parents 272fc89 + e699e9b commit d96ea6f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 94 deletions.
114 changes: 36 additions & 78 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,50 @@
import FC from '@alicloud/fc2';
import * as core from '@serverless-devs/core';
import _ from 'lodash';
import { extract } from './utils';

FC.prototype.on_demand_list = async function (options = {}) {
return this.get('/on-demand-configs', options);
};
FC.prototype.alias_list = async function (serviceName, options = {}, headers?) {
return this.get(`/services/${serviceName}/aliases`, options, headers);
};
FC.prototype.version_list = async function (serviceName, options = {}, headers?) {
return this.get(`/services/${serviceName}/versions`, options, headers);
};
FC.prototype.on_demand_get = async function (serviceName, qualifier, functionName) {
return this.get(`/services/${serviceName}.${qualifier}/functions/${functionName}/on-demand-config`);
};
FC.prototype.on_demand_put = async function (serviceName, qualifier, functionName, options = {}) {
return this.put(`/services/${serviceName}.${qualifier}/functions/${functionName}/on-demand-config`, options);
};
FC.prototype.on_demand_delete = async function (serviceName, qualifier, functionName) {
return this.delete(`/services/${serviceName}.${qualifier}/functions/${functionName}/on-demand-config`);
};
FC.prototype.get_all_list_data = async function (path, dataKeyword, options: { [key: string]: any } = {}, headers?) {
let data = [];
do {
const res = await this.get(path, options, headers);

const keywordData = res.data?.[dataKeyword];
options.nextToken = res.data?.nextToken;

if (!_.isEmpty(keywordData)) {
data = data.concat(keywordData);
}
} while (options.nextToken);

return data;
};

export default class Client {
static fcClient: any;

static async setFcClient(region: string, credentials) {
const {
AccountID,
AccessKeyID,
AccessKeySecret,
SecurityToken,
} = credentials;
const customFcEndpoint = await Client.getFcEndpoint();
const uid = customFcEndpoint?.accountId || AccountID;
const fcClient = new FC(uid, {
accessKeyID: AccessKeyID,
accessKeySecret: AccessKeySecret,
securityToken: SecurityToken,
region: customFcEndpoint?.region || region,
endpoint: customFcEndpoint?.fcEndpoint,
timeout: 6000000,
static async setFcClient(region: string, credentials, access: string) {
const fcCommon = await core.loadComponent('devsapp/fc-common');
const fcClient = await fcCommon.makeFcClient({
project: { access },
credentials,
props: {
region,
},
});
/**
* 获取所有的数据
* @param path 请求的路径
* @param dataKeyword 返回值的关键字
* @param options 请求的参数
* @param headers 请求头
* @returns 列表数据
*/
fcClient.get_all_list_data = async function (
path: string,
dataKeyword: string,
options: { [key: string]: any } = {},
headers?: any,
) {
let data = [];
do {
const res = await this.get(path, options, headers);

const keywordData = res.data?.[dataKeyword];
options.nextToken = res.data?.nextToken;

if (!_.isEmpty(keywordData)) {
data = data.concat(keywordData);
}
} while (options.nextToken);

return data;
};

this.fcClient = fcClient;

return fcClient;
}

static async getFcEndpoint(): Promise<any> {
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' });
if (!(enableFcEndpoint === true || enableFcEndpoint === 'true')) { return undefined; }
return {
fcEndpoint,
accountId: Client.extractAccountId(fcEndpoint),
region: Client.extractRegion(fcEndpoint),
protocol: Client.extractProtocol(fcEndpoint),
};
}

static extractAccountId(endpoint: string) {
return extract(/^https?:\/\/([^.]+)\..+$/, endpoint);
}

static extractRegion(endpoint: string) {
return extract(/^https?:\/\/[^.]+\.([^.]+)\..+$/, endpoint);
}

static extractProtocol(endpoint: string) {
const array = endpoint.split(':', 1);
return array.length !== 0 ? array[0] : '';
}
}
2 changes: 1 addition & 1 deletion src/lib/component/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class Alias {

const credentials: ICredentials = await getCredentials(inputs.credentials, inputs?.project?.access);
logger.debug(`handler inputs props: ${JSON.stringify(endProps)}`);
await Client.setFcClient(endProps.region, credentials);
await Client.setFcClient(endProps.region, inputs.credentials, inputs?.project?.access);

return {
credentials,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/component/on-demand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class OnDemand {

const credentials: ICredentials = await getCredentials(inputs.credentials, inputs?.project?.access);
logger.debug(`handler inputs props: ${JSON.stringify(endProps)}`);
await Client.setFcClient(endProps.region, credentials);
await Client.setFcClient(endProps.region, credentials, inputs?.project?.access);

return {
credentials,
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class OnDemand {
throw new Error('Not found service name');
}
logger.info(`Getting on-demand: ${serviceName}.${qualifier}/${functionName}`);
const { data } = await Client.fcClient.on_demand_get(serviceName, qualifier, functionName);
const { data } = await Client.fcClient.getOnDemandConfig(serviceName, qualifier, functionName);
if (data) {
return {
serviceName,
Expand All @@ -139,7 +139,7 @@ export default class OnDemand {
throw new Error('Not found service name');
}
logger.info(`Removing on-demand: ${serviceName}.${qualifier}/${functionName}`);
const { data } = await Client.fcClient.on_demand_delete(serviceName, qualifier, functionName);
const { data } = await Client.fcClient.deleteOnDemandConfig(serviceName, qualifier, functionName);
return data;
}

Expand All @@ -163,7 +163,7 @@ export default class OnDemand {
};

logger.info(`Updating on-demand: ${serviceName}.${qualifier}/${functionName}`);
const { data } = await Client.fcClient.on_demand_put(serviceName, qualifier, functionName, options);
const { data } = await Client.fcClient.putOnDemandConfig(serviceName, qualifier, functionName, options);
return data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/component/provision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class Provision {

const credentials: ICredentials = await getCredentials(inputs.credentials, inputs?.project?.access);
logger.debug(`handler inputs props: ${JSON.stringify(endProps)}`);
await Client.setFcClient(endProps.region, credentials);
await Client.setFcClient(endProps.region, credentials, inputs?.project?.access);

return {
credentials,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/component/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class Remove {

const credentials: ICredentials = await getCredentials(inputs.credentials, inputs?.project?.access);
logger.debug(`handler inputs props: ${JSON.stringify(endProps)}`);
await Client.setFcClient(endProps.region, credentials);
await Client.setFcClient(endProps.region, credentials, inputs?.project?.access);

return {
credentials,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/component/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class Version {

const credentials: ICredentials = await getCredentials(inputs.credentials, inputs?.project?.access);
logger.debug(`handler inputs props: ${JSON.stringify(endProps)}`);
await Client.setFcClient(endProps.region, credentials);
await Client.setFcClient(endProps.region, credentials, inputs?.project?.access);

return {
credentials,
Expand Down
8 changes: 0 additions & 8 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,3 @@ export async function componentMethodCaller(inputs: IInputs, componentName: stri
logger.debug(`Inputs of component: ${componentName} is: ${JSON.stringify(inputs, null, ' ')}`);
return await componentIns[methodName](inputs);
}

export function extract(regex: RegExp, endpoint: string) {
const matchs = endpoint.match(regex);
if (matchs) {
return matchs[1];
}
return null;
}

0 comments on commit d96ea6f

Please sign in to comment.