Skip to content

Commit a354030

Browse files
committed
fix(tag): optimize deploy tag
1 parent a787268 commit a354030

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

__tests__/tag.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ describe('Tag', () => {
4848
test('delete tags', async () => {
4949
const res = await tag.deleteTags([tagItem]);
5050

51+
const tagList = await tag.getTagList();
52+
const [exist] = tagList.filter(
53+
(item) => item.TagKey === tagItem.TagKey && item.TagValue === tagItem.TagValue,
54+
);
5155
expect(res).toBe(true);
52-
53-
const exist = await tag.isTagExist(tagItem);
54-
55-
expect(exist).toBe(false);
56+
expect(exist).toBeUndefined();
5657
});
5758
});

src/modules/tag/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,27 @@ export default class Tag {
6565
return Tags;
6666
}
6767

68-
async getTagList(offset: number = 0, limit: number = 100): Promise<TagData[]> {
68+
async getTagList(
69+
tagKeys: string[] = [],
70+
offset: number = 0,
71+
limit: number = 100,
72+
): Promise<TagData[]> {
6973
const { Tags, TotalCount } = await this.request({
7074
Action: 'DescribeTags',
7175
Limit: limit,
7276
Offset: offset,
77+
TagKeys: tagKeys,
7378
});
7479
if (TotalCount > limit) {
75-
return Tags.concat(await this.getTagList(offset + limit, limit));
80+
return Tags.concat(await this.getTagList(tagKeys, offset + limit, limit));
7681
}
7782

7883
return Tags;
7984
}
8085

81-
async isTagExist(tag: TagData) {
82-
const tagList = await this.getTagList();
86+
isTagExist(tag: TagData, tagList: TagData[] = []) {
8387
const [exist] = tagList.filter(
84-
(item) => item.TagKey === tag.TagKey && item.TagValue === tag.TagValue,
88+
(item) => item.TagKey === tag.TagKey && String(item.TagValue) === String(tag.TagValue),
8589
);
8690
return !!exist;
8791
}
@@ -104,12 +108,15 @@ export default class Tag {
104108
ResourceRegion: this.region,
105109
ResourcePrefix: resourcePrefix,
106110
};
107-
// if tag not exsit, create it
111+
if (tags && tags.length > 0) {
112+
const tagKeys = tags.map((item) => item.TagKey);
113+
const tagList = await this.getTagList(tagKeys);
108114

109-
if (tags) {
110115
for (let i = 0; i < tags.length; i++) {
111116
const currentTag = tags[i];
112-
const tagExist = await this.isTagExist(currentTag);
117+
const tagExist = await this.isTagExist(currentTag, tagList);
118+
119+
// if tag not exsit, create it
113120
if (!tagExist) {
114121
await this.createTag(currentTag);
115122
}
@@ -232,7 +239,7 @@ export default class Tag {
232239
const [inputTag] = tags.filter((t) => t.TagKey === item.TagKey);
233240
const oldTagVal = item.TagValue;
234241

235-
if (inputTag.TagValue !== oldTagVal) {
242+
if (String(inputTag.TagValue) !== String(oldTagVal)) {
236243
attachTags.push(inputTag);
237244
} else {
238245
leftTags.push(item);

0 commit comments

Comments
 (0)