Skip to content

Commit c15fdad

Browse files
committed
fix(cdn): optimize cdn deploy and use v3 sign
1 parent e633b82 commit c15fdad

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

__tests__/cdn.test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
const { Cdn } = require('../src');
2+
const { getCdnByDomain } = require('../src/modules/cdn/utils');
23

34
describe('Cdn', () => {
5+
jest.setTimeout(600000);
46
const credentials = {
57
SecretId: process.env.TENCENT_SECRET_ID,
68
SecretKey: process.env.TENCENT_SECRET_KEY,
79
};
810
const inputs = {
9-
async: true,
11+
async: false,
1012
area: 'overseas',
1113
domain: 'test.yuga.chat',
12-
hostType: 'cos',
1314
origin: {
1415
origins: ['up6pwd9-89hm718-xxx.cos-website.ap-guangzhou.myqcloud.com'],
1516
originType: 'cos',
@@ -31,10 +32,9 @@ describe('Cdn', () => {
3132
};
3233
const cdn = new Cdn(credentials, process.env.REGION);
3334

34-
test('should deploy CDN success', async () => {
35+
test('should deploy CDN success with originType = cos', async () => {
3536
const res = await cdn.deploy(inputs);
3637
expect(res).toEqual({
37-
created: true,
3838
https: true,
3939
domain: inputs.domain,
4040
origins: inputs.origin.origins,
@@ -43,5 +43,26 @@ describe('Cdn', () => {
4343
resourceId: expect.stringContaining('cdn-'),
4444
});
4545
});
46+
47+
48+
test('should deploy CDN success with originType = domain', async () => {
49+
inputs.origin.originType = 'domain';
50+
const res = await cdn.deploy(inputs);
51+
expect(res).toEqual({
52+
https: true,
53+
domain: inputs.domain,
54+
origins: inputs.origin.origins,
55+
cname: `${inputs.domain}.cdn.dnsv1.com`,
56+
inputCache: JSON.stringify(inputs),
57+
resourceId: expect.stringContaining('cdn-'),
58+
});
59+
});
60+
61+
test('should remove CDN success', async () => {
62+
const res = await cdn.remove(inputs);
63+
expect(res).toEqual({});
64+
const detail = await getCdnByDomain(cdn.capi, inputs.domain);
65+
expect(detail).toBeUndefined();
66+
});
4667
});
4768

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "src/index.js",
66
"scripts": {
77
"test": "jest",
8+
"test:cdn": "MODULE=cdn jest",
89
"format": "npm run lint && npm run prettier",
910
"commitlint": "commitlint -f HEAD@{15}",
1011
"lint": "eslint --ext .js,.ts,.tsx .",

src/modules/cdn/apis.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const { ApiFactory } = require('../../utils/api');
22

33
const ACTIONS = [
4+
'OpenCdnService',
45
'AddCdnDomain',
56
'DescribeDomains',
67
'UpdateDomainConfig',
8+
'StartCdnDomain',
79
'StopCdnDomain',
810
'DeleteCdnDomain',
911
'PushUrlsCache',
@@ -13,6 +15,7 @@ const ACTIONS = [
1315

1416
const APIS = ApiFactory({
1517
// debug: true,
18+
isV3: true,
1619
serviceType: 'cdn',
1720
version: '2018-06-06',
1821
actions: ACTIONS,

src/modules/cdn/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
camelCaseProperty,
1717
getCdnByDomain,
1818
flushEmptyValue,
19+
openCdnService,
1920
} = require('./utils');
2021

2122
class Cdn {
@@ -66,6 +67,7 @@ class Cdn {
6667
}
6768

6869
async deploy(inputs = {}) {
70+
await openCdnService(this.capi);
6971
const { oldState = {} } = inputs;
7072
delete inputs.oldState;
7173
const {
@@ -151,7 +153,7 @@ class Cdn {
151153
domain: Domain,
152154
origins: cdnInputs.Origin.Origins,
153155
cname: `${Domain}.cdn.dnsv1.com`,
154-
inputCache: JSON.stringify(cdnInputs),
156+
inputCache: JSON.stringify(inputs),
155157
};
156158

157159
if (Https) {
@@ -172,11 +174,17 @@ class Cdn {
172174
};
173175
}
174176

175-
const cdnInfo = await getCdnByDomain(this.capi, Domain);
177+
let cdnInfo = await getCdnByDomain(this.capi, Domain);
176178

177179
const sourceInputs = JSON.parse(JSON.stringify(cdnInputs));
178180

179181
const createOrUpdateCdn = async () => {
182+
if (cdnInfo && cdnInfo.Status === 'offline') {
183+
console.log(`The CDN domain ${Domain} is offline.`);
184+
console.log(`Recreating CDN domain ${Domain}`);
185+
await DeleteCdnDomain(this.capi, { Domain: Domain });
186+
cdnInfo = null;
187+
}
180188
if (cdnInfo) {
181189
// update
182190
console.log(`The CDN domain ${Domain} has existed.`);
@@ -195,15 +203,14 @@ class Cdn {
195203
cdnInputs.ServiceType = ServiceType || 'web';
196204
await AddCdnDomain(this.capi, cdnInputs);
197205
} catch (e) {
198-
if (e.code === 9111) {
206+
if (e.code === 'ResourceNotFound.CdnUserNotExists') {
199207
console.log(`Please goto https://console.cloud.tencent.com/cdn open CDN service.`);
200208
}
201209
throw e;
202210
}
203211
await sleep(1000);
204212
const detail = await getCdnByDomain(this.capi, Domain);
205213

206-
outputs.created = true;
207214
outputs.resourceId = detail && detail.ResourceId;
208215
}
209216

src/modules/cdn/utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const { DescribeDomains } = require('./apis');
3+
const { DescribeDomains, OpenCdnService } = require('./apis');
44

55
const ONE_SECOND = 1000;
66
// timeout 5 minutes
@@ -128,6 +128,19 @@ function flushEmptyValue(obj) {
128128
return newObj;
129129
}
130130

131+
async function openCdnService(capi) {
132+
try {
133+
await OpenCdnService(capi, {
134+
PayTypeMainland: 'flux',
135+
PayTypeOverseas: 'flux',
136+
});
137+
} catch (e) {
138+
if (e.code !== 'ResourceInUse.CdnUserExists') {
139+
throw e;
140+
}
141+
}
142+
}
143+
131144
module.exports = {
132145
ONE_SECOND,
133146
TIMEOUT,
@@ -139,4 +152,5 @@ module.exports = {
139152
formatOrigin,
140153
camelCaseProperty,
141154
flushEmptyValue,
155+
openCdnService,
142156
};

0 commit comments

Comments
 (0)