Skip to content

Commit 4f52532

Browse files
authored
feat(scf): support clb trigger (#203)
* feat(scf): support clb trigger * test: update snapshot * test: fix clb trigger test
1 parent d4208ac commit 4f52532

File tree

16 files changed

+801
-16
lines changed

16 files changed

+801
-16
lines changed

__tests__/__snapshots__/metrics.test.ts.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Object {
178178
"GET - /",
179179
"GET - /favicon.ico",
180180
"GET - /release",
181+
"POST - /",
181182
"POST - /upload",
182183
"POST - /upload/",
183184
],
@@ -193,6 +194,7 @@ Object {
193194
0,
194195
0,
195196
0,
197+
0,
196198
],
197199
},
198200
Object {
@@ -205,6 +207,7 @@ Object {
205207
0,
206208
0,
207209
0,
210+
0,
208211
],
209212
},
210213
],
@@ -217,7 +220,17 @@ Object {
217220
"values": Array [
218221
"DELETE - /user/1",
219222
"GET - /",
223+
"GET - /css/app.0e433876.css",
224+
"GET - /css/vendor.74750554.css",
220225
"GET - /favicon.ico",
226+
"GET - /fonts/fluhrq6tzzclqej-vdg-iuiadsncihq8tq.b8c10426.woff2",
227+
"GET - /fonts/kfomcnqeu92fr1mu4mxm.49ae34d4.woff",
228+
"GET - /js/2.a13ff69c.js",
229+
"GET - /js/3.c5fd7573.js",
230+
"GET - /js/4.f6f9cf3e.js",
231+
"GET - /js/app.5a5abe2e.js",
232+
"GET - /js/vendor.b157e443.js",
233+
"GET - /playground",
221234
"GET - /playground/",
222235
"GET - /release",
223236
"GET - /user",
@@ -243,6 +256,16 @@ Object {
243256
0,
244257
0,
245258
0,
259+
0,
260+
0,
261+
0,
262+
0,
263+
0,
264+
0,
265+
0,
266+
0,
267+
0,
268+
0,
246269
],
247270
},
248271
Object {
@@ -260,6 +283,16 @@ Object {
260283
0,
261284
0,
262285
0,
286+
0,
287+
0,
288+
0,
289+
0,
290+
0,
291+
0,
292+
0,
293+
0,
294+
0,
295+
0,
263296
],
264297
},
265298
],

__tests__/scf.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ describe('Scf', () => {
6363
},
6464
},
6565
},
66+
clb: {
67+
clb: {
68+
parameters: {
69+
qualifier: '$DEFAULT',
70+
loadBalanceId: 'lb-l6golr1k',
71+
protocol: 'HTTP',
72+
domain: '81.71.86.84',
73+
port: 80,
74+
url: '/',
75+
weight: 20,
76+
},
77+
},
78+
},
6679
// mps: {
6780
// mps: {
6881
// parameters: {
@@ -324,6 +337,20 @@ describe('Scf', () => {
324337
// `TriggerType/${triggers.mps.mps.parameters.type}Event`,
325338
// ),
326339
// },
340+
{
341+
NeedCreate: expect.any(Boolean),
342+
namespace: inputs.namespace || 'default',
343+
functionName: inputs.name,
344+
qualifier: expect.any(String),
345+
loadBalanceId: triggers.clb.clb.parameters.loadBalanceId,
346+
listenerId: expect.stringContaining('lbl-'),
347+
locationId: expect.stringContaining('loc-'),
348+
domain: triggers.clb.clb.parameters.domain,
349+
protocol: triggers.clb.clb.parameters.protocol,
350+
port: triggers.clb.clb.parameters.port,
351+
url: triggers.clb.clb.parameters.url,
352+
weight: triggers.clb.clb.parameters.weight,
353+
},
327354
]);
328355
});
329356
test('should update SCF success', async () => {
@@ -495,6 +522,20 @@ describe('Scf', () => {
495522
// `TriggerType/${triggers.mps.mps.parameters.type}Event`,
496523
// ),
497524
// },
525+
{
526+
NeedCreate: expect.any(Boolean),
527+
namespace: inputs.namespace || 'default',
528+
functionName: inputs.name,
529+
qualifier: expect.any(String),
530+
loadBalanceId: triggers.clb.clb.parameters.loadBalanceId,
531+
listenerId: expect.stringContaining('lbl-'),
532+
locationId: expect.stringContaining('loc-'),
533+
domain: triggers.clb.clb.parameters.domain,
534+
protocol: triggers.clb.clb.parameters.protocol,
535+
port: triggers.clb.clb.parameters.port,
536+
url: triggers.clb.clb.parameters.url,
537+
weight: triggers.clb.clb.parameters.weight,
538+
},
498539
]);
499540
});
500541
test('should invoke Scf success', async () => {

__tests__/triggers/clb.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { sleep } from '@ygkit/request';
2+
import { CreateClbTriggerOutput } from './../../src/modules/triggers/interface/clb';
3+
import { ClbTriggerInputsParams } from '../../src/modules/triggers/interface';
4+
import { Scf } from '../../src';
5+
import ClbTrigger from '../../src/modules/triggers/clb';
6+
7+
describe('Clb Trigger', () => {
8+
const credentials = {
9+
SecretId: process.env.TENCENT_SECRET_ID,
10+
SecretKey: process.env.TENCENT_SECRET_KEY,
11+
};
12+
const client = new ClbTrigger({ credentials, region: process.env.REGION });
13+
const scfClient = new Scf(credentials, process.env.REGION);
14+
15+
const data: ClbTriggerInputsParams = {
16+
qualifier: '$DEFAULT',
17+
loadBalanceId: 'lb-l6golr1k',
18+
protocol: 'HTTP',
19+
domain: '81.71.86.84',
20+
port: 80,
21+
url: '/trigger-test',
22+
weight: 20,
23+
};
24+
25+
const functionName = 'serverless-unit-test';
26+
const namespace = 'default';
27+
let output: CreateClbTriggerOutput;
28+
29+
test('get listeners', async () => {
30+
const res = await client.clb.getListenerList(data.loadBalanceId);
31+
expect(res.length).toBe(1);
32+
});
33+
34+
test('create clb trigger', async () => {
35+
output = await client.create({
36+
inputs: {
37+
namespace: namespace,
38+
functionName: functionName,
39+
parameters: data,
40+
},
41+
});
42+
43+
expect(output).toEqual({
44+
listenerId: expect.stringContaining('lbl-'),
45+
locationId: expect.stringContaining('loc-'),
46+
namespace: namespace,
47+
functionName: functionName,
48+
qualifier: '$DEFAULT',
49+
...data,
50+
});
51+
});
52+
53+
test('delete clb trigger', async () => {
54+
const { Triggers = [] } = await scfClient.request({
55+
Action: 'ListTriggers',
56+
Namespace: namespace,
57+
FunctionName: functionName,
58+
Limit: 100,
59+
});
60+
const [exist] = Triggers.filter((item) => {
61+
const { ResourceId } = item;
62+
63+
if (ResourceId.indexOf(`${output.loadBalanceId}/${output.listenerId}/${output.locationId}`)) {
64+
return true;
65+
}
66+
return false;
67+
});
68+
69+
const res = await client.delete({
70+
scf: scfClient,
71+
inputs: {
72+
namespace: namespace,
73+
functionName: functionName,
74+
triggerDesc: exist.TriggerDesc,
75+
triggerName: exist.TriggerName,
76+
qualifier: exist.Qualifier,
77+
},
78+
});
79+
expect(res).toEqual({ requestId: expect.any(String), success: true });
80+
});
81+
82+
test('delete clb rule', async () => {
83+
await sleep(3000);
84+
const res = await client.clb.deleteRule({
85+
loadBalanceId: output.loadBalanceId,
86+
listenerId: output.listenerId,
87+
locationId: output.locationId,
88+
});
89+
expect(res).toBe(true);
90+
});
91+
});

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const config = {
88
silent: process.env.CI && !mod,
99
testTimeout: 600000,
1010
testEnvironment: 'node',
11-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$',
11+
testRegex: '/__tests__/.*\\.(test|spec)\\.(js|ts)$',
1212
// 由于测试账号没有备案域名,所以线上 CI 忽略 CDN 测试
1313
testPathIgnorePatterns: [
1414
'/node_modules/',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"build": "tsc -p .",
99
"test": "jest",
10+
"test:update": "jest --updateSnapshot",
1011
"test:local": "DEBUG=true jest",
1112
"test:cdn": "MODULE=cdn jest",
1213
"test:cls": "MODULE=cls jest",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export { default as Layer } from './modules/layer';
1313
export { default as Cfs } from './modules/cfs';
1414
export { default as Cynosdb } from './modules/cynosdb';
1515
export { default as Cls } from './modules/cls';
16+
export { default as Clb } from './modules/clb';

src/modules/clb/apis.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ApiFactory } from '../../utils/api';
2+
import { ApiServiceType } from '../interface';
3+
4+
const ACTIONS = [
5+
'CreateRule',
6+
'DeleteRule',
7+
'DescribeListeners',
8+
'RegisterFunctionTargets',
9+
'ModifyFunctionTargets',
10+
'DescribeTaskStatus',
11+
] as const;
12+
13+
export type ActionType = typeof ACTIONS[number];
14+
15+
const APIS = ApiFactory({
16+
// debug: true,
17+
isV3: false,
18+
serviceType: ApiServiceType.clb,
19+
version: '2018-03-17',
20+
actions: ACTIONS,
21+
});
22+
23+
export default APIS;

0 commit comments

Comments
 (0)