Skip to content

Commit

Permalink
Merge pull request #6 from serverless-tencent/remove_appid
Browse files Browse the repository at this point in the history
remove appid
  • Loading branch information
anycodes committed Nov 11, 2019
2 parents cfd2e98 + 13e3893 commit 78b9185
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion docs/en/credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
100 changes: 100 additions & 0 deletions docs/en/yaml.md
Original file line number Diff line number Diff line change
@@ -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

```
100 changes: 100 additions & 0 deletions docs/zh/yaml.md
Original file line number Diff line number Diff line change
@@ -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

```
3 changes: 1 addition & 2 deletions docs/zh/配置账号.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

```ini
[default]
tencent_appid = appid
tencent_secret_id = secretid
tencent_secret_key = secretkey
```
Expand All @@ -37,4 +36,4 @@ provider:
credentials: ~/credentials
```

新建示例模板的教程[参考此处]()
新建示例模板的教程[参考此处]()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-tencent-scf",
"version": "0.2.0",
"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",
Expand Down
63 changes: 60 additions & 3 deletions provider/tencentProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,7 +140,7 @@ class TencentProvider {
return (vpcId && subnetId) ? {'VpcId': vpcId, 'SubnetId': subnetId} : null
}

getCredentials() {
async getCredentials() {

if (this.options.credentials) {
return
Expand All @@ -104,14 +155,20 @@ 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) => {
if (!this.options.credentials[field]) {
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
}
}
Expand Down

0 comments on commit 78b9185

Please sign in to comment.