bug 7517: Added an ability to specify vm's host name during vm Deploy using "name" parameter. The parameter is optional.

status 7517: resolved fixed

Name should follow these rules:
* must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z', the digits '0' through '9', and the hyphen ('-').
* can not start  with a hyphen, and must not end with a hyphen

Once name is set (during vm deploy), it can't be modified.
This commit is contained in:
alena 2010-12-15 14:37:58 -08:00
parent 7f199c5b71
commit b383aaceec
8 changed files with 61 additions and 9 deletions

View File

@ -57,6 +57,9 @@ public class DeployVMCmd extends BaseAsyncCreateCmd {
@Parameter(name=ApiConstants.DISPLAY_NAME, type=CommandType.STRING, description="an optional user generated name for the virtual machine")
private String displayName;
@Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="host name for the virtual machine")
private String name;
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used.")
private Long domainId;
@ -163,10 +166,11 @@ public class DeployVMCmd extends BaseAsyncCreateCmd {
return networkIds;
}
public void setNetworkList(List<Long> networkIds) {
this.networkIds = networkIds;
public String getName() {
return name;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

View File

@ -145,8 +145,8 @@ public class UserVmVO extends VMInstanceVO implements UserVm {
long domainId,
long accountId,
long serviceOfferingId,
String userData) {
super(id, serviceOfferingId, displayName == null ? instanceName : displayName, instanceName, Type.User, templateId, guestOsId, domainId, accountId, haEnabled);
String userData, String name) {
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, guestOsId, domainId, accountId, haEnabled);
this.userData = userData;
this.displayName = displayName != null ? displayName : null;
}

View File

@ -167,7 +167,6 @@ import com.cloud.utils.nio.HandlerFactory;
import com.cloud.utils.nio.Link;
import com.cloud.utils.nio.NioServer;
import com.cloud.utils.nio.Task;
import com.cloud.vm.State;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.VirtualMachineProfileImpl;

View File

@ -2421,7 +2421,7 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute
String zoneName = _dcDao.findById(config.getDataCenterId()).getName();
cmds.addCommand("vmdata", generateVmDataCommand(routerControlIpAddress, routerPublicIpAddress, nic.getIp4Address(), userData, serviceOffering, zoneName, nic.getIp4Address(), profile.getVirtualMachine().getName(), profile.getVirtualMachine().getName(), profile.getId()));
cmds.addCommand("vmdata", generateVmDataCommand(routerControlIpAddress, routerPublicIpAddress, nic.getIp4Address(), userData, serviceOffering, zoneName, nic.getIp4Address(), profile.getVirtualMachine().getName(), profile.getVirtualMachine().getInstanceName(), profile.getId()));
try {
_agentMgr.send(router.getHostId(), cmds);

View File

@ -3742,8 +3742,24 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
long id = _vmDao.getNextInSequence(Long.class, "id");
UserVmVO vm = new UserVmVO(id, VirtualMachineName.getVmName(id, owner.getId(), _instance), cmd.getDisplayName(),
template.getId(), template.getGuestOSId(), offering.getOfferHA(), domainId, owner.getId(), offering.getId(), userData);
String hostName = cmd.getName();
String instanceName = VirtualMachineName.getVmName(id, owner.getId(), _instance);
if (hostName == null) {
hostName = instanceName;
} else {
hostName = hostName.toLowerCase();
//verify hostName
UserVm vm = _vmDao.findVmByZoneIdAndName(dc.getId(), hostName);
if (vm != null && !(vm.getState() == State.Expunging || vm.getState() == State.Error)) {
throw new InvalidParameterValueException("Vm instance with name \"" + hostName + "\" already exists in zone " + dc.getId());
} else if (!NetUtils.verifyHostName(hostName)) {
throw new InvalidParameterValueException("Invalid name. Vm name can contain ASCII letters 'a' through 'z', the digits '0' through '9', " +
"and the hyphen ('-'), must be between 1 and 63 characters long, and can't start or end with \"-\"");
}
}
UserVmVO vm = new UserVmVO(id, instanceName, cmd.getDisplayName(),
template.getId(), template.getGuestOSId(), offering.getOfferHA(), domainId, owner.getId(), offering.getId(), userData, hostName);
try{
if (_itMgr.allocate(vm, template, offering, rootDiskOffering, dataDiskOfferings, networks, null, plan, cmd.getHypervisor(), owner) == null) {

View File

@ -96,4 +96,6 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long>, StateDao<State, V
List<UserVmVO> listVmsUsingGuestIpAddress(long dcId, String ipAddress);
UserVm findByZoneAndAcctAndGuestIpAddress(long zoneId, long accountId, String ipAddress);
UserVm findVmByZoneIdAndName(long zoneId, String name);
}

View File

@ -56,6 +56,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
protected final SearchBuilder<UserVmVO> StateChangeSearch;
protected final SearchBuilder<UserVmVO> GuestIpSearch;
protected final SearchBuilder<UserVmVO> ZoneAccountGuestIpSearch;
protected final SearchBuilder<UserVmVO> ZoneNameSearch;
protected final SearchBuilder<UserVmVO> DestroySearch;
protected SearchBuilder<UserVmVO> AccountDataCenterVirtualSearch;
@ -130,6 +131,11 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
ZoneAccountGuestIpSearch.and("accountId", ZoneAccountGuestIpSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
ZoneAccountGuestIpSearch.and("guestIpAddress", ZoneAccountGuestIpSearch.entity().getGuestIpAddress(), SearchCriteria.Op.EQ);
ZoneAccountGuestIpSearch.done();
ZoneNameSearch = createSearchBuilder();
ZoneNameSearch.and("dataCenterId", ZoneNameSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
ZoneNameSearch.and("name", ZoneNameSearch.entity().getName(), SearchCriteria.Op.EQ);
ZoneNameSearch.done();
_updateTimeAttr = _allAttributes.get("updateTime");
assert _updateTimeAttr != null : "Couldn't get this updateTime attribute";
@ -361,4 +367,12 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
sc.setParameters("state", State.Stopped);
return listBy(sc);
}
@Override
public UserVm findVmByZoneIdAndName(long zoneId, String name) {
SearchCriteria<UserVmVO> sc = ZoneNameSearch.create();
sc.setParameters("dataCenterId", zoneId);
sc.setParameters("name", name);
return findOneBy(sc);
}
}

View File

@ -856,6 +856,23 @@ public class NetUtils {
System.out.println(long2Ip(NumbersUtil.parseLong(args[1], 0)));
}
}
public static boolean verifyHostName(String hostName) {
//must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner),
//the digits '0' through '9', and the hyphen ('-').
//Can not start with a hyphen, and must not end with a hyphen
boolean result = true;
if (hostName.length() > 63 || hostName.length() < 1) {
result = false;
} else if (!hostName.matches("[a-zA-z0-9-]*")) {
result = false;
} else if (hostName.startsWith("-") || hostName.endsWith("-")) {
result = false;
}
return result;
}
}