-
Notifications
You must be signed in to change notification settings - Fork 17
refactor: add http get, update and delete endpoints #2306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
madhavilosetty-intel
merged 1 commit into
device-management-toolkit:main
from
sfmadrig:get-delete-update-http-proxy
Sep 30, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /********************************************************************* | ||
| * Copyright (c) Intel Corporation 2025 | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| **********************************************************************/ | ||
|
|
||
| import { createSpyObj } from '../../../test/helper/jest.js' | ||
| import { deleteProxyProfile } from './delete.js' | ||
| import { jest } from '@jest/globals' | ||
| import { type SpyInstance, spyOn } from 'jest-mock' | ||
|
|
||
| describe('Proxy - Delete', () => { | ||
| let resSpy | ||
| let req | ||
| let deleteSpy: SpyInstance<any> | ||
| let checkSpy: SpyInstance<any> | ||
|
|
||
| beforeEach(() => { | ||
| resSpy = createSpyObj('Response', [ | ||
| 'status', | ||
| 'json', | ||
| 'end', | ||
| 'send' | ||
| ]) | ||
| req = { | ||
| db: { proxyConfigs: { delete: jest.fn(), checkProfileExits: jest.fn() } }, | ||
| query: {}, | ||
| params: { proxyName: 'proxyConfigName' }, | ||
| tenantId: '', | ||
| method: 'DELETE' | ||
| } | ||
| deleteSpy = spyOn(req.db.proxyConfigs, 'delete').mockResolvedValue({}) | ||
| checkSpy = spyOn(req.db.proxyConfigs, 'checkProfileExits').mockResolvedValue(true) | ||
|
|
||
| resSpy.status.mockReturnThis() | ||
| resSpy.json.mockReturnThis() | ||
| resSpy.send.mockReturnThis() | ||
| }) | ||
| it('should delete', async () => { | ||
| checkSpy = spyOn(req.db.proxyConfigs, 'checkProfileExits').mockResolvedValue(true) | ||
| await deleteProxyProfile(req, resSpy) | ||
| expect(resSpy.status).toHaveBeenCalledWith(204) | ||
| }) | ||
| it('should handle not found', async () => { | ||
| checkSpy = spyOn(req.db.proxyConfigs, 'checkProfileExits').mockResolvedValue(false) | ||
| await deleteProxyProfile(req, resSpy) | ||
| expect(resSpy.status).toHaveBeenCalledWith(404) | ||
| }) | ||
| it('should handle error', async () => { | ||
| checkSpy = spyOn(req.db.proxyConfigs, 'checkProfileExits').mockResolvedValue(true) | ||
| spyOn(req.db.proxyConfigs, 'delete').mockRejectedValue(null) | ||
| await deleteProxyProfile(req, resSpy) | ||
| expect(deleteSpy).toHaveBeenCalledWith('proxyConfigName', req.tenantId) | ||
| expect(resSpy.status).toHaveBeenCalledWith(500) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /********************************************************************* | ||
| * Copyright (c) Intel Corporation 2022 | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| **********************************************************************/ | ||
|
|
||
| import Logger from '../../../Logger.js' | ||
| import { MqttProvider } from '../../../utils/MqttProvider.js' | ||
| import { type Request, type Response } from 'express' | ||
| import handleError from '../../../utils/handleError.js' | ||
| import { RPSError } from '../../../utils/RPSError.js' | ||
| import { API_UNEXPECTED_EXCEPTION, NOT_FOUND_EXCEPTION, NOT_FOUND_MESSAGE } from '../../../utils/constants.js' | ||
|
|
||
| export async function deleteProxyProfile(req: Request, res: Response): Promise<void> { | ||
| const { proxyName } = req.params | ||
| const tenantId = req.tenantId || '' | ||
| const log = new Logger('deleteProxyProfile') | ||
| try { | ||
| const proxyConfigExists: boolean = await req.db.proxyConfigs.checkProfileExits(proxyName, tenantId) | ||
|
madhavilosetty-intel marked this conversation as resolved.
|
||
| if (!proxyConfigExists) { | ||
| throw new RPSError(NOT_FOUND_MESSAGE('Proxy', proxyName), NOT_FOUND_EXCEPTION) | ||
| } else { | ||
| const results: boolean | null = await req.db.proxyConfigs.delete(proxyName, tenantId) | ||
| if (results) { | ||
| log.verbose(`Deleted proxy profile : ${proxyName}`) | ||
| MqttProvider.publishEvent('success', ['deleteProxyProfile'], `Deleted proxy configuration : ${proxyName}`) | ||
| res.status(204).json(results).end() | ||
| } else { | ||
| throw new RPSError(API_UNEXPECTED_EXCEPTION('Error deleting proxy configuration')) | ||
| } | ||
| } | ||
| } catch (error) { | ||
| handleError(log, 'deleteProxyProfile', req, res, error) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /********************************************************************* | ||
| * Copyright (c) Intel Corporation 2025 | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| **********************************************************************/ | ||
|
|
||
| import { createSpyObj } from '../../../test/helper/jest.js' | ||
| import { jest } from '@jest/globals' | ||
| import { type SpyInstance, spyOn } from 'jest-mock' | ||
| import { editProxyProfile } from './edit.js' | ||
|
|
||
| describe('Proxy - Edit', () => { | ||
| let resSpy | ||
| let req | ||
| let updateSpy: SpyInstance<any> | ||
| let getSpy: SpyInstance<any> | ||
|
|
||
| beforeEach(() => { | ||
| resSpy = createSpyObj('Response', [ | ||
| 'status', | ||
| 'json', | ||
| 'end', | ||
| 'send' | ||
| ]) | ||
| req = { | ||
| db: { proxyConfigs: { update: jest.fn(), getByName: jest.fn() } }, | ||
| query: {}, | ||
| params: { proxyConfigAddress: 'proxyConfigAddress' }, | ||
| tenantId: '', | ||
| method: 'PATCH', | ||
| body: { | ||
| address: 'intel.com', | ||
| infoFormat: 201, | ||
| networkDnsSuffix: 'vprodemo', | ||
| port: 443, | ||
| tenantId: 'foo' | ||
| } | ||
| } | ||
| updateSpy = spyOn(req.db.proxyConfigs, 'update').mockResolvedValue({}) | ||
| getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue({}) | ||
|
|
||
| resSpy.status.mockReturnThis() | ||
| resSpy.json.mockReturnThis() | ||
| resSpy.send.mockReturnThis() | ||
| }) | ||
| it('should update', async () => { | ||
| getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue({}) | ||
| await editProxyProfile(req, resSpy) | ||
| expect(getSpy).toHaveBeenCalledWith(req.body.proxyName, req.tenantId) | ||
| expect(resSpy.status).toHaveBeenCalledWith(200) | ||
| }) | ||
| it('should handle not found', async () => { | ||
| getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue(null) | ||
| await editProxyProfile(req, resSpy) | ||
| expect(getSpy).toHaveBeenCalledWith(req.body.proxyName, req.tenantId) | ||
| expect(resSpy.status).toHaveBeenCalledWith(404) | ||
| }) | ||
| it('should handle error', async () => { | ||
| getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue({}) | ||
| spyOn(req.db.proxyConfigs, 'update').mockRejectedValue(null) | ||
| await editProxyProfile(req, resSpy) | ||
| expect(getSpy).toHaveBeenCalledWith(req.body.proxyName, req.tenantId) | ||
| expect(resSpy.status).toHaveBeenCalledWith(500) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /********************************************************************* | ||
| * Copyright (c) Intel Corporation 2022 | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| **********************************************************************/ | ||
|
|
||
| import Logger from '../../../Logger.js' | ||
| import { MqttProvider } from '../../../utils/MqttProvider.js' | ||
| import { type Request, type Response } from 'express' | ||
| import handleError from '../../../utils/handleError.js' | ||
| import { RPSError } from '../../../utils/RPSError.js' | ||
| import { API_UNEXPECTED_EXCEPTION, NOT_FOUND_EXCEPTION, NOT_FOUND_MESSAGE } from '../../../utils/constants.js' | ||
| import { ProxyConfig } from 'models/RCS.Config.js' | ||
|
|
||
| export async function editProxyProfile(req: Request, res: Response): Promise<void> { | ||
| const newProxy: ProxyConfig = req.body | ||
| newProxy.tenantId = req.tenantId || '' | ||
| const log = new Logger('editProxyProfile') | ||
| try { | ||
| const oldProxy: ProxyConfig | null = await req.db.proxyConfigs.getByName(newProxy.proxyName, req.tenantId) | ||
|
|
||
| if (oldProxy == null) { | ||
| throw new RPSError(NOT_FOUND_MESSAGE('Proxy', newProxy.proxyName), NOT_FOUND_EXCEPTION) | ||
| } else { | ||
| const proxiConfig: ProxyConfig = await getUpdatedData(newProxy, oldProxy) | ||
| const results = await req.db.proxyConfigs.update(proxiConfig) | ||
| if (results) { | ||
| MqttProvider.publishEvent('success', ['editProxyConfig'], `Updated proxy configuration : ${newProxy.proxyName}`) | ||
| res.status(200).json(results).end() | ||
| } else { | ||
| throw new RPSError(API_UNEXPECTED_EXCEPTION('Error updating proxy configuration')) | ||
| } | ||
| } | ||
| } catch (error) { | ||
| handleError(log, 'proxyConfigAccessInfo', req, res, error) | ||
| } | ||
| } | ||
|
|
||
| export const getUpdatedData = async (newProxy: ProxyConfig, oldProxy: ProxyConfig): Promise<ProxyConfig> => { | ||
| const proxyConfig: ProxyConfig = { proxyName: newProxy.proxyName } as ProxyConfig | ||
| proxyConfig.address = newProxy.address ?? oldProxy.address | ||
| proxyConfig.infoFormat = newProxy.infoFormat ?? oldProxy.infoFormat | ||
| proxyConfig.networkDnsSuffix = newProxy.networkDnsSuffix ?? oldProxy.networkDnsSuffix | ||
| proxyConfig.port = newProxy.port ?? oldProxy.port | ||
| proxyConfig.tenantId = oldProxy.tenantId | ||
| return proxyConfig | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /********************************************************************* | ||
| * Copyright (c) Intel Corporation 2025 | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| **********************************************************************/ | ||
|
|
||
| import { createSpyObj } from '../../../test/helper/jest.js' | ||
| import { jest } from '@jest/globals' | ||
| import { type SpyInstance, spyOn } from 'jest-mock' | ||
| import { getProxyProfile } from './get.js' | ||
| import { ProxyConfig } from 'models/RCS.Config.js' | ||
|
|
||
| describe('Proxy - Get', () => { | ||
| let resSpy | ||
| let req | ||
| let proxyConfig: ProxyConfig | ||
| let getSpy: SpyInstance<any> | ||
|
|
||
| beforeEach(() => { | ||
| resSpy = createSpyObj('Response', [ | ||
| 'status', | ||
| 'json', | ||
| 'end', | ||
| 'send' | ||
| ]) | ||
|
|
||
| req = { | ||
| db: { proxyConfigs: { getByName: jest.fn() } }, | ||
| query: {}, | ||
| params: { proxyName: 'proxyConfigName' }, | ||
| tenantId: '', | ||
| method: 'GET' | ||
| } | ||
|
|
||
| getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue( | ||
| (proxyConfig = { | ||
| proxyName: 'proxyConfigName', | ||
| address: 'intel.com', | ||
| infoFormat: 201, | ||
| networkDnsSuffix: 'vprodemo', | ||
| port: 443, | ||
| tenantId: 'foo' | ||
| }) | ||
| ) | ||
|
|
||
| resSpy.status.mockReturnThis() | ||
| resSpy.json.mockReturnThis() | ||
| resSpy.send.mockReturnThis() | ||
| }) | ||
| it('should get', async () => { | ||
| await getProxyProfile(req, resSpy) | ||
| expect(getSpy).toHaveBeenCalledWith('proxyConfigName', req.tenantId) | ||
| expect(resSpy.status).toHaveBeenCalledWith(200) | ||
| }) | ||
| it('should handle not found', async () => { | ||
| spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue(null) | ||
| await getProxyProfile(req, resSpy) | ||
| expect(resSpy.status).toHaveBeenCalledWith(404) | ||
| }) | ||
| it('should handle error', async () => { | ||
| spyOn(req.db.proxyConfigs, 'getByName').mockRejectedValue(null) | ||
| await getProxyProfile(req, resSpy) | ||
| expect(getSpy).toHaveBeenCalledWith('proxyConfigName', req.tenantId) | ||
| expect(resSpy.status).toHaveBeenCalledWith(500) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import Logger from '../../../Logger.js' | ||
| import { MqttProvider } from '../../../utils/MqttProvider.js' | ||
| import { type Request, type Response } from 'express' | ||
| import handleError from '../../../utils/handleError.js' | ||
| import { RPSError } from '../../../utils/RPSError.js' | ||
| import { API_RESPONSE, NOT_FOUND_EXCEPTION, NOT_FOUND_MESSAGE } from '../../../utils/constants.js' | ||
| import { ProxyConfig } from 'models/RCS.Config.js' | ||
|
|
||
| export async function getProxyProfile(req: Request, res: Response) { | ||
| const { proxyName } = req.params | ||
| const tenantId = req.tenantId || '' | ||
| const log = new Logger('getProxyProfile') | ||
| try { | ||
| const result: ProxyConfig | null = await req.db.proxyConfigs.getByName(proxyName, tenantId) | ||
| if (result == null) { | ||
| throw new RPSError(NOT_FOUND_MESSAGE('Proxy', proxyName), NOT_FOUND_EXCEPTION) | ||
| } else { | ||
| MqttProvider.publishEvent('success', ['getProxyProfile'], `Sent Profile : ${proxyName}`) | ||
| res.status(200).json(API_RESPONSE(result)).end() | ||
| } | ||
| } catch (error) { | ||
| handleError(log, 'getProxyProfile', req, res, error) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.