com.amazonaws.services.ec2.model.TerminateInstancesResult Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.TerminateInstancesResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TerminateInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Terminate specified instances (power-on the instances).
 *
 * @param instanceIDs
 *            IDs of the instances to terminate
 * @return a list of state changes for the instances
 */
public static List<InstanceStateChange> terminateInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // send the terminate request with args as instance IDs
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    request.withInstanceIds(instanceIDs);
    TerminateInstancesResult result = amazonEC2Client.terminateInstances(request);

    return result.getTerminatingInstances();
}
 
Example #2
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Tests that the terminate code works when no matching results are returned by the client
public void testTerminateInstanceNoInstanceEmpty() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    terminateInstancesResult.setTerminatingInstances(CollectionUtils.EMPTY_COLLECTION);
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match",1,request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertFalse("Termination call should have not been successful", success);
}
 
Example #3
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Test terminating instances works correctly
public void testTerminateInstance() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    InstanceStateChange stateChange = new InstanceStateChange();
    stateChange.withInstanceId(instanceId);
    stateChange.setCurrentState(new InstanceState().withCode(32));
    terminateInstancesResult.setTerminatingInstances(Arrays.asList(stateChange));
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match", 1, request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertTrue("Termination call should have been successful", success);
}
 
Example #4
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Tests terminating an invalid instance is handled correctly
public void testTerminateInstanceInvalidRunningCode() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    InstanceStateChange stateChange = new InstanceStateChange();
    stateChange.withInstanceId(instanceId);
    stateChange.setCurrentState(new InstanceState().withCode(8));
    terminateInstancesResult.setTerminatingInstances(Arrays.asList(stateChange));
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match", 1, request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertFalse("Termination call should have not been successful", success);
}
 
Example #5
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Test terminating a valid but not matching instances is handled correctly
public void testTerminateInstanceNoMatchingInstance() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    InstanceStateChange stateChange = new InstanceStateChange();
    stateChange.withInstanceId("notMatching");
    stateChange.setCurrentState(new InstanceState().withCode(8));
    terminateInstancesResult.setTerminatingInstances(Arrays.asList(stateChange));
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match", 1, request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertFalse("Termination call should have not been successful", success);
}
 
Example #6
Source File: AwsInstanceCloudConnector.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Completable doTerminate(List<String> instanceIds) {
    TerminateInstancesRequest request = new TerminateInstancesRequest().withInstanceIds(instanceIds);
    Observable<TerminateInstancesResult> observable = toObservable(request, ec2Client::terminateInstancesAsync);
    return observable
            .doOnCompleted(() -> logger.info("Terminated instances: {}", instanceIds))
            .doOnError(e -> logger.warn("Failed to terminate instances: {}, due to {}", instanceIds, e.getMessage()))
            .toCompletable();
}
 
Example #7
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Terminate instances.
 *
 * @param instanceIds
 *            instances' IDs
 * @return list of instances change
 */
protected final List<InstanceStateChange> terminateInstances(
        final Collection<String> instanceIds) {
    log.info("Terminate instances:" + toString(instanceIds));
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    request.setInstanceIds(instanceIds);
    TerminateInstancesResult result = amazonEC2Client
            .terminateInstances(request);
    return result.getTerminatingInstances();
}
 
Example #8
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public TerminateInstancesResult terminate(
        ResultCapture<TerminateInstancesResult> extractor) {

    TerminateInstancesRequest request = new TerminateInstancesRequest();
    return terminate(request, extractor);
}
 
Example #9
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public TerminateInstancesResult terminate(TerminateInstancesRequest request,
        ResultCapture<TerminateInstancesResult> extractor) {

    ActionResult result = resource.performAction("Terminate", request,
            extractor);

    if (result == null) return null;
    return (TerminateInstancesResult) result.getData();
}
 
Example #10
Source File: AwsVmManager.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Terminates the specified instance.
 *
 * @param  instanceId  Id of the instance to terminate
 */
public boolean terminateInstance(final String instanceId) {
    TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest();
    terminateRequest.withInstanceIds(instanceId);

    if(client == null){
        throw new RuntimeException("The client is not initialized");
    }
    TerminateInstancesResult result = client.terminateInstances(terminateRequest);
    List<InstanceStateChange> stateChanges = result.getTerminatingInstances();
    boolean terminatedInstance = false;
    for (InstanceStateChange stateChange : stateChanges) {
        if (instanceId.equals(stateChange.getInstanceId())) {
            terminatedInstance = true;

            InstanceState currentState = stateChange.getCurrentState();
            if (currentState.getCode() != 32 && currentState.getCode() != 48) {
                log.error(String.format(
                        "Machine state for id %s should be terminated (48) or shutting down (32) but was %s instead",
                        instanceId, currentState.getCode()));
                return false;
            }
        }
    }

    if (!terminatedInstance) {
        log.error("Matching terminated instance was not found for instance " + instanceId);
        return false;
    }

    return true;
}
 
Example #11
Source File: AwsInstanceProcess.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
public void terminate(AwsProcessClient awsProcessClient, Long instanceNo) {
    AwsInstance awsInstance = awsInstanceDao.read(instanceNo);
    String instanceId = awsInstance.getInstanceId();

    // イベントログ出力
    Instance instance = instanceDao.read(instanceNo);
    processLogger.debug(null, instance, "AwsInstanceDelete",
            new Object[] { awsProcessClient.getPlatform().getPlatformName(), instanceId });

    // インスタンスの削除
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    request.withInstanceIds(instanceId);
    TerminateInstancesResult result = awsProcessClient.getEc2Client().terminateInstances(request);
    List<InstanceStateChange> terminatingInstances = result.getTerminatingInstances();

    // API実行結果チェック
    if (terminatingInstances.size() == 0) {
        // インスタンス削除失敗時
        throw new AutoException("EPROCESS-000107", instanceId);

    } else if (terminatingInstances.size() > 1) {
        // 複数のインスタンスが削除された場合
        AutoException exception = new AutoException("EPROCESS-000108", instanceId);
        exception.addDetailInfo("result=" + terminatingInstances);
        throw exception;
    }

    // ログ出力
    if (log.isInfoEnabled()) {
        log.info(MessageUtils.getMessage("IPROCESS-100117", instanceId));
    }

    // データベース更新
    awsInstance.setStatus(terminatingInstances.get(0).getCurrentState().getName());
    awsInstanceDao.update(awsInstance);
}
 
Example #12
Source File: AwsInstanceCloudConnector.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used internally by the connector itself for housekeeping.
 */
Completable terminateDetachedInstances(List<String> ids) {
    Observable<TerminateInstancesResult> observable = toObservable(
            new TerminateInstancesRequest(ids),
            ec2Client::terminateInstancesAsync
    );
    return observable.toCompletable();
}
 
Example #13
Source File: MockAmazonEc2Client.java    From SeleniumGridScaler with GNU General Public License v2.0 4 votes vote down vote up
public void setTerminateInstancesResult(TerminateInstancesResult terminateInstancesResult) {
    this.terminateInstancesResult = terminateInstancesResult;
}
 
Example #14
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public TerminateInstancesResult terminate(TerminateInstancesRequest request)
        {

    return terminate(request, null);
}
 
Example #15
Source File: MockAmazonEc2Client.java    From SeleniumGridScaler with GNU General Public License v2.0 4 votes vote down vote up
@Override
public TerminateInstancesResult terminateInstances(TerminateInstancesRequest terminateInstancesRequest) throws AmazonServiceException, AmazonClientException {
    this.terminateInstancesRequest = terminateInstancesRequest;
    return terminateInstancesResult;
}
 
Example #16
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public TerminateInstancesResult terminate() {
    return terminate((ResultCapture<TerminateInstancesResult>)null);
}
 
Example #17
Source File: EC2IntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateInstance() throws Exception {

    AmazonEC2Client ec2Client = provider.getClient();
    Assume.assumeNotNull("AWS client not null", ec2Client);

    assertNoStaleInstances(ec2Client, "before");

    try {
        WildFlyCamelContext camelctx = new WildFlyCamelContext();
        camelctx.getNamingContext().bind("ec2Client", ec2Client);
        camelctx.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:createAndRun").to("aws-ec2://TestDomain?amazonEc2Client=#ec2Client&operation=createAndRunInstances");
                from("direct:terminate").to("aws-ec2://TestDomain?amazonEc2Client=#ec2Client&operation=terminateInstances");
            }
        });

        camelctx.start();
        try {

            // Create and run an instance
            Map<String, Object> headers = new HashMap<>();
            headers.put(EC2Constants.IMAGE_ID, "ami-02ace471");
            headers.put(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro);
            headers.put(EC2Constants.SUBNET_ID, EC2Utils.getSubnetId(ec2Client));
            headers.put(EC2Constants.INSTANCE_MIN_COUNT, 1);
            headers.put(EC2Constants.INSTANCE_MAX_COUNT, 1);
            headers.put(EC2Constants.INSTANCES_TAGS, Arrays.asList(new Tag("Name", "wildfly-camel")));

            ProducerTemplate template = camelctx.createProducerTemplate();
            RunInstancesResult result1 = template.requestBodyAndHeaders("direct:createAndRun", null, headers, RunInstancesResult.class);
            String instanceId = result1.getReservation().getInstances().get(0).getInstanceId();
            System.out.println("InstanceId: " + instanceId);

            // Terminate the instance
            headers = new HashMap<>();
            headers.put(EC2Constants.INSTANCES_IDS, Collections.singleton(instanceId));

            TerminateInstancesResult result2 = template.requestBodyAndHeaders("direct:terminate", null, headers, TerminateInstancesResult.class);
            Assert.assertEquals(instanceId, result2.getTerminatingInstances().get(0).getInstanceId());
        } finally {
            camelctx.close();
        }
    } finally {
        assertNoStaleInstances(ec2Client, "after");
    }
}
 
Example #18
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>Terminate</code> action.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Instance</code> resource, and any conflicting parameter value set
 * in the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>InstanceIds.0</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return The response of the low-level client operation associated with
 *         this resource action.
 * @see TerminateInstancesRequest
 */
TerminateInstancesResult terminate(TerminateInstancesRequest request);
 
Example #19
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>Terminate</code> action and use a ResultCapture to
 * retrieve the low-level client response.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Instance</code> resource, and any conflicting parameter value set
 * in the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>InstanceIds.0</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return The response of the low-level client operation associated with
 *         this resource action.
 * @see TerminateInstancesRequest
 */
TerminateInstancesResult terminate(TerminateInstancesRequest request,
        ResultCapture<TerminateInstancesResult> extractor);
 
Example #20
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * The convenient method form for the <code>Terminate</code> action.
 *
 * @see #terminate(TerminateInstancesRequest)
 */
TerminateInstancesResult terminate();
 
Example #21
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * The convenient method form for the <code>Terminate</code> action.
 *
 * @see #terminate(TerminateInstancesRequest, ResultCapture)
 */
TerminateInstancesResult terminate(ResultCapture<TerminateInstancesResult>
        extractor);