Example usage for com.amazonaws.services.ec2 AmazonEC2Client stopInstances

List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client stopInstances

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2Client stopInstances.

Prototype

@Override
public StopInstancesResult stopInstances(StopInstancesRequest request) 

Source Link

Document

Stops an Amazon EBS-backed instance.

Usage

From source file:au.edu.unsw.cse.soc.federatedcloud.deployers.aws.ec2.redmine.RedmineEC2DeploymentWrapper.java

License:Open Source License

@Override
public void undeployResource(String resourceID) throws Exception {
    //Reading the credentials
    Properties properties = new Properties();
    properties.load(this.getClass().getResourceAsStream("/AwsCredentials.properties"));
    String accessKey = properties.getProperty("accessKey");
    String secretKey = properties.getProperty("secretKey-NULL");
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonEC2Client client = new AmazonEC2Client(credentials);

    StopInstancesRequest request = new StopInstancesRequest();
    List<String> idList = new ArrayList<String>();
    idList.add(resourceID);// w  ww. j  av  a 2  s.  c  o  m
    request.setInstanceIds(idList);

    StopInstancesResult result = client.stopInstances(request);
}

From source file:org.elasticdroid.model.ControlInstancesModel.java

License:Open Source License

/**
 * Method that does the actual work of starting or stopping the instances
 * /* w w  w. ja va  2 s.c  om*/
 * This method uses the stop boolean to identify whether the instances should be stopped 
 * (stop = true) or started (stop = false).
 * 
 * @return Returns one of the following:
 * <ul>
 *    <li>newInstanceStates: Returns a list of stateCodes and state names for all of the instances
 *    </li>
 *    <li> AmazonServiceException</li>
 *    <li> AmazonClientException</li>
 * </ul>
 * 
 */
public Object controlInstances(List<String> instances) {

    for (String instance : instances) {
        Log.v(TAG, "Starting instance: " + instance);
    }

    //create credentials using the BasicAWSCredentials class
    BasicAWSCredentials credentials = new BasicAWSCredentials(connectionData.get("accessKey"),
            connectionData.get("secretAccessKey"));
    //create Amazon EC2 Client object, and set tye end point to the region. params[3]
    //contains endpoint
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);
    //override the default connection endpoint if provided.
    if (connectionData.get("endpoint") != null) {
        amazonEC2Client.setEndpoint(connectionData.get("endpoint"));
    }

    //if you want to start an instance
    if (operationType == ControlType.START_INSTANCE) {
        StartInstancesRequest request = new StartInstancesRequest(instances);
        StartInstancesResult result = null;
        try {
            result = amazonEC2Client.startInstances(request);
        } catch (AmazonServiceException amazonServiceException) {
            return amazonServiceException;
        } catch (AmazonClientException amazonClientException) {
            return amazonClientException;
        }
        //redundant check.
        if (result != null) {
            return result.getStartingInstances();
        }
    }
    //stop = true, start the instance.
    else {
        StopInstancesRequest request = new StopInstancesRequest(instances);
        StopInstancesResult result = null;

        try {
            result = amazonEC2Client.stopInstances(request);
        } catch (AmazonServiceException amazonServiceException) {
            return amazonServiceException;
        } catch (AmazonClientException amazonClientException) {
            return amazonClientException;
        }

        if (result != null) {
            return result.getStoppingInstances();
        }
    }

    return null;
}