diff --git a/.eslintrc.js b/.eslintrc.js index 60a9b4f..420e593 100755 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -43,7 +43,6 @@ module.exports = { } ], 'no-alert': 'error', - 'no-console': 'error', 'no-const-assign': 'error', 'no-else-return': 'error', 'no-empty': 'off', diff --git a/Changelog.md b/Changelog.md index e44ee92..185fea1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,12 @@ # 变更历史 + +## 2020-02-06 +* 优化COS实例创建代码 + +## 2020-02-06 +* 修复rollback操作可能出现函数状态错误问题 +* 优化函数状态检测部分代码 + ## 2019-11-26 * 更新函数状态,提高部署成功率 diff --git a/deploy/lib/deployFunction.js b/deploy/lib/deployFunction.js index 2ab1a9d..e206538 100644 --- a/deploy/lib/deployFunction.js +++ b/deploy/lib/deployFunction.js @@ -21,19 +21,7 @@ class DeployFunction extends AbstractHandler { } this.serverless.cli.log('Updating code... ') await this.updateFunctionCode(ns, funcObject) - // when update code Status is Active, continue - let status = 'Updating' - let times = 90 - while (status == 'Updating') { - const tempFunc = await this.getFunction(ns, funcObject.FuncName) - status = tempFunc.Status - await utils.sleep(1000) - times = times - 1 - if (times <= 0) { - throw `Function ${funcObject.FuncName} update failed` - } - } - if (status != 'Active') { + if ((await this.checkStatus(ns, funcObject)) == false) { throw `Function ${funcObject.FuncName} update failed` } this.serverless.cli.log('Updating configure... ') @@ -43,6 +31,18 @@ class DeployFunction extends AbstractHandler { return null } + async checkStatus(ns, funcObject) { + let status = 'Updating' + let times = 90 + while ((status == 'Updating' || status == 'Creating') && times > 0) { + const tempFunc = await this.getFunction(ns, funcObject.FuncName) + status = tempFunc.Status + await utils.sleep(1000) + times = times - 1 + } + return status != 'Active' ? false : true + } + async addRole() { try { const roleName = 'SCF_QcsRole' diff --git a/deploy/lib/deployTrigger.js b/deploy/lib/deployTrigger.js index f7051d9..9af731f 100644 --- a/deploy/lib/deployTrigger.js +++ b/deploy/lib/deployTrigger.js @@ -96,9 +96,12 @@ class DeployTrigger extends AbstractHandler { api: { authRequired: 'FALSE', requestConfig: { - method: trigger.Properties.HttpMethod || 'GET' + method: trigger.Properties.HttpMethod || 'GET', + path: trigger.Properties.Path || '/' + funcObject.FuncName }, - isIntegratedResponse: trigger.Properties.IntegratedResponse ? 'TRUE' : 'FALSE' + isIntegratedResponse: trigger.Properties.IntegratedResponse ? 'TRUE' : 'FALSE', + enableCORS: trigger.Properties.EnableCORS ? 'TRUE' : 'FALSE', + serviceTimeout: trigger.Properties.ServiceTimeout || 10 }, service: { serviceName: 'SCF_API_SERVICE' diff --git a/deploy/tencentDeploy.js b/deploy/tencentDeploy.js index 3471702..01eec4d 100644 --- a/deploy/tencentDeploy.js +++ b/deploy/tencentDeploy.js @@ -26,17 +26,9 @@ class TencentDeploy { } async deploy() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) const services = this.provider.getServiceResource() const func = new DeployFunction(this.options, this.serverless) @@ -74,19 +66,7 @@ class TencentDeploy { this.serverless.cli.log(`Setting tags for function ${funcObject.FuncName}`) await func.createTags('default', funcObject.FuncName, funcObject.Properties.Tags) - // Function status - let status = 'Updating' - let times = 90 - while (status == 'Updating' || status == 'Creating') { - const tempFunc = await func.getFunction('default', funcObject.FuncName) - status = tempFunc.Status - await utils.sleep(1000) - times = times - 1 - if (times <= 0) { - throw `Function ${funcObject.FuncName} create/update failed` - } - } - if (status != 'Active') { + if ((await func.checkStatus('default', funcObject)) == false) { throw `Function ${funcObject.FuncName} create/update failed` } @@ -132,11 +112,17 @@ class TencentDeploy { if (this.options.function && this.options.function != funcName) { continue } + const funcObject = _.cloneDeep(services.Resources.default[funcName]) + funcObject.Name = funcName + funcObject.FuncName = this.provider.getFunctionName(funcName) const deployFunctionName = this.provider.getFunctionName(funcName) outputInformation = outputInformation + ` ${funcName}: ${deployFunctionName}\n` functionInformation = await func.getFunction('default', deployFunctionName, false) if (functionInformation.Triggers && functionInformation.Triggers.length > 0) { for (let i = 0; i <= functionInformation.Triggers.length; i++) { + if ((await func.checkStatus('default', funcObject)) == false) { + throw `Function ${funcObject.FuncName} create/update failed` + } const thisTrigger = functionInformation.Triggers[i] try { if (thisTrigger.Type == 'apigw') { diff --git a/deploy/tencentDeployFunction.js b/deploy/tencentDeployFunction.js index b2c7aca..01ed6f2 100644 --- a/deploy/tencentDeployFunction.js +++ b/deploy/tencentDeployFunction.js @@ -31,17 +31,9 @@ class TencentDeployFunction { } async deploy() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) const services = this.provider.getServiceResource() @@ -87,25 +79,17 @@ class TencentDeployFunction { const oldFunc = await func.deploy('default', funcObject) this.serverless.cli.log(`Created function ${funcObject.FuncName}`) + if ((await func.checkStatus('default', funcObject)) == false) { + throw `Function ${funcObject.FuncName} create/update failed` + } + this.serverless.cli.log(`Updating configure for function ${funcObject.FuncName}`) await func.updateConfiguration('default', oldFunc, funcObject) this.serverless.cli.log(`Setting tags for function ${funcObject.FuncName}`) await func.createTags('default', funcObject.FuncName, funcObject.Properties.Tags) - // Function status - let status = 'Updating' - let times = 90 - while (status == 'Updating' || status == 'Creating') { - const tempFunc = await func.getFunction('default', funcObject.FuncName) - status = tempFunc.Status - await utils.sleep(1000) - times = times - 1 - if (times <= 0) { - throw `Function ${funcObject.FuncName} create/update failed` - } - } - if (status != 'Active') { + if ((await func.checkStatus('default', funcObject)) == false) { throw `Function ${funcObject.FuncName} create/update failed` } @@ -146,6 +130,9 @@ class TencentDeployFunction { const functionInformation = await func.getFunction('default', deployFunctionName, false) if (functionInformation.Triggers && functionInformation.Triggers.length > 0) { for (let i = 0; i <= functionInformation.Triggers.length; i++) { + if ((await func.checkStatus('default', funcObject)) == false) { + throw `Function ${funcObject.FuncName} create/update failed` + } const thisTrigger = functionInformation.Triggers[i] try { if (thisTrigger.Type == 'apigw') { diff --git a/deploy/tencentDeployList.js b/deploy/tencentDeployList.js index ab3956f..4484548 100644 --- a/deploy/tencentDeployList.js +++ b/deploy/tencentDeployList.js @@ -23,17 +23,10 @@ class TencentDeployList { } async serviceList() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) + const Handler = new RollbackService(this.options, this.serverless) const fileKeyPrefix = this.serverless.service.service + '-' + this.options.stage const cosBucket = this.provider.getDeployCosBucket() diff --git a/deploy/tencentDeployListFunctions.js b/deploy/tencentDeployListFunctions.js index 99059f8..3ae3335 100644 --- a/deploy/tencentDeployListFunctions.js +++ b/deploy/tencentDeployListFunctions.js @@ -25,17 +25,10 @@ class TencentDeployListFunction { } async functionList() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) + try { const infoHandler = new InfoFunction(this.options, this.serverless) const deployHandler = new ListFunctions(this.options, this.serverless) diff --git a/docs/en/yaml.md b/docs/en/yaml.md index b484556..d2b19e3 100644 --- a/docs/en/yaml.md +++ b/docs/en/yaml.md @@ -85,6 +85,9 @@ functions: # serviceId: # httpMethod: ANY # integratedResponse: true +# path: /abc/cde +# enableCORS: true +# serviceTimeout: 10 # - cmq: # name: cmq_trigger # parameters: diff --git a/docs/zh/yaml.md b/docs/zh/yaml.md index 900403f..f07b422 100644 --- a/docs/zh/yaml.md +++ b/docs/zh/yaml.md @@ -83,6 +83,10 @@ functions: # stageName: release # serviceId: # httpMethod: ANY +# integratedResponse: true +# path: /abc/cde +# enableCORS: true +# serviceTimeout: 10 # - cmq: # name: cmq_trigger # parameters: diff --git "a/docs/zh/\346\211\223\345\214\205\346\234\215\345\212\241.md" "b/docs/zh/\346\211\223\345\214\205\346\234\215\345\212\241.md" index 58661e5..a066fcf 100644 --- "a/docs/zh/\346\211\223\345\214\205\346\234\215\345\212\241.md" +++ "b/docs/zh/\346\211\223\345\214\205\346\234\215\345\212\241.md" @@ -11,7 +11,7 @@ serverless package ## 参数说明 - `--stage` 或`-s` 目标部署环境,您可以自定义指定诸如`dev`,`pro` 等环境参数,默认为`dev`。部署后,环境参数将追加至云函数名之后,且会作为云函数的标签。 -- `--region`或`-r` 目标部署区域,默认为 `ap-guangzhou` +- `--region`或`-r` 目标部署区域,默认为广州 `ap-guangzhou` - `--package`或`-p` 自定义部署包目录 diff --git a/index.js b/index.js index feb1846..8aa36de 100755 --- a/index.js +++ b/index.js @@ -27,7 +27,6 @@ class TencentIndex { this.serverless.pluginManager.addPlugin(TencentInfo) this.serverless.pluginManager.addPlugin(TencentLogs) } - } module.exports = TencentIndex diff --git a/info/tencentInfo.js b/info/tencentInfo.js index c280e9a..380d0a8 100644 --- a/info/tencentInfo.js +++ b/info/tencentInfo.js @@ -24,17 +24,9 @@ class TencentInfo { } async info() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) try { const region = this.options.region const handler = new InfoFunction(this.options, this.serverless) diff --git a/invoke/tencentInvoke.js b/invoke/tencentInvoke.js index 34984f2..0ac662f 100644 --- a/invoke/tencentInvoke.js +++ b/invoke/tencentInvoke.js @@ -23,21 +23,10 @@ class TencentInvoke { } async invoke() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) try { - const options = { - region: this.options.region - } const invokeHandler = new Invoke(this.options, this.serverless) let context = null if (this.options.data) { diff --git a/logs/tencentLogs.js b/logs/tencentLogs.js index 98dae68..f9a9e3b 100644 --- a/logs/tencentLogs.js +++ b/logs/tencentLogs.js @@ -49,17 +49,9 @@ class TencentLogs { } async logs() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) try { const timeFormat = 'yyyy-MM-dd hh:mm:ss' this.serverless.cli.log(`Get function logs...`) @@ -103,6 +95,7 @@ class TencentLogs { } } } else { + const handler = new LogsFunction(this.options, this.serverless) const result = await handler.logs( functionName, this.options.startTime || this.getTimeFunction(timeFormat, 1 * 60 * 60 * 1000), diff --git a/metrics/tencentMetrics.js b/metrics/tencentMetrics.js index 12c7252..2ba4723 100644 --- a/metrics/tencentMetrics.js +++ b/metrics/tencentMetrics.js @@ -57,17 +57,9 @@ class TencentInfo { } async metrics() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) try { const Handler = new MetricsFunction(this.options, this.serverless) const functionList = await Handler.functionList( diff --git a/package.json b/package.json index df8aac9..bbdc032 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-tencent-scf", - "version": "0.1.19", + "version": "0.1.37", "description": "Provider plugin for the Serverless Framework v1.x which adds support for Tencent Cloud Functions.", "main": "index.js", "author": "Tencent Cloud, Inc.", @@ -43,7 +43,9 @@ "request": "^2.88.0", "tencentcloud-sdk-nodejs": "^3.0.87", "universal-analytics": "^0.4.20", - "tencent-login": "^0.1.6" + "tencent-login": "^0.1.6", + "serverless-tencent-tools": "^0.0.11", + "serverless-tencent-auth-tool": "^1.0.18" }, "devDependencies": { "coveralls": "^3.0.5", diff --git a/provider/tencentProvider.js b/provider/tencentProvider.js index ad5d844..3cb97e7 100644 --- a/provider/tencentProvider.js +++ b/provider/tencentProvider.js @@ -4,12 +4,17 @@ const os = require('os') const ini = require('ini') const _ = require('lodash') const util = require('util') +const QRCode = require('qrcode') const TencentLogin = require('tencent-login') const tencentcloud = require('tencentcloud-sdk-nodejs') +const serverlessTencentTools = require('serverless-tencent-tools') +const { GetUserAuthInfo } = serverlessTencentTools.Account +const { DataReport } = serverlessTencentTools.Others.DataReport const ClientProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/client_profile.js') const HttpProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/http_profile.js') const AbstractModel = require('tencentcloud-sdk-nodejs/tencentcloud/common/abstract_model') const AbstractClient = require('tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client') + const constants = { providerName: 'tencent' } @@ -17,7 +22,6 @@ const constants = { class GetUserAppIdResponse extends AbstractModel { constructor() { super() - this.RequestId = null } @@ -26,6 +30,7 @@ class GetUserAppIdResponse extends AbstractModel { return } this.AppId = 'RequestId' in params ? params.AppId : null + this.OwnerUin = 'RequestId' in params ? params.OwnerUin : null this.RequestId = 'RequestId' in params ? params.RequestId : null } } @@ -48,12 +53,83 @@ class TencentProvider { this.getCredentials(this.serverless, this.options) this.serverless.setProvider(constants.providerName, this) this.provider = this + let commands = '' + const commandsAttr = this.serverless.pluginManager.cliCommands + for (let i = 0; i < commandsAttr.length; i++) { + commands = commands + (i == 0 ? '' : '_') + commandsAttr[i] + } + this.reportInputs = { + name: 'serverless-tencent-scf', + project: this.serverless.service.service, + action: commands + } + try { + new DataReport().report(this.reportInputs) + } catch (e) {} } static getProviderName() { return constants.providerName } + async getUserCred(options) { + if (!options.credentials || !options.credentials.tencent_secret_id) { + const tencentTemp = await this.getTempKey() + this.options.credentials = { + tencent_secret_id: tencentTemp.tencent_secret_id, + tencent_secret_key: tencentTemp.tencent_secret_key, + tencent_appid: tencentTemp.tencent_appid, + tencent_owneruin: tencentTemp.tencent_owneruin + } + options.token = tencentTemp.token + options.timestamp = tencentTemp.timestamp + } + if (!options.credentials.tencent_owneruin || !options.credentials.tencent_appid) { + const appid = await this.getAppid({ + SecretId: options.credentials.tencent_secret_id, + SecretKey: options.credentials.tencent_secret_key + }) + options.credentials.tencent_appid = appid.AppId + options.credentials.tencent_owneruin = appid.OwnerUin + this.tempUin = appid.OwnerUin + } + return options + } + + async getUserAuth(uin) { + try { + try { + this.reportInputs.uin = uin + new DataReport().report(this.reportInputs) + } catch (e) {} + const getUserAuthInfo = new GetUserAuthInfo() + const result = await getUserAuthInfo.isAuth(uin, this.reportInputs) + if (result['Error'] == true) { + console.log('未获取到实名认证结果,请重试。') + process.exit(-1) + } else { + if (result['Message']['Authentication'] == 1) { + return true + } + const verifyUrl = 'https://cloud.tencent.com/verify/identity' + console.log( + '系统检测您还未进行实名认证,请您扫描二下方二维码或者进入下方的地址,进行实名认证,实名认证之后可重新操作。' + ) + console.log('实名认证地址: ') + console.log('https://console.cloud.tencent.com/developer/auth') + console.log('实名认证二维码: ') + QRCode.toString(verifyUrl, { type: 'terminal' }, function(err, url) { + console.log(url) + }) + console.log('请实名认证之后,重新执行本次操作。') + process.exit(-1) + } + } catch (e) { + console.log(e) + process.exit(-1) + } + } + getAppid(credentials) { const secret_id = credentials.SecretId const secret_key = credentials.SecretKey @@ -137,6 +213,14 @@ class TencentProvider { uuid: tencent_credentials.uuid, timestamp: tencent_credentials.timestamp } + // From cam to getting appid + const userInfo = await this.getAppid({ + SecretId: tencent.tencent_secret_id, + SecretKey: tencent.tencent_secret_key, + token: tencent.token + }) + tencent.tencent_owneruin = userInfo.OwnerUin + tencent.tencent_appid = userInfo.AppId await fs.writeFileSync('./.env_temp', JSON.stringify(tencent)) return tencent } catch (e) { @@ -152,10 +236,16 @@ class TencentProvider { try { const tencent = {} const tencent_credentials_read = JSON.parse(data) - if ( - Date.now() / 1000 - tencent_credentials_read.timestamp <= 6000 && - tencent_credentials_read.tencent_appid - ) { + if (Date.now() / 1000 - tencent_credentials_read.timestamp <= 6000) { + const userInfo = await this.getAppid({ + SecretId: tencent_credentials_read.tencent_secret_id, + SecretKey: tencent_credentials_read.tencent_secret_key, + token: tencent_credentials_read.token + }) + + // From cam to getting appid + tencent_credentials_read.tencent_owneruin = userInfo.OwnerUin + tencent_credentials_read.tencent_appid = userInfo.AppId return tencent_credentials_read } const login = new TencentLogin() @@ -175,6 +265,14 @@ class TencentProvider { tencent.uuid = tencent_credentials_read.uuid tencent.timestamp = Date.now() / 1000 await fs.writeFileSync('./.env_temp', JSON.stringify(tencent)) + // From cam to getting appid + const userInfo = await this.getAppid({ + SecretId: tencent.tencent_secret_id, + SecretKey: tencent.tencent_secret_key, + token: tencent.token + }) + tencent.tencent_owneruin = userInfo.OwnerUin + tencent.tencent_appid = userInfo.AppId return tencent } return await that.doLogin() @@ -236,13 +334,12 @@ class TencentProvider { } }) // From cam to getting appid - if (!this.options.credentials.tencent_appid) { - const appid = await this.getAppid({ - SecretId: this.options.credentials.tencent_secret_id, - SecretKey: this.options.credentials.tencent_secret_key - }) - this.options.credentials.tencent_appid = appid.AppId - } + const appid = await this.getAppid({ + SecretId: this.options.credentials.tencent_secret_id, + SecretKey: this.options.credentials.tencent_secret_key + }) + this.options.credentials.tencent_appid = appid.AppId + this.options.credentials.tencent_owneruin = appid.OwnerUin } catch (e) {} return } @@ -261,7 +358,7 @@ class TencentProvider { enableRoleAuth: provider.enableRoleAuth || true, Type: 'Event', Description: funcObject.description || provider.description || '', - Role: funcObject.role || provider.role || 'QCS_SCFExcuteRole', + Role: funcObject.role || provider.role, Handler: funcObject.handler || 'index.main_handler', MemorySize: funcObject.memorySize || provider.memorySize || 128, Timeout: funcObject.timeout || provider.timeout || 3, @@ -340,7 +437,10 @@ class TencentProvider { HttpMethod: event.parameters.httpMethod, ServiceId: serviceId, IntegratedResponse: event.parameters.integratedResponse, - Enable: event.parameters.enable + Enable: event.parameters.enable, + Path: event.parameters.path, + ServiceTimeout: event.parameters.serviceTimeout, + EnableCORS: event.parameters.enableCORS } } return trigger diff --git a/remove/tencentRemove.js b/remove/tencentRemove.js index 0a9b268..8b19884 100644 --- a/remove/tencentRemove.js +++ b/remove/tencentRemove.js @@ -24,17 +24,9 @@ class TencentRemove { } async remove() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) try { const handler = new RemoveFunction(this.options, this.serverless) const service = this.provider.getServiceResource() diff --git a/rollback/tencentRollback.js b/rollback/tencentRollback.js index a52d648..440156a 100644 --- a/rollback/tencentRollback.js +++ b/rollback/tencentRollback.js @@ -26,17 +26,9 @@ class TencentRollback { } async rollback() { - if (!this.options.credentials || !this.options.credentials.tencent_secret_id) { - const provider = new tencentProvider(this.serverless, this.options) - const tencentTemp = await provider.getTempKey() - this.options.credentials = { - tencent_secret_id: tencentTemp.tencent_secret_id, - tencent_secret_key: tencentTemp.tencent_secret_key, - tencent_appid: tencentTemp.tencent_appid - } - this.options.token = tencentTemp.token - this.options.timestamp = tencentTemp.timestamp - } + const provider = new tencentProvider(this.serverless, this.options) + this.options = await provider.getUserCred(this.options) + await provider.getUserAuth(this.options.credentials.tencent_owneruin) const Handler = new RollbackService(this.options, this.serverless) const fileKeyPrefix = this.serverless.service.service + '-' + this.options.stage const cosBucket = this.provider.getDeployCosBucket() @@ -96,9 +88,17 @@ class TencentRollback { const oldFunc = await func.deploy('default', funcObject) this.serverless.cli.log(`Rollback function ${funcObject.FuncName}`) + if ((await func.checkStatus('default', funcObject)) == false) { + throw `Function ${funcObject.FuncName} create/update failed` + } + this.serverless.cli.log(`Rollback configure for function ${funcObject.FuncName}`) await func.updateConfiguration('default', oldFunc, funcObject) + if ((await func.checkStatus('default', funcObject)) == false) { + throw `Function ${funcObject.FuncName} create/update failed` + } + this.serverless.cli.log(`Setting tags for function ${funcObject.FuncName}`) await func.createTags('default', funcObject.FuncName, funcObject.Properties.Tags) diff --git a/shared/handler.js b/shared/handler.js index c352978..149db09 100644 --- a/shared/handler.js +++ b/shared/handler.js @@ -11,7 +11,6 @@ const MonitorClinet = tencentcloud.monitor.v20180724.Client class AbstractHandler { constructor(options, serverless) { - const { credentials, region } = options const { tencent_appid, tencent_secret_id, tencent_secret_key } = credentials this.options = options @@ -98,26 +97,17 @@ class AbstractHandler { const chunkSize = options.chunkSize || 1024 * 1024 * 8 const timeout = options.timeout || 120 - if (!options.token) { - return new tencentcloudCos({ - SecretId: secret_id, - SecretKey: secret_key, - FileParallelLimit: fileParallelLimit, - ChunkParallelLimit: chunkParallelLimit, - ChunkSize: chunkSize, - Timeout: timeout * 1000 - }) - } - return new tencentcloudCos({ - getAuthorization: function(option, callback) { - callback({ - TmpSecretId: secret_id, - TmpSecretKey: secret_key, - XCosSecurityToken: options.token, - ExpiredTime: options.timestamp - }) - } + SecretId: secret_id, + SecretKey: secret_key, + FileParallelLimit: fileParallelLimit, + ChunkParallelLimit: chunkParallelLimit, + ChunkSize: chunkSize, + Timeout: timeout * 1000, + TmpSecretId: secret_id, + TmpSecretKey: secret_key, + XCosSecurityToken: options.token, + ExpiredTime: options.timestamp }) }