From 91a9864489278543b411b390d611e56dcedcc7f9 Mon Sep 17 00:00:00 2001 From: Msarawan Date: Fri, 26 Jul 2024 16:51:01 +0530 Subject: [PATCH] [#204] Upon adding a second acceptor update the port number When user adds a second acceptor, updated port number to 5555+n instead of a static 5555. --- src/reducers/7.12/reducer.test.ts | 120 ++++++++++++++++++ src/reducers/7.12/reducer.ts | 64 +++++++++- .../AcceptorConfigPage/AcceptorConfigPage.tsx | 10 +- .../AcceptorsConfigPage.tsx | 13 ++ 4 files changed, 199 insertions(+), 8 deletions(-) diff --git a/src/reducers/7.12/reducer.test.ts b/src/reducers/7.12/reducer.test.ts index 54d47a92..ff6e3649 100644 --- a/src/reducers/7.12/reducer.test.ts +++ b/src/reducers/7.12/reducer.test.ts @@ -19,6 +19,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const newState = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); expect(newState.cr.spec.acceptors).toHaveLength(1); expect(newState.cr.spec.acceptors[0].name).toBe('acceptors0'); @@ -34,6 +38,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const newState = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); expect(newState.cr.spec.connectors).toHaveLength(1); expect(newState.cr.spec.connectors[0].name).toBe('connectors0'); @@ -70,6 +78,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.deleteAcceptor, @@ -85,6 +97,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.deleteConnector, @@ -118,6 +134,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); expect(stateWith1Acceptor.cr.spec.acceptors[0].bindToAllInterfaces).toBe( undefined, @@ -144,6 +164,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorName, @@ -162,9 +186,17 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const stateWith2Acceptor = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState3 = artemisCrReducer(stateWith2Acceptor, { operation: ArtemisReducerOperations.setAcceptorName, @@ -181,6 +213,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorOtherParams, @@ -217,6 +253,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorPort, @@ -232,6 +272,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorProtocols, @@ -247,6 +291,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorSSLEnabled, @@ -262,6 +310,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorSecret, @@ -297,6 +349,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); expect(stateWith1Connector.cr.spec.connectors[0].bindToAllInterfaces).toBe( undefined, @@ -323,6 +379,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorHost, @@ -338,6 +398,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorName, @@ -356,9 +420,17 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const stateWith2Connector = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState3 = artemisCrReducer(stateWith2Connector, { operation: ArtemisReducerOperations.setConnectorName, @@ -375,6 +447,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorOtherParams, @@ -411,6 +487,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorPort, @@ -426,6 +506,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorProtocols, @@ -441,6 +525,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorSSLEnabled, @@ -456,6 +544,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Connector = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Connector, { operation: ArtemisReducerOperations.setConnectorSecret, @@ -561,6 +653,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.updateAcceptorFactoryClass, @@ -588,6 +684,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addConnector, + payload: { + name: 'connectors0', + port: 5555, + }, }); const newState2 = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.updateConnectorFactoryClass, @@ -615,6 +715,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const stateWithPEM = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.activatePEMGenerationForAcceptor, @@ -743,6 +847,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const stateWithPEM = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.activatePEMGenerationForAcceptor, @@ -764,6 +872,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const stateExposeModeIngress = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorExposeMode, @@ -781,6 +893,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const stateExposeModeIngress = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setAcceptorIngressHost, @@ -798,6 +914,10 @@ describe('test the creation broker reducer', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { operation: ArtemisReducerOperations.addAcceptor, + payload: { + name: 'acceptors0', + port: 5555, + }, }); const stateExposeModeIngress = artemisCrReducer(stateWith1Acceptor, { operation: ArtemisReducerOperations.setIsAcceptorExposed, diff --git a/src/reducers/7.12/reducer.ts b/src/reducers/7.12/reducer.ts index 9729e2fd..7157c736 100644 --- a/src/reducers/7.12/reducer.ts +++ b/src/reducers/7.12/reducer.ts @@ -298,10 +298,18 @@ interface DeletePEMGenerationForAcceptorAction interface AddAcceptorAction extends ArtemisReducerActionBase { operation: ArtemisReducerOperations.addAcceptor; + payload: { + name: string; + port: number; + }; } interface AddConnectorAction extends ArtemisReducerActionBase { operation: ArtemisReducerOperations.addConnector; + payload: { + name: string; + port: number; + }; } interface DecrementReplicasAction extends ArtemisReducerActionBase { @@ -1653,6 +1661,10 @@ export const getAcceptor = (cr: BrokerCR, name: string) => { return undefined; }; +export const getAllAcceptors = (brokerModel: BrokerCR) => { + return brokerModel.spec?.acceptors || []; +}; + export const getAcceptorFromCertManagerResourceTemplate = ( cr: BrokerCR, rt: ResourceTemplate, @@ -1698,23 +1710,63 @@ export const getConnector = (cr: BrokerCR, name: string) => { return undefined; }; +export const getAllConnectors = (brokerModel: BrokerCR) => { + return brokerModel.spec?.connectors || []; +}; + +export const generatePortNumber = (usedPorts: number[]): number => { + let port = 5555; + while (usedPorts.includes(port)) { + port += 1; + //console.log('increament port', port); + } + return port; +}; + export const getConfigPort = ( brokerModel: BrokerCR, configType: ConfigType, configName: string, ): number => { - if (configType === ConfigType.connectors) { - const connector = getConnector(brokerModel, configName); - if (connector?.port) { - return connector.port; - } - } if (configType === ConfigType.acceptors) { const acceptor = getAcceptor(brokerModel, configName); if (acceptor?.port) { + //console.log(`Existing port for acceptor ${configName}: ${acceptor.port}`); return acceptor.port; } + + const acceptors = getAllAcceptors(brokerModel); + const usedPorts: number[] = []; + acceptors.forEach((acceptor) => { + if (acceptor.port) { + usedPorts.push(acceptor.port); + } + }); + const newPort = generatePortNumber(usedPorts); + //console.log(`Generated new port for acceptor ${configName}: ${newPort}`); + return newPort; + } else if (configType === ConfigType.connectors) { + const connector = getConnector(brokerModel, configName); + if (connector?.port) { + // console.log( + // `Existing port for connector ${configName}: ${connector.port}`, + // ); + return connector.port; + } + + const connectors = getAllConnectors(brokerModel); + const usedPorts: number[] = []; + connectors.forEach((connector) => { + if (connector.port) { + usedPorts.push(connector.port); + } + }); + const newPort = generatePortNumber(usedPorts); + //console.log(`Generated new port for connector ${configName}: ${newPort}`); + return newPort; } + + //console.log(`default port 5555 for configName: ${configName}`); return 5555; }; diff --git a/src/shared-components/FormView/BrokerProperties/ConfigurationPage/AcceptorsConfigPage/AcceptorConfigSection/AcceptorConfigPage/AcceptorConfigPage.tsx b/src/shared-components/FormView/BrokerProperties/ConfigurationPage/AcceptorsConfigPage/AcceptorConfigSection/AcceptorConfigPage/AcceptorConfigPage.tsx index 0d221573..0e68d9af 100644 --- a/src/shared-components/FormView/BrokerProperties/ConfigurationPage/AcceptorsConfigPage/AcceptorConfigSection/AcceptorConfigPage/AcceptorConfigPage.tsx +++ b/src/shared-components/FormView/BrokerProperties/ConfigurationPage/AcceptorsConfigPage/AcceptorConfigSection/AcceptorConfigPage/AcceptorConfigPage.tsx @@ -22,7 +22,7 @@ import { getConfigProtocols, getConfigSSLEnabled, } from '../../../../../../../reducers/7.12/reducer'; -import { FC, useContext } from 'react'; +import { FC, useContext, useEffect, useState } from 'react'; import { ConfigType } from '../../../ConfigurationPage'; import { useTranslation } from '../../../../../../../i18n/i18n'; import { PresetAlertPopover } from './PresetAlertPopover/PresetAlertPopover'; @@ -45,7 +45,8 @@ export const AcceptorConfigPage: FC = ({ const dispatch = useContext(BrokerCreationFormDispatch); const selectedClass = getConfigFactoryClass(cr, configType, configName); - const port = getConfigPort(cr, configType, configName); + const initialPort = getConfigPort(cr, configType, configName); + //console.log(`Port for ${configName}: `, initialPort); const host = getConfigHost(cr, configType, configName); const protocols = getConfigProtocols(cr, configType, configName); const isSSLEnabled = getConfigSSLEnabled(cr, configType, configName); @@ -54,6 +55,7 @@ export const AcceptorConfigPage: FC = ({ configType, configName, ); + const [port, setPort] = useState(initialPort); const onChangeClass = (value: string) => { if (configType === ConfigType.acceptors) { @@ -176,6 +178,10 @@ export const AcceptorConfigPage: FC = ({ ]; const { t } = useTranslation(); + useEffect(() => { + setPort(initialPort); + }, [initialPort]); + return ( <> = ({ brokerId }) => { const addNewConfig = () => { if (configType === ConfigType.acceptors) { + const newPort = getConfigPort( + cr, + configType, + `acceptor-${configs.length}`, + ); dispatch({ operation: ArtemisReducerOperations.addAcceptor, + payload: { name: `acceptor-${configs.length}`, port: newPort }, }); } if (configType === ConfigType.connectors) { + const newPort = getConfigPort( + cr, + configType, + `connector-${configs.length}`, + ); dispatch({ operation: ArtemisReducerOperations.addConnector, + payload: { name: `connector-${configs.length}`, port: newPort }, }); } };