Skip to content

Commit

Permalink
fix: multi-scf support yunti tag issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wangpand0508 committed Apr 26, 2024
1 parent b6328d7 commit 411d4a8
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 42 deletions.
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
29 changes: 28 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,31 @@ 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 {
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;
};

0 comments on commit 411d4a8

Please sign in to comment.