AddHost and AddSecondaryStorage have different required parameters, so avoid subclassing these commands and just call a common method in the agent manager. Fix the response name for listAccounts. CreateUser returns one user, so don't always get response.user[0] since that's pointless. A secondary storage host does not have a pod id (NfsSecondaryStorageResource) so make sure not to dereference variables that will be null.

This commit is contained in:
Kris McQueen 2010-09-21 16:28:31 -07:00
parent ebd06ae6f3
commit 5a3f4af638
7 changed files with 115 additions and 51 deletions

View File

@ -24,7 +24,6 @@ import com.cloud.agent.Listener;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.api.commands.AddHostCmd;
import com.cloud.api.commands.AddHostOrStorageCmd;
import com.cloud.api.commands.AddSecondaryStorageCmd;
import com.cloud.api.commands.CancelMaintenanceCmd;
import com.cloud.api.commands.DeleteHostCmd;
@ -231,7 +230,7 @@ public interface AgentManager extends Manager {
public boolean reconnect(final long hostId) throws AgentUnavailableException;
public boolean reconnectHost(ReconnectHostCmd cmd) throws AgentUnavailableException;
public List<HostVO> discoverHosts(AddHostOrStorageCmd cmd) throws DiscoveryException, InvalidParameterValueException;
public List<HostVO> discoverHosts(Long dcId, Long podId, Long clusterId, String clusterName, String url, String username, String password) throws DiscoveryException, InvalidParameterValueException;
public List<HostVO> discoverHosts(AddHostCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
public List<HostVO> discoverHosts(AddSecondaryStorageCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
}

View File

@ -73,7 +73,6 @@ import com.cloud.alert.AlertManager;
import com.cloud.api.BaseCmd;
import com.cloud.api.ServerApiException;
import com.cloud.api.commands.AddHostCmd;
import com.cloud.api.commands.AddHostOrStorageCmd;
import com.cloud.api.commands.AddSecondaryStorageCmd;
import com.cloud.api.commands.CancelMaintenanceCmd;
import com.cloud.api.commands.DeleteHostCmd;
@ -485,23 +484,25 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory {
@Override
public List<HostVO> discoverHosts(AddHostCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException {
return discoverHosts((AddHostOrStorageCmd)cmd);
Long dcId = cmd.getZoneId();
Long podId = cmd.getPodId();
Long clusterId = cmd.getClusterId();
String clusterName = cmd.getClusterName();
String url = cmd.getUrl();
String username = cmd.getUsername();
String password = cmd.getPassword();
return discoverHosts(dcId, podId, clusterId, clusterName, url, username, password);
}
@Override
public List<HostVO> discoverHosts(AddSecondaryStorageCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException {
return discoverHosts((AddHostOrStorageCmd)cmd);
Long dcId = cmd.getZoneId();
String url = cmd.getUrl();
return discoverHosts(dcId, null, null, null, url, null, null);
}
@Override
public List<HostVO> discoverHosts(AddHostOrStorageCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException {
Long dcId = cmd.getZoneId();
Long podId = cmd.getPodId();
Long clusterId = cmd.getClusterId();
String clusterName = cmd.getName();
String url = cmd.getUrl();
String username = cmd.getUsername();
String password = cmd.getPassword();
public List<HostVO> discoverHosts(Long dcId, Long podId, Long clusterId, String clusterName, String url, String username, String password) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException {
URI uri = null;
//Check if the zone exists in the system

View File

@ -31,6 +31,7 @@ import com.cloud.api.ApiDBUtils;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.response.ApiResponseSerializer;
import com.cloud.api.response.HostResponse;
@ -45,14 +46,72 @@ import com.cloud.storage.GuestOSCategoryVO;
import com.cloud.vm.UserVmVO;
@Implementation(method="discoverHosts", manager=Manager.AgentManager)
public class AddHostCmd extends AddHostOrStorageCmd {
public class AddHostCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(AddHostCmd.class.getName());
private static final String s_name = "addhostresponse";
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name="clusterid", type=CommandType.LONG)
private Long clusterId;
@Parameter(name="clustername", type=CommandType.STRING)
private String clusterName;
@Parameter(name="password", type=CommandType.STRING, required=true)
private String password;
@Parameter(name="podid", type=CommandType.LONG)
private Long podId;
@Parameter(name="url", type=CommandType.STRING, required=true)
private String url;
@Parameter(name="username", type=CommandType.STRING, required=true)
private String username;
@Parameter(name="zoneid", type=CommandType.LONG, required=true)
private Long zoneId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getClusterId() {
return clusterId;
}
public String getClusterName() {
return clusterName;
}
public String getPassword() {
return password;
}
public Long getPodId() {
return podId;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public Long getZoneId() {
return zoneId;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getName() {
return s_name;

View File

@ -30,6 +30,7 @@ import com.cloud.api.ApiDBUtils;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.response.ApiResponseSerializer;
import com.cloud.api.response.HostResponse;
@ -40,36 +41,32 @@ import com.cloud.host.Status.Event;
import com.cloud.storage.GuestOSCategoryVO;
@Implementation(method="discoverHosts", manager=Manager.AgentManager)
public class AddSecondaryStorageCmd extends AddHostOrStorageCmd {
public class AddSecondaryStorageCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(AddSecondaryStorageCmd.class.getName());
private static final String s_name = "addsecondarystorageresponse";
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
public Long getClusterId() {
return null;
}
public String getClusterName() {
return null;
@Parameter(name="url", type=CommandType.STRING, required=true)
private String url;
@Parameter(name="zoneid", type=CommandType.LONG)
private Long zoneId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public String getUrl() {
return url;
}
public String getPassword() {
return null;
public Long getZoneId() {
return zoneId;
}
public Long getPodId() {
return null;
}
public String getUsername() {
return null;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@ -106,16 +103,18 @@ public class AddSecondaryStorageCmd extends AddHostOrStorageCmd {
hostResponse.setState(host.getStatus());
hostResponse.setIpAddress(host.getPrivateIpAddress());
hostResponse.setVersion(host.getVersion());
GuestOSCategoryVO guestOSCategory = ApiDBUtils.getHostGuestOSCategory(host.getId());
if (guestOSCategory != null) {
hostResponse.setOsCategoryId(guestOSCategory.getId());
hostResponse.setOsCategoryName(guestOSCategory.getName());
}
hostResponse.setZoneName(ApiDBUtils.findZoneById(host.getDataCenterId()).getName());
hostResponse.setPodName(ApiDBUtils.findPodById(host.getPodId()).getName());
if (host.getPodId() != null) {
hostResponse.setPodName(ApiDBUtils.findPodById(host.getPodId()).getName());
}
if (host.getType().toString().equals("Storage")) {
hostResponse.setDiskSizeTotal(host.getTotalSize());
hostResponse.setDiskSizeAllocated(0L);

View File

@ -223,7 +223,7 @@ public class ListAccountsCmd extends BaseListCmd {
acctResponse.setCleanupRequired(account.getNeedsCleanup());
}
acctResponse.setName("account");
acctResponse.setResponseName("account");
accountResponses.add(acctResponse);
}

View File

@ -149,19 +149,25 @@ public class ListHostsCmd extends BaseListCmd {
hostResponse.setOsCategoryName(guestOSCategory.getName());
}
hostResponse.setZoneName(ApiDBUtils.findZoneById(host.getDataCenterId()).getName());
hostResponse.setPodName(ApiDBUtils.findPodById(host.getPodId()).getName());
if (host.getPodId() != null) {
hostResponse.setPodName(ApiDBUtils.findPodById(host.getPodId()).getName());
}
DecimalFormat decimalFormat = new DecimalFormat("#.##");
// calculate cpu allocated by vm
int cpu = 0;
String cpuAlloc = null;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
List<UserVmVO> instances = ApiDBUtils.listUserVMsByHostId(host.getId());
for (UserVmVO vm : instances) {
ServiceOffering so = ApiDBUtils.findServiceOfferingById(vm.getServiceOfferingId());
cpu += so.getCpu() * so.getSpeed();
if ((host.getCpus() != null) && (host.getSpeed() != null)) {
int cpu = 0;
String cpuAlloc = null;
List<UserVmVO> instances = ApiDBUtils.listUserVMsByHostId(host.getId());
for (UserVmVO vm : instances) {
ServiceOffering so = ApiDBUtils.findServiceOfferingById(vm.getServiceOfferingId());
cpu += so.getCpu() * so.getSpeed();
}
cpuAlloc = decimalFormat.format(((float) cpu / (float) (host.getCpus() * host.getSpeed())) * 100f) + "%";
hostResponse.setCpuAllocated(cpuAlloc);
}
cpuAlloc = decimalFormat.format(((float) cpu / (float) (host.getCpus() * host.getSpeed())) * 100f) + "%";
hostResponse.setCpuAllocated(cpuAlloc);
// calculate cpu utilized
String cpuUsed = null;

View File

@ -322,7 +322,7 @@ $(document).ready(function() {
dataType: "json",
async: false,
success: function(json) {
userJSONToTemplate(json.createuserresponse.user[0], template);
userJSONToTemplate(json.createuserresponse, template);
loadingImg.hide();
rowContainer.show();
},