Skip to content

Commit cfef151

Browse files
committed
feat: add asw and account module
1 parent 400fa5f commit cfef151

File tree

14 files changed

+602
-3
lines changed

14 files changed

+602
-3
lines changed

__tests__/account.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Account } from '../src';
2+
3+
describe('Account', () => {
4+
const credentials = {
5+
SecretId: process.env.TENCENT_SECRET_ID,
6+
SecretKey: process.env.TENCENT_SECRET_KEY,
7+
};
8+
const account = new Account(credentials);
9+
10+
test('get', async () => {
11+
const res = await account.get();
12+
expect(res).toEqual({
13+
ownerUin: +process.env.TENCENT_UIN,
14+
uin: +process.env.TENCENT_UIN,
15+
appId: +process.env.TENCENT_APP_ID,
16+
account: expect.any(String),
17+
userType: expect.any(String),
18+
type: expect.any(String),
19+
area: expect.any(String),
20+
tel: expect.any(String),
21+
});
22+
});
23+
});

__tests__/asw.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { sleep } from '@ygkit/request';
2+
import Asw from '../src/modules/asw';
3+
import { UpdateOptions, CreateResult } from './../src/modules/asw/interface';
4+
5+
describe('Account', () => {
6+
const credentials = {
7+
SecretId: process.env.TENCENT_SECRET_ID,
8+
SecretKey: process.env.TENCENT_SECRET_KEY,
9+
};
10+
const client = new Asw(credentials);
11+
12+
const options: {
13+
definition: string;
14+
name: string;
15+
resourceId?: string;
16+
role?: string;
17+
} = {
18+
definition: JSON.stringify({
19+
Comment: 'Serverless Test',
20+
StartAt: 'Hello',
21+
States: {
22+
Hello: { Type: 'Pass', Comment: '传递', Next: 'World' },
23+
World: { Type: 'Pass', Comment: '传递', End: true },
24+
},
25+
}),
26+
name: 'serverless-test',
27+
};
28+
29+
const input = JSON.stringify({
30+
key1: 'test value 1',
31+
key2: 'test value 2',
32+
});
33+
34+
let executeName: string;
35+
let createResult: CreateResult;
36+
37+
test('create', async () => {
38+
const res = await client.create(options);
39+
expect(res).toEqual({
40+
requestId: expect.any(String),
41+
resourceId: expect.any(String),
42+
isNewRole: expect.any(Boolean),
43+
roleName: expect.stringContaining('serverless-test_'),
44+
});
45+
createResult = res;
46+
});
47+
48+
test('update', async () => {
49+
options.resourceId = createResult.resourceId;
50+
options.role = createResult.roleName;
51+
const res = await client.update(options as UpdateOptions);
52+
expect(res).toEqual({
53+
requestId: expect.any(String),
54+
resourceId: createResult.resourceId,
55+
isNewRole: expect.any(Boolean),
56+
roleName: expect.stringContaining('serverless-test_'),
57+
});
58+
});
59+
60+
test('execute', async () => {
61+
const res = await client.execute({
62+
resourceId: createResult.resourceId,
63+
name: 'serverless',
64+
input,
65+
});
66+
67+
expect(res).toEqual({
68+
requestId: expect.any(String),
69+
resourceId: createResult.resourceId,
70+
executeName: expect.stringContaining('qrn:qcs:asw:'),
71+
});
72+
73+
({ executeName } = res);
74+
});
75+
76+
test('getExecuteState', async () => {
77+
// 等待执行完成
78+
await sleep(5000);
79+
const res = await client.getExecuteState(executeName);
80+
81+
expect(res.ExecutionResourceName).toBe(executeName);
82+
expect(res.Name).toBe('serverless');
83+
expect(res.Input).toBe(input);
84+
});
85+
86+
test('delete', async () => {
87+
const res = await client.delete(createResult.resourceId);
88+
expect(res).toEqual({
89+
requestId: expect.any(String),
90+
resourceId: createResult.resourceId,
91+
});
92+
93+
// 删除测试创建的角色
94+
if (createResult.isNewRole) {
95+
await client.cam.DeleteRole(createResult.roleName);
96+
}
97+
});
98+
});

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ export { default as Cynosdb } from './modules/cynosdb';
1515
export { default as Cls } from './modules/cls';
1616
export { default as Clb } from './modules/clb';
1717
export { default as Monitor } from './modules/monitor';
18+
export { default as Account } from './modules/account';
19+
export { default as Asw } from './modules/asw';

src/modules/account/apis.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ApiFactory } from '../../utils/api';
2+
import { ApiServiceType } from '../interface';
3+
4+
const ACTIONS = ['DescribeCurrentUserDetails'] as const;
5+
6+
export type ActionType = typeof ACTIONS[number];
7+
8+
const APIS = ApiFactory({
9+
// debug: true,
10+
serviceType: ApiServiceType.account,
11+
version: '2018-12-25',
12+
actions: ACTIONS,
13+
});
14+
15+
export default APIS;

src/modules/account/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ActionType } from './apis';
2+
import { CapiCredentials, RegionType, ApiServiceType } from '../interface';
3+
import { Capi } from '@tencent-sdk/capi';
4+
import APIS from './apis';
5+
import { AccountDetail } from './interface';
6+
7+
/** CAM (访问管理)for serverless */
8+
export default class Cam {
9+
region: RegionType;
10+
credentials: CapiCredentials;
11+
capi: Capi;
12+
13+
constructor(credentials: CapiCredentials, region: RegionType = 'ap-guangzhou') {
14+
this.region = region;
15+
this.credentials = credentials;
16+
17+
this.capi = new Capi({
18+
Region: this.region,
19+
ServiceType: ApiServiceType.cam,
20+
SecretId: this.credentials.SecretId!,
21+
SecretKey: this.credentials.SecretKey!,
22+
Token: this.credentials.Token,
23+
});
24+
}
25+
26+
async request({ Action, ...data }: { Action: ActionType; [key: string]: any }) {
27+
const result = await APIS[Action](this.capi, data);
28+
return result;
29+
}
30+
31+
/**
32+
* 获取账号详情
33+
* @returns 账号详情
34+
*/
35+
async get(): Promise<AccountDetail> {
36+
const res = await this.request({
37+
Action: 'DescribeCurrentUserDetails',
38+
});
39+
40+
return {
41+
ownerUin: res.OwnerUin,
42+
uin: res.Uin,
43+
appId: res.AppId![0] || '',
44+
account: res.Account,
45+
userType: res.UserType,
46+
type: res.Type,
47+
area: res.Area,
48+
tel: res.Tel,
49+
};
50+
}
51+
}

src/modules/account/interface.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface AccountDetail {
2+
ownerUin: string;
3+
uin: string;
4+
appId: string;
5+
account: string;
6+
userType: string;
7+
type: string;
8+
area: string;
9+
tel: string;
10+
}

src/modules/asw/apis.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ApiFactory } from '../../utils/api';
2+
import { ApiServiceType } from '../interface';
3+
4+
const ACTIONS = [
5+
'DescribeFlowServices',
6+
'DescribeFlowServiceDetail',
7+
'CreateFlowService',
8+
'ModifyFlowService',
9+
'DeleteFlowService',
10+
'StartExecution',
11+
'DescribeExecution',
12+
'StopExecution',
13+
] as const;
14+
15+
export type ActionType = typeof ACTIONS[number];
16+
17+
const APIS = ApiFactory({
18+
debug: false,
19+
isV3: true,
20+
serviceType: ApiServiceType.asw,
21+
version: '2020-07-22',
22+
actions: ACTIONS,
23+
});
24+
25+
export default APIS;

src/modules/asw/constants.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)