Java Code Examples for com.amazonaws.services.autoscaling.AmazonAutoScaling#describeAutoScalingGroups()

The following examples show how to use com.amazonaws.services.autoscaling.AmazonAutoScaling#describeAutoScalingGroups() . 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: AWSMembership.java    From Raigad with Apache License 2.0 6 votes vote down vote up
/**
 * Actual membership AWS source of truth...
 */
@Override
public int getRacMembershipSize() {
    AmazonAutoScaling client = null;

    try {
        client = getAutoScalingClient();
        DescribeAutoScalingGroupsRequest asgReq = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(config.getASGName());
        DescribeAutoScalingGroupsResult res = client.describeAutoScalingGroups(asgReq);

        int size = 0;
        for (AutoScalingGroup asg : res.getAutoScalingGroups()) {
            size += asg.getMaxSize();
        }

        logger.info(String.format("Query on ASG returning %d instances", size));

        return size;
    } finally {
        if (client != null) {
            client.shutdown();
        }
    }
}
 
Example 2
Source File: AWSMembership.java    From Raigad with Apache License 2.0 6 votes vote down vote up
@Override
public void expandRacMembership(int count) {
    AmazonAutoScaling client = null;

    try {
        client = getAutoScalingClient();
        DescribeAutoScalingGroupsRequest asgReq = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(config.getASGName());
        DescribeAutoScalingGroupsResult res = client.describeAutoScalingGroups(asgReq);
        AutoScalingGroup asg = res.getAutoScalingGroups().get(0);
        UpdateAutoScalingGroupRequest ureq = new UpdateAutoScalingGroupRequest();
        ureq.setAutoScalingGroupName(asg.getAutoScalingGroupName());
        ureq.setMinSize(asg.getMinSize() + 1);
        ureq.setMaxSize(asg.getMinSize() + 1);
        ureq.setDesiredCapacity(asg.getMinSize() + 1);
        client.updateAutoScalingGroup(ureq);
    } finally {
        if (client != null) {
            client.shutdown();
        }
    }
}
 
Example 3
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 4
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 5
Source File: AWSMembership.java    From Raigad with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, List<String>> getRacMembership(Collection<String> autoScalingGroupNames) {
    if (CollectionUtils.isEmpty(autoScalingGroupNames)) {
        return Collections.emptyMap();
    }

    AmazonAutoScaling client = null;

    try {
        client = getAutoScalingClient();
        DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest =
                new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(autoScalingGroupNames);
        DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = client.describeAutoScalingGroups(describeAutoScalingGroupsRequest);

        Map<String, List<String>> asgs = new HashMap<>();
        for (AutoScalingGroup autoScalingGroup : describeAutoScalingGroupsResult.getAutoScalingGroups()) {
            List<String> asgInstanceIds = Lists.newArrayList();
            for (Instance asgInstance : autoScalingGroup.getInstances()) {
                if (!(asgInstance.getLifecycleState().equalsIgnoreCase("terminating") ||
                        asgInstance.getLifecycleState().equalsIgnoreCase("shutting-down") ||
                        asgInstance.getLifecycleState().equalsIgnoreCase("terminated"))) {
                    asgInstanceIds.add(asgInstance.getInstanceId());
                }
            }
            asgs.put(autoScalingGroup.getAutoScalingGroupName(), asgInstanceIds);
            logger.info("AWS returned the following instance ID's for {} ASG: {}",
                    autoScalingGroup.getAutoScalingGroupName(),
                    StringUtils.join(asgInstanceIds, ","));
        }

        return asgs;
    } finally {
        if (client != null) {
            client.shutdown();
        }
    }
}
 
Example 6
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;
}