@Test public void testExecuteForNullResult() { ResourceService resourceService = Mockito.mock(ResourceService.class); try { Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(null); } catch (ResourceInUseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DiscoveryException e) { // TODO Auto-generated catch block e.printStackTrace(); } addClusterCmd._resourceService = resourceService; try { addClusterCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to add cluster", exception.getDescription()); } }
@Override public void execute() { try { List<? extends Swift> result = _resourceService.discoverSwift(this); SwiftResponse swiftResponse = null; if (result != null && result.size() > 0) { for (Swift swift : result) { // There should only be one secondary storage host per add swiftResponse = _responseGenerator.createSwiftResponse(swift); swiftResponse.setResponseName(getCommandName()); swiftResponse.setObjectName("Swift"); this.setResponseObject(swiftResponse); } } else { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to add Swift"); } } catch (DiscoveryException ex) { String errMsg = "Failed to add Swift due to " + ex.toString(); s_logger.warn(errMsg, ex); throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errMsg); } }
@Override public void execute() { try { if ((getRamOvercommitRaito().compareTo(1f) <= 0) | (getCpuOvercommitRatio().compareTo(1f) <= 0)) { throw new InvalidParameterValueException( "Cpu and ram overcommit ratios should not be less than 1"); } List<? extends Cluster> result = _resourceService.discoverCluster(this); ListResponse<ClusterResponse> response = new ListResponse<ClusterResponse>(); List<ClusterResponse> clusterResponses = new ArrayList<ClusterResponse>(); if (result != null && result.size() > 0) { for (Cluster cluster : result) { ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false); clusterResponses.add(clusterResponse); } } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add cluster"); } response.setResponses(clusterResponses); response.setResponseName(getCommandName()); this.setResponseObject(response); } catch (DiscoveryException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } catch (ResourceInUseException ex) { s_logger.warn("Exception: ", ex); ServerApiException e = new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); for (String proxyObj : ex.getIdProxyList()) { e.addProxyObject(proxyObj); } throw e; } }
@Override public void execute() { try { List<? extends Host> result = _resourceService.discoverHosts(this); ListResponse<HostResponse> response = new ListResponse<HostResponse>(); List<HostResponse> hostResponses = new ArrayList<HostResponse>(); if (result != null && result.size() > 0) { for (Host host : result) { HostResponse hostResponse = _responseGenerator.createHostResponse(host); hostResponses.add(hostResponse); } } else { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to add host"); } response.setResponses(hostResponses); response.setResponseName(getCommandName()); this.setResponseObject(response); } catch (DiscoveryException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage()); } }
protected void loadResource(Long hostId) { HostVO host = hostDao.findById(hostId); Map<String, Object> params = new HashMap<String, Object>(); params.put("guid", host.getGuid()); params.put("ipaddress", host.getPrivateIpAddress()); params.put("username", "root"); params.put("password", "password"); params.put("zone", String.valueOf(host.getDataCenterId())); params.put("pod", String.valueOf(host.getPodId())); ServerResource resource = null; if (host.getHypervisorType() == HypervisorType.XenServer) { resource = new XcpOssResource(); try { resource.configure(host.getName(), params); } catch (ConfigurationException e) { logger.debug("Failed to load resource:" + e.toString()); } } else if (host.getHypervisorType() == HypervisorType.KVM) { resource = new LibvirtComputingResource(); try { params.put("public.network.device", "cloudbr0"); params.put("private.network.device", "cloudbr0"); resource.configure(host.getName(), params); } catch (ConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (host.getHypervisorType() == HypervisorType.VMware) { ClusterVO cluster = clusterDao.findById(host.getClusterId()); String url = clusterDetailsDao.findDetail(cluster.getId(), "url").getValue(); URI uri; try { uri = new URI(url); String userName = clusterDetailsDao.findDetail(cluster.getId(), "username").getValue(); String password = clusterDetailsDao.findDetail(cluster.getId(), "password").getValue(); VmwareServerDiscoverer discover = new VmwareServerDiscoverer(); Map<? extends ServerResource, Map<String, String>> resources = discover.find( host.getDataCenterId(), host.getPodId(), host.getClusterId(), uri, userName, password, null); for (Map.Entry<? extends ServerResource, Map<String, String>> entry : resources.entrySet()) { resource = entry.getKey(); } if (resource == null) { throw new CloudRuntimeException("can't find resource"); } } catch (DiscoveryException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } } hostResourcesMap.put(hostId, resource); HostEnvironment env = new HostEnvironment(); SetupCommand cmd = new SetupCommand(env); cmd.setNeedSetup(true); resource.executeRequest(cmd); }