@Test public void testExecuteForResult() throws Exception { resourceService = Mockito.mock(ResourceService.class); responseGenerator = Mockito.mock(ResponseGenerator.class); addClusterCmd._resourceService = resourceService; addClusterCmd._responseGenerator = responseGenerator; Cluster cluster = Mockito.mock(Cluster.class); Cluster[] clusterArray = new Cluster[] {cluster}; Mockito.doReturn(Arrays.asList(clusterArray)).when(resourceService).discoverCluster(addClusterCmd); addClusterCmd.execute(); }
@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()); } } }
@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() { Cluster cluster = _resourceService.getCluster(getId()); if (cluster == null) { throw new InvalidParameterValueException("Unable to find the cluster by id=" + getId()); } Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate()); if (result != null) { ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false); clusterResponse.setResponseName(getCommandName()); this.setResponseObject(clusterResponse); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update cluster"); } } }
Host destinationHost = _resourceService.getHost(getHostId());
@Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { final Cluster cluster = _resourceService.getCluster(getClusterId()); if (cluster == null) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to find cluster by ID: " + getClusterId()); } final boolean result = haConfigManager.enableHA(cluster); CallContext.current().setEventDetails("Cluster Id:" + cluster.getId() + " HA enabled: true"); CallContext.current().putContextParameter(Cluster.class, cluster.getUuid()); setupResponse(result); }
@Override public void execute() { boolean result = _resourceService.releaseHostReservation(getId()); if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to release host reservation"); } } }
@Override public void execute() { boolean result = _resourceService.deleteCluster(this); if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete cluster"); } } }
@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() { 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()); } } }
@Override public void execute() { boolean result = _resourceService.deleteHost(getId(), isForced(), isForceDestoryLocalStorage()); if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete host"); } } }
private void setupResponse(final List<HAConfig> hostHAConfigList) { final ListResponse<HostHAResponse> response = new ListResponse<>(); final List<HostHAResponse> hostHAResponses = new ArrayList<>(); for (final HAConfig config : hostHAConfigList) { final Host host = _resourceService.getHost(config.getResourceId()); if (host == null) { continue; } final HostHAResponse hostHAResponse = new HostHAResponse(); hostHAResponse.setId(host.getUuid()); hostHAResponse.setEnabled(config.isEnabled()); hostHAResponse.setHaState(config.getState()); hostHAResponse.setProvider(config.getHaProvider()); hostHAResponses.add(hostHAResponse); } response.setResponses(hostHAResponses); response.setResponseName(getCommandName()); setResponseObject(response); }
@Override public void execute() { final Cluster cluster = _resourceService.getCluster(getId()); if (cluster == null) { throw new InvalidParameterValueException("Unable to find the cluster by id=" + getId()); } final Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate()); if (result != null) { final ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false); clusterResponse.setResponseName(getCommandName()); this.setResponseObject(clusterResponse); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update cluster"); } }
@Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { final Cluster cluster = _resourceService.getCluster(getClusterId()); if (cluster == null) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to find cluster by ID: " + getClusterId()); } final boolean result = haConfigManager.disableHA(cluster); CallContext.current().setEventDetails("Cluster Id:" + cluster.getId() + " HA enabled: false"); CallContext.current().putContextParameter(Cluster.class, cluster.getUuid()); setupResponse(result); }
@Override public void execute() { final boolean result = _resourceService.releaseHostReservation(getId()); if (result) { final SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to release host reservation"); } }
@Override public void execute() { final boolean result = _resourceService.deleteCluster(this); if (result) { final SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete cluster"); } }
@Override public void execute() { final Host result = _resourceService.maintain(this); if (result != null) { final 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() { try { final Host result = _resourceService.reconnectHost(this); if (result != null) { final HostResponse response = _responseGenerator.createHostResponse(result); response.setResponseName(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reconnect host"); } } catch (final Exception ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); } }
@Override public void execute() { final boolean result = _resourceService.deleteHost(getId(), isForced(), isForceDestoryLocalStorage()); if (result) { final SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete host"); } }
@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()); }