@Test void testGetNewPrimaryGatewayInstanceIdWhenExistingPrimaryGwIsGood() throws Exception { Stack stack = mock(Stack.class); GatewayConfig gatewayConfig = mock(GatewayConfig.class); InstanceMetaData im1 = mock(InstanceMetaData.class); InstanceMetaData im2 = mock(InstanceMetaData.class); when(gatewayConfigService.getPrimaryGatewayConfig(any())).thenReturn(gatewayConfig); when(hostOrchestrator.getFreeIpaMasterHostname(any())).thenReturn(Optional.of(HOSTNAME_1)); when(stack.getNotDeletedInstanceMetaDataList()).thenReturn(List.of(im1, im2)); when(im1.getDiscoveryFQDN()).thenReturn(HOSTNAME_1); lenient().when(im2.getDiscoveryFQDN()).thenReturn(HOSTNAME_2); when(im1.getInstanceId()).thenReturn(INSTANCE_ID_1); lenient().when(im2.getInstanceId()).thenReturn(INSTANCE_ID_2); assertEquals(INSTANCE_ID_1, underTest.selectNewPrimaryGatewayInstanceId(stack, List.of())); verify(gatewayConfigService).getPrimaryGatewayConfig(eq(stack)); verify(hostOrchestrator).getFreeIpaMasterHostname(eq(gatewayConfig)); }
public String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds) { String newPrimaryGatewayInstanceId; try { GatewayConfig currentPrimaryGatewayConfig = gatewayConfigService.getPrimaryGatewayConfig(stack); Optional<String> freeIpaMasterInstanceId = findFreeIpaMasterInstanceId(stack, currentPrimaryGatewayConfig); if (freeIpaMasterInstanceId.isPresent() && !repairInstanceIds.contains(freeIpaMasterInstanceId.get())) { LOGGER.debug("Using the FreeIPA master as the new primary gateway"); newPrimaryGatewayInstanceId = freeIpaMasterInstanceId.get(); } else { newPrimaryGatewayInstanceId = currentPrimaryGatewayConfig.getInstanceId(); if (repairInstanceIds.contains(newPrimaryGatewayInstanceId)) { LOGGER.debug("The current primary gateway is on the list of to avoid, searching for a different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } } } catch (NotFoundException e) { LOGGER.debug("No primary gateway found, searching for an different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } catch (CloudbreakOrchestratorException e) { LOGGER.debug("The orchestrator failed to identify the primary gateway node", e); throw new NotFoundException("No FreeIPA primary gateway could be selected", e); } LOGGER.debug("The new primary gateway will be {}", newPrimaryGatewayInstanceId); return newPrimaryGatewayInstanceId; }
ChangePrimaryGatewayService { public String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds) { String newPrimaryGatewayInstanceId; try { GatewayConfig currentPrimaryGatewayConfig = gatewayConfigService.getPrimaryGatewayConfig(stack); Optional<String> freeIpaMasterInstanceId = findFreeIpaMasterInstanceId(stack, currentPrimaryGatewayConfig); if (freeIpaMasterInstanceId.isPresent() && !repairInstanceIds.contains(freeIpaMasterInstanceId.get())) { LOGGER.debug("Using the FreeIPA master as the new primary gateway"); newPrimaryGatewayInstanceId = freeIpaMasterInstanceId.get(); } else { newPrimaryGatewayInstanceId = currentPrimaryGatewayConfig.getInstanceId(); if (repairInstanceIds.contains(newPrimaryGatewayInstanceId)) { LOGGER.debug("The current primary gateway is on the list of to avoid, searching for a different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } } } catch (NotFoundException e) { LOGGER.debug("No primary gateway found, searching for an different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } catch (CloudbreakOrchestratorException e) { LOGGER.debug("The orchestrator failed to identify the primary gateway node", e); throw new NotFoundException("No FreeIPA primary gateway could be selected", e); } LOGGER.debug("The new primary gateway will be {}", newPrimaryGatewayInstanceId); return newPrimaryGatewayInstanceId; } }
ChangePrimaryGatewayService { public String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds) { String newPrimaryGatewayInstanceId; try { GatewayConfig currentPrimaryGatewayConfig = gatewayConfigService.getPrimaryGatewayConfig(stack); Optional<String> freeIpaMasterInstanceId = findFreeIpaMasterInstanceId(stack, currentPrimaryGatewayConfig); if (freeIpaMasterInstanceId.isPresent() && !repairInstanceIds.contains(freeIpaMasterInstanceId.get())) { LOGGER.debug("Using the FreeIPA master as the new primary gateway"); newPrimaryGatewayInstanceId = freeIpaMasterInstanceId.get(); } else { newPrimaryGatewayInstanceId = currentPrimaryGatewayConfig.getInstanceId(); if (repairInstanceIds.contains(newPrimaryGatewayInstanceId)) { LOGGER.debug("The current primary gateway is on the list of to avoid, searching for a different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } } } catch (NotFoundException e) { LOGGER.debug("No primary gateway found, searching for an different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } catch (CloudbreakOrchestratorException e) { LOGGER.debug("The orchestrator failed to identify the primary gateway node", e); throw new NotFoundException("No FreeIPA primary gateway could be selected", e); } LOGGER.debug("The new primary gateway will be {}", newPrimaryGatewayInstanceId); return newPrimaryGatewayInstanceId; }  }
ChangePrimaryGatewayService { public String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds) { String newPrimaryGatewayInstanceId; try { GatewayConfig currentPrimaryGatewayConfig = gatewayConfigService.getPrimaryGatewayConfig(stack); Optional<String> freeIpaMasterInstanceId = findFreeIpaMasterInstanceId(stack, currentPrimaryGatewayConfig); if (freeIpaMasterInstanceId.isPresent() && !repairInstanceIds.contains(freeIpaMasterInstanceId.get())) { LOGGER.debug("Using the FreeIPA master as the new primary gateway"); newPrimaryGatewayInstanceId = freeIpaMasterInstanceId.get(); } else { newPrimaryGatewayInstanceId = currentPrimaryGatewayConfig.getInstanceId(); if (repairInstanceIds.contains(newPrimaryGatewayInstanceId)) { LOGGER.debug("The current primary gateway is on the list of to avoid, searching for a different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } } } catch (NotFoundException e) { LOGGER.debug("No primary gateway found, searching for an different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } catch (CloudbreakOrchestratorException e) { LOGGER.debug("The orchestrator failed to identify the primary gateway node", e); throw new NotFoundException("No FreeIPA primary gateway could be selected", e); } LOGGER.debug("The new primary gateway will be {}", newPrimaryGatewayInstanceId); return newPrimaryGatewayInstanceId; }  Optional<String> getPrimaryGatewayInstanceId(Stack stack); String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds); void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId); }
ChangePrimaryGatewayService { public String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds) { String newPrimaryGatewayInstanceId; try { GatewayConfig currentPrimaryGatewayConfig = gatewayConfigService.getPrimaryGatewayConfig(stack); Optional<String> freeIpaMasterInstanceId = findFreeIpaMasterInstanceId(stack, currentPrimaryGatewayConfig); if (freeIpaMasterInstanceId.isPresent() && !repairInstanceIds.contains(freeIpaMasterInstanceId.get())) { LOGGER.debug("Using the FreeIPA master as the new primary gateway"); newPrimaryGatewayInstanceId = freeIpaMasterInstanceId.get(); } else { newPrimaryGatewayInstanceId = currentPrimaryGatewayConfig.getInstanceId(); if (repairInstanceIds.contains(newPrimaryGatewayInstanceId)) { LOGGER.debug("The current primary gateway is on the list of to avoid, searching for a different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } } } catch (NotFoundException e) { LOGGER.debug("No primary gateway found, searching for an different primary gateway"); newPrimaryGatewayInstanceId = assignNewPrimaryGatewayInstanceId(stack, repairInstanceIds); } catch (CloudbreakOrchestratorException e) { LOGGER.debug("The orchestrator failed to identify the primary gateway node", e); throw new NotFoundException("No FreeIPA primary gateway could be selected", e); } LOGGER.debug("The new primary gateway will be {}", newPrimaryGatewayInstanceId); return newPrimaryGatewayInstanceId; }  Optional<String> getPrimaryGatewayInstanceId(Stack stack); String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds); void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId);  }