-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathproxyConfigs.ts
More file actions
238 lines (222 loc) · 7 KB
/
Copy pathproxyConfigs.ts
File metadata and controls
238 lines (222 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*********************************************************************
* Copyright (c) Intel Corporation 2025
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
import { type ProxyConfig } from '../../../models/RCS.Config.js'
import { type IProxyConfigsTable } from '../../../interfaces/database/IProxyConfigsDB.js'
import {
API_UNEXPECTED_EXCEPTION,
CONCURRENCY_EXCEPTION,
CONCURRENCY_MESSAGE,
DEFAULT_SKIP,
DEFAULT_TOP,
NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT,
NETWORK_CONFIG_ERROR,
NETWORK_CONFIG_INSERTION_FAILED_DUPLICATE
} from '../../../utils/constants.js'
import { RPSError } from '../../../utils/RPSError.js'
import Logger from '../../../Logger.js'
import type PostgresDb from '../index.js'
import { PostgresErr } from '../errors.js'
export class ProxyConfigsTable implements IProxyConfigsTable {
db: PostgresDb
log: Logger
constructor(db: PostgresDb) {
this.db = db
this.log = new Logger('ProxyConfigsDb')
}
/**
* @description Get count of all proxies from DB
* @returns {number}
*/
async getCount(tenantId = ''): Promise<number> {
const result = await this.db.query<{ total_count: number }>(
`
SELECT count(*) OVER() AS total_count
FROM proxyconfigs
WHERE tenant_id = $1`,
[tenantId]
)
let count = 0
if (result?.rows?.length > 0) {
count = Number(result.rows[0].total_count)
}
return count
}
/**
* @description Get all proxies profiles from DB
* @param {number} top
* @param {number} skip
* @returns {ProxyConfig []} returns an array of ProxyConfig objects
*/
async get(top: number = DEFAULT_TOP, skip: number = DEFAULT_SKIP, tenantId = ''): Promise<ProxyConfig[]> {
const results = await this.db.query<ProxyConfig>(
`
SELECT
proxy_config_name as "proxyName",
address as "address",
info_format as "infoFormat",
port as "port",
network_dns_suffix as "networkDnsSuffix",
tenant_id as "tenantId"
FROM proxyconfigs
WHERE tenant_id = $3
ORDER BY proxy_config_name
LIMIT $1 OFFSET $2`,
[
top,
skip,
tenantId
]
)
return results.rows
}
/**
* @description Get proxy profile from DB by name
* @param {string} proxyName
* @returns {ProxyConfig} ProxyConfig object
*/
async getByName(proxyName: string, tenantId = ''): Promise<ProxyConfig | null> {
const results = await this.db.query<ProxyConfig>(
`
SELECT
proxy_config_name as "proxyName",
address as "address",
info_format as "infoFormat",
port as "port",
network_dns_suffix as "networkDnsSuffix",
tenant_id as "tenantId"
FROM proxyconfigs
WHERE proxy_config_name = $1 and tenant_id = $2`,
[proxyName, tenantId]
)
if ((results?.rowCount ?? 0) > 0) {
return results.rows[0]
}
return null
}
/**
* @description Check proxy profile exists in DB by name
* @param {string} proxyName
* @returns {string[]}
*/
async checkProfileExits(proxyName: string, tenantId = ''): Promise<boolean> {
const results = await this.db.query(
`
SELECT 1
FROM proxyconfigs
WHERE proxy_config_name = $1 and tenant_id = $2`,
[proxyName, tenantId]
)
if ((results?.rowCount ?? 0) > 0) {
return true
}
return false
}
/**
* @description Delete proxy profile from DB by name
* @param {string} proxyName
* @returns {boolean} Return true on successful deletion
*/
async delete(proxyName: string, tenantId = ''): Promise<boolean> {
const profiles = await this.db.query(
`
SELECT 1
FROM profiles_proxyconfigs
WHERE proxy_config_name = $1 and tenant_id = $2`,
[proxyName, tenantId]
)
if ((profiles?.rowCount ?? 0) > 0) {
throw new RPSError(NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', proxyName), 'Foreign key violation')
}
try {
const results = await this.db.query(
`
DELETE
FROM proxyconfigs
WHERE proxy_config_name = $1 and tenant_id = $2`,
[proxyName, tenantId]
)
if (results?.rowCount) {
return results.rowCount > 0
}
} catch (error) {
this.log.error(`Failed to delete proxy configuration : ${proxyName}`, error)
if (error.code === PostgresErr.C23_FOREIGN_KEY_VIOLATION) {
throw new RPSError(NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', proxyName))
}
throw new RPSError(API_UNEXPECTED_EXCEPTION(`Delete proxy configuration : ${proxyName}`))
}
return false
}
/**
* @description Insert proxy profile into DB
* @param {ProxyConfig} proxyConfig
* @returns {ProxyConfig} Returns ProxyConfig object
*/
async insert(proxyConfig: ProxyConfig): Promise<ProxyConfig | null> {
try {
const date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
const results = await this.db.query(
`
INSERT INTO proxyconfigs
(proxy_config_name, address, info_format, port, network_dns_suffix, creation_date, tenant_id)
values($1, $2, $3, $4, $5, $6, $7)`,
[
proxyConfig.proxyName,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
proxyConfig.networkDnsSuffix,
date,
proxyConfig.tenantId
]
)
if ((results?.rowCount ?? 0) > 0) {
const config = await this.getByName(proxyConfig.proxyName, proxyConfig.tenantId)
return config
}
} catch (error) {
if (error.code === PostgresErr.C23_UNIQUE_VIOLATION) {
throw new RPSError(
NETWORK_CONFIG_INSERTION_FAILED_DUPLICATE('Proxy', proxyConfig.proxyName),
'Unique key violation'
)
}
throw new RPSError(NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName))
}
return null
}
/**
* @description Update proxy profile into DB
* @param {ProxyConfig} proxyConfig
* @returns {boolean} Returns ProxyConfig object
*/
async update(proxyConfig: ProxyConfig): Promise<ProxyConfig | null> {
let latestItem: ProxyConfig | null
try {
const results = await this.db.query(
`
UPDATE proxyconfigs
SET address=$2, info_format=$3, port=$4, network_dns_suffix=$5
WHERE proxy_config_name=$1 and tenant_id = $6`,
[
proxyConfig.proxyName,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
proxyConfig.networkDnsSuffix,
proxyConfig.tenantId
]
)
latestItem = await this.getByName(proxyConfig.proxyName, proxyConfig.tenantId)
if ((results?.rowCount ?? 0) > 0) {
return latestItem
}
} catch (error) {
throw new RPSError(NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName))
}
// making assumption that if no records are updated, that it is due to concurrency. We've already checked for if it doesn't exist before calling update.
throw new RPSError(CONCURRENCY_MESSAGE, CONCURRENCY_EXCEPTION, latestItem)
}
}