@@ -4472,18 +4472,64 @@ private Map<String, Set<Long>> getNetworkIdPerNetworkDomain(List<? extends Netwo
44724472
44734473 private void checkIfHostNameUniqueInNtwkDomain (String hostName , List <? extends Network > networkList ) {
44744474 // Check that hostName is unique in the network domain
4475- Map <String , Set <Long >> ntwkDomains = getNetworkIdPerNetworkDomain (networkList );
4476- for (Entry <String , Set <Long >> ntwkDomain : ntwkDomains .entrySet ()) {
4477- for (Long ntwkId : ntwkDomain .getValue ()) {
4478- // * get all vms hostNames in the network
4479- List <String > hostNames = _vmInstanceDao .listDistinctHostNames (ntwkId );
4480- // * verify that there are no duplicates
4481- if (hostNames .contains (hostName )) {
4482- throw new InvalidParameterValueException ("The vm with hostName " + hostName + " already exists in the network domain: " + ntwkDomain .getKey () + "; network="
4483- + ((_networkModel .getNetwork (ntwkId ) != null ) ? _networkModel .getNetwork (ntwkId ).getName () : "<unknown>" ));
4475+ Set <Long > domainIdList = new HashSet <>();
4476+ switch (VmDistinctHostNameScope .value ()) {
4477+ case "global" :
4478+ // Check that hostName is unique in the zone
4479+ VMInstanceVO vm = _vmInstanceDao .findVMByHostName (hostName );
4480+ if (vm != null ) {
4481+ throw new InvalidParameterValueException ("The vm with hostName " + hostName + " already exists in the zone" );
4482+ }
4483+ case "domain" :
4484+ // Check that hostName is unique in the domain
4485+ for (Network network : networkList ) {
4486+ domainIdList .add (network .getDomainId ());
4487+ }
4488+ if (_vmInstanceDao .hostNameExistsInDomainIds (hostName , domainIdList )) {
4489+ throw new InvalidParameterValueException ("The vm with hostName " + hostName + " already exists in the domain" );
4490+ }
4491+ break ;
4492+ case "subdomain" :
4493+ for (Network network : networkList ) {
4494+ domainIdList .add (network .getDomainId ());
4495+ }
4496+ Set <Long > finalDomainIdList = new HashSet <>();
4497+ for (Long domainId : domainIdList ) {
4498+ finalDomainIdList .add (domainId );
4499+ DomainVO domain = _domainDao .findById (domainId );
4500+ List <Long > childDomainIds = _domainDao .getDomainChildrenIds (domain .getPath ());
4501+ finalDomainIdList .addAll (childDomainIds );
4502+ }
4503+
4504+ if (_vmInstanceDao .hostNameExistsInDomainIds (hostName , finalDomainIdList )) {
4505+ throw new InvalidParameterValueException ("The vm with hostName " + hostName + " already exists in the domain or subdomain" );
4506+ }
4507+ break ;
4508+ case "account" :
4509+ // Check that hostName is unique in the account
4510+ Set <Long > accountIdList = new HashSet <>();
4511+ for (Network network : networkList ) {
4512+ accountIdList .add (network .getAccountId ());
4513+ }
4514+ if (_vmInstanceDao .hostNameExistsInDomainIdsAccountIds (hostName , accountIdList )) {
4515+ throw new InvalidParameterValueException ("The vm with hostName " + hostName + " already exists in the account" );
4516+ }
4517+ break ;
4518+ default :
4519+ Map <String , Set <Long >> ntwkDomains = getNetworkIdPerNetworkDomain (networkList );
4520+ for (Entry <String , Set <Long >> ntwkDomain : ntwkDomains .entrySet ()) {
4521+ for (Long ntwkId : ntwkDomain .getValue ()) {
4522+ // * get all vms hostNames in the network
4523+ List <String > hostNames = _vmInstanceDao .listDistinctHostNames (ntwkId );
4524+ // * verify that there are no duplicates
4525+ if (hostNames .contains (hostName )) {
4526+ throw new InvalidParameterValueException ("The vm with hostName " + hostName + " already exists in the network domain: " + ntwkDomain .getKey () + "; network="
4527+ + ((_networkModel .getNetwork (ntwkId ) != null ) ? _networkModel .getNetwork (ntwkId ).getName () : "<unknown>" ));
4528+ }
4529+ }
44844530 }
4485- }
44864531 }
4532+
44874533 }
44884534
44894535 private String generateHostName (String uuidName ) {
@@ -8250,7 +8296,7 @@ public String getConfigComponentName() {
82508296 public ConfigKey <?>[] getConfigKeys () {
82518297 return new ConfigKey <?>[] {EnableDynamicallyScaleVm , AllowDiskOfferingChangeDuringScaleVm , AllowUserExpungeRecoverVm , VmIpFetchWaitInterval , VmIpFetchTrialMax ,
82528298 VmIpFetchThreadPoolMax , VmIpFetchTaskWorkers , AllowDeployVmIfGivenHostFails , EnableAdditionalVmConfig , DisplayVMOVFProperties ,
8253- KvmAdditionalConfigAllowList , XenServerAdditionalConfigAllowList , VmwareAdditionalConfigAllowList , DestroyRootVolumeOnVmDestruction };
8299+ KvmAdditionalConfigAllowList , XenServerAdditionalConfigAllowList , VmwareAdditionalConfigAllowList , DestroyRootVolumeOnVmDestruction , VmDistinctHostNameScope };
82548300 }
82558301
82568302 @ Override
0 commit comments