@SuppressWarnings("static-access") @Test public void fetchScalingPoliciesTest() throws Exception { mockStatic(AmazonAutoScalingClientBuilder.class); AmazonAutoScaling asgClient = PowerMockito.mock(AmazonAutoScaling.class); AmazonAutoScalingClientBuilder amazonAutoScalingClientBuilder = PowerMockito.mock(AmazonAutoScalingClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonAutoScalingClientBuilder.standard()).thenReturn(amazonAutoScalingClientBuilder); when(amazonAutoScalingClientBuilder.withCredentials(anyObject())).thenReturn(amazonAutoScalingClientBuilder); when(amazonAutoScalingClientBuilder.withRegion(anyString())).thenReturn(amazonAutoScalingClientBuilder); when(amazonAutoScalingClientBuilder.build()).thenReturn(asgClient); DescribeAutoScalingGroupsResult autoScalingGroupsResult = new DescribeAutoScalingGroupsResult(); List<AutoScalingGroup> asgList = new ArrayList<>(); AutoScalingGroup autoScalingGroup = new AutoScalingGroup(); autoScalingGroup.setAutoScalingGroupName("autoScalingGrpName"); asgList.add(autoScalingGroup); autoScalingGroupsResult.setAutoScalingGroups(asgList); when(asgClient.describeAutoScalingGroups(anyObject())).thenReturn(autoScalingGroupsResult); DescribePoliciesResult policiesResult = new DescribePoliciesResult(); List<ScalingPolicy> scalingPolicies = new ArrayList<>(); scalingPolicies.add(new ScalingPolicy()); policiesResult.setScalingPolicies(scalingPolicies); when(asgClient.describePolicies(anyObject())).thenReturn(policiesResult); assertThat(asgInventoryUtil.fetchScalingPolicies(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
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())){ 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; }
ASGInventoryUtil { 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())){ 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; } }
ASGInventoryUtil { 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())){ 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; } private  ASGInventoryUtil(); }
ASGInventoryUtil { 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())){ 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; } 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<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())){ 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; } 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);  }