@Test public void testGetAutoScalingGroupDesiredCapacity() { final int desiredCapacity = 5; final DescribeAutoScalingGroupsResult result = mock(DescribeAutoScalingGroupsResult.class, RETURNS_DEEP_STUBS); when(result.getAutoScalingGroups().get(0).getDesiredCapacity()).thenReturn(desiredCapacity); when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(result); final String groupName = "testGroupName"; assertThat(awsHelperService.getAutoScalingGroupDesiredCapacity(groupName), equalTo(desiredCapacity)); final ArgumentCaptor<DescribeAutoScalingGroupsRequest> argumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class); verify(amazonAutoScalingClient).describeAutoScalingGroups(argumentCaptor.capture()); assertThat(argumentCaptor.getValue().getAutoScalingGroupNames().get(0), equalTo(groupName)); }
@Test public void testGetPublicDnsForAutoScalingGroupNoInstancesFound() { String logicalId = "fake-logical-id"; when(autoScalingClient.describeAutoScalingGroups( new DescribeAutoScalingGroupsRequest() .withAutoScalingGroupNames(logicalId) ) ).thenReturn( new DescribeAutoScalingGroupsResult() ); // invoke method under test List<String> results = autoScalingService.getPublicDnsForAutoScalingGroup(logicalId); assertEquals(0, results.size()); }
@Test public void testIncrementMinInstancesForAsgHappy() { String logicalId = "asg id"; String instanceId = "instance id"; int minSize = 2; when(autoScalingClient.describeAutoScalingGroups( new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(logicalId)) ).thenReturn( new DescribeAutoScalingGroupsResult() .withAutoScalingGroups( new AutoScalingGroup().withInstances( new Instance().withInstanceId(instanceId)) .withMinSize(minSize) ) ); autoScalingService.updateMinInstancesForAutoScalingGroup(logicalId, minSize - 1); verify(autoScalingClient).updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(logicalId) .withMinSize(minSize - 1)); }
/** * Returns auto scaling groups that have the CLUSTER_TAG_KEY tag * @return collection of AutoScalingGroup that contain the CLUSTER_TAG_KEY */ private Collection<AutoScalingGroup> findAutoscalingGroups() { String token = null; List<AutoScalingGroup> groupList = new ArrayList<>(); do { DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest(); request.setNextToken(token); DescribeAutoScalingGroupsResult result = asgClient.describeAutoScalingGroups(request); result.getAutoScalingGroups().stream() .filter(a -> containsTag(a.getTags())) .forEach(groupList::add); token = result.getNextToken(); } while(!Strings.isNullOrEmpty(token)); return groupList; }
@Test public void testGetInstanceIdsForAutoScalingGroup() { final com.amazonaws.services.autoscaling.model.Instance instance = new com.amazonaws.services.autoscaling.model.Instance(); instance.setInstanceId(TEST_INSTANCE_ID); final List<com.amazonaws.services.autoscaling.model.Instance> instanceList = new ArrayList<>(); instanceList.add(instance); final DescribeAutoScalingGroupsResult result = mock(DescribeAutoScalingGroupsResult.class, RETURNS_DEEP_STUBS); when(result.getAutoScalingGroups().get(0).getInstances()).thenReturn(instanceList); when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(result); final String groupName = "testGroupName"; assertThat(awsHelperService.getInstanceIdsForAutoScalingGroup(groupName).get(0), equalTo(TEST_INSTANCE_ID)); final ArgumentCaptor<DescribeAutoScalingGroupsRequest> argumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class); verify(amazonAutoScalingClient).describeAutoScalingGroups(argumentCaptor.capture()); assertThat(argumentCaptor.getValue().getAutoScalingGroupNames().get(0), equalTo(groupName)); }
@Test public void testGetInstancesForAutoScalingGroup() { final com.amazonaws.services.autoscaling.model.Instance instance = new com.amazonaws.services.autoscaling.model.Instance(); instance.setInstanceId(TEST_INSTANCE_ID); instance.setAvailabilityZone("testZone"); final List<com.amazonaws.services.autoscaling.model.Instance> instanceList = new ArrayList<>(); instanceList.add(instance); final DescribeAutoScalingGroupsResult result = mock(DescribeAutoScalingGroupsResult.class, RETURNS_DEEP_STUBS); when(result.getAutoScalingGroups().get(0).getInstances()).thenReturn(instanceList); when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(result); final String groupName = "testGroupName"; assertThat(awsHelperService.getInstancesForAutoScalingGroup(groupName).get(0), allOf( hasProperty("instanceId", equalTo(TEST_INSTANCE_ID)), hasProperty("availabilityZone", equalTo("testZone")) )); final ArgumentCaptor<DescribeAutoScalingGroupsRequest> argumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class); verify(amazonAutoScalingClient).describeAutoScalingGroups(argumentCaptor.capture()); assertThat(argumentCaptor.getValue().getAutoScalingGroupNames().get(0), equalTo(groupName)); }
private Optional<AutoScalingGroup> describeAutoScalingGroup(final String autoscalingGroupName) { final DescribeAutoScalingGroupsRequest describeAsg = new DescribeAutoScalingGroupsRequest() .withAutoScalingGroupNames(autoscalingGroupName); final DescribeAutoScalingGroupsResult result = autoScalingClient.describeAutoScalingGroups(describeAsg); return result.getAutoScalingGroups().stream().findFirst(); }
/*** * Get list of {@link AutoScalingGroup}s for a given tag * * @param tag Tag to filter the auto scaling groups * @return List of {@link AutoScalingGroup}s qualifying the filter tag */ public List<AutoScalingGroup> getAutoScalingGroupsWithTag(Tag tag) { final AmazonAutoScaling autoScaling = getAmazonAutoScalingClient(); final DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest = new DescribeAutoScalingGroupsRequest(); final List<AutoScalingGroup> allAutoScalingGroups = autoScaling .describeAutoScalingGroups(describeAutoScalingGroupsRequest) .getAutoScalingGroups(); final List<AutoScalingGroup> filteredAutoScalingGroups = Lists.newArrayList(); for (AutoScalingGroup autoScalingGroup : allAutoScalingGroups) { for (TagDescription tagDescription : autoScalingGroup.getTags()) { if (tagDescription.getKey().equalsIgnoreCase(tag.getKey()) && tagDescription.getValue().equalsIgnoreCase(tag.getValue())) { filteredAutoScalingGroups.add(autoScalingGroup); } } } return filteredAutoScalingGroups; }
@Test public void testGetInstances_awsException() throws Exception { when(asgClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenThrow(new AmazonClientException("Unit Test Intentional Exception")); Collection<Instance> instanceList = new AsgTagInstanceDiscovery(asgClient, ec2Client).getInstanceList(); assertNotNull(instanceList); assertTrue(instanceList.isEmpty()); }
public AutoScalingGroup createASGroup(CreateAutoScalingGroupRequest request) { logger.info("create auto scaling group, request={}", request); autoScaling.createAutoScalingGroup(request); DescribeAutoScalingGroupsResult result = autoScaling.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(request.getAutoScalingGroupName())); return result.getAutoScalingGroups().get(0); }
public List<AutoScalingGroup> listASGroups() { logger.info("list all auto scaling groups"); List<AutoScalingGroup> asGroups = new ArrayList<>(); String nextToken = null; while (true) { DescribeAutoScalingGroupsResult result = autoScaling.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withNextToken(nextToken)); asGroups.addAll(result.getAutoScalingGroups()); nextToken = result.getNextToken(); if (nextToken == null) break; } return asGroups; }
public AutoScalingGroup describeASGroup(String asGroupName) { logger.info("describe auto scaling group, name={}", asGroupName); List<AutoScalingGroup> groups = autoScaling.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest() .withAutoScalingGroupNames(asGroupName)) .getAutoScalingGroups(); if (groups.isEmpty()) return null; return groups.get(0); }
@Override public AutoScalingGroup call() { DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest() .withAutoScalingGroupNames(this.groupName); DescribeAutoScalingGroupsResult result = getClient().getApi().describeAutoScalingGroups(request); List<AutoScalingGroup> autoScalingGroups = result.getAutoScalingGroups(); if (autoScalingGroups.isEmpty()) { throw new IllegalArgumentException(format("Auto Scaling Group '%s' doesn't exist in region '%s'.", this.groupName, getClient().getRegion())); } return getOnlyElement(autoScalingGroups); }
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; } } } }
public List<String> getInstanceIds(AmazonAutoScalingClient amazonASClient, String asGroupName) { DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = amazonASClient .describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asGroupName)); List<String> instanceIds = new ArrayList<>(); if (describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances() != null) { for (Instance instance : describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances()) { if ("InService".equals(instance.getLifecycleState())) { instanceIds.add(instance.getInstanceId()); } } } return instanceIds; }
@Override public List<AbstractResource<?>> describeAutoScalingGroups(Account account, Region region, DateTime dt) { AmazonAutoScaling as = findClient(account, region); DescribeAutoScalingGroupsRequest req = new DescribeAutoScalingGroupsRequest(); log.debug("start describing auto scaling groups for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeAutoScalingGroupsResult res = as.describeAutoScalingGroups(req); return converter.toAsGroups(res.getAutoScalingGroups(), account.getId(), region, dt); }
@Test public void testGetPublicDnsForAutoScalingGroup() { String logicalId = "fake-logical-id"; String instanceId = "fake-instance-id"; String dnsName = "dns.name"; when(autoScalingClient.describeAutoScalingGroups( new DescribeAutoScalingGroupsRequest() .withAutoScalingGroupNames(logicalId) ) ).thenReturn( new DescribeAutoScalingGroupsResult() .withAutoScalingGroups( new AutoScalingGroup() .withInstances( new Instance() .withInstanceId(instanceId) ) ) ); when(ec2Client.describeInstances( new DescribeInstancesRequest() .withInstanceIds(instanceId) ) ).thenReturn( new DescribeInstancesResult() .withReservations( new Reservation() .withInstances( new com.amazonaws.services.ec2.model.Instance() .withPublicDnsName(dnsName) ) ) ); // invoke method under test List<String> results = autoScalingService.getPublicDnsForAutoScalingGroup(logicalId); assertEquals(1, results.size()); assertEquals(dnsName, results.get(0)); }