diff --git a/src/lib/client.ts b/src/lib/client.ts index 16f20f51..7b93e6dc 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -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 { - 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] : ''; - } } diff --git a/src/lib/component/alias.ts b/src/lib/component/alias.ts index 621fd13e..47060457 100644 --- a/src/lib/component/alias.ts +++ b/src/lib/component/alias.ts @@ -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, diff --git a/src/lib/component/on-demand.ts b/src/lib/component/on-demand.ts index 0564013f..27f80cbd 100644 --- a/src/lib/component/on-demand.ts +++ b/src/lib/component/on-demand.ts @@ -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, @@ -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, @@ -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; } @@ -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; } diff --git a/src/lib/component/provision.ts b/src/lib/component/provision.ts index 975248a4..18739021 100644 --- a/src/lib/component/provision.ts +++ b/src/lib/component/provision.ts @@ -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, diff --git a/src/lib/component/remove.ts b/src/lib/component/remove.ts index 5260bb89..74e3bb8a 100644 --- a/src/lib/component/remove.ts +++ b/src/lib/component/remove.ts @@ -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, diff --git a/src/lib/component/version.ts b/src/lib/component/version.ts index db0f1ab8..4f19f89a 100644 --- a/src/lib/component/version.ts +++ b/src/lib/component/version.ts @@ -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, diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 0ff9f570..bbad6a5c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -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; -}