@Override public void execute() { Host result = _resourceService.maintain(this); if (result != null) { HostResponse response = _responseGenerator.createHostResponse(result); response.setResponseName("host"); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to prepare host for maintenance"); } } }
@Override public void execute() { Host result = _resourceService.cancelMaintenance(this); if (result != null) { HostResponse response = _responseGenerator.createHostResponse(result); response.setResponseName(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to cancel host maintenance"); } } }
@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(ApiErrorCode.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(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } } }
@Test public void testExecuteForResult() throws Exception { addHostCmd._resourceService = resourceService; addHostCmd._responseGenerator = responseGenerator; Host host = Mockito.mock(Host.class); Host[] mockArray = new Host[] {host}; HostResponse responseHost = new HostResponse(); responseHost.setName("Test"); Mockito.doReturn(Arrays.asList(mockArray)).when(resourceService).discoverHosts(addHostCmd); Mockito.when(responseGenerator.createHostResponse(host)).thenReturn(responseHost); addHostCmd.execute(); Mockito.verify(responseGenerator).createHostResponse(host); @SuppressWarnings("unchecked") ListResponse<HostResponse> actualResponse = ((ListResponse<HostResponse>)addHostCmd.getResponseObject()); Assert.assertEquals(responseHost, actualResponse.getResponses().get(0)); Assert.assertEquals("addhostresponse", actualResponse.getResponseName()); }
@Override public void execute() { Host result; try { result = _resourceService.updateHost(this); if(getAnnotation() != null) { annotationService.addAnnotation(getAnnotation(), AnnotationService.EntityType.HOST, result.getUuid()); } HostResponse hostResponse = _responseGenerator.createHostResponse(result); hostResponse.setResponseName(getCommandName()); this.setResponseObject(hostResponse); } catch (Exception e) { s_logger.debug("Failed to update host:" + getId(), e); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update host:" + getId() + "," + e.getMessage()); } } }
protected ListResponse<HostResponse> getHostResponses() { ListResponse<HostResponse> response = new ListResponse<>(); if (getVirtualMachineId() == null) { response = _queryService.searchForServers(this); } else { Pair<List<? extends Host>, Integer> result; Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>> hostsForMigration = _mgr.listHostsForMigrationOfVM(getVirtualMachineId(), this.getStartIndex(), this.getPageSizeVal(), null); result = hostsForMigration.first(); List<? extends Host> hostsWithCapacity = hostsForMigration.second(); List<HostResponse> hostResponses = new ArrayList<HostResponse>(); for (Host host : result.first()) { HostResponse hostResponse = _responseGenerator.createHostResponse(host, getDetails()); Boolean suitableForMigration = false; if (hostsWithCapacity.contains(host)) { suitableForMigration = true; } hostResponse.setSuitableForMigration(suitableForMigration); hostResponse.setObjectName("host"); hostResponses.add(hostResponse); } response.setResponses(hostResponses, result.second()); } return response; }
@Override public void execute() { try { Host result = _resourceService.reconnectHost(this); HostResponse response = _responseGenerator.createHostResponse(result); response.setResponseName(getCommandName()); this.setResponseObject(response); } catch (InvalidParameterValueException e) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.getMessage()); } catch (CloudRuntimeException e) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); } catch (AgentUnavailableException e) { throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, e.getMessage()); } } }