Skip to content

Commit d369471

Browse files
authored
fix(apigw): bind same usage plan to different apis (#157)
* fix(apigw): bind same usage plan to different apis * fix: optimize log
1 parent 4d49c25 commit d369471

File tree

3 files changed

+461
-333
lines changed

3 files changed

+461
-333
lines changed

__tests__/apigw.test.js

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
const { Apigw } = require('../src');
22

3+
const deepClone = (obj) => {
4+
return JSON.parse(JSON.stringify(obj));
5+
};
6+
37
describe('apigw', () => {
48
const credentials = {
59
SecretId: process.env.TENCENT_SECRET_ID,
@@ -25,6 +29,15 @@ describe('apigw', () => {
2529
// protocols: ['http', 'https'],
2630
// },
2731
// ],
32+
usagePlan: {
33+
usagePlanId: 'usagePlan-8bbr8pup',
34+
usagePlanName: 'slscmp',
35+
usagePlanDesc: 'sls create',
36+
maxRequestNum: 1000,
37+
},
38+
auth: {
39+
secretName: 'authName',
40+
},
2841
endpoints: [
2942
{
3043
apiId: 'api-i84p7rla',
@@ -35,17 +48,6 @@ describe('apigw', () => {
3548
function: {
3649
functionName: 'egg-function',
3750
},
38-
usagePlan: {
39-
usagePlanId: 'usagePlan-8bbr8pup',
40-
usagePlanName: 'slscmp',
41-
usagePlanDesc: 'sls create',
42-
maxRequestNum: 1000,
43-
},
44-
auth: {
45-
serviceTimeout: 15,
46-
secretName: 'authName',
47-
secretIds: ['xxx'],
48-
},
4951
},
5052
{
5153
path: '/mo',
@@ -97,19 +99,99 @@ describe('apigw', () => {
9799
const apigw = new Apigw(credentials, process.env.REGION);
98100
let outputs;
99101

100-
test('should deploy a apigw success', async () => {
101-
outputs = await apigw.deploy(inputs);
102+
test('[Environment UsagePlan] should deploy a apigw success', async () => {
103+
const apigwInputs = deepClone(inputs);
104+
outputs = await apigw.deploy(apigwInputs);
105+
expect(outputs).toEqual({
106+
created: true,
107+
serviceId: expect.stringContaining('service-'),
108+
serviceName: 'serverless_test',
109+
subDomain: expect.stringContaining('.apigw.tencentcs.com'),
110+
protocols: 'http&https',
111+
environment: 'release',
112+
usagePlan: {
113+
created: true,
114+
secrets: {
115+
created: true,
116+
secretIds: expect.any(Array),
117+
},
118+
usagePlanId: expect.stringContaining('usagePlan-'),
119+
},
120+
apiList: [
121+
{
122+
path: '/',
123+
internalDomain: null,
124+
method: 'GET',
125+
apiName: 'index',
126+
apiId: expect.stringContaining('api-'),
127+
created: true,
128+
},
129+
{
130+
path: '/mo',
131+
method: 'GET',
132+
apiName: 'mo',
133+
internalDomain: null,
134+
apiId: expect.stringContaining('api-'),
135+
created: true,
136+
},
137+
{
138+
path: '/auto',
139+
method: 'GET',
140+
apiName: 'auto-http',
141+
internalDomain: null,
142+
apiId: expect.stringContaining('api-'),
143+
created: true,
144+
},
145+
{
146+
path: '/ws',
147+
method: 'GET',
148+
apiName: 'ws-test',
149+
internalDomain: null,
150+
apiId: expect.stringContaining('api-'),
151+
created: true,
152+
},
153+
{
154+
path: '/wsf',
155+
method: 'GET',
156+
apiName: 'ws-scf',
157+
internalDomain: expect.stringContaining(
158+
'http://set-websocket.cb-common.apigateway.tencentyun.com',
159+
),
160+
apiId: expect.stringContaining('api-'),
161+
created: true,
162+
},
163+
],
164+
});
165+
});
166+
167+
test('[Environment UsagePlan] should remove apigw success', async () => {
168+
await apigw.remove(outputs);
169+
const detail = await apigw.request({
170+
Action: 'DescribeService',
171+
ServiceId: outputs.serviceId,
172+
});
173+
174+
expect(detail).toBeNull();
175+
});
176+
177+
test('[Api UsagePlan] should deploy a apigw success', async () => {
178+
const apigwInputs = deepClone(inputs);
179+
apigwInputs.endpoints[0].usagePlan = apigwInputs.usagePlan;
180+
apigwInputs.endpoints[0].auth = apigwInputs.auth;
181+
delete apigwInputs.usagePlan;
182+
delete apigwInputs.auth;
183+
184+
outputs = await apigw.deploy(apigwInputs);
102185
expect(outputs).toEqual({
103186
created: true,
104187
serviceId: expect.stringContaining('service-'),
105188
serviceName: 'serverless_test',
106189
subDomain: expect.stringContaining('.apigw.tencentcs.com'),
107-
protocols: inputs.protocols,
190+
protocols: 'http&https',
108191
environment: 'release',
109192
apiList: [
110193
{
111194
path: '/',
112-
bindType: 'API',
113195
internalDomain: null,
114196
method: 'GET',
115197
apiName: 'index',
@@ -118,8 +200,8 @@ describe('apigw', () => {
118200
usagePlan: {
119201
created: true,
120202
secrets: {
121-
created: false,
122-
secretIds: [],
203+
created: true,
204+
secretIds: expect.any(Array),
123205
},
124206
usagePlanId: expect.stringContaining('usagePlan-'),
125207
},
@@ -162,7 +244,7 @@ describe('apigw', () => {
162244
});
163245
});
164246

165-
test('should remove apigw success', async () => {
247+
test('[Api UsagePlan] should remove apigw success', async () => {
166248
await apigw.remove(outputs);
167249
const detail = await apigw.request({
168250
Action: 'DescribeService',

src/modules/apigw/apis.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ACTIONS = [
1414
'ModifyApi',
1515
'DescribeApisStatus',
1616
'CreateUsagePlan',
17+
'DescribeServiceUsagePlan',
1718
'DescribeApiUsagePlan',
1819
'DescribeUsagePlanSecretIds',
1920
'DescribeUsagePlan',

0 commit comments

Comments
 (0)