@SuppressWarnings("static-access") @Test public void fetchLaunchConfigurationsTest_Exception() throws Exception { PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenThrow(new Exception()); assertThat(asgInventoryUtil.fetchLaunchConfigurations(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(0)); }
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())){ 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; }
ASGInventoryUtil { 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())){ 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; } }
ASGInventoryUtil { 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())){ 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; } private  ASGInventoryUtil(); }
ASGInventoryUtil { 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())){ 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; } private  ASGInventoryUtil(); static Map<String,List<LaunchConfiguration>> fetchLaunchConfigurations(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<ScalingPolicy>> fetchScalingPolicies(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); }
ASGInventoryUtil { 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())){ 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; } private  ASGInventoryUtil(); static Map<String,List<LaunchConfiguration>> fetchLaunchConfigurations(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<ScalingPolicy>> fetchScalingPolicies(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName);  }