Skip to content

Commit 127ff5c

Browse files
committed
feat: add cls module
1 parent 373ac42 commit 127ff5c

File tree

9 files changed

+339
-24
lines changed

9 files changed

+339
-24
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ jobs:
4141
- name: Running integration tests
4242
run: npm run test
4343
env:
44-
TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID}}
45-
TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY}}
46-
TENCENT_UIN: ${{ secrets.TENCENT_UIN}}
47-
TENCENT_APP_ID: ${{ secrets.TENCENT_APP_ID}}
48-
BUCKET: ${{ secrets.BUCKET}}
49-
DOMAIN: ${{ secrets.DOMAIN}}
50-
SUB_DOMAIN: ${{ secrets.SUB_DOMAIN}}
51-
REGION: ${{ secrets.REGION}}
52-
ZONE: ${{ secrets.ZONE}}
53-
VPC_ID: ${{ secrets.VPC_ID}}
54-
SUBNET_ID: ${{ secrets.SUBNET_ID}}
55-
CFS_VPC_ID: ${{ secrets.CFS_VPC_ID}}
56-
CFS_SUBNET_ID: ${{ secrets.CFS_SUBNET_ID}}
44+
TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }}
45+
TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }}
46+
TENCENT_UIN: ${{ secrets.TENCENT_UIN }}
47+
TENCENT_APP_ID: ${{ secrets.TENCENT_APP_ID }}
48+
BUCKET: ${{ secrets.BUCKET }}
49+
DOMAIN: ${{ secrets.DOMAIN }}
50+
SUB_DOMAIN: ${{ secrets.SUB_DOMAIN }}
51+
REGION: ${{ secrets.REGION }}
52+
ZONE: ${{ secrets.ZONE }}
53+
VPC_ID: ${{ secrets.VPC_ID }}
54+
SUBNET_ID: ${{ secrets.SUBNET_ID }}
55+
CFS_VPC_ID: ${{ secrets.CFS_VPC_ID }}
56+
CFS_SUBNET_ID: ${{ secrets.CFS_SUBNET_ID }}

__tests__/apigw.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('apigw', () => {
120120
apiList: [
121121
{
122122
path: '/',
123-
internalDomain: null,
123+
internalDomain: expect.any(String),
124124
method: 'GET',
125125
apiName: 'index',
126126
apiId: expect.stringContaining('api-'),
@@ -130,23 +130,23 @@ describe('apigw', () => {
130130
path: '/mo',
131131
method: 'GET',
132132
apiName: 'mo',
133-
internalDomain: null,
133+
internalDomain: expect.any(String),
134134
apiId: expect.stringContaining('api-'),
135135
created: true,
136136
},
137137
{
138138
path: '/auto',
139139
method: 'GET',
140140
apiName: 'auto-http',
141-
internalDomain: null,
141+
internalDomain: expect.any(String),
142142
apiId: expect.stringContaining('api-'),
143143
created: true,
144144
},
145145
{
146146
path: '/ws',
147147
method: 'GET',
148148
apiName: 'ws-test',
149-
internalDomain: null,
149+
internalDomain: expect.any(String),
150150
apiId: expect.stringContaining('api-'),
151151
created: true,
152152
},
@@ -192,7 +192,7 @@ describe('apigw', () => {
192192
apiList: [
193193
{
194194
path: '/',
195-
internalDomain: null,
195+
internalDomain: expect.any(String),
196196
method: 'GET',
197197
apiName: 'index',
198198
apiId: expect.stringContaining('api-'),
@@ -210,23 +210,23 @@ describe('apigw', () => {
210210
path: '/mo',
211211
method: 'GET',
212212
apiName: 'mo',
213-
internalDomain: null,
213+
internalDomain: expect.any(String),
214214
apiId: expect.stringContaining('api-'),
215215
created: true,
216216
},
217217
{
218218
path: '/auto',
219219
method: 'GET',
220220
apiName: 'auto-http',
221-
internalDomain: null,
221+
internalDomain: expect.any(String),
222222
apiId: expect.stringContaining('api-'),
223223
created: true,
224224
},
225225
{
226226
path: '/ws',
227227
method: 'GET',
228228
apiName: 'ws-test',
229-
internalDomain: null,
229+
internalDomain: expect.any(String),
230230
apiId: expect.stringContaining('api-'),
231231
created: true,
232232
},

__tests__/cls.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { Cls } = require('../src');
2+
3+
describe('Cls', () => {
4+
const credentials = {
5+
SecretId: process.env.TENCENT_SECRET_ID,
6+
SecretKey: process.env.TENCENT_SECRET_KEY,
7+
};
8+
const client = new Cls(credentials, process.env.REGION, 600000);
9+
10+
let outputs = {};
11+
12+
const inputs = {
13+
region: 'ap-guangzhou',
14+
name: 'cls-test',
15+
topic: 'cls-topic-test',
16+
period: 7,
17+
rule: {
18+
full_text: {
19+
case_sensitive: true,
20+
tokenizer: '!@#%^&*()_="\', <>/?|\\;:\n\t\r[]{}',
21+
},
22+
key_value: {
23+
case_sensitive: true,
24+
keys: ['SCF_RetMsg'],
25+
types: ['text'],
26+
tokenizers: [' '],
27+
},
28+
},
29+
};
30+
31+
test('should deploy cls success', async () => {
32+
const res = await client.deploy(inputs);
33+
expect(res).toEqual({
34+
region: process.env.REGION,
35+
name: inputs.name,
36+
topic: inputs.topic,
37+
logsetId: expect.any(String),
38+
topicId: expect.any(String),
39+
});
40+
41+
outputs = res;
42+
});
43+
44+
test('should remove cls success', async () => {
45+
await client.remove(outputs);
46+
47+
const detail = await client.cls.getLogset({
48+
logset_id: outputs.logsetId,
49+
});
50+
expect(detail.logset_id).toBeUndefined();
51+
expect(detail.error).toEqual({
52+
message: expect.any(String),
53+
});
54+
});
55+
});

__tests__/scf.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ describe('Scf', () => {
217217
apiList: [
218218
{
219219
path: '/',
220-
internalDomain: null,
220+
internalDomain: expect.any(String),
221221
method: 'GET',
222222
apiName: 'index',
223223
apiId: expect.stringContaining('api-'),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
},
7676
"dependencies": {
7777
"@tencent-sdk/capi": "^1.1.5",
78+
"@tencent-sdk/cls": "^0.1.6",
7879
"@ygkit/request": "^0.1.1",
7980
"cos-nodejs-sdk-v5": "^2.6.2",
8081
"moment": "^2.25.3",

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ module.exports = {
1515
Layer: require('./modules/layer'),
1616
Cfs: require('./modules/cfs'),
1717
Cynosdb: require('./modules/cynosdb'),
18+
Cls: require('./modules/cls'),
1819
};

src/modules/apigw/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class Apigw {
625625
serviceId: serviceId,
626626
apiId: output.apiId,
627627
});
628-
output.internalDomain = apiDetail.InternalDomain;
628+
output.internalDomain = apiDetail.InternalDomain || '';
629629
} else {
630630
console.log(`Updating api ${endpoint.apiId}.`);
631631
this.marshalApiInput(endpoint, apiInputs);
@@ -636,7 +636,7 @@ class Apigw {
636636
});
637637
output.apiId = endpoint.apiId;
638638
output.created = !!created;
639-
output.internalDomain = apiDetail.InternalDomain;
639+
output.internalDomain = apiDetail.InternalDomain || '';
640640
console.log(`Api ${output.apiId} updated`);
641641
}
642642

src/modules/cls/index.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
const ClsClient = require('@tencent-sdk/cls').Cls;
2+
const { ApiError } = require('../../utils/error');
3+
const { createLogset, createTopic, updateIndex } = require('./utils');
4+
5+
class Cls {
6+
constructor(credentials = {}, region, expire) {
7+
this.region = region || 'ap-guangzhou';
8+
this.credentials = credentials;
9+
this.cls = new ClsClient({
10+
region: this.region,
11+
secretId: credentials.SecretId,
12+
secretKey: credentials.SecretKey,
13+
token: credentials.Token,
14+
debug: false,
15+
expire: expire || 300000,
16+
});
17+
}
18+
19+
async deployLogset(inputs) {
20+
const outputs = {
21+
region: this.region,
22+
name: inputs.name,
23+
logsetId: '',
24+
};
25+
let exist = false;
26+
const { logsetId } = inputs;
27+
if (logsetId) {
28+
const detail = await this.cls.getLogset({
29+
logset_id: logsetId,
30+
});
31+
if (detail.error) {
32+
throw new ApiError({
33+
type: 'API_getLogset',
34+
message: detail.error.message,
35+
});
36+
}
37+
38+
// update it
39+
if (detail.logset_id) {
40+
exist = true;
41+
console.log(`Updating cls ${logsetId}`);
42+
const res = await this.cls.updateLogset({
43+
logset_id: logsetId,
44+
logset_name: inputs.name,
45+
});
46+
if (res.error) {
47+
throw new ApiError({
48+
type: 'API_updateLogset',
49+
message: detail.error.message,
50+
});
51+
}
52+
53+
console.log(`Update cls ${logsetId} success`);
54+
55+
outputs.logsetId = logsetId;
56+
}
57+
}
58+
59+
// if not exist, create cls
60+
if (!exist) {
61+
const res = await createLogset(this.cls, {
62+
name: inputs.name,
63+
period: inputs.period,
64+
});
65+
outputs.logsetId = res.logset_id;
66+
}
67+
68+
return outputs;
69+
}
70+
71+
async deployTopic(inputs) {
72+
const outputs = {
73+
region: this.region,
74+
name: inputs.topic,
75+
topicId: '',
76+
};
77+
let exist = false;
78+
const { topicId } = inputs;
79+
if (topicId) {
80+
const detail = await this.cls.getTopic({
81+
topic_id: topicId,
82+
});
83+
if (detail.error) {
84+
throw new ApiError({
85+
type: 'API_getTopic',
86+
message: detail.error.message,
87+
});
88+
}
89+
90+
// update it
91+
if (detail.topic_id) {
92+
exist = true;
93+
console.log(`Updating cls topic ${topicId}`);
94+
const res = await this.cls.updateTopic({
95+
topic_id: topicId,
96+
topic_name: inputs.topic,
97+
});
98+
if (res.error) {
99+
throw new ApiError({
100+
type: 'API_updateTopic',
101+
message: detail.error.message,
102+
});
103+
}
104+
105+
console.log(`Update cls topic ${topicId} success`);
106+
107+
outputs.topicId = topicId;
108+
}
109+
}
110+
111+
// if not exist, create cls
112+
if (!exist) {
113+
const res = await createTopic(this.cls, {
114+
logsetId: inputs.logsetId,
115+
name: inputs.topic,
116+
});
117+
outputs.topicId = res.topic_id;
118+
}
119+
120+
return outputs;
121+
}
122+
123+
async deployIndex(inputs) {
124+
await updateIndex(this.cls, {
125+
topicId: inputs.topicId,
126+
effective: inputs.effective !== false ? true : false,
127+
rule: inputs.rule,
128+
});
129+
}
130+
131+
async deploy(inputs = {}) {
132+
const outputs = {
133+
region: this.region,
134+
name: inputs.name,
135+
topic: inputs.topic,
136+
};
137+
138+
const logsetOutput = await this.deployLogset(inputs);
139+
outputs.logsetId = inputs.logsetId = logsetOutput.logsetId;
140+
const topicOutput = await this.deployTopic(inputs);
141+
outputs.topicId = inputs.topicId = topicOutput.topicId;
142+
await this.deployIndex(inputs);
143+
144+
return outputs;
145+
}
146+
147+
async remove(inputs = {}) {
148+
try {
149+
console.log(`Start removing cls`);
150+
console.log(`Removing cls topic id ${inputs.topicId}`);
151+
const res1 = await this.cls.deleteTopic({
152+
topic_id: inputs.topicId,
153+
});
154+
if (res1.error) {
155+
throw new ApiError({
156+
type: 'API_deleteTopic',
157+
message: res1.error.message,
158+
});
159+
}
160+
console.log(`Removed cls topic id ${inputs.logsetId} success`);
161+
console.log(`Removing cls id ${inputs.logsetId}`);
162+
const res2 = await this.cls.deleteLogset({
163+
logset_id: inputs.logsetId,
164+
});
165+
if (res2.error) {
166+
throw new ApiError({
167+
type: 'API_deleteLogset',
168+
message: res2.error.message,
169+
});
170+
}
171+
console.log(`Removed cls id ${inputs.logsetId} success`);
172+
} catch (e) {
173+
console.log(e);
174+
}
175+
176+
return {};
177+
}
178+
}
179+
180+
module.exports = Cls;

0 commit comments

Comments
 (0)