|
| 1 | +const { cam } = require('tencent-cloud-sdk') |
| 2 | + |
| 3 | +class Cam { |
| 4 | + constructor(credentials = {}, region) { |
| 5 | + this.region = region || 'ap-guangzhou' |
| 6 | + this.credentials = credentials |
| 7 | + this.camClient = new cam(this.credentials) |
| 8 | + } |
| 9 | + |
| 10 | + async sleep(ms) { |
| 11 | + return new Promise((resolve) => { |
| 12 | + setTimeout(resolve, ms) |
| 13 | + }) |
| 14 | + } |
| 15 | + |
| 16 | + async DescribeRoleList(page, limit) { |
| 17 | + const reqParams = { |
| 18 | + Action: 'DescribeRoleList', |
| 19 | + Version: '2019-01-16', |
| 20 | + Page: page, |
| 21 | + Rp: limit |
| 22 | + } |
| 23 | + return await this.camClient.request(reqParams) |
| 24 | + } |
| 25 | + |
| 26 | + async ListRolePoliciesByRoleId(roleId, page, limit) { |
| 27 | + const reqParams = { |
| 28 | + Action: 'ListAttachedRolePolicies', |
| 29 | + Version: '2019-01-16', |
| 30 | + Page: page, |
| 31 | + Rp: limit, |
| 32 | + RoleId: roleId |
| 33 | + } |
| 34 | + return await this.camClient.request(reqParams) |
| 35 | + } |
| 36 | + |
| 37 | + async CreateRole(roleName, policiesDocument) { |
| 38 | + const reqParams = { |
| 39 | + Action: 'CreateRole', |
| 40 | + Version: '2019-01-16', |
| 41 | + RoleName: roleName, |
| 42 | + PolicyDocument: policiesDocument, |
| 43 | + Description: 'Serverless Framework' |
| 44 | + } |
| 45 | + return await this.camClient.request(reqParams) |
| 46 | + } |
| 47 | + |
| 48 | + // api limit qps 3/s |
| 49 | + async AttachRolePolicyByName(roleId, policyName) { |
| 50 | + const reqParams = { |
| 51 | + Action: 'AttachRolePolicy', |
| 52 | + Version: '2019-01-16', |
| 53 | + AttachRoleId: roleId, |
| 54 | + PolicyName: policyName |
| 55 | + } |
| 56 | + return await this.camClient.request(reqParams) |
| 57 | + } |
| 58 | + |
| 59 | + async CheckSCFExcuteRole() { |
| 60 | + const ScfExcuteRoleName = 'QCS_SCFExcuteRole' |
| 61 | + |
| 62 | + const roles = await this.DescribeRoleList(1, 200) |
| 63 | + if (roles.Response.Error) { |
| 64 | + throw new Error(roles.Response.Error.Message) |
| 65 | + } |
| 66 | + |
| 67 | + const len = roles.Response.List.length |
| 68 | + for (var i = 0; i < len; i++) { |
| 69 | + const roleItem = roles.Response.List[i] |
| 70 | + |
| 71 | + if (roleItem.RoleName == ScfExcuteRoleName) { |
| 72 | + return true |
| 73 | + } |
| 74 | + } |
| 75 | + return false |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +module.exports = Cam |
0 commit comments