Skip to content

Commit e4716fa

Browse files
author
dfounderliu
committed
增加多地域部署SCF/APIGW
1 parent ce55646 commit e4716fa

File tree

4 files changed

+319
-0
lines changed

4 files changed

+319
-0
lines changed

src/baas/multi-apigw/index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const apigwUtils = require('../apigw/index')
2+
3+
class TencentAPIGWMultiRegion {
4+
constructor(credentials = {}, region) {
5+
this.regionList = typeof region == 'string' ? [region] : region
6+
this.credentials = credentials
7+
}
8+
9+
mergeJson(sourceJson, targetJson) {
10+
for (const eveKey in sourceJson) {
11+
if (targetJson.hasOwnProperty(eveKey)) {
12+
if (['protocols', 'endpoints', 'customDomain'].indexOf(eveKey) != -1) {
13+
for (let i = 0; i < sourceJson[eveKey].length; i++) {
14+
const sourceEvents = JSON.stringify(sourceJson[eveKey][i])
15+
const targetEvents = JSON.stringify(targetJson[eveKey])
16+
if (targetEvents.indexOf(sourceEvents) == -1) {
17+
targetJson[eveKey].push(sourceJson[eveKey][i])
18+
}
19+
}
20+
} else {
21+
if (typeof sourceJson[eveKey] != 'string') {
22+
this.mergeJson(sourceJson[eveKey], targetJson[eveKey])
23+
} else {
24+
targetJson[eveKey] = sourceJson[eveKey]
25+
}
26+
}
27+
} else {
28+
targetJson[eveKey] = sourceJson[eveKey]
29+
}
30+
}
31+
return targetJson
32+
}
33+
34+
35+
async doDeploy(tempInputs, output) {
36+
const scfClient = new apigwUtils(this.credentials, tempInputs.region)
37+
output[tempInputs.region] = await scfClient.deploy(tempInputs)
38+
}
39+
40+
async doDelete(tempInputs, region) {
41+
const scfClient = new apigwUtils(this.credentials, region)
42+
await scfClient.remove(tempInputs)
43+
}
44+
45+
async deploy(inputs = {}) {
46+
if (!this.regionList) {
47+
this.regionList = typeof inputs.region == 'string' ? [inputs.region] : inputs.region
48+
}
49+
50+
const baseInputs = {}
51+
for (const eveKey in inputs) {
52+
if (eveKey != 'region' && eveKey.indexOf('ap-') != 0) {
53+
baseInputs[eveKey] = inputs[eveKey]
54+
}
55+
}
56+
57+
const apigwOutputs = {}
58+
59+
if (inputs.serviceId && this.regionList.length > 1) {
60+
throw new Error(
61+
'For multi region deployment, please specify serviceid under the corresponding region'
62+
)
63+
}
64+
65+
const apigwHandler = []
66+
for (let i = 0; i < this.regionList.length; i++) {
67+
let tempInputs = JSON.parse(JSON.stringify(baseInputs)) // clone
68+
tempInputs.region = this.regionList[i]
69+
if (inputs[this.regionList[i]]) {
70+
tempInputs = this.mergeJson(inputs[this.regionList[i]], tempInputs)
71+
}
72+
apigwHandler.push(this.doDeploy(tempInputs, apigwOutputs))
73+
}
74+
75+
await Promise.all(apigwHandler)
76+
return apigwOutputs
77+
78+
}
79+
80+
async remove(inputs = {}) {
81+
const apigwHandler = []
82+
for(let item in inputs){
83+
apigwHandler.push(this.doDelete(inputs[item],item))
84+
}
85+
await Promise.all(apigwHandler)
86+
return {}
87+
}
88+
}
89+
90+
// don't forget to export the new Componnet you created!
91+
module.exports = TencentAPIGWMultiRegion

src/baas/multi-apigw/index.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const secret = require('../../../../secret')
2+
const apigwUtils = require('./index')
3+
4+
class ClientTest {
5+
async apigwTest() {
6+
const apigw = new apigwUtils({
7+
SecretId: secret.SecretId,
8+
SecretKey: secret.SecretKey
9+
}, ['ap-shanghai', 'ap-guangzhou'])
10+
const apigwDemo = {
11+
region: 'ap-guangzhou',
12+
protocols: ['http', 'https'],
13+
serviceName: 'serverless',
14+
environment: 'release',
15+
endpoints: [
16+
{
17+
path: '/',
18+
protocol: 'HTTP',
19+
method: 'GET',
20+
apiName: 'index',
21+
function: {
22+
functionName: 'egg-function'
23+
}
24+
}
25+
]
26+
}
27+
const result = await apigw.deploy(apigwDemo)
28+
console.log(JSON.stringify(result))
29+
await apigw.remove(result)
30+
}
31+
}
32+
33+
new ClientTest().apigwTest()

src/baas/multi-scf/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const scfUtils = require('../scf/index')
2+
3+
class TencentSCFMultiRegion {
4+
constructor(credentials = {}, region) {
5+
this.regionList = typeof region == 'string' ? [region] : region
6+
this.credentials = credentials
7+
}
8+
9+
mergeJson(sourceJson, targetJson) {
10+
for (const eveKey in sourceJson) {
11+
if (targetJson.hasOwnProperty(eveKey)) {
12+
if (eveKey == 'events') {
13+
for (let i = 0; i < sourceJson[eveKey].length; i++) {
14+
const sourceEvents = JSON.stringify(sourceJson[eveKey][i])
15+
const targetEvents = JSON.stringify(targetJson[eveKey])
16+
if (targetEvents.indexOf(sourceEvents) == -1) {
17+
targetJson[eveKey].push(sourceJson[eveKey][i])
18+
}
19+
}
20+
} else {
21+
if (typeof sourceJson[eveKey] != 'string') {
22+
this.mergeJson(sourceJson[eveKey], targetJson[eveKey])
23+
} else {
24+
targetJson[eveKey] = sourceJson[eveKey]
25+
}
26+
}
27+
} else {
28+
targetJson[eveKey] = sourceJson[eveKey]
29+
}
30+
}
31+
return targetJson
32+
}
33+
34+
35+
async doDeploy(tempInputs, output) {
36+
const scfClient = new scfUtils(this.credentials, tempInputs.region)
37+
output[tempInputs.region] = await scfClient.deploy(tempInputs)
38+
}
39+
40+
async doDelete(tempInputs, region) {
41+
const scfClient = new scfUtils(this.credentials, region)
42+
await scfClient.remove(tempInputs)
43+
}
44+
45+
async deploy(inputs = {}) {
46+
if(!this.regionList){
47+
this.regionList = typeof inputs.region == 'string' ? [inputs.region] : inputs.region
48+
}
49+
const baseInputs = {}
50+
const functions = {}
51+
52+
for (const eveKey in inputs) {
53+
if (eveKey != 'region' && eveKey.indexOf('ap-') != 0) {
54+
baseInputs[eveKey] = inputs[eveKey]
55+
}
56+
}
57+
const functionHandler = []
58+
for (let i = 0; i < this.regionList.length; i++) {
59+
let tempInputs = JSON.parse(JSON.stringify(baseInputs)) // clone
60+
tempInputs.region = this.regionList[i]
61+
if (inputs[this.regionList[i]]) {
62+
tempInputs = this.mergeJson(inputs[this.regionList[i]], tempInputs)
63+
}
64+
functionHandler.push(this.doDeploy(tempInputs, functions))
65+
}
66+
67+
await Promise.all(functionHandler)
68+
69+
return functions
70+
}
71+
72+
async remove(inputs = {}) {
73+
const functionHandler = []
74+
for(let item in inputs){
75+
functionHandler.push(this.doDelete(inputs[item],item))
76+
}
77+
await Promise.all(functionHandler)
78+
return {}
79+
}
80+
}
81+
82+
module.exports = TencentSCFMultiRegion

src/baas/multi-scf/index.test.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const secret = require('../../../../secret')
2+
const ScfUtils = require('./index')
3+
4+
class ClientTest {
5+
async scfTest() {
6+
const scf = new ScfUtils({
7+
SecretId: secret.SecretId,
8+
SecretKey: secret.SecretKey
9+
}, ['ap-shanghai', 'ap-guangzhou'])
10+
const scfDemo = {
11+
name: 'myFunctionttest',
12+
handler: 'index.main_handler',
13+
runtime: 'Python3.6',
14+
role: 'SCF_PythonLogsRole',
15+
// eip: true,
16+
region: 'ap-shanghai',
17+
description: 'My Serverless Function',
18+
memorySize: '256',
19+
timeout: '20',
20+
tags: {
21+
mytest: 'abc'
22+
},
23+
environment: {
24+
variables: {
25+
TEST: 'value'
26+
}
27+
},
28+
events: [
29+
{
30+
timer: {
31+
name: 'timer',
32+
parameters: {
33+
cronExpression: '*/6 * * * *',
34+
enable: true,
35+
argument: 'mytest argument'
36+
}
37+
}
38+
},
39+
{
40+
apigw: {
41+
name: 'serverless',
42+
parameters: {
43+
protocols: ['http'],
44+
serviceName: 'serverless',
45+
description: 'the serverless service',
46+
environment: 'release',
47+
endpoints: [{
48+
path: '/users',
49+
method: 'POST'
50+
}]
51+
}
52+
53+
}
54+
}
55+
],
56+
'ap-shanghai': {
57+
code: {
58+
bucket: 'sls-cloudfunction-ap-shanghai-code',
59+
object: 'sls-cloudfunction-default-Album_Add_Album-1585359218.zip'
60+
},
61+
},
62+
'ap-guangzhou': {
63+
code: {
64+
bucket: 'sls-cloudfunction-ap-guangzhou',
65+
object: 'sls-cloudfunction-default-hello_world-1584670117.zip'
66+
},
67+
}
68+
}
69+
const result = await scf.deploy(scfDemo)
70+
console.log(JSON.stringify(result))
71+
// console.log(await scf.invoke(result.FunctionName))
72+
await scf.remove(result)
73+
}
74+
}
75+
76+
new ClientTest().scfTest()
77+
78+
/* 测试结果:
79+
Creating funtion myFunction1 in ap-guangzhou ...
80+
Getting function myFunction1's configure ...
81+
Adding tags for funtion myFunction1 in ap-guangzhou ...
82+
Deploying myFunction1's triggers in ap-guangzhou.
83+
Checking function myFunction1 status ...
84+
Modify tags ...
85+
Modified tags.
86+
Checking function myFunction1 status ...
87+
Deploying timer triggers: timer.
88+
Deployed funtion undefined.
89+
{"RequestId":"ea9d3a73-1842-413a-be71-56a7bc385381","ModTime":"2020-03-27 17:12:40","CodeInfo":"","Description":"My Serverless Function","Triggers":[{"Response":{"RequestId":"210e2f21-0ac9-4259-99d3-e2abf4d1b159","TriggerInfo":{"AddTime":"2020-03-27 17:12:44","AvailableStatus":"Available","CustomArgument":"mytest argument","Enable":1,"ModTime":"2020-03-27 17:12:44","TriggerDesc":"{\"cron\":\"0 *6"}","TriggerName":"timer","Type":"timer"}}}],"Handler":"index.main_handler","CodeSize":0,"Timeout":20,"FunctionVersion":"$LATEST","MemorySize":256,"Runtime":"Python3.6","FunctionName":"myFunction1","VpcConfig":{"VpcId":"","SubnetId":""},"UseGpu":"FALSE","Environment":{"Variables":[{"Key":"TEST","Value":"value"}]},"CodeResult":"success","CodeError":"","ErrNo":0,"Namespace":"default","Role":"SCF_PythonLogsRole","InstallDependency":"FALSE","Status":"Creating","StatusDesc":"","ClsLogsetId":"","ClsTopicId":"","FunctionId":"lam-r19fhf72","Tags":[],"EipConfig":{"EipFixed":"FALSE","Eips":[]},"AccessInfo":{"Host":"","Vip":""},"Type":"Event","DeadLetterConfig":{"Type":"","Name":"","FilterType":""},"Layers":[],"L5Enable":"FALSE","AddTime":"2020-03-27 17:12:40","PublicNetConfig":{"PublicNetStatus":"ENABLE","EipConfig":{"EipStatus":"DISABLE","EipAddress":[]}},"OnsEnable":"FALSE"}
90+
{
91+
RequestId: '02ff599b-5720-4dfb-94c0-b3f333868d31',
92+
Result: {
93+
BillDuration: 2,
94+
Duration: 2,
95+
ErrMsg: '',
96+
FunctionRequestId: '02ff599b-5720-4dfb-94c0-b3f333868d31',
97+
InvokeResult: 0,
98+
Log: 'START RequestId: 02ff599b-5720-4dfb-94c0-b3f333868d31\n' +
99+
'Event RequestId: 02ff599b-5720-4dfb-94c0-b3f333868d31\n' +
100+
'{}\n' +
101+
'Received event: {}\n' +
102+
`Received context: {'memory_limit_in_mb': 256, 'time_limit_in_ms': 20000, 'request_id': '02ff599b-5720-4dfb-94c0-b3f333868d31', 'environment': '{"TENCENTCLOUD_SECRETID":"AKIDPH1aD0k43GtL17YuoszTJYKABNa6B8FAddTfay3WoYMb79G9QXSesbSz2yiREMdN","TENCENTCLOUD_SECRETKEY":"LtunGCMtsfkGqQmgDECkHkskse5+WennrMPJbreXE84=","TENCENTCLOUD_SESSIONTOKEN":"26Z03TWGXBrt0b3QHemtqOdM4s6vEMe6d455a7dd94c5e489a1cdea1b1126f5e8-M_KpWmubhn_Xgr0j6ULJItvuEYSCcQGWgWyvW39L8nksAfRnzKx-CsGcYXRTVyq7_SrldrOZwHgYf3lzQKXkt9qJ4tGb50VmLmaCoSWy8D8ggbbeJ4vaj_DRg_QE8ZBm6fMlSdRkKe95MqkN0Sukmj_OVjG_tv6pr-svZiAEsspT6Ga5QwuBecFl7sJOE8GpgmfUQKEa68284ryfMCd0SRZXaP1FfMefozBabrwD28lWLtwh8a7L3vklOhTIFZSL_tyZO1AGMvKasiuGjuvqm7mKQeq5fUaFJkBdblX9hbFLdQZoQ8tLxv2yz0rjiBQGTXrC901kb1NqZqJISQlAVikaqdetDIxnvrq3bI881U","TEST":"value"}', 'environ': 'TEST=value;TENCENTCLOUD_SESSIONTOKEN=26Z03TWGXBrt0b3QHemtqOdM4s6vEMe6d455a7dd94c5e489a1cdea1b1126f5e8-M_KpWmubhn_Xgr0j6ULJItvuEYSCcQGWgWyvW39L8nksAfRnzKx-CsGcYXRTVyq7_SrldrOZwHgYf3lzQKXkt9qJ4tGb50VmLmaCoSWy8D8ggbbeJ4vaj_DRg_QE8ZBm6fMlSdRkKe95MqkN0Sukmj_OVjG_tv6pr-svZiAEsspT6Ga5QwuBecFl7sJOE8GpgmfUQKEa68284ryfMCd0SRZXaP1FfMefozBabrwD28lWLtwh8a7L3vklOhTIFZSL_tyZO1AGMvKasiuGjuvqm7mKQeq5fUaFJkBdblX9hbFLdQZoQ8tLxv2yz0rjiBQGTXrC901kb1NqZqJISQlAVikaqdetDIxnvrq3bI881U;TENCENTCLOUD_SECRETID=AKIDPH1aD0k43GtL17YuoszTJYKABNa6B8FAddTfay3WoYMb79G9QXSesbSz2yiREMdN;TENCENTCLOUD_SECRETKEY=LtunGCMtsfkGqQmgDECkHkskse5+WennrMPJbreXE84=;SCF_NAMESPACE=default', 'function_version': '$LATEST', 'function_name': 'myFunction1', 'namespace': 'default'}\n` +
103+
'Hello world\n' +
104+
'\n' +
105+
'END RequestId: 02ff599b-5720-4dfb-94c0-b3f333868d31\n' +
106+
'Report RequestId: 02ff599b-5720-4dfb-94c0-b3f333868d31 Duration:2ms Memory:256MB MemUsage:11.2188MB',
107+
MemUsage: 11763712,
108+
RetMsg: '"Hello World"'
109+
}
110+
}
111+
Deleteing funtion myFunction1 ...
112+
Removed function and triggers.
113+
*/

0 commit comments

Comments
 (0)