|
| 1 | +const { Capi } = require('@tencent-sdk/capi'); |
| 2 | +const { |
| 3 | + createCluster, |
| 4 | + getClusterDetail, |
| 5 | + deleteCluster, |
| 6 | + generatePwd, |
| 7 | + formatConnectOutput, |
| 8 | + resetPwd, |
| 9 | +} = require('./utils'); |
| 10 | +const { ApiError } = require('../../utils/error'); |
| 11 | + |
| 12 | +class Cynosdb { |
| 13 | + constructor(credentials = {}, region) { |
| 14 | + this.region = region || 'ap-guangzhou'; |
| 15 | + this.credentials = credentials; |
| 16 | + this.capi = new Capi({ |
| 17 | + Region: this.region, |
| 18 | + AppId: this.credentials.AppId, |
| 19 | + SecretId: this.credentials.SecretId, |
| 20 | + SecretKey: this.credentials.SecretKey, |
| 21 | + Token: this.credentials.Token, |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + async deploy(inputs = {}) { |
| 26 | + const { |
| 27 | + clusterId, |
| 28 | + region, |
| 29 | + zone, |
| 30 | + vpcConfig, |
| 31 | + projectId = 0, |
| 32 | + dbVersion = '5.7', |
| 33 | + dbType = 'MYSQL', |
| 34 | + port = 3306, |
| 35 | + cpu = 1, |
| 36 | + memory = 1, |
| 37 | + storageLimit = 1000, |
| 38 | + instanceCount = 2, |
| 39 | + adminPassword, |
| 40 | + } = inputs; |
| 41 | + |
| 42 | + const outputs = { |
| 43 | + region: region, |
| 44 | + zone: zone, |
| 45 | + vpcConfig: vpcConfig, |
| 46 | + instanceCount, |
| 47 | + }; |
| 48 | + |
| 49 | + let isExisted = false; |
| 50 | + let clusterDetail = null; |
| 51 | + if (clusterId) { |
| 52 | + clusterDetail = await getClusterDetail(this.capi, clusterId); |
| 53 | + if (clusterDetail && clusterDetail.ClusterId) { |
| 54 | + isExisted = true; |
| 55 | + outputs.clusterId = clusterDetail.ClusterId; |
| 56 | + if (adminPassword) { |
| 57 | + outputs.adminPassword = adminPassword; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + if (!isExisted) { |
| 62 | + // not exist, create |
| 63 | + const dbInputs = { |
| 64 | + Zone: zone, |
| 65 | + ProjectId: projectId, |
| 66 | + DbType: dbType, |
| 67 | + DbVersion: dbVersion, |
| 68 | + Port: port, |
| 69 | + Cpu: cpu, |
| 70 | + Memory: memory, |
| 71 | + StorageLimit: storageLimit, |
| 72 | + InstanceCount: instanceCount, |
| 73 | + PayMode: 0, |
| 74 | + AutoVoucher: 1, |
| 75 | + RollbackStrategy: 'noneRollback', |
| 76 | + OrderSource: 'serverless', |
| 77 | + VpcId: vpcConfig.vpcId, |
| 78 | + SubnetId: vpcConfig.subnetId, |
| 79 | + AdminPassword: adminPassword, |
| 80 | + VpcId: vpcConfig.vpcId, |
| 81 | + SubnetId: vpcConfig.subnetId, |
| 82 | + AdminPassword: adminPassword || generatePwd(), |
| 83 | + }; |
| 84 | + |
| 85 | + clusterDetail = await createCluster(this.capi, dbInputs); |
| 86 | + outputs.clusterId = clusterDetail.ClusterId; |
| 87 | + |
| 88 | + outputs.adminPassword = dbInputs.AdminPassword; |
| 89 | + } |
| 90 | + |
| 91 | + outputs.connection = formatConnectOutput(clusterDetail); |
| 92 | + |
| 93 | + return outputs; |
| 94 | + } |
| 95 | + |
| 96 | + async remove(inputs = {}) { |
| 97 | + const { clusterId } = inputs; |
| 98 | + |
| 99 | + const clusterDetail = await getClusterDetail(this.capi, clusterId); |
| 100 | + if (clusterDetail && clusterDetail.ClusterId) { |
| 101 | + // need circle for deleting, after host status is 6, then we can delete it |
| 102 | + await deleteCluster(this.capi, clusterId); |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + async resetPwd(inputs = {}) { |
| 108 | + const { clusterId } = inputs; |
| 109 | + |
| 110 | + const clusterDetail = await getClusterDetail(this.capi, clusterId); |
| 111 | + if (clusterDetail && clusterDetail.ClusterId) { |
| 112 | + // need circle for deleting, after host status is 6, then we can delete it |
| 113 | + await resetPwd(this.capi, inputs); |
| 114 | + } else { |
| 115 | + throw ApiError({ |
| 116 | + type: 'PARAMETER_CYNOSDB', |
| 117 | + message: `CynosDB cluster id: ${clusterId} not exist.`, |
| 118 | + }); |
| 119 | + } |
| 120 | + return true; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = Cynosdb; |
0 commit comments