From ad269f09dba8f80e5c162d65dc8394f0ec267563 Mon Sep 17 00:00:00 2001 From: dfounderliu Date: Mon, 11 Nov 2019 13:38:19 +0800 Subject: [PATCH 1/2] remove appid --- README.md | 2 + docs/en/credentials.md | 1 - docs/en/yaml.md | 100 ++++++++++++++++++ docs/zh/yaml.md | 100 ++++++++++++++++++ ...15\347\275\256\350\264\246\345\217\267.md" | 3 +- package.json | 2 +- provider/tencentProvider.js | 63 ++++++++++- 7 files changed, 264 insertions(+), 7 deletions(-) create mode 100644 docs/en/yaml.md create mode 100644 docs/zh/yaml.md diff --git a/README.md b/README.md index d6051f8..e57b571 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,8 @@ functions: httpMethod: ANY ``` +* [Click here you could get `Serverless.yml Reference`](./docs/en/yaml.md) + ### Deploy the Service Use this command to deploy your service for the first time and after you make changes to your Functions, Events or Resources in `serverless.yml` and want to deploy all changes within your Service at the same time. diff --git a/docs/en/credentials.md b/docs/en/credentials.md index beba317..c661a96 100755 --- a/docs/en/credentials.md +++ b/docs/en/credentials.md @@ -24,7 +24,6 @@ You need to create credentials Serverless can use to create resources in your Pr 4. Create a file named `credentials` containing the credentials that you have collected. ```ini [default] -tencent_appid = 1251000000 tencent_secret_id = AKIDteBxxxxxxxxxxnZfk tencent_secret_key = AKID2qsxxxxxxxxxxxxxtTo ``` diff --git a/docs/en/yaml.md b/docs/en/yaml.md new file mode 100644 index 0000000..8e8f8db --- /dev/null +++ b/docs/en/yaml.md @@ -0,0 +1,100 @@ +# Serverless.yml Reference +Here is a list of all available properties in serverless.yml when the provider is set to tencent. +```yaml +# Welcome to Serverless! +# +# This file is the main config file for your service. +# It's very minimal at this point and uses default values. +# You can always add more config options for more control. +# We've included some commented out config examples here. +# Just uncomment any of them to get that config option. +# +# For full config options, check the docs: +# docs.serverless.com +# +# Happy Coding! + +service: my-service # service name + +provider: # provider information + name: tencent + runtime: Nodejs8.9 # Nodejs8.9 or Nodejs6.10 + credentials: ~/credentials + +# you can overwrite defaults here +# stage: dev +# cosBucket: DEFAULT +# role: QCS_SCFExcuteRole +# memorySize: 256 +# timeout: 10 +# region: ap-shanghai +# environment: +# variables: +# ENV_FIRST: env1 +# ENV_SECOND: env2 +# vpcConfig: +# vpcId: test +# subnetId: test + +plugins: + - serverless-tencent-scf + +# you can add packaging information here +#package: +# include: +# - include-me.js +# - include-me-dir/** +# exclude: +# - exclude-me.js +# - exclude-me-dir/** + +functions: + function_one: + handler: index.main_handler +# description: Tencent Serverless Cloud Function +# runtime: Nodejs8.9 # Nodejs8.9 or Nodejs6.10 +# memorySize: 256 +# timeout: 10 +# environment: +# variables: +# ENV_FIRST: env1 +# ENV_Third: env2 +# vpcConfig: +# vpcId: test +# subnetId: test +# events: +# - timer: +# name: timer +# parameters: +# cronExpression: '*/5 * * * *' +# enable: true +# - cos: +# name: cli-appid.cos.ap-beijing.myqcloud.com +# parameters: +# bucket: cli-appid.cos.ap-beijing.myqcloud.com +# filter: +# prefix: filterdir/ +# suffix: .jpg +# events: cos:ObjectCreated:* +# enable: true +# - apigw: +# name: hello_world_apigw +# parameters: +# stageName: release +# serviceId: +# httpMethod: ANY +# - cmq: +# name: cmq_trigger +# parameters: +# name: test-topic-queue +# enable: true +# - ckafka: +# name: ckafka_trigger +# parameters: +# name: ckafka-2o10hua5 +# topic: test +# maxMsgNum: 999 +# offset: latest +# enable: true + +``` diff --git a/docs/zh/yaml.md b/docs/zh/yaml.md new file mode 100644 index 0000000..900403f --- /dev/null +++ b/docs/zh/yaml.md @@ -0,0 +1,100 @@ +# Serverless.yml 参考 +以下是当提供商设置为tencent时,serverless.yml中所有可用属性的列表。 +```yaml +# Welcome to Serverless! +# +# This file is the main config file for your service. +# It's very minimal at this point and uses default values. +# You can always add more config options for more control. +# We've included some commented out config examples here. +# Just uncomment any of them to get that config option. +# +# For full config options, check the docs: +# docs.serverless.com +# +# Happy Coding! + +service: my-service # service name + +provider: # provider information + name: tencent + runtime: Nodejs8.9 # Nodejs8.9 or Nodejs6.10 + credentials: ~/credentials + +# you can overwrite defaults here +# stage: dev +# cosBucket: DEFAULT +# role: QCS_SCFExcuteRole +# memorySize: 256 +# timeout: 10 +# region: ap-shanghai +# environment: +# variables: +# ENV_FIRST: env1 +# ENV_SECOND: env2 +# vpcConfig: +# vpcId: test +# subnetId: test + +plugins: + - serverless-tencent-scf + +# you can add packaging information here +#package: +# include: +# - include-me.js +# - include-me-dir/** +# exclude: +# - exclude-me.js +# - exclude-me-dir/** + +functions: + function_one: + handler: index.main_handler +# description: Tencent Serverless Cloud Function +# runtime: Nodejs8.9 # Nodejs8.9 or Nodejs6.10 +# memorySize: 256 +# timeout: 10 +# environment: +# variables: +# ENV_FIRST: env1 +# ENV_Third: env2 +# vpcConfig: +# vpcId: test +# subnetId: test +# events: +# - timer: +# name: timer +# parameters: +# cronExpression: '*/5 * * * *' +# enable: true +# - cos: +# name: cli-appid.cos.ap-beijing.myqcloud.com +# parameters: +# bucket: cli-appid.cos.ap-beijing.myqcloud.com +# filter: +# prefix: filterdir/ +# suffix: .jpg +# events: cos:ObjectCreated:* +# enable: true +# - apigw: +# name: hello_world_apigw +# parameters: +# stageName: release +# serviceId: +# httpMethod: ANY +# - cmq: +# name: cmq_trigger +# parameters: +# name: test-topic-queue +# enable: true +# - ckafka: +# name: ckafka_trigger +# parameters: +# name: ckafka-2o10hua5 +# topic: test +# maxMsgNum: 999 +# offset: latest +# enable: true + +``` diff --git "a/docs/zh/\351\205\215\347\275\256\350\264\246\345\217\267.md" "b/docs/zh/\351\205\215\347\275\256\350\264\246\345\217\267.md" index 1bed83c..82f41fd 100644 --- "a/docs/zh/\351\205\215\347\275\256\350\264\246\345\217\267.md" +++ "b/docs/zh/\351\205\215\347\275\256\350\264\246\345\217\267.md" @@ -17,7 +17,6 @@ ```ini [default] -tencent_appid = appid tencent_secret_id = secretid tencent_secret_key = secretkey ``` @@ -37,4 +36,4 @@ provider: credentials: ~/credentials ``` -新建示例模板的教程[参考此处]()。 \ No newline at end of file +新建示例模板的教程[参考此处]()。 diff --git a/package.json b/package.json index 453f214..d1f1c80 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-tencent-scf", - "version": "0.2.0", + "version": "0.1.8", "description": "Provider plugin for the Serverless Framework v1.x which adds support for Tencent Cloud Functions.", "main": "index.js", "author": "serverless.com", diff --git a/provider/tencentProvider.js b/provider/tencentProvider.js index ddd45bb..12e6a15 100644 --- a/provider/tencentProvider.js +++ b/provider/tencentProvider.js @@ -5,16 +5,67 @@ const fs = require('fs'); const os = require('os'); const ini = require('ini'); const _ = require('lodash'); - +const util = require('util') +const tencentcloud = require('tencentcloud-sdk-nodejs') +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', }; +class GetUserAppIdResponse extends AbstractModel { + constructor() { + super() + + this.RequestId = null + } + + deserialize(params) { + if (!params) { + return + } + this.AppId = 'RequestId' in params ? params.AppId : null + this.RequestId = 'RequestId' in params ? params.RequestId : null + } +} + +class AppidClient extends AbstractClient { + constructor(credential, region, profile) { + super('cam.tencentcloudapi.com', '2019-01-16', credential, region, profile) + } + + GetUserAppId(req, cb) { + const resp = new GetUserAppIdResponse() + this.request('GetUserAppId', req, resp, cb) + } +} + class TencentProvider { static getProviderName() { return constants.providerName; } + getAppid(credentials) { + const secret_id = credentials.SecretId + const secret_key = credentials.SecretKey + const cred = new tencentcloud.common.Credential(secret_id, secret_key) + const httpProfile = new HttpProfile() + httpProfile.reqTimeout = 30 + const clientProfile = new ClientProfile('HmacSHA256', httpProfile) + const cam = new AppidClient(cred, 'ap-guangzhou', clientProfile) + const req = new GetUserAppIdResponse() + const body = {} + req.from_json_string(JSON.stringify(body)) + const handler = util.promisify(cam.GetUserAppId.bind(cam)) + try { + return handler(req) + } catch (e) { + throw 'Get Appid failed! ' + } + } + constructor(serverless, options) { this.options = options; this.serverless = serverless; @@ -89,7 +140,7 @@ class TencentProvider { return (vpcId && subnetId) ? {'VpcId': vpcId, 'SubnetId': subnetId} : null } - getCredentials() { + async getCredentials() { if (this.options.credentials) { return @@ -104,7 +155,6 @@ class TencentProvider { // TODO(dfounderliu) support profiles other than [default] this.options.credentials = ini.parse(keyFileContent).default; [ - 'tencent_appid', 'tencent_secret_id', 'tencent_secret_key' ].forEach((field) => { @@ -112,6 +162,13 @@ class TencentProvider { throw new Error(`Credentials in ${credentials} does not contain ${field}`); } }); + + // From cam to getting 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 return } } From 13e3893e0b3dfbcff2549332d40af0b71bc1123f Mon Sep 17 00:00:00 2001 From: dfounderliu Date: Mon, 11 Nov 2019 13:41:36 +0800 Subject: [PATCH 2/2] remove appid --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1f1c80..1144502 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-tencent-scf", - "version": "0.1.8", + "version": "0.1.13", "description": "Provider plugin for the Serverless Framework v1.x which adds support for Tencent Cloud Functions.", "main": "index.js", "author": "serverless.com",