@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(); }
@Test public void getInstances_emptyList() throws Exception { when(asgClient.describeAutoScalingGroups(anyObject())).thenReturn(new DescribeAutoScalingGroupsResult()); AsgTagInstanceDiscovery discovery = new AsgTagInstanceDiscovery(asgClient, ec2Client); Collection<Instance> instanceList = discovery.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; }
@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); }
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)); }
@Test public void getInstances() throws Exception { AutoScalingGroup groupWithTag1 = new AutoScalingGroup() .withTags(new TagDescription().withKey(TAG_KEY).withValue("Cluster1")) .withInstances(createMockInstance("id1"), createMockInstance("id2")); AutoScalingGroup groupWithTag2 = new AutoScalingGroup() .withTags(new TagDescription().withKey(TAG_KEY).withValue("Cluster2")) .withInstances(createMockInstance("id3"), createMockInstance("id4")); AutoScalingGroup groupWithoutTag = new AutoScalingGroup() .withTags(new TagDescription().withKey("WrongTag").withValue("Cluster3")) .withInstances(createMockInstance("id5"), createMockInstance("id6")); Set<String> badIds = Sets.newHashSet("id5", "id6"); DescribeAutoScalingGroupsResult result = new DescribeAutoScalingGroupsResult(); result.setAutoScalingGroups(Lists.newArrayList(groupWithTag1, groupWithTag2, groupWithoutTag)); when(asgClient.describeAutoScalingGroups(anyObject())).thenReturn(result); // mock the ec2 client request to get instance details when(ec2Client.describeInstances(any(DescribeInstancesRequest.class))).thenAnswer(m -> { DescribeInstancesRequest req = m.getArgumentAt(0, DescribeInstancesRequest.class); // make sure id5 and id6 didnt get requested. They dont have the right tag assertTrue(Sets.intersection(Sets.newHashSet(req.getInstanceIds()), badIds).isEmpty()); List<com.amazonaws.services.ec2.model.Instance> ec2Instances = req.getInstanceIds().stream() .map(id -> new com.amazonaws.services.ec2.model.Instance() .withInstanceId(id) .withTags(new Tag(TAG_KEY, "Unused")) .withState(new InstanceState().withName("running")) .withPublicDnsName("www.public.com")) .collect(Collectors.toList()); return new DescribeInstancesResult().withReservations(new Reservation().withInstances(ec2Instances)); }); Collection<Instance> instanceList = new AsgTagInstanceDiscovery(asgClient, ec2Client).getInstanceList(); assertNotNull(instanceList); assertEquals(4, instanceList.size()); Set<String> validClusters = Sets.newHashSet("Cluster1", "Cluster2"); for (Instance i : instanceList) { assertTrue(validClusters.contains(i.getCluster())); } }
public static void main( String[] args ) throws IOException { final String env = "prod"; AWSCredentials credentials = new PropertiesCredentials( ShutdownWorkers.class.getResourceAsStream( "/AwsCredentials.properties" ) ); try { // Scalo a zero int min = 0; int desired = 0; int max = 0; AmazonAutoScaling as = new AmazonAutoScalingClient(credentials); as.setEndpoint("autoscaling.eu-west-1.amazonaws.com"); DescribeAutoScalingGroupsResult dasgres = as.describeAutoScalingGroups(); List<AutoScalingGroup> lasg = dasgres.getAutoScalingGroups(); for (AutoScalingGroup asg : lasg) { if(asg.getAutoScalingGroupName().equals("asg-kopjra-prod-workers")){ desired = asg.getDesiredCapacity(); max = asg.getMaxSize(); min = asg.getMinSize(); break; } } UpdateAutoScalingGroupRequest uasgr = new UpdateAutoScalingGroupRequest(); uasgr.setAutoScalingGroupName("asg-kopjra-prod-workers"); uasgr.setDesiredCapacity(0); uasgr.setMaxSize(0); uasgr.setMinSize(0); as.updateAutoScalingGroup(uasgr); System.out.println("Done: workers shutting down!"); System.out.println("When you run the StartupWorkers, remember to use the following parameters: "+min+" "+max+" "+desired); } catch (AmazonServiceException ase) { System.err.println( "AmazonServiceException" ); } catch (AmazonClientException ace) { System.err.println( "AmazonClientException" ); } catch (Exception e){ System.err.println( "OtherException" ); } }