Skip to content

Commit 5f253c5

Browse files
committed
fix(cfs): support tag config
1 parent a8f9fcc commit 5f253c5

File tree

5 files changed

+124
-67
lines changed

5 files changed

+124
-67
lines changed

__tests__/cfs.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('Cfs', () => {
77
SecretId: process.env.TENCENT_SECRET_ID,
88
SecretKey: process.env.TENCENT_SECRET_KEY,
99
};
10+
1011
const inputs = {
1112
fsName: 'cfs-test',
1213
region: 'ap-guangzhou',
@@ -16,6 +17,12 @@ describe('Cfs', () => {
1617
vpcId: process.env.CFS_VPC_ID,
1718
subnetId: process.env.CFS_SUBNET_ID,
1819
},
20+
tags: [
21+
{
22+
key: 'slstest',
23+
value: 'slstest',
24+
},
25+
],
1926
};
2027
const cfs = new Cfs(credentials, process.env.REGION);
2128

@@ -29,6 +36,7 @@ describe('Cfs', () => {
2936
protocol: 'NFS',
3037
storageType: 'SD',
3138
fileSystemId: expect.stringContaining('cfs-'),
39+
tags: inputs.tags,
3240
});
3341
inputs.fileSystemId = res.fileSystemId;
3442
});

src/modules/cfs/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { Capi } = require('@tencent-sdk/capi');
22
const apis = require('./apis');
3+
const Tag = require('../tag/index');
34

45
class CFS {
56
constructor(credentials = {}, region) {
@@ -11,6 +12,8 @@ class CFS {
1112
SecretKey: credentials.SecretKey,
1213
Token: credentials.Token,
1314
});
15+
16+
this.tagClient = new Tag(this.credentials, this.region);
1417
}
1518

1619
async deploy(inputs = {}) {
@@ -71,15 +74,30 @@ class CFS {
7174
}
7275
}
7376

74-
if (inputs.tags) {
75-
cfsInputs.ResourceTags = inputs.tags;
76-
}
7777
console.log(`Creating CFS ${inputs.fsName}`);
7878
const { FileSystemId } = await apis.createCfs(this.capi, cfsInputs);
7979
console.log(`Created CFS ${inputs.fsName}, id ${FileSystemId} successful`);
8080
outputs.fileSystemId = FileSystemId;
8181
}
8282

83+
if (inputs.tags) {
84+
try {
85+
const tags = await this.tagClient.deployResourceTags({
86+
tags: inputs.tags.map((item) => ({ TagKey: item.key, TagValue: item.value })),
87+
serviceType: 'cfs',
88+
resourcePrefix: 'filesystem',
89+
resourceId: outputs.fileSystemId,
90+
});
91+
92+
outputs.tags = tags.map((item) => ({
93+
key: item.TagKey,
94+
value: item.TagValue,
95+
}));
96+
} catch (e) {
97+
console.log(`Deploy cfs tags error: ${e.message}`);
98+
}
99+
}
100+
83101
return outputs;
84102
}
85103

src/modules/scf/index.js

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -313,62 +313,6 @@ class Scf {
313313
return triggerResult;
314314
}
315315

316-
// deploy tags
317-
async deployTags(funcInfo, inputs) {
318-
console.log(`Adding tags for function ${inputs.name} in ${this.region}`);
319-
const inputKeys = [];
320-
const inputTags = Object.entries(inputs.tags).map(([key, value]) => {
321-
inputKeys.push(key);
322-
return {
323-
Key: key,
324-
Value: value,
325-
};
326-
});
327-
const funcTags = funcInfo.Tags;
328-
const funcTagKeys = funcTags.map((item) => item.Key);
329-
330-
const detachTags = [];
331-
const attachTags = [];
332-
333-
for (let i = 0; i < funcTags.length; i++) {
334-
const funcTag = funcTags[i];
335-
if (inputKeys.indexOf(funcTag.Key) === -1) {
336-
detachTags.push({
337-
TagKey: funcTag.Key,
338-
});
339-
} else {
340-
const inputTagVal = inputs.tags[funcTag.Key];
341-
const funcTagVal = funcTags[i].Value;
342-
if (inputTagVal !== funcTagVal) {
343-
attachTags.push({
344-
TagKey: funcTag.Key,
345-
TagValue: inputTagVal,
346-
});
347-
}
348-
}
349-
}
350-
351-
for (let i = 0; i < inputTags.length; i++) {
352-
const inputTag = inputTags[i];
353-
if (funcTagKeys.indexOf(inputTag.Key) === -1) {
354-
attachTags.push({
355-
TagKey: inputTag.Key,
356-
TagValue: inputTag.Value,
357-
});
358-
}
359-
}
360-
361-
await this.tagClient.deploy({
362-
resourceIds: [`${funcInfo.Namespace}/function/${funcInfo.FunctionName}`],
363-
resourcePrefix: 'namespace',
364-
serviceType: 'scf',
365-
detachTags,
366-
attachTags,
367-
});
368-
369-
return attachTags;
370-
}
371-
372316
// delete function
373317
async deleteFunction(namespace, functionName) {
374318
namespace = namespace || CONFIGS.defaultNamespace;
@@ -676,7 +620,13 @@ class Scf {
676620

677621
// create/update tags
678622
if (inputs.tags) {
679-
const deployedTags = await this.deployTags(funcInfo, inputs);
623+
const deployedTags = await this.tagClient.deployResourceTags({
624+
tags: Object.entries(inputs.tags).map(([TagKey, TagValue]) => ({ TagKey, TagValue })),
625+
resourceId: `${funcInfo.Namespace}/function/${funcInfo.FunctionName}`,
626+
serviceType: 'scf',
627+
resourcePrefix: 'namespace',
628+
});
629+
680630
outputs.Tags = deployedTags.map((item) => ({
681631
Key: item.TagKey,
682632
Value: item.TagValue,

src/modules/tag/apis.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ACTIONS = [
88
'CreateTag',
99
'DeleteTag',
1010
'DescribeTags',
11+
'DescribeResourceTagsByResourceIds',
1112
];
1213

1314
const APIS = ApiFactory({

src/modules/tag/index.js

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ class Tag {
1919
return result;
2020
}
2121

22+
async getResourceTags({ resourceId, serviceType, resourcePrefix, offset = 0, limit = 100 }) {
23+
const { Tags, TotalCount } = await this.request({
24+
Action: 'DescribeResourceTagsByResourceIds',
25+
Limit: limit,
26+
Offset: offset,
27+
ServiceType: serviceType,
28+
ResourceRegion: this.region,
29+
ResourcePrefix: resourcePrefix,
30+
ResourceIds: [resourceId],
31+
});
32+
if (TotalCount > limit) {
33+
return Tags.concat(
34+
await this.getResourceTags({
35+
resourceId,
36+
serviceType,
37+
resourcePrefix,
38+
offset: offset + limit,
39+
limit,
40+
}),
41+
);
42+
}
43+
44+
return Tags;
45+
}
46+
2247
async getTagList(offset = 0, limit = 100) {
2348
const { Tags, TotalCount } = await this.request({
2449
Action: 'DescribeTags',
@@ -41,14 +66,13 @@ class Tag {
4166
}
4267

4368
async getScfResourceTags(inputs) {
44-
const data = {
45-
Action: 'DescribeResourceTags',
46-
ResourcePrefix: 'namespace',
47-
ResourceId: `${inputs.namespace || 'default'}/function/${inputs.functionName}`,
48-
};
69+
const tags = await this.getResourceTags({
70+
resourceId: `${inputs.namespace || 'default'}/function/${inputs.functionName}`,
71+
serviceType: 'scf',
72+
resourcePrefix: 'namespace',
73+
});
4974

50-
const { Rows } = await this.request(data);
51-
return Rows;
75+
return tags;
5276
}
5377

5478
async attachTags({ serviceType, resourcePrefix, resourceIds, tags }) {
@@ -145,6 +169,62 @@ class Tag {
145169

146170
return true;
147171
}
172+
173+
async deployResourceTags({ tags, resourceId, serviceType, resourcePrefix }) {
174+
console.log(`Adding tags for ${resourceId} in ${this.region}`);
175+
const inputKeys = [];
176+
tags.forEach(({ TagKey }) => {
177+
inputKeys.push(TagKey);
178+
});
179+
180+
const oldTags = await this.getResourceTags({
181+
resourceId: resourceId,
182+
serviceType: serviceType,
183+
resourcePrefix: resourcePrefix,
184+
});
185+
186+
const oldTagKeys = [];
187+
oldTags.forEach(({ TagKey }) => {
188+
oldTagKeys.push(TagKey);
189+
});
190+
191+
const detachTags = [];
192+
const attachTags = [];
193+
const leftTags = [];
194+
195+
oldTags.forEach((item) => {
196+
if (inputKeys.indexOf(item.TagKey) === -1) {
197+
detachTags.push({
198+
TagKey: item.TagKey,
199+
});
200+
} else {
201+
const [inputTag] = tags.filter((t) => t.TagKey === item.TagKey);
202+
const oldTagVal = item.TagValue;
203+
204+
if (inputTag.TagValue !== oldTagVal) {
205+
attachTags.push(inputTag);
206+
} else {
207+
leftTags.push(item);
208+
}
209+
}
210+
});
211+
212+
tags.forEach((item) => {
213+
if (oldTagKeys.indexOf(item.TagKey) === -1) {
214+
attachTags.push(item);
215+
}
216+
});
217+
218+
await this.deploy({
219+
resourceIds: [resourceId],
220+
resourcePrefix: resourcePrefix,
221+
serviceType: serviceType,
222+
detachTags,
223+
attachTags,
224+
});
225+
226+
return leftTags.concat(attachTags);
227+
}
148228
}
149229

150230
module.exports = Tag;

0 commit comments

Comments
 (0)