Refactoring upgradeVmCmd (note that the response obj still needs to be constructed)

This commit is contained in:
abhishek 2010-08-12 14:22:30 -07:00
parent 8825147f0d
commit 5a0735edd6

View File

@ -18,33 +18,21 @@
package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.utils.Pair;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.serializer.Param;
import com.cloud.vm.UserVmVO;
@Implementation(method="upgradeVirtualMachine", manager=Manager.UserVmManager)
public class UpgradeVMCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(UpgradeVMCmd.class.getName());
private static final String s_name = "changeserviceforvirtualmachineresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.SERVICE_OFFERING_ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -72,6 +60,8 @@ public class UpgradeVMCmd extends BaseCmd {
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
private UserVmVO responseObject = null;
public String getName() {
return s_name;
}
@ -79,53 +69,343 @@ public class UpgradeVMCmd extends BaseCmd {
public static String getResultObjectName() {
return "virtualmachine";
}
@Override
public String getResponse()
{
UpgradeVmResponse response = new UpgradeVmResponse();
return null;//TODO -- construct response
// if (responseObject != null) {
// response.set
//
// Account acct = ms.findAccountById(Long.valueOf(vm.getAccountId()));
// resultObj.setAccount(acct.getAccountName());
//
// ServiceOfferingVO offering = ms.findServiceOfferingById(vm.getServiceOfferingId());
// resultObj.setCpuSpeed(offering.getSpeed());
// resultObj.setMemory(offering.getRamSize());
// if(offering.getDisplayText()!=null)
// resultObj.setServiceOfferingName(offering.getDisplayText());
// else
// resultObj.setServiceOfferingName(offering.getName());
// resultObj.setServiceOfferingId(vm.getServiceOfferingId());
//
// VmStats vmStats = ms.getVmStatistics(vm.getId());
// if(vmStats != null)
// {
// resultObj.setCpuUsed((long) vmStats.getCPUUtilization());
// resultObj.setNetworkKbsRead((long) vmStats.getNetworkReadKBs());
// resultObj.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs());
// }
//
// resultObj.setCreated(vm.getCreated());
// resultObj.setDisplayName(vm.getDisplayName());
// resultObj.setDomain(ms.findDomainIdById(acct.getDomainId()).getName());
// resultObj.setDomainId(acct.getDomainId());
// resultObj.setHaEnable(vm.isHaEnabled());
// if(vm.getHostId() != null)
// {
// resultObj.setHostId(vm.getHostId());
// resultObj.setHostName(ms.getHostBy(vm.getHostId()).getName());
// }
// resultObj.setIpAddress(vm.getPrivateIpAddress());
// resultObj.setName(vm.getName());
// resultObj.setState(vm.getState().toString());
// resultObj.setZoneId(vm.getDataCenterId());
// resultObj.setZoneName(ms.findDataCenterById(vm.getDataCenterId()).getName());
//
// VMTemplateVO template = ms.findTemplateById(vm.getTemplateId());
// resultObj.setPasswordEnabled(template.getEnablePassword());
// resultObj.setTemplateDisplayText(template.getDisplayText());
// resultObj.setTemplateId(template.getId());
// resultObj.setTemplateName(template.getName());
// }
// else
// {
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update zone; internal error.");
// }
//
// return SerializerHelper.toSerializedString(responseObject);
}
public void setResponseObject(UserVmVO userVm) {
responseObject = userVm;
}
// helper class for the response object
private class UpgradeVmResponse
{
@Param(name="id")
private long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public long getDomainId() {
return domainId;
}
public void setDomainId(long domainId) {
this.domainId = domainId;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public boolean isHaEnable() {
return haEnable;
}
public void setHaEnable(boolean haEnable) {
this.haEnable = haEnable;
}
public long getZoneId() {
return zoneId;
}
public void setZoneId(long zoneId) {
this.zoneId = zoneId;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getZoneName() {
return zoneName;
}
public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}
public long getHostId() {
return hostId;
}
public void setHostId(long hostId) {
this.hostId = hostId;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public long getTemplateId() {
return templateId;
}
public void setTemplateId(long templateId) {
this.templateId = templateId;
}
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
public String getTemplateDisplayText() {
return templateDisplayText;
}
public void setTemplateDisplayText(String templateDisplayText) {
this.templateDisplayText = templateDisplayText;
}
public boolean isPasswordEnabled() {
return passwordEnabled;
}
public void setPasswordEnabled(boolean passwordEnabled) {
this.passwordEnabled = passwordEnabled;
}
public long getServiceOfferingId() {
return serviceOfferingId;
}
public void setServiceOfferingId(long serviceOfferingId) {
this.serviceOfferingId = serviceOfferingId;
}
public String getServiceOfferingName() {
return serviceOfferingName;
}
public void setServiceOfferingName(String serviceOfferingName) {
this.serviceOfferingName = serviceOfferingName;
}
public long getCpuSpeed() {
return cpuSpeed;
}
public void setCpuSpeed(long cpuSpeed) {
this.cpuSpeed = cpuSpeed;
}
public long getMemory() {
return memory;
}
public void setMemory(long memory) {
this.memory = memory;
}
public long getCpuUsed() {
return cpuUsed;
}
public void setCpuUsed(long cpuUsed) {
this.cpuUsed = cpuUsed;
}
public long getNetworkKbsRead() {
return networkKbsRead;
}
public void setNetworkKbsRead(long networkKbsRead) {
this.networkKbsRead = networkKbsRead;
}
public long getNetworkKbsWrite() {
return networkKbsWrite;
}
public void setNetworkKbsWrite(long networkKbsWrite) {
this.networkKbsWrite = networkKbsWrite;
}
public long isId()
{
return id;
}
@Param(name="name")
private String name;
@Param(name="created")
private Date created;
@Param(name="ipaddress")
private String ipAddress;
@Param(name="state")
private String state;
@Param(name="account")
private String account;
@Param(name="domainid")
private long domainId;
@Param(name="domain")
private String domain;
@Param(name="haenable")
private boolean haEnable;
@Param(name="zoneid")
private long zoneId;
@Param(name="displayname")
private String displayName;
@Param(name="zonename")
private String zoneName;
@Param(name="hostid")
private long hostId;
@Param(name="hostname")
private String hostName;
@Param(name="templateid")
private long templateId;
@Param(name="templatename")
private String templateName;
@Param(name="templatedisplaytext")
private String templateDisplayText;
@Param(name="passwordenabled")
private boolean passwordEnabled;
@Param(name="serviceofferingid")
private long serviceOfferingId;
@Param(name="serviceofferingname")
private String serviceOfferingName;
@Param(name="cpunumber")
private long cpuSpeed;
@Param(name="memory")
private long memory;
@Param(name="cpuused")
private long cpuUsed;
@Param(name="networkkbsread")
private long networkKbsRead;
@Param(name="networkkbswrite")
private long networkKbsWrite;
}
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Long virtualMachineId = (Long)params.get(BaseCmd.Properties.ID.getName());
Long serviceOfferingId = (Long)params.get(BaseCmd.Properties.SERVICE_OFFERING_ID.getName());
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
// Verify input parameters
UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(virtualMachineId.longValue());
if (vmInstance == null) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + virtualMachineId);
}
if (account != null) {
if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + virtualMachineId + " for this account");
} else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + virtualMachineId + ") given, unable to upgrade virtual machine.");
}
}
// If command is executed via 8096 port, set userId to the id of System account (1)
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
long jobId = 0;
try {
jobId = getManagementServer().upgradeVirtualMachineAsync(userId, virtualMachineId, serviceOfferingId);
} catch (InvalidParameterValueException e) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "Failed to upgrade VM: " + e.getMessage());
}
if (jobId == 0) {
s_logger.warn("Unable to schedule async-job for UpgradeVM comamnd");
} else {
if(s_logger.isDebugEnabled())
s_logger.debug("UpgradeVM command has been accepted, job id: " + jobId);
}
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
return returnValues;
}
}