@@ -799,6 +799,8 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c
799799 throw new InvalidParameterValueException (url + " is not a valid uri" );
800800 }
801801
802+ checkForDuplicateHost (url );
803+
802804 final List <HostVO > hosts = new ArrayList <>();
803805 logger .info ("Trying to add a new host at {} in data center {}" , url , zone );
804806 boolean isHypervisorTypeSupported = false ;
@@ -2609,6 +2611,24 @@ private Host createHostAndAgent(final ServerResource resource, final Map<String,
26092611 return host ;
26102612 }
26112613
2614+ void checkForDuplicateHost (final String url ) {
2615+ String hostIpOrName = null ;
2616+ try {
2617+ hostIpOrName = new URI (UriUtils .encodeURIComponent (url )).getHost ();
2618+ } catch (final URISyntaxException ignore ) {
2619+ // unparseable URL - discoverer will reject it shortly anyway
2620+ }
2621+ if (StringUtils .isBlank (hostIpOrName )) {
2622+ return ;
2623+ }
2624+ final HostVO existingByIp = _hostDao .findByIp (hostIpOrName );
2625+ if (existingByIp != null ) {
2626+ throw new InvalidParameterValueException (String .format (
2627+ "A host with IP address / hostname '%s' already exists (id: %s). Remove it before adding again." ,
2628+ hostIpOrName , existingByIp .getUuid ()));
2629+ }
2630+ }
2631+
26122632 private Host createHostAndAgentDeferred (final ServerResource resource , final Map <String , String > details , final boolean old , final List <String > hostTags , final boolean forRebalance ) {
26132633 HostVO host = null ;
26142634 StartupCommand [] cmds = null ;
0 commit comments