/*
* Copyright 2010 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.autoscaling;
import org.w3c.dom.Node;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.security.SignatureException;
import com.amazonaws.*;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWS3Signer;
import com.amazonaws.auth.QueryStringSigner;
import com.amazonaws.handlers.HandlerChainFactory;
import com.amazonaws.handlers.RequestHandler;
import com.amazonaws.http.StaxResponseHandler;
import com.amazonaws.http.DefaultErrorResponseHandler;
import com.amazonaws.http.HttpClient;
import com.amazonaws.http.HttpMethodName;
import com.amazonaws.http.HttpRequest;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.transform.StaxUnmarshallerContext;
import com.amazonaws.transform.StandardErrorUnmarshaller;
import com.amazonaws.services.autoscaling.model.*;
import com.amazonaws.services.autoscaling.model.transform.*;
/**
* Client for accessing AmazonAutoScaling. All service calls made
* using this client are blocking, and will not return until the service call
* completes.
* <p>
* <p>
* Amazon Auto Scaling is an easy-to-use web service designed to
* automatically launch or terminate EC2 instances based on user defined
* triggers. Users can set up Auto Scaling groups and associate triggers
* with these groups to automatically scale computing resources based on
* parameters such as bandwidth usage or CPU utilization. Auto Scaling
* groups can work across multiple Availability Zones - distinct physical
* locations for the hosted EC2 instances - so that if an Availability
* Zone becomes unhealthy or unavailable, Auto Scaling will automatically
* re-distribute applications to a healthy Availability Zone.
* </p>
*/
public class AmazonAutoScalingClient extends AmazonWebServiceClient implements AmazonAutoScaling {
/**
* The AWS credentials (access key ID and secret key) to use when
* authenticating with AWS services.
*/
private AWSCredentials awsCredentials;
/**
* List of exception unmarshallers for all AmazonAutoScaling exceptions.
*/
protected final List<Unmarshaller<AmazonServiceException, Node>> exceptionUnmarshallers;
/** Low level client for sending requests to AWS services. */
protected final HttpClient client;
/** Optional request handlers for additional request processing. */
private List<RequestHandler> requestHandlers = new ArrayList<RequestHandler>();
/** AWS signer for authenticating requests. */
private QueryStringSigner signer;
/**
* Constructs a new client to invoke service methods on
* AmazonAutoScaling using the specified AWS account credentials.
*
* <p>
* All service calls made using this new client object are blocking, and will not
* return until the service call completes.
*
* @param awsCredentials The AWS credentials (access key ID and secret key) to use
* when authenticating with AWS services.
*/
public AmazonAutoScalingClient(AWSCredentials awsCredentials) {
this(awsCredentials, new ClientConfiguration());
}
/**
* Constructs a new client to invoke service methods on
* AmazonAutoScaling using the specified AWS account credentials
* and client configuration options.
*
* <p>
* All service calls made using this new client object are blocking, and will not
* return until the service call completes.
*
* @param awsCredentials The AWS credentials (access key ID and secret key) to use
* when authenticating with AWS services.
* @param clientConfiguration The client configuration options controlling how this
* client connects to AmazonAutoScaling
* (ex: proxy settings, retry counts, etc.).
*/
public AmazonAutoScalingClient(AWSCredentials awsCredentials, ClientConfiguration clientConfiguration) {
super(clientConfiguration);
this.awsCredentials = awsCredentials;
exceptionUnmarshallers = new ArrayList<Unmarshaller<AmazonServiceException, Node>>();
exceptionUnmarshallers.add(new AlreadyExistsExceptionUnmarshaller());
exceptionUnmarshallers.add(new ScalingActivityInProgressExceptionUnmarshaller());
exceptionUnmarshallers.add(new InvalidNextTokenExceptionUnmarshaller());
exceptionUnmarshallers.add(new LimitExceededExceptionUnmarshaller());
exceptionUnmarshallers.add(new ResourceInUseExceptionUnmarshaller());
exceptionUnmarshallers.add(new StandardErrorUnmarshaller());
setEndpoint("autoscaling.amazonaws.com");
signer = new QueryStringSigner(awsCredentials);
requestHandlers = new HandlerChainFactory().newRequestHandlerChain(
"/com/amazonaws/services/autoscaling/request.handlers");
client = new HttpClient(clientConfiguration);
}
/**
* <p>
* Deletes the specified LaunchConfiguration.
* </p>
* <p>
* The specified LaunchConfiguration must not be currently attached to
* any Auto Scaling group. Once this call completes, the launch
* configuration is no longer available for use.
* </p>
*
* @param deleteLaunchConfigurationRequest Container for the necessary
* parameters to execute the DeleteLaunchConfiguration service method on
* AmazonAutoScaling.
*
* @throws ResourceInUseException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void deleteLaunchConfiguration(DeleteLaunchConfigurationRequest deleteLaunchConfigurationRequest)
throws AmazonServiceException, AmazonClientException {
Request<DeleteLaunchConfigurationRequest> request = new DeleteLaunchConfigurationRequestMarshaller().marshall(deleteLaunchConfigurationRequest);
invoke(request, null);
}
/**
* <p>
* Returns a full description of the specified AutoScalingGroup's
* triggers.
* </p>
*
* @param describeTriggersRequest Container for the necessary parameters
* to execute the DescribeTriggers service method on AmazonAutoScaling.
*
* @return The response from the DescribeTriggers service method, as
* returned by AmazonAutoScaling.
*
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public DescribeTriggersResult describeTriggers(DescribeTriggersRequest describeTriggersRequest)
throws AmazonServiceException, AmazonClientException {
Request<DescribeTriggersRequest> request = new DescribeTriggersRequestMarshaller().marshall(describeTriggersRequest);
return invoke(request, new DescribeTriggersResultStaxUnmarshaller());
}
/**
* <p>
* Updates the configuration for the specified AutoScalingGroup.
* </p>
* <p>
* The new settings are registered upon the completion of this call. Any
* launch configuration settings take effect on any triggers after this
* call returns. Triggers that are currently in progress aren't affected.
* </p>
* <p>
* <b>NOTE:</b> If the new values are specified for the MinSize or
* MaxSize parameters, then there will be an implicit call to
* SetDesiredCapacity to set the group to the new MaxSize. All optional
* parameters are left unchanged if not passed in the request.
* </p>
*
* @param updateAutoScalingGroupRequest Container for the necessary
* parameters to execute the UpdateAutoScalingGroup service method on
* AmazonAutoScaling.
*
* @throws ScalingActivityInProgressException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void updateAutoScalingGroup(UpdateAutoScalingGroupRequest updateAutoScalingGroupRequest)
throws AmazonServiceException, AmazonClientException {
Request<UpdateAutoScalingGroupRequest> request = new UpdateAutoScalingGroupRequestMarshaller().marshall(updateAutoScalingGroupRequest);
invoke(request, null);
}
/**
* <p>
* Creates a new Auto Scaling group with the specified name. Once the
* creation request is completed, the AutoScalingGroup is ready to be
* used in other calls.
* </p>
* <p>
* <b>NOTE:</b> The client must not have already used up their entire
* quota of AutoScaling groups in order for this call to be successful.
* </p>
*
* @param createAutoScalingGroupRequest Container for the necessary
* parameters to execute the CreateAutoScalingGroup service method on
* AmazonAutoScaling.
*
* @throws LimitExceededException
* @throws AlreadyExistsException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void createAutoScalingGroup(CreateAutoScalingGroupRequest createAutoScalingGroupRequest)
throws AmazonServiceException, AmazonClientException {
Request<CreateAutoScalingGroupRequest> request = new CreateAutoScalingGroupRequestMarshaller().marshall(createAutoScalingGroupRequest);
invoke(request, null);
}
/**
* <p>
* Deletes the specified trigger.
* </p>
* <p>
* If a trigger is currently in progress, the trigger continues to run
* until its activities complete.
* </p>
*
* @param deleteTriggerRequest Container for the necessary parameters to
* execute the DeleteTrigger service method on AmazonAutoScaling.
*
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void deleteTrigger(DeleteTriggerRequest deleteTriggerRequest)
throws AmazonServiceException, AmazonClientException {
Request<DeleteTriggerRequest> request = new DeleteTriggerRequestMarshaller().marshall(deleteTriggerRequest);
invoke(request, null);
}
/**
* <p>
* Terminates the specified instance. Optionally, the desired group size
* can be adjusted.
* </p>
* <p>
* <b>NOTE:</b> This call simply registers a termination request. The
* termination of the instance cannot happen immediately.
* </p>
*
* @param terminateInstanceInAutoScalingGroupRequest Container for the
* necessary parameters to execute the
* TerminateInstanceInAutoScalingGroup service method on
* AmazonAutoScaling.
*
* @return The response from the TerminateInstanceInAutoScalingGroup
* service method, as returned by AmazonAutoScaling.
*
* @throws ScalingActivityInProgressException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public TerminateInstanceInAutoScalingGroupResult terminateInstanceInAutoScalingGroup(TerminateInstanceInAutoScalingGroupRequest terminateInstanceInAutoScalingGroupRequest)
throws AmazonServiceException, AmazonClientException {
Request<TerminateInstanceInAutoScalingGroupRequest> request = new TerminateInstanceInAutoScalingGroupRequestMarshaller().marshall(terminateInstanceInAutoScalingGroupRequest);
return invoke(request, new TerminateInstanceInAutoScalingGroupResultStaxUnmarshaller());
}
/**
* <p>
* Sets the parameters that govern how and when to scale an AutoScaling
* group.
* </p>
*
* @param createOrUpdateScalingTriggerRequest Container for the necessary
* parameters to execute the CreateOrUpdateScalingTrigger service method
* on AmazonAutoScaling.
*
* @throws LimitExceededException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void createOrUpdateScalingTrigger(CreateOrUpdateScalingTriggerRequest createOrUpdateScalingTriggerRequest)
throws AmazonServiceException, AmazonClientException {
Request<CreateOrUpdateScalingTriggerRequest> request = new CreateOrUpdateScalingTriggerRequestMarshaller().marshall(createOrUpdateScalingTriggerRequest);
invoke(request, null);
}
/**
* <p>
* Returns a full description of the AutoScalingGroup instances
* specified in the given list. This includes all Amazon EC2 instances
* that are members of the Auto Scaling group. If a list of names is not
* provided, then the full details of all AutoScalingGroups is returned.
* </p>
*
* @param describeAutoScalingGroupsRequest Container for the necessary
* parameters to execute the DescribeAutoScalingGroups service method on
* AmazonAutoScaling.
*
* @return The response from the DescribeAutoScalingGroups service
* method, as returned by AmazonAutoScaling.
*
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public DescribeAutoScalingGroupsResult describeAutoScalingGroups(DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest)
throws AmazonServiceException, AmazonClientException {
Request<DescribeAutoScalingGroupsRequest> request = new DescribeAutoScalingGroupsRequestMarshaller().marshall(describeAutoScalingGroupsRequest);
return invoke(request, new DescribeAutoScalingGroupsResultStaxUnmarshaller());
}
/**
* <p>
* Creates a new launch configuration. Once created, the new launch
* configuration is available for immediate use.
* </p>
* <p>
* <b>NOTE:</b> The launch configuration name used must be unique, within
* the scope of the client's AWS account, and the maximum limit of launch
* configurations must not yet have been met, or else the call will fail.
* </p>
*
* @param createLaunchConfigurationRequest Container for the necessary
* parameters to execute the CreateLaunchConfiguration service method on
* AmazonAutoScaling.
*
* @throws LimitExceededException
* @throws AlreadyExistsException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void createLaunchConfiguration(CreateLaunchConfigurationRequest createLaunchConfigurationRequest)
throws AmazonServiceException, AmazonClientException {
Request<CreateLaunchConfigurationRequest> request = new CreateLaunchConfigurationRequestMarshaller().marshall(createLaunchConfigurationRequest);
invoke(request, null);
}
/**
* <p>
* Returns a full description of the launch configurations given the
* specified names.
* </p>
* <p>
* If no names are specified, then the full details of all launch
* configurations are returned.
* </p>
*
* @param describeLaunchConfigurationsRequest Container for the necessary
* parameters to execute the DescribeLaunchConfigurations service method
* on AmazonAutoScaling.
*
* @return The response from the DescribeLaunchConfigurations service
* method, as returned by AmazonAutoScaling.
*
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public DescribeLaunchConfigurationsResult describeLaunchConfigurations(DescribeLaunchConfigurationsRequest describeLaunchConfigurationsRequest)
throws AmazonServiceException, AmazonClientException {
Request<DescribeLaunchConfigurationsRequest> request = new DescribeLaunchConfigurationsRequestMarshaller().marshall(describeLaunchConfigurationsRequest);
return invoke(request, new DescribeLaunchConfigurationsResultStaxUnmarshaller());
}
/**
* <p>
* Returns the scaling activities for the specified Auto Scaling group.
* </p>
* <p>
* If the specified <i>ActivityIds</i> list is empty, all the activities
* from the past six weeks are returned. Activities will be sorted by
* completion time. Activities that have no completion time are
* considered as using the most recent possible time.
* </p>
*
* @param describeScalingActivitiesRequest Container for the necessary
* parameters to execute the DescribeScalingActivities service method on
* AmazonAutoScaling.
*
* @return The response from the DescribeScalingActivities service
* method, as returned by AmazonAutoScaling.
*
* @throws InvalidNextTokenException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public DescribeScalingActivitiesResult describeScalingActivities(DescribeScalingActivitiesRequest describeScalingActivitiesRequest)
throws AmazonServiceException, AmazonClientException {
Request<DescribeScalingActivitiesRequest> request = new DescribeScalingActivitiesRequestMarshaller().marshall(describeScalingActivitiesRequest);
return invoke(request, new DescribeScalingActivitiesResultStaxUnmarshaller());
}
/**
* <p>
* Adjusts the desired size of the AutoScalingGroup by initiating
* scaling activities. When reducing the size of the group, it is not
* possible to define which EC2 instances will be terminated. This
* applies to any auto-scaling decisions that might result in the
* termination of instances.
* </p>
*
* @param setDesiredCapacityRequest Container for the necessary
* parameters to execute the SetDesiredCapacity service method on
* AmazonAutoScaling.
*
* @throws ScalingActivityInProgressException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void setDesiredCapacity(SetDesiredCapacityRequest setDesiredCapacityRequest)
throws AmazonServiceException, AmazonClientException {
Request<SetDesiredCapacityRequest> request = new SetDesiredCapacityRequestMarshaller().marshall(setDesiredCapacityRequest);
invoke(request, null);
}
/**
* <p>
* Deletes all configuration for the specified AutoScalingGroup and
* deletes the group.
* </p>
* <p>
* In order to successfully call this API, no triggers can be currently
* in progress. Once this call successfully executes, no further triggers
* will begin, and the specified AutoScalingGroup will not be available
* for use in other API calls.
* </p>
* <p>
* <b>NOTE:</b> The client cannot delete an AutoScalingGroup if it
* contains instances or if there are scaling activities in progress.
* Before attempting to delete an AutoScalingGroup, call the
* UpdateAutoScalingGroup operation to set the minimum and maximum size
* of the AutoScalingGroup to zero. This will remove all instances and
* avoid problems with a previously set trigger preventing the deletion
* of the AutoScalingGroup.
* </p>
*
* @param deleteAutoScalingGroupRequest Container for the necessary
* parameters to execute the DeleteAutoScalingGroup service method on
* AmazonAutoScaling.
*
* @throws ScalingActivityInProgressException
* @throws ResourceInUseException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public void deleteAutoScalingGroup(DeleteAutoScalingGroupRequest deleteAutoScalingGroupRequest)
throws AmazonServiceException, AmazonClientException {
Request<DeleteAutoScalingGroupRequest> request = new DeleteAutoScalingGroupRequestMarshaller().marshall(deleteAutoScalingGroupRequest);
invoke(request, null);
}
/**
* <p>
* Returns a full description of the AutoScalingGroup instances
* specified in the given list. This includes all Amazon EC2 instances
* that are members of the Auto Scaling group. If a list of names is not
* provided, then the full details of all AutoScalingGroups is returned.
* </p>
*
* @return The response from the DescribeAutoScalingGroups service
* method, as returned by AmazonAutoScaling.
*
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public DescribeAutoScalingGroupsResult describeAutoScalingGroups() throws AmazonServiceException, AmazonClientException {
return describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest());
}
/**
* <p>
* Returns a full description of the launch configurations given the
* specified names.
* </p>
* <p>
* If no names are specified, then the full details of all launch
* configurations are returned.
* </p>
*
* @return The response from the DescribeLaunchConfigurations service
* method, as returned by AmazonAutoScaling.
*
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonAutoScaling indicating
* either a problem with the data in the request, or a server side issue.
*/
public DescribeLaunchConfigurationsResult describeLaunchConfigurations() throws AmazonServiceException, AmazonClientException {
return describeLaunchConfigurations(new DescribeLaunchConfigurationsRequest());
}
/**
* Returns additional metadata for a previously executed successful, request, typically used for
* debugging issues where a service isn't acting as expected. This data isn't considered part
* of the result data returned by an operation, so it's available through this separate,
* diagnostic interface.
* <p>
* Response metadata is only cached for a limited period of time, so if you need to access
* this extra diagnostic information for an executed request, you should use this method
* to retrieve it as soon as possible after executing the request.
*
* @param request
* The originally executed request
*
* @return The response metadata for the specified request, or null if none
* is available.
*/
public ResponseMetadata getCachedResponseMetadata(AmazonWebServiceRequest request) {
return client.getResponseMetadataForRequest(request);
}
private <X, Y extends AmazonWebServiceRequest> X invoke(Request<Y> request, Unmarshaller<X, StaxUnmarshallerContext> unmarshaller) {
request.setEndpoint(endpoint);
for (Entry<String, String> entry : request.getOriginalRequest().copyPrivateRequestParameters().entrySet()) {
request.addParameter(entry.getKey(), entry.getValue());
}
// Apply any additional service specific request handlers that need to be run
if (requestHandlers != null) {
for (RequestHandler requestHandler : requestHandlers) {
request = requestHandler.handleRequest(request);
}
}
try {
signer.sign(request);
} catch (SignatureException e) {
throw new AmazonServiceException("Unable to sign request", e);
}
HttpRequest httpRequest = convertToHttpRequest(request, HttpMethodName.POST);
StaxResponseHandler<X> responseHandler = new StaxResponseHandler<X>(unmarshaller);
DefaultErrorResponseHandler errorResponseHandler = new DefaultErrorResponseHandler(exceptionUnmarshallers);
return (X)client.execute(httpRequest, responseHandler, errorResponseHandler);
}
}