-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.js
123 lines (104 loc) · 4.3 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const ensureIterable = require('type/iterable/ensure')
const ensurePlainObject = require('type/plain-object/ensure')
const ensureString = require('type/string/ensure')
const random = require('ext/string/random')
const path = require('path')
const { Component } = require('@serverless/core')
const DEFAULTS = {
handler: 'index.main_handler',
runtime: 'Python3.6',
exclude: ['.git/**', '.gitignore', '.DS_Store']
}
class TencentBottle extends Component {
getDefaultProtocol(protocols) {
if (protocols.map((i) => i.toLowerCase()).includes('https')) {
return 'https'
}
return 'http'
}
async prepareInputs(inputs = {}) {
inputs.name =
ensureString(inputs.functionName, { isOptional: true }) ||
this.state.functionName ||
`BottleComponent_${random({ length: 6 })}`
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd()
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' })
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString })
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString })
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
inputs.include = [path.join(__dirname, 'component')]
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')
inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
if (inputs.functionConf) {
inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3
inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128
if (inputs.functionConf.environment) {
inputs.environment = inputs.functionConf.environment
}
if (inputs.functionConf.vpcConfig) {
inputs.vpcConfig = inputs.functionConf.vpcConfig
}
}
return inputs
}
async default(inputs = {}) {
inputs = await this.prepareInputs(inputs)
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-pyramid'
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs)
const apigwParam = {
serviceName: inputs.serviceName,
description: 'Serverless Framework Tencent-Bottle Component',
serviceId: inputs.serviceId,
region: inputs.region,
protocols: inputs.apigatewayConf.protocols || ['http'],
environment:
inputs.apigatewayConf && inputs.apigatewayConf.environment
? inputs.apigatewayConf.environment
: 'release',
endpoints: [
{
path: '/',
method: 'ANY',
function: {
isIntegratedResponse: true,
functionName: tencentCloudFunctionOutputs.Name
}
}
]
}
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan
}
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth
}
apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-bottle'
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam)
const outputs = {
region: inputs.region,
functionName: inputs.name,
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
tencentApiGatewayOutputs.subDomain
}/${tencentApiGatewayOutputs.environment}/`
}
this.state = outputs
await this.save()
return outputs
}
async remove(inputs = {}) {
const removeInput = {
fromClientRemark: inputs.fromClientRemark || 'tencent-bottle'
}
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
await tencentCloudFunction.remove(removeInput)
await tencentApiGateway.remove(removeInput)
return {}
}
}
module.exports = TencentBottle