Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"ci-prettify": "npx prettier --check '**/*.{ts,js,yaml,css,scss,json}'"
},
"dependencies": {
"@device-management-toolkit/wsman-messages": "^5.12.0",
"@device-management-toolkit/wsman-messages": "^5.13.0",
"body-parser": "^2.2.0",
"consul": "^2.0.1",
"cors": "^2.8.5",
Expand Down Expand Up @@ -93,7 +93,7 @@
"prettier-plugin-multiline-arrays": "^4.0.3",
"sinon": "^21.0.0",
"ts-jest": "^29.4.1",
"ts-jest-mock-import-meta": "^1.3.1",
"ts-jest-mock-import-meta": "^1.3.0",
"ts-node": "^10.9.2",
"typescript": "^5.8.3",
"typescript-eslint": "8.45.0"
Expand Down
4 changes: 2 additions & 2 deletions src/data/postgres/tables/profileProxyConfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('profileproxyconfig tests', () => {
let profilesProxyConfigsTable: ProfileProxyConfigsTable
let querySpy: SpyInstance<any>
const proxyConfigs: ProfileProxyConfigs[] = [
{ profileName: 'proxyConfig', priority: 1 } as any
{ name: 'proxyConfig', priority: 1 } as any
]
const profileName = 'profileName'
const tenantId = 'tenantId'
Expand All @@ -38,7 +38,7 @@ describe('profileproxyconfig tests', () => {
`
SELECT
priority as "priority",
proxy_config_name as "profileName"
proxy_config_name as "name"
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2
ORDER BY priority`,
Expand Down
4 changes: 2 additions & 2 deletions src/data/postgres/tables/profileProxyConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
`
SELECT
priority as "priority",
proxy_config_name as "profileName"
proxy_config_name as "name"
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2
ORDER BY priority`,
Expand All @@ -53,7 +53,7 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
}
// Preparing data for inserting multiple rows
const configs = proxyConfigs.map((config) => [
config.profileName,
config.name,
profileName,
config.priority,
tenantId
Expand Down
4 changes: 2 additions & 2 deletions src/data/postgres/tables/profiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('profiles tests', () => {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('name',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('profiles tests', () => {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('name',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down
4 changes: 2 additions & 2 deletions src/data/postgres/tables/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class ProfilesTable implements IProfilesTable {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('name',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down Expand Up @@ -143,7 +143,7 @@ export class ProfilesTable implements IProfilesTable {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('name',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down
26 changes: 13 additions & 13 deletions src/data/postgres/tables/proxyConfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('proxy configs tests', () => {
let proxyConfigsTable: ProxyConfigsTable
let querySpy: SpyInstance<any>
let proxyConfig: ProxyConfig
const profileName = 'proxyName'
const profileName = 'name'
beforeEach(() => {
proxyConfig = {
proxyName: 'proxy profile',
name: 'proxy profile',
address: 'proxy.com',
infoFormat: 201, // FQDN (201)
port: 1000,
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('proxy configs tests', () => {
expect(querySpy).toBeCalledWith(
`
SELECT
proxy_config_name as "proxyName",
proxy_config_name as "name",
address as "address",
info_format as "infoFormat",
port as "port",
Expand All @@ -123,7 +123,7 @@ describe('proxy configs tests', () => {
expect(querySpy).toBeCalledWith(
`
SELECT
proxy_config_name as "proxyName",
proxy_config_name as "name",
address as "address",
info_format as "infoFormat",
port as "port",
Expand Down Expand Up @@ -226,15 +226,15 @@ describe('proxy configs tests', () => {
const result = await proxyConfigsTable.insert(proxyConfig)

expect(result).toBe(proxyConfig)
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.proxyName, proxyConfig.tenantId)
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.name, proxyConfig.tenantId)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
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.name,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
Expand All @@ -256,13 +256,13 @@ describe('proxy configs tests', () => {
test('should NOT insert when duplicate name', async () => {
querySpy.mockRejectedValueOnce({ code: '23505' })
await expect(proxyConfigsTable.insert(proxyConfig)).rejects.toThrow(
NETWORK_CONFIG_INSERTION_FAILED_DUPLICATE('Proxy', proxyConfig.proxyName)
NETWORK_CONFIG_INSERTION_FAILED_DUPLICATE('Proxy', proxyConfig.name)
)
})
test('should NOT insert when unexpected exception', async () => {
querySpy.mockRejectedValueOnce(new Error('unknown'))
await expect(proxyConfigsTable.insert(proxyConfig)).rejects.toThrow(
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName)
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.name)
)
})
})
Expand All @@ -273,15 +273,15 @@ describe('proxy configs tests', () => {
getByNameSpy.mockResolvedValue(proxyConfig)
const result = await proxyConfigsTable.update(proxyConfig)
expect(result).toBe(proxyConfig)
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.proxyName, proxyConfig.tenantId)
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.name, proxyConfig.tenantId)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
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.name,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
Expand All @@ -307,7 +307,7 @@ describe('proxy configs tests', () => {
const getByNameSpy = spyOn(proxyConfigsTable, 'getByName')
getByNameSpy.mockResolvedValue(proxyConfig)
await expect(proxyConfigsTable.update(proxyConfig)).rejects.toThrow(
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName)
NETWORK_CONFIG_ERROR('Proxy', proxyConfig.name)
)
})

Expand All @@ -316,15 +316,15 @@ describe('proxy configs tests', () => {
const getByNameSpy = spyOn(proxyConfigsTable, 'getByName')
getByNameSpy.mockResolvedValue(proxyConfig)
await expect(proxyConfigsTable.update(proxyConfig)).rejects.toThrow(CONCURRENCY_MESSAGE)
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.proxyName, proxyConfig.tenantId)
expect(getByNameSpy).toHaveBeenCalledWith(proxyConfig.name, proxyConfig.tenantId)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
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.name,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
Expand Down
49 changes: 23 additions & 26 deletions src/data/postgres/tables/proxyConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ProxyConfigsTable implements IProxyConfigsTable {
const results = await this.db.query<ProxyConfig>(
`
SELECT
proxy_config_name as "proxyName",
proxy_config_name as "name",
address as "address",
info_format as "infoFormat",
port as "port",
Expand All @@ -78,22 +78,22 @@ export class ProxyConfigsTable implements IProxyConfigsTable {

/**
* @description Get proxy profile from DB by name
* @param {string} proxyName
* @param {string} name
* @returns {ProxyConfig} ProxyConfig object
*/
async getByName(proxyName: string, tenantId = ''): Promise<ProxyConfig | null> {
async getByName(name: string, tenantId = ''): Promise<ProxyConfig | null> {
const results = await this.db.query<ProxyConfig>(
`
SELECT
proxy_config_name as "proxyName",
proxy_config_name as "name",
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]
[name, tenantId]
)

if ((results?.rowCount ?? 0) > 0) {
Expand All @@ -105,16 +105,16 @@ export class ProxyConfigsTable implements IProxyConfigsTable {

/**
* @description Check proxy profile exists in DB by name
* @param {string} proxyName
* @param {string} name
* @returns {string[]}
*/
async checkProfileExits(proxyName: string, tenantId = ''): Promise<boolean> {
async checkProfileExits(name: 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]
[name, tenantId]
)

if ((results?.rowCount ?? 0) > 0) {
Expand All @@ -126,37 +126,37 @@ export class ProxyConfigsTable implements IProxyConfigsTable {

/**
* @description Delete proxy profile from DB by name
* @param {string} proxyName
* @param {string} name
* @returns {boolean} Return true on successful deletion
*/
async delete(proxyName: string, tenantId = ''): Promise<boolean> {
async delete(name: 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]
[name, tenantId]
)
if ((profiles?.rowCount ?? 0) > 0) {
throw new RPSError(NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', proxyName), 'Foreign key violation')
throw new RPSError(NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', name), 'Foreign key violation')
}
try {
const results = await this.db.query(
`
DELETE
FROM proxyconfigs
WHERE proxy_config_name = $1 and tenant_id = $2`,
[proxyName, tenantId]
[name, tenantId]
)
if (results?.rowCount) {
return results.rowCount > 0
}
} catch (error) {
this.log.error(`Failed to delete proxy configuration : ${proxyName}`, error)
this.log.error(`Failed to delete proxy configuration : ${name}`, error)
if (error.code === PostgresErr.C23_FOREIGN_KEY_VIOLATION) {
throw new RPSError(NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', proxyName))
throw new RPSError(NETWORK_CONFIG_DELETION_FAILED_CONSTRAINT('Proxy', name))
}
throw new RPSError(API_UNEXPECTED_EXCEPTION(`Delete proxy configuration : ${proxyName}`))
throw new RPSError(API_UNEXPECTED_EXCEPTION(`Delete proxy configuration : ${name}`))
}
return false
}
Expand All @@ -175,7 +175,7 @@ export class ProxyConfigsTable implements IProxyConfigsTable {
(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.name,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
Expand All @@ -186,17 +186,14 @@ export class ProxyConfigsTable implements IProxyConfigsTable {
)

if ((results?.rowCount ?? 0) > 0) {
const config = await this.getByName(proxyConfig.proxyName, proxyConfig.tenantId)
const config = await this.getByName(proxyConfig.name, 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_INSERTION_FAILED_DUPLICATE('Proxy', proxyConfig.name), 'Unique key violation')
}
throw new RPSError(NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName))
throw new RPSError(NETWORK_CONFIG_ERROR('Proxy', proxyConfig.name))
}
return null
}
Expand All @@ -216,7 +213,7 @@ export class ProxyConfigsTable implements IProxyConfigsTable {
SET address=$2, info_format=$3, port=$4, network_dns_suffix=$5
WHERE proxy_config_name=$1 and tenant_id = $6`,
[
proxyConfig.proxyName,
proxyConfig.name,
proxyConfig.address,
proxyConfig.infoFormat,
proxyConfig.port,
Expand All @@ -225,12 +222,12 @@ export class ProxyConfigsTable implements IProxyConfigsTable {
]
)

latestItem = await this.getByName(proxyConfig.proxyName, proxyConfig.tenantId)
latestItem = await this.getByName(proxyConfig.name, proxyConfig.tenantId)
if ((results?.rowCount ?? 0) > 0) {
return latestItem
}
} catch (error) {
throw new RPSError(NETWORK_CONFIG_ERROR('Proxy', proxyConfig.proxyName))
throw new RPSError(NETWORK_CONFIG_ERROR('Proxy', proxyConfig.name))
}
// 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)
Expand Down
Loading