|
| 1 | +const ClsClient = require('@tencent-sdk/cls').Cls; |
| 2 | +const { ApiError } = require('../../utils/error'); |
| 3 | +const { createLogset, createTopic, updateIndex } = require('./utils'); |
| 4 | + |
| 5 | +class Cls { |
| 6 | + constructor(credentials = {}, region, expire) { |
| 7 | + this.region = region || 'ap-guangzhou'; |
| 8 | + this.credentials = credentials; |
| 9 | + this.cls = new ClsClient({ |
| 10 | + region: this.region, |
| 11 | + secretId: credentials.SecretId, |
| 12 | + secretKey: credentials.SecretKey, |
| 13 | + token: credentials.Token, |
| 14 | + debug: false, |
| 15 | + expire: expire || 300000, |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + async deployLogset(inputs) { |
| 20 | + const outputs = { |
| 21 | + region: this.region, |
| 22 | + name: inputs.name, |
| 23 | + logsetId: '', |
| 24 | + }; |
| 25 | + let exist = false; |
| 26 | + const { logsetId } = inputs; |
| 27 | + if (logsetId) { |
| 28 | + const detail = await this.cls.getLogset({ |
| 29 | + logset_id: logsetId, |
| 30 | + }); |
| 31 | + if (detail.error) { |
| 32 | + throw new ApiError({ |
| 33 | + type: 'API_getLogset', |
| 34 | + message: detail.error.message, |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // update it |
| 39 | + if (detail.logset_id) { |
| 40 | + exist = true; |
| 41 | + console.log(`Updating cls ${logsetId}`); |
| 42 | + const res = await this.cls.updateLogset({ |
| 43 | + logset_id: logsetId, |
| 44 | + logset_name: inputs.name, |
| 45 | + }); |
| 46 | + if (res.error) { |
| 47 | + throw new ApiError({ |
| 48 | + type: 'API_updateLogset', |
| 49 | + message: detail.error.message, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + console.log(`Update cls ${logsetId} success`); |
| 54 | + |
| 55 | + outputs.logsetId = logsetId; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // if not exist, create cls |
| 60 | + if (!exist) { |
| 61 | + const res = await createLogset(this.cls, { |
| 62 | + name: inputs.name, |
| 63 | + period: inputs.period, |
| 64 | + }); |
| 65 | + outputs.logsetId = res.logset_id; |
| 66 | + } |
| 67 | + |
| 68 | + return outputs; |
| 69 | + } |
| 70 | + |
| 71 | + async deployTopic(inputs) { |
| 72 | + const outputs = { |
| 73 | + region: this.region, |
| 74 | + name: inputs.topic, |
| 75 | + topicId: '', |
| 76 | + }; |
| 77 | + let exist = false; |
| 78 | + const { topicId } = inputs; |
| 79 | + if (topicId) { |
| 80 | + const detail = await this.cls.getTopic({ |
| 81 | + topic_id: topicId, |
| 82 | + }); |
| 83 | + if (detail.error) { |
| 84 | + throw new ApiError({ |
| 85 | + type: 'API_getTopic', |
| 86 | + message: detail.error.message, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + // update it |
| 91 | + if (detail.topic_id) { |
| 92 | + exist = true; |
| 93 | + console.log(`Updating cls topic ${topicId}`); |
| 94 | + const res = await this.cls.updateTopic({ |
| 95 | + topic_id: topicId, |
| 96 | + topic_name: inputs.topic, |
| 97 | + }); |
| 98 | + if (res.error) { |
| 99 | + throw new ApiError({ |
| 100 | + type: 'API_updateTopic', |
| 101 | + message: detail.error.message, |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + console.log(`Update cls topic ${topicId} success`); |
| 106 | + |
| 107 | + outputs.topicId = topicId; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // if not exist, create cls |
| 112 | + if (!exist) { |
| 113 | + const res = await createTopic(this.cls, { |
| 114 | + logsetId: inputs.logsetId, |
| 115 | + name: inputs.topic, |
| 116 | + }); |
| 117 | + outputs.topicId = res.topic_id; |
| 118 | + } |
| 119 | + |
| 120 | + return outputs; |
| 121 | + } |
| 122 | + |
| 123 | + async deployIndex(inputs) { |
| 124 | + await updateIndex(this.cls, { |
| 125 | + topicId: inputs.topicId, |
| 126 | + effective: inputs.effective !== false ? true : false, |
| 127 | + rule: inputs.rule, |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + async deploy(inputs = {}) { |
| 132 | + const outputs = { |
| 133 | + region: this.region, |
| 134 | + name: inputs.name, |
| 135 | + topic: inputs.topic, |
| 136 | + }; |
| 137 | + |
| 138 | + const logsetOutput = await this.deployLogset(inputs); |
| 139 | + outputs.logsetId = inputs.logsetId = logsetOutput.logsetId; |
| 140 | + const topicOutput = await this.deployTopic(inputs); |
| 141 | + outputs.topicId = inputs.topicId = topicOutput.topicId; |
| 142 | + await this.deployIndex(inputs); |
| 143 | + |
| 144 | + return outputs; |
| 145 | + } |
| 146 | + |
| 147 | + async remove(inputs = {}) { |
| 148 | + try { |
| 149 | + console.log(`Start removing cls`); |
| 150 | + console.log(`Removing cls topic id ${inputs.topicId}`); |
| 151 | + const res1 = await this.cls.deleteTopic({ |
| 152 | + topic_id: inputs.topicId, |
| 153 | + }); |
| 154 | + if (res1.error) { |
| 155 | + throw new ApiError({ |
| 156 | + type: 'API_deleteTopic', |
| 157 | + message: res1.error.message, |
| 158 | + }); |
| 159 | + } |
| 160 | + console.log(`Removed cls topic id ${inputs.logsetId} success`); |
| 161 | + console.log(`Removing cls id ${inputs.logsetId}`); |
| 162 | + const res2 = await this.cls.deleteLogset({ |
| 163 | + logset_id: inputs.logsetId, |
| 164 | + }); |
| 165 | + if (res2.error) { |
| 166 | + throw new ApiError({ |
| 167 | + type: 'API_deleteLogset', |
| 168 | + message: res2.error.message, |
| 169 | + }); |
| 170 | + } |
| 171 | + console.log(`Removed cls id ${inputs.logsetId} success`); |
| 172 | + } catch (e) { |
| 173 | + console.log(e); |
| 174 | + } |
| 175 | + |
| 176 | + return {}; |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +module.exports = Cls; |
0 commit comments