com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest Java Examples

The following examples show how to use com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomAmazonWaiterProvider.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public Waiter<DescribeAutoScalingGroupsRequest> getAutoscalingInstancesInServiceWaiter(AmazonAutoScalingClient asClient, Integer requiredCount) {
    return new WaiterBuilder<DescribeAutoScalingGroupsRequest, DescribeAutoScalingGroupsResult>()
            .withSdkFunction(new DescribeAutoScalingGroupsFunction(asClient))
            .withAcceptors(new WaiterAcceptor<DescribeAutoScalingGroupsResult>() {
                @Override
                public boolean matches(DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult) {
                    return describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances()
                            .stream().filter(instance -> IN_SERVICE.equals(instance.getLifecycleState())).count() == requiredCount;
                }

                @Override
                public WaiterState getState() {
                    return WaiterState.SUCCESS;
                }
            })
            .withDefaultPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(DEFAULT_MAX_ATTEMPTS),
                    new FixedDelayStrategy(DEFAULT_DELAY_IN_SECONDS)))
            .withExecutorService(WaiterExecutorServiceFactory.buildExecutorServiceForWaiter("AmazonRDSWaiters")).build();
}
 
Example #2
Source File: AWSSdkClient.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/***
 * 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;
}
 
Example #3
Source File: CloudFormationStackUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Map<Group, List<String>> getInstanceIdsByGroups(AmazonAutoScalingRetryClient amazonASClient, Map<String, Group> groupNameMapping) {
    DescribeAutoScalingGroupsResult result = amazonASClient
            .describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(groupNameMapping.keySet()));
    return result.getAutoScalingGroups().stream()
            .collect(Collectors.toMap(
                    ag -> groupNameMapping.get(ag.getAutoScalingGroupName()),
                    ag -> ag.getInstances().stream()
                            .filter(instance -> INSTANCE_LIFECYCLE_IN_SERVICE.equals(instance.getLifecycleState()))
                            .map(Instance::getInstanceId)
                            .collect(Collectors.toList())));
}
 
Example #4
Source File: AutoScalingGroupHandlerTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAutoScalingGroups() {
    CloudResource cfResource = CloudResource.builder()
            .type(ResourceType.CLOUDFORMATION_STACK)
            .name("cf")
            .build();
    DescribeStackResourcesResult resourcesResult = new DescribeStackResourcesResult();
    StackResource stackResource = new StackResource()
            .withLogicalResourceId("logicalResourceId")
            .withPhysicalResourceId("physicalResourceId")
            .withResourceType("AWS::AutoScaling::AutoScalingGroup");
    resourcesResult.getStackResources().add(stackResource);
    resourcesResult.getStackResources().add(new StackResource().withResourceType("other"));
    when(cloudFormationClient.describeStackResources(any(DescribeStackResourcesRequest.class))).thenReturn(resourcesResult);

    DescribeAutoScalingGroupsResult scalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup().withAutoScalingGroupName(stackResource.getPhysicalResourceId());
    scalingGroupsResult.getAutoScalingGroups().add(autoScalingGroup);
    when(autoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(scalingGroupsResult);

    Map<AutoScalingGroup, String> autoScalingGroups = underTest.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
    assertEquals(1, autoScalingGroups.size());
    assertEquals(autoScalingGroup, autoScalingGroups.entrySet().stream().findFirst().get().getKey());
    assertEquals(stackResource.getLogicalResourceId(), autoScalingGroups.entrySet().stream().findFirst().get().getValue());

    ArgumentCaptor<DescribeStackResourcesRequest> stackResourcesRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeStackResourcesRequest.class);
    verify(cloudFormationClient).describeStackResources(stackResourcesRequestArgumentCaptor.capture());
    assertEquals(cfResource.getName(), stackResourcesRequestArgumentCaptor.getValue().getStackName());

    ArgumentCaptor<DescribeAutoScalingGroupsRequest> scalingGroupsRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    verify(autoScalingClient).describeAutoScalingGroups(scalingGroupsRequestArgumentCaptor.capture());
    assertEquals(1, scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().size());
    assertEquals(stackResource.getPhysicalResourceId(), scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().get(0));
}
 
Example #5
Source File: AwsTerminateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
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) {
                AmazonAutoScalingRetryClient amazonASClient = awsClient.createAutoScalingRetryClient(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.debug("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.debug(e.getMessage());
            } else {
                throw e;
            }
        }
    }
}
 
Example #6
Source File: AwsUpscaleService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Map<String, Integer> getAutoScalingGroupsBySize(Set<String> autoScalingGroupNames, AmazonAutoScalingRetryClient amazonASClient) {
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(new ArrayList<>(autoScalingGroupNames));
    return amazonASClient.describeAutoScalingGroups(request).getAutoScalingGroups().stream()
            .collect(Collectors.toMap(AutoScalingGroup::getAutoScalingGroupName,
                    autoScalingGroup -> autoScalingGroup.getInstances().size()));
}
 
Example #7
Source File: CloudFormationStackUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public List<String> getInstanceIds(AmazonAutoScalingRetryClient amazonASClient, String asGroupName) {
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = amazonASClient
            .describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asGroupName));
    List<String> instanceIds = new ArrayList<>();
    if (!describeAutoScalingGroupsResult.getAutoScalingGroups().isEmpty()
            && describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances() != null) {
        for (Instance instance : describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances()) {
            if (INSTANCE_LIFECYCLE_IN_SERVICE.equals(instance.getLifecycleState())) {
                instanceIds.add(instance.getInstanceId());
            }
        }
    }
    return instanceIds;
}
 
Example #8
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch asg.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @param accountName the account name
 * @return the map
 */
public static Map<String,List<AutoScalingGroup>> fetchAsg(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){

	AmazonAutoScaling asgClient;
	Map<String,List<AutoScalingGroup>> asgList = new LinkedHashMap<>();

	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"ASG\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				List<AutoScalingGroup> asgListTemp = new ArrayList<>();
				asgClient = AmazonAutoScalingClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				String nextToken = null;
				DescribeAutoScalingGroupsResult  describeResult ;
				do{
					describeResult =  asgClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withNextToken(nextToken).withMaxRecords(asgMaxRecord));
					asgListTemp.addAll(describeResult.getAutoScalingGroups());
					nextToken = describeResult.getNextToken();
				}while(nextToken!=null);

				if(!asgListTemp.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId + " Type : ASG "+region.getName()+" >> " + asgListTemp.size());
					asgList.put(accountId+delimiter+accountName+delimiter+region.getName(), asgListTemp);
				}
		   	}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"asg",e.getMessage());
		}
	}
	return asgList;
}
 
Example #9
Source File: AutoScalingGroupHandler.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Map<AutoScalingGroup, String> getAutoScalingGroups(AmazonCloudFormationClient cloudFormationClient, AmazonAutoScalingClient autoScalingClient,
        CloudResource cfResource) {
    DescribeStackResourcesRequest resourcesRequest = new DescribeStackResourcesRequest();
    resourcesRequest.setStackName(cfResource.getName());
    DescribeStackResourcesResult resourcesResult = cloudFormationClient.describeStackResources(resourcesRequest);
    Map<String, String> autoScalingGroups = resourcesResult.getStackResources().stream()
            .filter(stackResource -> "AWS::AutoScaling::AutoScalingGroup".equalsIgnoreCase(stackResource.getResourceType()))
            .collect(Collectors.toMap(StackResource::getPhysicalResourceId, StackResource::getLogicalResourceId));
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(autoScalingGroups.keySet());
    List<AutoScalingGroup> scalingGroups = autoScalingClient.describeAutoScalingGroups(request).getAutoScalingGroups();
    return scalingGroups.stream()
            .collect(Collectors.toMap(scalingGroup -> scalingGroup, scalingGroup -> autoScalingGroups.get(scalingGroup.getAutoScalingGroupName())));
}
 
Example #10
Source File: AsgLocalMasterReadinessResolver.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Mono<ReadinessStatus> refresh() {
    return AwsReactorExt
            .<DescribeAutoScalingGroupsRequest, DescribeAutoScalingGroupsResult>toMono(
                    () -> {
                        DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
                        request.setAutoScalingGroupNames(Collections.singletonList(configuration.getTitusMasterAsgName()));
                        return request;
                    },
                    autoScalingClient::describeAutoScalingGroupsAsync
            )
            .map(this::resolveStatus);
}
 
Example #11
Source File: AwsInstanceCloudConnector.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<InstanceGroup>> getInstanceGroups(List<String> instanceGroupIds) {
    if (instanceGroupIds.isEmpty()) {
        return Observable.just(Collections.emptyList());
    }

    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(instanceGroupIds);
    Observable<DescribeAutoScalingGroupsResult> observable = toObservable(request, autoScalingClient::describeAutoScalingGroupsAsync);
    return observable.map(
            response -> toInstanceGroups(response.getAutoScalingGroups())
    ).timeout(configuration.getAwsRequestTimeoutMs(), TimeUnit.MILLISECONDS);
}
 
Example #12
Source File: AwsInstanceCloudConnector.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<InstanceGroup>> getInstanceGroups() {
    PageCollector<DescribeAutoScalingGroupsRequest, AutoScalingGroup> pageCollector = new PageCollector<>(
            token -> new DescribeAutoScalingGroupsRequest().withMaxRecords(AWS_PAGE_MAX).withNextToken(token),
            request -> {
                Observable<DescribeAutoScalingGroupsResult> observable = toObservable(request, autoScalingClient::describeAutoScalingGroupsAsync);
                return observable.map(result -> Pair.of(result.getAutoScalingGroups(), result.getNextToken()));
            }
    );
    return pageCollector.getAll()
            .map(this::toInstanceGroups)
            .timeout(configuration.getInstanceGroupsFetchTimeoutMs(), TimeUnit.MILLISECONDS);
}
 
Example #13
Source File: ASGInventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch scaling policies.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<ScalingPolicy>> fetchScalingPolicies(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
		
		AmazonAutoScaling asgClient;
		Map<String,List<ScalingPolicy>> scalingPolicyList = new LinkedHashMap<>();
		
		String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"ASG\" , \"region\":\"" ;
		for(Region region : RegionUtils.getRegions()){ 
			try{
				if(!skipRegions.contains(region.getName())){ //!skipRegions
					List<ScalingPolicy> _scalingPolicyList = new ArrayList<>();
					asgClient = AmazonAutoScalingClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
					String nextToken = null;
					DescribeAutoScalingGroupsResult  describeResult ;
					do{
						describeResult =  asgClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withNextToken(nextToken).withMaxRecords(asgMaxRecord));
						for(AutoScalingGroup _asg : describeResult.getAutoScalingGroups()) {
							_scalingPolicyList.addAll(asgClient.describePolicies(new DescribePoliciesRequest().withAutoScalingGroupName(_asg.getAutoScalingGroupName())).getScalingPolicies());
						}
						nextToken = describeResult.getNextToken();
					}while(nextToken!=null);
					
					if(!_scalingPolicyList.isEmpty() ){
						log.debug("Account : " + accountId + " Type : ASG Scaling Policy "+region.getName()+" >> " + _scalingPolicyList.size());
						scalingPolicyList.put(accountId+delimiter+accountName+delimiter+region.getName(), _scalingPolicyList);
					}
			   	}
			}catch(Exception e){
				log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,region.getName(),"asgpolicy",e.getMessage());
			}
		}
		return scalingPolicyList;
	}
 
Example #14
Source File: AsgLocalMasterReadinessResolverTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    when(configuration.getTitusMasterAsgName()).thenReturn("MasterAsg");

    when(autoScalingClient.describeAutoScalingGroupsAsync(any(), any())).thenAnswer(invocation -> {
        DescribeAutoScalingGroupsRequest request = invocation.getArgument(0);
        AsyncHandler asyncHandler = invocation.getArgument(1);

        if (ERROR_MARKER.equals(currentTagState.get())) {
            asyncHandler.onError(SIMULATED_ERROR);
            invocationCounter.incrementAndGet();
            return Futures.immediateFailedFuture(SIMULATED_ERROR);
        }

        DescribeAutoScalingGroupsResult response = new DescribeAutoScalingGroupsResult();
        AutoScalingGroup autoScalingGroup = new AutoScalingGroup();

        if (currentTagState.get() != null) {
            TagDescription tag = new TagDescription();
            tag.setKey(TAG_MASTER_ENABLED);
            tag.setValue(currentTagState.get());

            autoScalingGroup.setTags(Collections.singletonList(tag));
        }

        response.setAutoScalingGroups(Collections.singletonList(autoScalingGroup));

        asyncHandler.onSuccess(request, response);

        invocationCounter.incrementAndGet();
        return Futures.immediateFuture(response);
    });

    resolver = new AsgLocalMasterReadinessResolver(
            configuration,
            autoScalingClient,
            REFRESH_SCHEDULER_DESCRIPTOR.toBuilder()
                    .withInitialDelay(Duration.ofMillis(0))
                    .withInterval(Duration.ofMillis(1))
                    .build(),
            titusRuntime,
            Schedulers.parallel()
    );
}
 
Example #15
Source File: AwsAutoScalingService.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public List<AutoScalingGroup> getAutoscalingGroups(AmazonAutoScalingRetryClient amazonASClient, Set<String> groupNames) {
    return amazonASClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(groupNames)).getAutoScalingGroups();
}
 
Example #16
Source File: AmazonAutoScalingRetryClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public DescribeAutoScalingGroupsResult describeAutoScalingGroups(DescribeAutoScalingGroupsRequest request) {
    return retry.testWith2SecDelayMax15Times(() -> mapThrottlingError(() -> client.describeAutoScalingGroups(request)));
}
 
Example #17
Source File: AwsUpscaleServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
void upscaleTest() throws AmazonAutoscalingFailed {
    AmazonAutoScalingRetryClient amazonAutoScalingRetryClient = mock(AmazonAutoScalingRetryClient.class);
    AmazonCloudFormationRetryClient amazonCloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class);
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    List<AutoScalingGroup> autoScalingGroups = new ArrayList<>();

    AutoScalingGroup masterASGroup = new AutoScalingGroup();
    masterASGroup.setAutoScalingGroupName("masterASG");
    List<Instance> masterASGInstances = new ArrayList<>();
    masterASGInstances.add(new Instance().withInstanceId("i-master1"));
    masterASGInstances.add(new Instance().withInstanceId("i-master2"));
    masterASGroup.setInstances(masterASGInstances);

    AutoScalingGroup workerASGroup = new AutoScalingGroup();
    workerASGroup.setAutoScalingGroupName("workerASG");
    List<Instance> workerASGInstances = new ArrayList<>();
    workerASGInstances.add(new Instance().withInstanceId("i-worker1"));
    workerASGInstances.add(new Instance().withInstanceId("i-worker2"));
    workerASGInstances.add(new Instance().withInstanceId("i-worker3"));
    workerASGroup.setInstances(workerASGInstances);

    autoScalingGroups.add(masterASGroup);
    autoScalingGroups.add(workerASGroup);

    describeAutoScalingGroupsResult.setAutoScalingGroups(autoScalingGroups);
    when(amazonAutoScalingRetryClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class)))
            .thenReturn(describeAutoScalingGroupsResult);
    when(awsClient.createAutoScalingRetryClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingRetryClient);
    when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationRetryClient);
    when(awsClient.createAccess(any(), any())).thenReturn(new AmazonEC2Client());

    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationRetryClient.class), eq("worker")))
            .thenReturn("workerASG");
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationRetryClient.class), eq("master")))
            .thenReturn("masterASG");

    AuthenticatedContext authenticatedContext = new AuthenticatedContext(new CloudContext(1L, "teststack", "AWS", "AWS",
            Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a")), "1", "1"), new CloudCredential());

    ArrayList<CloudResource> allInstances = new ArrayList<>();
    allInstances.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker1").group("worker").instanceId("i-worker1").build());
    allInstances.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker2").group("worker").instanceId("i-worker2").build());
    allInstances.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker3").group("worker").instanceId("i-worker3").build());
    CloudResource workerInstance4 = CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker4").group("worker").instanceId("i-worker4").build();
    allInstances.add(workerInstance4);
    CloudResource workerInstance5 = CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker5").group("worker").instanceId("i-worker5").build();
    allInstances.add(workerInstance5);
    when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationRetryClient), eq(amazonAutoScalingRetryClient), anyList()))
            .thenReturn(allInstances);

    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();

    Group master = getMasterGroup(instanceAuthentication);
    groups.add(master);

    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);

    Map<String, String> tags = new HashMap<>();
    tags.put("owner", "cbuser");
    tags.put("created", "yesterday");
    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), tags, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);

    List<CloudResource> cloudResourceList = Collections.emptyList();
    awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList);
    verify(awsAutoScalingService, times(1)).updateAutoscalingGroup(any(AmazonAutoScalingRetryClient.class), eq("workerASG"), eq(5));
    verify(awsAutoScalingService, times(1)).scheduleStatusChecks(eq(List.of(worker)), eq(authenticatedContext),  eq(amazonCloudFormationRetryClient), any());
    verify(awsAutoScalingService, times(1)).suspendAutoScaling(eq(authenticatedContext), eq(cloudStack));
    ArgumentCaptor<List<CloudResource>> captor = ArgumentCaptor.forClass(List.class);
    verify(awsComputeResourceService, times(1))
            .buildComputeResourcesForUpscale(eq(authenticatedContext), eq(cloudStack), anyList(), captor.capture(), any(), any());
    verify(awsTaggingService, times(1)).tagRootVolumes(eq(authenticatedContext), any(AmazonEC2Client.class), eq(allInstances), eq(tags));
    verify(awsCloudWatchService, times(1)).addCloudWatchAlarmsForSystemFailures(any(), eq(cloudStack), eq("eu-west-1"),
            any(AwsCredentialView.class));
    List<CloudResource> newInstances = captor.getValue();
    assertEquals("Two new instances should be created", 2, newInstances.size());
    assertThat(newInstances, hasItem(workerInstance4));
    assertThat(newInstances, hasItem(workerInstance5));
}
 
Example #18
Source File: AwsUpscaleServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
void upscaleAwsASGroupFail() throws AmazonAutoscalingFailed {
    AmazonAutoScalingRetryClient amazonAutoScalingRetryClient = mock(AmazonAutoScalingRetryClient.class);
    AmazonCloudFormationRetryClient amazonCloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class);
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    List<AutoScalingGroup> autoScalingGroups = new ArrayList<>();

    AutoScalingGroup masterASGroup = new AutoScalingGroup();
    masterASGroup.setAutoScalingGroupName("masterASG");
    List<Instance> masterASGInstances = new ArrayList<>();
    masterASGInstances.add(new Instance().withInstanceId("i-master1"));
    masterASGInstances.add(new Instance().withInstanceId("i-master2"));
    masterASGroup.setInstances(masterASGInstances);

    AutoScalingGroup workerASGroup = new AutoScalingGroup();
    workerASGroup.setAutoScalingGroupName("workerASG");
    List<Instance> workerASGInstances = new ArrayList<>();
    workerASGInstances.add(new Instance().withInstanceId("i-worker1"));
    workerASGInstances.add(new Instance().withInstanceId("i-worker2"));
    workerASGInstances.add(new Instance().withInstanceId("i-worker3"));
    workerASGroup.setInstances(workerASGInstances);

    autoScalingGroups.add(masterASGroup);
    autoScalingGroups.add(workerASGroup);

    describeAutoScalingGroupsResult.setAutoScalingGroups(autoScalingGroups);
    when(amazonAutoScalingRetryClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class)))
            .thenReturn(describeAutoScalingGroupsResult);
    when(awsClient.createAutoScalingRetryClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingRetryClient);
    when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationRetryClient);

    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationRetryClient.class), eq("worker")))
            .thenReturn("workerASG");
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationRetryClient.class), eq("master")))
            .thenReturn("masterASG");

    AuthenticatedContext authenticatedContext = new AuthenticatedContext(new CloudContext(1L, "teststack", "AWS", "AWS",
            Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a")), "1", "1"), new CloudCredential());

    List<CloudResource> allInstances = new ArrayList<>();
    allInstances.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker1").group("worker").instanceId("i-worker1").build());
    allInstances.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker2").group("worker").instanceId("i-worker2").build());
    allInstances.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker3").group("worker").instanceId("i-worker3").build());
    CloudResource workerInstance4 = CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker4").group("worker").instanceId("i-worker4").build();
    allInstances.add(workerInstance4);
    CloudResource workerInstance5 = CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED)
            .name("worker5").group("worker").instanceId("i-worker5").build();
    allInstances.add(workerInstance5);
    when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationRetryClient), eq(amazonAutoScalingRetryClient), anyList()))
            .thenReturn(allInstances);

    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();

    Group master = getMasterGroup(instanceAuthentication);
    groups.add(master);

    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);

    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);

    List<CloudResource> cloudResourceList = Collections.emptyList();

    AutoScalingGroup newWorkerASGroup = new AutoScalingGroup();
    newWorkerASGroup.setAutoScalingGroupName("workerASG");
    List<Instance> newWorkerASGInstances = new ArrayList<>();
    newWorkerASGInstances.add(new Instance().withInstanceId("i-worker1"));
    newWorkerASGInstances.add(new Instance().withInstanceId("i-worker2"));
    newWorkerASGInstances.add(new Instance().withInstanceId("i-worker3"));
    newWorkerASGInstances.add(new Instance().withInstanceId("i-worker4"));
    newWorkerASGInstances.add(new Instance().withInstanceId("i-worker5"));
    newWorkerASGroup.setInstances(newWorkerASGInstances);

    when(awsAutoScalingService.getAutoscalingGroups(eq(amazonAutoScalingRetryClient), any()))
            .thenReturn(Collections.singletonList(newWorkerASGroup));

    doThrow(new AmazonAutoscalingFailed("autoscaling failed"))
            .when(awsAutoScalingService).scheduleStatusChecks(eq(List.of(worker)),
            eq(authenticatedContext), eq(amazonCloudFormationRetryClient), any(Date.class));

    assertThrows(CloudConnectorException.class, () -> awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList),
            "Autoscaling group update failed: 'autoscaling failed' Original autoscaling group state has been recovered.");
    verify(awsAutoScalingService, times(1)).updateAutoscalingGroup(any(AmazonAutoScalingRetryClient.class), eq("workerASG"), eq(5));
    verify(awsAutoScalingService, times(1)).scheduleStatusChecks(eq(List.of(worker)), eq(authenticatedContext),  eq(amazonCloudFormationRetryClient), any());
    verify(awsComputeResourceService, times(0)).buildComputeResourcesForUpscale(eq(authenticatedContext), eq(cloudStack),
            anyList(), anyList(), any(), any());
    verify(awsAutoScalingService, times(1)).suspendAutoScaling(eq(authenticatedContext), eq(cloudStack));
    verify(awsAutoScalingService, times(1)).terminateInstance(eq(amazonAutoScalingRetryClient), eq("i-worker4"));
    verify(awsAutoScalingService, times(1)).terminateInstance(eq(amazonAutoScalingRetryClient), eq("i-worker5"));
    Map<String, Integer> desiredGroups = new HashMap<>();
    desiredGroups.put("workerASG", 3);
    desiredGroups.put("masterASG", 2);
    verify(awsAutoScalingService, times(1)).scheduleStatusChecks(eq(desiredGroups), eq(authenticatedContext), any(Date.class));
}
 
Example #19
Source File: ASGInventoryUtil.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch launch configurations.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<LaunchConfiguration>> fetchLaunchConfigurations(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	
	AmazonAutoScaling asgClient;
	Map<String,List<LaunchConfiguration>> launchConfigurationList = new LinkedHashMap<>();
	List<String> launchConfigurationNames = new ArrayList<>();
	
	String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"ASG\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){ 
		try{
			if(!skipRegions.contains(region.getName())){ //!skipRegions
				List<LaunchConfiguration> launchConfigurationListTemp = new ArrayList<>();
				asgClient = AmazonAutoScalingClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				String nextToken = null;
				DescribeAutoScalingGroupsResult  describeResult ;
				do{
					describeResult =  asgClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withNextToken(nextToken).withMaxRecords(asgMaxRecord));
					for(AutoScalingGroup _asg : describeResult.getAutoScalingGroups()) {
						launchConfigurationNames.add(_asg.getLaunchConfigurationName());
					}
					nextToken = describeResult.getNextToken();
				}while(nextToken!=null);
				List<String> launchConfigurationNamesTemp = new ArrayList<>();
				
				for(int i =0 ; i<launchConfigurationNames.size();i++){
					launchConfigurationNamesTemp.add(launchConfigurationNames.get(i));
					if((i+1)%50==0 || i == launchConfigurationNames.size()-1){
						launchConfigurationListTemp.addAll(asgClient.describeLaunchConfigurations(new DescribeLaunchConfigurationsRequest().withLaunchConfigurationNames(launchConfigurationNamesTemp)).getLaunchConfigurations());
						launchConfigurationNamesTemp = new ArrayList<>();
					}
					
				}
				
				if(!launchConfigurationListTemp.isEmpty() ){
					log.debug("Account : " + accountId + " Type : ASG Launch Configurations "+region.getName()+" >> " + launchConfigurationListTemp.size());
					launchConfigurationList.put(accountId+delimiter+accountName+delimiter+region.getName(), launchConfigurationListTemp);
				}
		   	}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"launchconfig",e.getMessage());
		}
	}
	return launchConfigurationList;
}