Skip to content

Commit 837b943

Browse files
authored
fix: multi-scf support yunti tag issue fix (#299)
修复multi-scf组件apigw绑定云梯标签不生效问题
1 parent b6328d7 commit 837b943

File tree

10 files changed

+48
-43
lines changed

10 files changed

+48
-43
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tencent-component-toolkit",
3-
"version": "2.25.0",
3+
"version": "2.25.1",
44
"description": "Tencent component toolkit",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/modules/apigw/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default class Apigw {
152152
}
153153

154154
try {
155-
const { tags } = inputs;
155+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
156156
if (tags) {
157157
await this.tagClient.deployResourceTags({
158158
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
@@ -301,7 +301,7 @@ export default class Apigw {
301301
apiList,
302302
};
303303

304-
const { tags } = inputs;
304+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
305305
if (tags) {
306306
await this.tagClient.deployResourceTags({
307307
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),

src/modules/cdn/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export default class Cdn {
240240
}
241241

242242
try {
243-
const { tags } = inputs;
243+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
244244
if (tags) {
245245
await this.tagClient.deployResourceTags({
246246
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),

src/modules/cfs/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default class CFS {
102102
}
103103

104104
try {
105-
const { tags } = inputs;
105+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
106106
if (tags) {
107107
await this.tagClient.deployResourceTags({
108108
tags: tags.map((item) => ({ TagKey: item.key, TagValue: item.value })),

src/modules/cynosdb/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export default class Cynosdb {
160160
}));
161161

162162
try {
163-
const { tags } = inputs;
163+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
164164
if (tags) {
165165
await this.tagClient.deployResourceTags({
166166
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),

src/modules/postgresql/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export default class Postgresql {
131131
}
132132

133133
try {
134-
const { tags } = inputs;
134+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
135135
if (tags) {
136136
await this.tagClient.deployResourceTags({
137137
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),

src/modules/scf/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ActionType } from './apis';
33
import { RegionType, ApiServiceType, CapiCredentials } from './../interface';
44
import { Capi } from '@tencent-sdk/capi';
55
import { ApiTypeError } from '../../utils/error';
6-
import { deepClone, formatInputTags, strip } from '../../utils';
6+
import { deepClone, strip } from '../../utils';
77
import TagsUtils from '../tag/index';
88
import ApigwUtils from '../apigw';
99
import CONFIGS from './config';
@@ -260,7 +260,7 @@ export default class Scf {
260260
namespace: funcInfo.Namespace,
261261
functionName: funcInfo.FunctionName,
262262
...trigger,
263-
tags: formatInputTags(tags),
263+
tags: this.tagClient.formatInputTags(tags),
264264
},
265265
});
266266

@@ -371,9 +371,10 @@ export default class Scf {
371371
}
372372

373373
// create/update tags
374-
if (inputs.tags) {
374+
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
375+
if (tags) {
375376
const deployedTags = await this.tagClient.deployResourceTags({
376-
tags: Object.entries(inputs.tags).map(([TagKey, TagValue]) => ({ TagKey, TagValue })),
377+
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
377378
resourceId: `${funcInfo!.Namespace}/function/${funcInfo!.FunctionName}`,
378379
serviceType: ApiServiceType.scf,
379380
resourcePrefix: 'namespace',
@@ -461,7 +462,7 @@ export default class Scf {
461462
}
462463

463464
checkAddedYunTiTags(tags: Array<{ [key: string]: string }>): boolean {
464-
const formatTags = formatInputTags(tags);
465+
const formatTags = this.tagClient.formatInputTags(tags);
465466
const result =
466467
formatTags?.length > 0 &&
467468
['运营部门', '运营产品', '负责人'].every((tagKey) =>

src/modules/tag/index.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ActionType } from './apis';
2-
import { RegionType, CapiCredentials, ApiServiceType } from './../interface';
2+
import { RegionType, CapiCredentials, ApiServiceType, TagInput } from './../interface';
33
import { Capi } from '@tencent-sdk/capi';
44
import APIS from './apis';
55
import {
@@ -274,4 +274,34 @@ export default class Tag {
274274

275275
return leftTags.concat(attachTags);
276276
}
277+
278+
/**
279+
* 格式化输入标签
280+
* @param inputs 输入标签
281+
* @returns 格式化后的标签列表
282+
*/
283+
formatInputTags(inputs: Array<any> | { [key: string]: string }): TagInput[] {
284+
let tags: TagInput[];
285+
if (Array.isArray(inputs)) {
286+
tags = inputs.map((item) => {
287+
return {
288+
key: item?.key ?? item?.Key ?? '',
289+
value: item?.value ?? item?.Value ?? '',
290+
};
291+
});
292+
} else if (typeof inputs === 'object' && inputs) {
293+
tags = Object.entries(inputs).map(([key, value]) => {
294+
return {
295+
key: (key ?? '').toString(),
296+
value: (value ?? '').toString(),
297+
};
298+
});
299+
} else if (typeof inputs !== 'object' && inputs) {
300+
// 非数组或者对象key-value类型的数据,需要返回原始输入数据
301+
tags = inputs;
302+
} else {
303+
tags = undefined as any;
304+
}
305+
return tags;
306+
}
277307
}

src/modules/vpc/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ export default class Vpc {
7878
vId = res.VpcId;
7979
}
8080

81-
if (tags) {
81+
const formateTags = this.tagClient.formatInputTags(tags as any);
82+
if (formateTags) {
8283
try {
8384
await this.tagClient.deployResourceTags({
84-
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
85+
tags: formateTags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
8586
resourceId: vId,
8687
serviceType: ApiServiceType.vpc,
8788
resourcePrefix: 'vpc',
@@ -141,7 +142,7 @@ export default class Vpc {
141142
}
142143
}
143144

144-
const subnetTagList = subnetTags ? subnetTags : tags;
145+
const subnetTagList = this.tagClient.formatInputTags((subnetTags ? subnetTags : tags) as any);
145146
if (subnetTagList) {
146147
try {
147148
await this.tagClient.deployResourceTags({

src/utils/index.ts

-27
Original file line numberDiff line numberDiff line change
@@ -298,30 +298,3 @@ export const getYunTiApiUrl = (): string => {
298298
const url = `${apiUrl}?api_key=${apiKey}&api_ts=${timeStamp}&api_sign=${apiSign}`;
299299
return url;
300300
};
301-
302-
/**
303-
* formatInputTags 格式化输入标签
304-
*/
305-
export const formatInputTags = (
306-
input: Array<any> | { [key: string]: string },
307-
): { key: string; value: string }[] => {
308-
let tags: { key: string; value: string }[];
309-
if (Array.isArray(input)) {
310-
tags = input.map((item) => {
311-
return {
312-
key: item?.key ?? item?.Key ?? '',
313-
value: item?.value ?? item?.Value ?? '',
314-
};
315-
});
316-
} else if (typeof input === 'object' && input) {
317-
tags = Object.entries(input).map(([key, value]) => {
318-
return {
319-
key: (key ?? '').toString(),
320-
value: (value ?? '').toString(),
321-
};
322-
});
323-
} else {
324-
tags = undefined as any;
325-
}
326-
return tags;
327-
};

0 commit comments

Comments
 (0)