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

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

Introduction

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

Prototype

@Override
public StartInstancesResult startInstances(StartInstancesRequest request) 

Source Link

Document

Starts an Amazon EBS-backed instance that you've previously stopped.

Usage

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

License:Open Source License

/**
 * Method that does the actual work of starting or stopping the instances
 * // www.  j  ava 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;
}