Skip to content

Commit 1969e63

Browse files
authored
feat: support tags for all resources (#218)
* feat: support tags for all resources * test: fix vpc test case
1 parent 9aae2c9 commit 1969e63

File tree

18 files changed

+241
-23
lines changed

18 files changed

+241
-23
lines changed

__tests__/apigw.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ describe('apigw', () => {
88
SecretId: process.env.TENCENT_SECRET_ID,
99
SecretKey: process.env.TENCENT_SECRET_KEY,
1010
};
11+
const tags = [
12+
{
13+
key: 'slstest',
14+
value: 'slstest',
15+
},
16+
];
1117
const inputs: ApigwDeployInputs = {
1218
protocols: ['http', 'https'],
1319
serviceName: 'serverless_test',
@@ -126,6 +132,7 @@ describe('apigw', () => {
126132
serviceMockReturnMessage: 'helloworld',
127133
},
128134
],
135+
tags,
129136
};
130137
const apigw = new Apigw(credentials, process.env.REGION);
131138
let outputs: ApigwDeployOutputs;
@@ -231,6 +238,7 @@ describe('apigw', () => {
231238
isBase64Encoded: false,
232239
},
233240
],
241+
tags,
234242
});
235243
});
236244

@@ -349,6 +357,7 @@ describe('apigw', () => {
349357
isBase64Encoded: false,
350358
},
351359
],
360+
tags,
352361
});
353362
});
354363

@@ -458,6 +467,7 @@ describe('apigw', () => {
458467
isBase64Encoded: false,
459468
},
460469
],
470+
tags,
461471
});
462472
});
463473

__tests__/cdn.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ describe('Cdn', () => {
88
SecretId: process.env.TENCENT_SECRET_ID,
99
SecretKey: process.env.TENCENT_SECRET_KEY,
1010
};
11+
const tags = [
12+
{
13+
key: 'slstest',
14+
value: 'slstest',
15+
},
16+
];
1117
const inputs: CdnDeployInputs = {
1218
async: false,
1319
area: 'overseas',
@@ -32,6 +38,7 @@ describe('Cdn', () => {
3238
redirectType: 'https',
3339
redirectStatusCode: 301,
3440
},
41+
tags,
3542
};
3643
const cdn = new Cdn(credentials);
3744

@@ -45,6 +52,7 @@ describe('Cdn', () => {
4552
cname: `${inputs.domain}.cdn.dnsv1.com`,
4653
inputCache: JSON.stringify(inputs),
4754
resourceId: expect.stringContaining('cdn-'),
55+
tags,
4856
});
4957
});
5058

@@ -59,6 +67,7 @@ describe('Cdn', () => {
5967
cname: `${inputs.domain}.cdn.dnsv1.com`,
6068
inputCache: JSON.stringify(inputs),
6169
resourceId: expect.stringContaining('cdn-'),
70+
tags,
6271
});
6372
});
6473

__tests__/cynosdb.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ describe('Cynosdb', () => {
1616
const region = 'ap-guangzhou';
1717
const client = new Cynosdb(credentials, region);
1818

19+
const tags = [
20+
{
21+
key: 'slstest',
22+
value: 'slstest',
23+
},
24+
];
25+
1926
const inputs: CynosdbDeployInputs = {
2027
region,
2128
zone: 'ap-guangzhou-4',
2229
vpcConfig: {
2330
vpcId: 'vpc-p2dlmlbj',
2431
subnetId: 'subnet-a1v3k07o',
2532
},
33+
tags,
2634
};
2735

2836
let clusterId;
@@ -121,6 +129,7 @@ describe('Cynosdb', () => {
121129
status: 'running',
122130
},
123131
],
132+
tags,
124133
});
125134

126135
expect(isValidPwd(res.adminPassword)).toBe(true);
@@ -160,6 +169,7 @@ describe('Cynosdb', () => {
160169
status: 'running',
161170
},
162171
],
172+
tags,
163173
});
164174
});
165175

@@ -190,6 +200,7 @@ describe('Cynosdb', () => {
190200
status: 'running',
191201
},
192202
],
203+
tags,
193204
});
194205
inputs.clusterId = undefined;
195206
});

__tests__/pg.test.ts renamed to __tests__/postgres.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PostgresqlDeployInputs } from './../src/modules/postgresql/interface';
1+
import { PostgresqlDeployInputs } from '../src/modules/postgresql/interface';
22
import { Postgresql } from '../src';
33
import { getDbInstanceDetail } from '../src/modules/postgresql/utils';
44
import { sleep } from '@ygkit/request';
@@ -10,6 +10,12 @@ describe('Postgresql', () => {
1010
};
1111
const pg = new Postgresql(credentials, process.env.REGION);
1212

13+
const tags = [
14+
{
15+
key: 'slstest',
16+
value: 'slstest',
17+
},
18+
];
1319
const inputs: PostgresqlDeployInputs = {
1420
region: process.env.REGION,
1521
zone: process.env.ZONE,
@@ -22,6 +28,7 @@ describe('Postgresql', () => {
2228
subnetId: process.env.SUBNET_ID,
2329
},
2430
extranetAccess: false,
31+
tags,
2532
};
2633

2734
test('should deploy postgresql success', async () => {
@@ -40,6 +47,7 @@ describe('Postgresql', () => {
4047
password: expect.any(String),
4148
dbname: expect.stringContaining('tencentdb_'),
4249
},
50+
tags,
4351
});
4452
inputs.dBInstanceId = res.dBInstanceId;
4553
});
@@ -68,6 +76,7 @@ describe('Postgresql', () => {
6876
password: expect.any(String),
6977
dbname: expect.stringContaining('tencentdb_'),
7078
},
79+
tags,
7180
});
7281
});
7382
test('should remove postgresql success', async () => {

__tests__/tag.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('Tag', () => {
88
SecretKey: process.env.TENCENT_SECRET_KEY,
99
};
1010
const functionName = 'serverless-unit-test';
11-
const tagItem = { TagKey: 'slstest', TagValue: 'slstest' };
11+
const tagItem = { TagKey: 'slstest1', TagValue: 'slstest1' };
1212
const commonInputs: TagDeployInputs = {
1313
resourceIds: [`default/function/${functionName}`],
1414
resourcePrefix: 'namespace',

__tests__/vpc.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ describe('Vpc', () => {
77
SecretId: process.env.TENCENT_SECRET_ID,
88
SecretKey: process.env.TENCENT_SECRET_KEY,
99
};
10+
const tags = [
11+
{
12+
key: 'slstest',
13+
value: 'slstest',
14+
},
15+
];
1016
const inputs: VpcDeployInputs = {
1117
region: process.env.REGION,
1218
zone: process.env.ZONE,
1319
vpcName: 'serverless-test',
1420
subnetName: 'serverless-test',
1521
cidrBlock: '10.0.0.0/16',
22+
tags,
1623
};
1724
const vpc = new Vpc(credentials, process.env.REGION);
1825

@@ -56,6 +63,7 @@ describe('Vpc', () => {
5663
vpcName: 'serverless-test',
5764
subnetId: expect.stringContaining('subnet-'),
5865
subnetName: 'serverless-test',
66+
tags,
5967
});
6068

6169
inputs.vpcId = res.vpcId;

src/modules/apigw/entities/service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TagInput } from './../../interface';
12
import { Capi } from '@tencent-sdk/capi';
23
import {
34
ApigwCreateServiceInputs,
@@ -20,6 +21,7 @@ interface Detail {
2021
ServiceName: string;
2122
ServiceDesc: string;
2223
Protocol: string;
24+
Tags: TagInput[];
2325
}
2426

2527
export default class ServiceEntity {

src/modules/apigw/index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import ApiEntity from './entities/api';
2121
import UsagePlanEntity from './entities/usage-plan';
2222
import CustomDomainEntity from './entities/custom-domain';
2323
import { ApiError } from '../../utils/error';
24+
import TagClient from '../tag';
2425

2526
export default class Apigw {
2627
credentials: CapiCredentials;
@@ -31,6 +32,7 @@ export default class Apigw {
3132
api: ApiEntity;
3233
customDomain: CustomDomainEntity;
3334
usagePlan: UsagePlanEntity;
35+
tagClient: TagClient;
3436

3537
constructor(credentials: CapiCredentials, region: RegionType = 'ap-guangzhou') {
3638
this.credentials = credentials;
@@ -48,6 +50,8 @@ export default class Apigw {
4850
this.api = new ApiEntity(this.capi, this.trigger);
4951
this.usagePlan = new UsagePlanEntity(this.capi);
5052
this.customDomain = new CustomDomainEntity(this.capi);
53+
54+
this.tagClient = new TagClient(this.credentials, this.region);
5155
}
5256

5357
async request({ Action, ...data }: { Action: ActionType; [key: string]: any }) {
@@ -119,6 +123,21 @@ export default class Apigw {
119123
outputs.usagePlan = usagePlan;
120124
}
121125

126+
const { tags = [] } = inputs;
127+
if (tags.length > 0) {
128+
const deployedTags = await this.tagClient.deployResourceTags({
129+
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
130+
resourceId: serviceId,
131+
serviceType: ApiServiceType.apigw,
132+
resourcePrefix: 'service',
133+
});
134+
135+
outputs.tags = deployedTags.map((item) => ({
136+
key: item.TagKey,
137+
value: item.TagValue!,
138+
}));
139+
}
140+
122141
return outputs;
123142
}
124143

@@ -188,6 +207,16 @@ export default class Apigw {
188207
});
189208
console.log(`Unrelease service ${serviceId}, environment ${environment} success`);
190209

210+
// 在删除之前,如果关联了标签,需要先删除标签关联
211+
if (detail.Tags && detail.Tags.length > 0) {
212+
await this.tagClient.deployResourceTags({
213+
tags: [],
214+
resourceId: serviceId,
215+
serviceType: ApiServiceType.apigw,
216+
resourcePrefix: 'service',
217+
});
218+
}
219+
191220
// delete service
192221
console.log(`Removing service ${serviceId}`);
193222
await this.removeRequest({
@@ -233,6 +262,21 @@ export default class Apigw {
233262
apiList,
234263
};
235264

265+
const { tags = [] } = inputs;
266+
if (tags.length > 0) {
267+
const deployedTags = await this.tagClient.deployResourceTags({
268+
tags: tags.map(({ key, value }) => ({ TagKey: key, TagValue: value })),
269+
resourceId: serviceId,
270+
serviceType: ApiServiceType.apigw,
271+
resourcePrefix: 'service',
272+
});
273+
274+
outputs.tags = deployedTags.map((item) => ({
275+
key: item.TagKey,
276+
value: item.TagValue!,
277+
}));
278+
}
279+
236280
return outputs;
237281
}
238282
throw new ApiError({

src/modules/apigw/interface.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RegionType } from '../interface';
1+
import { RegionType, TagInput } from '../interface';
22

33
export interface Secret {
44
AccessKeyId: string;
@@ -113,6 +113,8 @@ export interface ApigwCreateServiceInputs {
113113

114114
usagePlan?: ApigwSetupUsagePlanInputs;
115115
auth?: ApigwSetupUsagePlanSecretInputs;
116+
117+
tags?: TagInput[];
116118
}
117119
export interface ApigwUpdateServiceInputs {
118120
environment?: EnviromentType;
@@ -200,6 +202,8 @@ export interface ApigwDeployOutputs {
200202
apiList: ApiEndpoint[];
201203
customDomains?: ApigwBindCustomDomainOutputs[];
202204
usagePlan?: ApigwUsagePlanOutputs;
205+
206+
tags?: TagInput[];
203207
}
204208

205209
export interface ApigwRemoveOrUnbindUsagePlanInputs {

0 commit comments

Comments
 (0)