use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.
the class AwsClient method createAutoScalingClient.
public AmazonAutoScalingClient createAutoScalingClient(AwsCredentialView awsCredential, String regionName) {
AmazonAutoScalingClient client = isRoleAssumeRequired(awsCredential) ? new AmazonAutoScalingClient(credentialClient.retrieveCachedSessionCredentials(awsCredential)) : new AmazonAutoScalingClient(createAwsCredentials(awsCredential));
client.setRegion(RegionUtils.getRegion(regionName));
return client;
}
use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method suspendAutoScaling.
private void suspendAutoScaling(AuthenticatedContext ac, CloudStack stack) {
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
for (Group group : stack.getGroups()) {
String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, group.getName(), ac.getCloudContext().getLocation().getRegion().value());
amazonASClient.suspendProcesses(new SuspendProcessesRequest().withAutoScalingGroupName(asGroupName).withScalingProcesses(SUSPENDED_PROCESSES));
}
}
use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method resumeAutoScalingPolicies.
private void resumeAutoScalingPolicies(AuthenticatedContext ac, CloudStack stack) {
for (Group instanceGroup : stack.getGroups()) {
try {
String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, instanceGroup.getName(), ac.getCloudContext().getLocation().getRegion().value());
if (asGroupName != null) {
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
List<AutoScalingGroup> asGroups = amazonASClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asGroupName)).getAutoScalingGroups();
if (!asGroups.isEmpty()) {
if (!asGroups.get(0).getSuspendedProcesses().isEmpty()) {
amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(asGroupName).withMinSize(0).withDesiredCapacity(0));
amazonASClient.resumeProcesses(new ResumeProcessesRequest().withAutoScalingGroupName(asGroupName));
}
}
} else {
LOGGER.info("Autoscaling Group's physical id is null (the resource doesn't exist), it is not needed to resume scaling policies.");
}
} catch (AmazonServiceException e) {
if (e.getErrorMessage().matches("Resource.*does not exist for stack.*") || e.getErrorMessage().matches("Stack '.*' does not exist.*")) {
LOGGER.info(e.getMessage());
} else {
throw e;
}
}
}
}
use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method upscale.
@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
resumeAutoScaling(ac, stack);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
List<Group> scaledGroups = getScaledGroups(stack);
for (Group group : scaledGroups) {
String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName());
amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(asGroupName).withMaxSize(group.getInstancesSize()).withDesiredCapacity(group.getInstancesSize()));
LOGGER.info("Updated Auto Scaling group's desiredCapacity: [stack: '{}', to: '{}']", ac.getCloudContext().getId(), resources.size());
}
scheduleStatusChecks(stack, ac, cloudFormationClient);
suspendAutoScaling(ac, stack);
boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(new AwsNetworkView(stack.getNetwork()), amazonEC2Client);
List<Group> gateways = getGatewayGroups(scaledGroups);
if (mapPublicIpOnLaunch && !gateways.isEmpty()) {
String cFStackName = getCloudFormationStackResource(resources).getName();
Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, cloudFormationClient);
for (Group gateway : gateways) {
List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway);
List<String> freeEips = getFreeIps(eips, amazonEC2Client);
List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, cloudFormationClient, gateway);
List<String> newInstances = instanceIds.stream().filter(iid -> gateway.getInstances().stream().noneMatch(inst -> iid.equals(inst.getInstanceId()))).collect(Collectors.toList());
associateElasticIpsToInstances(amazonEC2Client, freeEips, newInstances);
}
}
return singletonList(new CloudResourceStatus(getCloudFormationStackResource(resources), ResourceStatus.UPDATED));
}
use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.
the class ASGroupStatusCheckerTaskTest method successTest.
@Test
public void successTest() throws Exception {
int requiredInstances = 160;
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
String regionName = "eu-west-1";
when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
CloudCredential cloudCredential = mock(CloudCredential.class);
when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
String asGroupName = "as-group";
AwsClient awsClient = mock(AwsClient.class);
AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 100);
firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
List<String> instancIds = new ArrayList<>();
for (int i = 0; i < requiredInstances; i++) {
instancIds.add(Integer.toString(i));
}
AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
Boolean taskResult = asGroupStatusCheckerTask.call();
ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
assertEquals(100, allValues.get(0).getInstanceIds().size());
assertEquals(60, allValues.get(1).getInstanceIds().size());
assertTrue(taskResult);
}
Aggregations