Skip to content

Commit

Permalink
feat: support auto create api
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun committed Jul 13, 2020
1 parent e567443 commit a41a06d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
7 changes: 3 additions & 4 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ inputs:
name: myFunction # 云函数名称
namespace: abc # 云函数命名空间
role: exRole # 云函数执行角色
enableRoleAuth:
true # 默认会尝试创建 SCF_QcsRole 角色,如果不需要配置成 false 即可
# 1. 默认写法,新建特定命名的 cos bucket 并上传
enableRoleAuth: true # 默认会尝试创建 SCF_QcsRole 角色,如果不需要配置成 false 即可
# 1. 默认写法,新建特定命名的 cos bucket 并上传
src: ./code
# 2. src 为对象,并且制定忽略上传文件夹 node_modules
# src:
Expand Down Expand Up @@ -154,7 +153,7 @@ inputs:
| name | 是 | | 创建的函数名称,函数名称支持 26 个英文字母大小写、数字、连接符和下划线,第一个字符只能以字母开头,最后一个字符不能为连接符或者下划线,名称长度 2-60 |
| namesapce | 否 | default | 命名空间。默认为 default。 |
| role | 否 | | 函数绑定的角色 |
| enableRoleAuth | | true | 默认会尝试创建 SCF_QcsRole 角色。SCF_QcsRole 为 SCF 默认配置角色。该服务角色用于提供 SCF 配置对接其他云上资源的权限,包括但不限于代码文件访问、触发器配置。配置角色的预设策略可支持函数执行的基本操作。如果不需要配置成 false 即可。[相关文档](https://cloud.tencent.com/document/product/583/32389#.E8.A7.92.E8.89.B2.E8.AF.A6.E6.83.85) |
| enableRoleAuth | | true | 默认会尝试创建 SCF_QcsRole 角色。SCF_QcsRole 为 SCF 默认配置角色。该服务角色用于提供 SCF 配置对接其他云上资源的权限,包括但不限于代码文件访问、触发器配置。配置角色的预设策略可支持函数执行的基本操作。如果不需要配置成 false 即可。[相关文档](https://cloud.tencent.com/document/product/583/32389#.E8.A7.92.E8.89.B2.E8.AF.A6.E6.83.85) |
| src | 是 | | 函数代码路径。如果是对象,配置参数参考 [执行目录](#执行目录) |
| handler | 是 | | 函数处理方法名称,名称格式支持 "文件名称.方法名称" 形式,文件名称和函数名称之间以"."隔开,文件名称和函数名称要求以字母开始和结尾,中间允许插入字母、数字、下划线和连接符,文件名称和函数名字的长度要求是 2-60 个字符 |
| runtime | 是 | | 函数运行环境,目前仅支持 Python2.7,Python3.6,Nodejs6.10,Nodejs8.9,Nodejs10.15,Nodejs12.16, PHP5, PHP7,Go1 和 Java8,默认 Python2.7 |
Expand Down
4 changes: 2 additions & 2 deletions serverless.component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ author: Tencent Cloud, Inc.
org: Tencent Cloud, Inc.
description: Deploys Tencent SCF.
keywords: tencent, serverless, SCF
repo: https://github.com/serverless-components/tencent-scf/tree/v2
readme: https://github.com/serverless-components/tencent-scf/tree/v2/README.md
repo: https://github.com/serverless-components/tencent-scf
readme: https://github.com/serverless-components/tencent-scf/tree/master/README.md
license: MIT
main: ./src
webDeployable: true
18 changes: 17 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ const CONFIGS = {
region: 'ap-guangzhou',
compName: 'scf',
componentFullname: 'SCF',
description: 'Created by serverless component'
runtime: 'Nodejs10.15',
handler: 'index.main_handler',
description: 'Created by Serverless Component',
defaultApigw: {
name: `serverless_api`,
parameters: {
protocols: ['http', 'https'],
description: 'Created By Serverless Component',
environment: 'release',
endpoints: [
{
path: '/',
method: 'GET'
}
]
}
}
}

module.exports = CONFIGS
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ const prepareInputs = async (instance, credentials, appId, inputs) => {
(oldState.function && oldState.function.FunctionName) ||
`${CONFIGS.compName}_component_${generateId()}`
inputs.description = inputs.description || CONFIGS.description
inputs.handler = inputs.handler || CONFIGS.handler
inputs.runtime = inputs.runtime || CONFIGS.runtime
inputs.code = code
inputs.events = inputs.events || []

const stateApigw = oldState.apigw
const triggers = {}
const apigwName = []

let existApigwTrigger = false
// initial apigw event parameters
inputs.events = inputs.events.map((event) => {
Expand Down Expand Up @@ -161,6 +164,16 @@ const prepareInputs = async (instance, credentials, appId, inputs) => {
return event
})

// if not config apig trigger, and make autoCreateApi true
if (inputs.autoCreateApi && !existApigwTrigger) {
triggers.apigw = []
inputs.events.push({
apigw: CONFIGS.defaultApigw
})

existApigwTrigger = true
}

// validate traffic config
if (inputs.traffic !== undefined) {
validateTraffic(inputs.traffic)
Expand Down

0 comments on commit a41a06d

Please sign in to comment.