Skip to content

fix: multi-scf support yunti tag issue fix #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tencent-component-toolkit",
"version": "2.25.0",
"version": "2.25.1",
"description": "Tencent component toolkit",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/modules/apigw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class Apigw {
}

try {
const { tags } = inputs;
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
if (tags) {
await this.tagClient.deployResourceTags({
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
Expand Down Expand Up @@ -301,7 +301,7 @@ export default class Apigw {
apiList,
};

const { tags } = inputs;
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
if (tags) {
await this.tagClient.deployResourceTags({
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cdn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default class Cdn {
}

try {
const { tags } = inputs;
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
if (tags) {
await this.tagClient.deployResourceTags({
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cfs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class CFS {
}

try {
const { tags } = inputs;
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
if (tags) {
await this.tagClient.deployResourceTags({
tags: tags.map((item) => ({ TagKey: item.key, TagValue: item.value })),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cynosdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default class Cynosdb {
}));

try {
const { tags } = inputs;
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
if (tags) {
await this.tagClient.deployResourceTags({
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/postgresql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class Postgresql {
}

try {
const { tags } = inputs;
const tags = this.tagClient.formatInputTags(inputs?.tags as any);
if (tags) {
await this.tagClient.deployResourceTags({
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
Expand Down
11 changes: 6 additions & 5 deletions src/modules/scf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionType } from './apis';
import { RegionType, ApiServiceType, CapiCredentials } from './../interface';
import { Capi } from '@tencent-sdk/capi';
import { ApiTypeError } from '../../utils/error';
import { deepClone, formatInputTags, strip } from '../../utils';
import { deepClone, strip } from '../../utils';
import TagsUtils from '../tag/index';
import ApigwUtils from '../apigw';
import CONFIGS from './config';
Expand Down Expand Up @@ -260,7 +260,7 @@ export default class Scf {
namespace: funcInfo.Namespace,
functionName: funcInfo.FunctionName,
...trigger,
tags: formatInputTags(tags),
tags: this.tagClient.formatInputTags(tags),
},
});

Expand Down Expand Up @@ -371,9 +371,10 @@ export default class Scf {
}

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

checkAddedYunTiTags(tags: Array<{ [key: string]: string }>): boolean {
const formatTags = formatInputTags(tags);
const formatTags = this.tagClient.formatInputTags(tags);
const result =
formatTags?.length > 0 &&
['运营部门', '运营产品', '负责人'].every((tagKey) =>
Expand Down
32 changes: 31 additions & 1 deletion src/modules/tag/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionType } from './apis';
import { RegionType, CapiCredentials, ApiServiceType } from './../interface';
import { RegionType, CapiCredentials, ApiServiceType, TagInput } from './../interface';
import { Capi } from '@tencent-sdk/capi';
import APIS from './apis';
import {
Expand Down Expand Up @@ -274,4 +274,34 @@ export default class Tag {

return leftTags.concat(attachTags);
}

/**
* 格式化输入标签
* @param inputs 输入标签
* @returns 格式化后的标签列表
*/
formatInputTags(inputs: Array<any> | { [key: string]: string }): TagInput[] {
let tags: TagInput[];
if (Array.isArray(inputs)) {
tags = inputs.map((item) => {
return {
key: item?.key ?? item?.Key ?? '',
value: item?.value ?? item?.Value ?? '',
};
});
} else if (typeof inputs === 'object' && inputs) {
tags = Object.entries(inputs).map(([key, value]) => {
return {
key: (key ?? '').toString(),
value: (value ?? '').toString(),
};
});
} else if (typeof inputs !== 'object' && inputs) {
// 非数组或者对象key-value类型的数据,需要返回原始输入数据
tags = inputs;
} else {
tags = undefined as any;
}
return tags;
}
}
7 changes: 4 additions & 3 deletions src/modules/vpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ export default class Vpc {
vId = res.VpcId;
}

if (tags) {
const formateTags = this.tagClient.formatInputTags(tags as any);
if (formateTags) {
try {
await this.tagClient.deployResourceTags({
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
tags: formateTags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
resourceId: vId,
serviceType: ApiServiceType.vpc,
resourcePrefix: 'vpc',
Expand Down Expand Up @@ -141,7 +142,7 @@ export default class Vpc {
}
}

const subnetTagList = subnetTags ? subnetTags : tags;
const subnetTagList = this.tagClient.formatInputTags((subnetTags ? subnetTags : tags) as any);
if (subnetTagList) {
try {
await this.tagClient.deployResourceTags({
Expand Down
27 changes: 0 additions & 27 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,30 +298,3 @@ export const getYunTiApiUrl = (): string => {
const url = `${apiUrl}?api_key=${apiKey}&api_ts=${timeStamp}&api_sign=${apiSign}`;
return url;
};

/**
* formatInputTags 格式化输入标签
*/
export const formatInputTags = (
input: Array<any> | { [key: string]: string },
): { key: string; value: string }[] => {
let tags: { key: string; value: string }[];
if (Array.isArray(input)) {
tags = input.map((item) => {
return {
key: item?.key ?? item?.Key ?? '',
value: item?.value ?? item?.Value ?? '',
};
});
} else if (typeof input === 'object' && input) {
tags = Object.entries(input).map(([key, value]) => {
return {
key: (key ?? '').toString(),
value: (value ?? '').toString(),
};
});
} else {
tags = undefined as any;
}
return tags;
};