Source:
<code>public void deleteLoadBalancerDnsEntry(Stack stack, String environmentName) { Set<LoadBalancer> loadBalancers = loadBalancerPersistenceService.findByStackId(stack.getId()); if (loadBalancers.isEmpty()) { LOGGER.info("No load balancers in stack {}", stack.getId()); return; } String userCrn = ThreadBasedUserCrnProvider.getUserCrn(); String accountId = ThreadBasedUserCrnProvider.getAccountId(); if (StringUtils.isEmpty(environmentName)) { DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn()); environmentName = environment.getName(); } <START> Optional<LoadBalancer> loadBalancerOptional = selectLoadBalancer(loadBalancers); <END> if (loadBalancerOptional.isEmpty()) { LOGGER.error("Unabled to determine load balancer type. Load balancer public domain name will not be registered."); } else { LoadBalancer loadBalancer = loadBalancerOptional.get(); Optional<String> endpoint = Optional.ofNullable(loadBalancer.getEndpoint()); if (endpoint.isEmpty()) { LOGGER.debug("No endpoint set for load balancer. Skipping DNS entry deletion."); } else { if (loadBalancer.getDns() != null && loadBalancer.getHostedZoneId() != null) { LOGGER.info("Deleting load balancer DNS entry with endpoint name: '{}', environment name: '{}' and cloud DNS: '{}'", endpoint.get(), environmentName, loadBalancer.getDns()); getDnsManagementService().deleteDnsEntryWithCloudDns(userCrn, accountId, endpoint.get(), environmentName, loadBalancer.getDns(), loadBalancer.getHostedZoneId()); } else if (loadBalancer.getIp() != null) { LOGGER.info("Deleting load balancer DNS entry with endpoint name: '{}', environment name: '{}' and IP: '{}'", endpoint.get(), environmentName, loadBalancer.getIp()); getDnsManagementService().deleteDnsEntryWithIp(userCrn, accountId, endpoint.get(), environmentName, false, List.of(loadBalancer.getIp())); } } } }</code><technical_language>this logic duplicated. I beneficial refactor</technical_language>

Target:
public void deleteLoadBalancerDnsEntry(Stack stack, String environmentName) { Optional<LoadBalancer> loadBalancerOptional = getLoadBalancerWithEndpoint(stack); if (loadBalancerOptional.isEmpty()) { LOGGER.error("Unable to find appropriate load balancer in stack. Load balancer public domain name will not be deleted."); } else { String userCrn = ThreadBasedUserCrnProvider.getUserCrn(); String accountId = ThreadBasedUserCrnProvider.getAccountId(); if (StringUtils.isEmpty(environmentName)) { DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn()); environmentName = environment.getName(); } LoadBalancer loadBalancer = loadBalancerOptional.get(); String endpoint = loadBalancer.getEndpoint(); if (loadBalancer.getDns() != null && loadBalancer.getHostedZoneId() != null) { LOGGER.info("Deleting load balancer DNS entry with endpoint name: '{}', environment name: '{}' and cloud DNS: '{}'", endpoint, environmentName, loadBalancer.getDns()); getDnsManagementService().deleteDnsEntryWithCloudDns(userCrn, accountId, endpoint, environmentName, loadBalancer.getDns(), loadBalancer.getHostedZoneId()); } else if (loadBalancer.getIp() != null) { LOGGER.info("Deleting load balancer DNS entry with endpoint name: '{}', environment name: '{}' and IP: '{}'", endpoint, environmentName, loadBalancer.getIp()); getDnsManagementService().deleteDnsEntryWithIp(userCrn, accountId, endpoint, environmentName, false, List.of(loadBalancer.getIp())); } } }