@SuppressWarnings("static-access") @Test public void fetchElasticIPAddressesTest_Exception() throws Exception { PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenThrow(new Exception()); assertThat(ec2InventoryUtil.fetchElasticIPAddresses(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(0)); }
public static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){ Map<String,List<Address>> elasticIPMap = new LinkedHashMap<>(); AmazonEC2 ec2Client ; String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Address> elasticIPList = ec2Client.describeAddresses().getAddresses(); if(!elasticIPList.isEmpty() ) { log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Elastic IP "+ region.getName()+" >> " + elasticIPList.size()); elasticIPMap.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticIPList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"elasticip",e.getMessage()); } } return elasticIPMap; }
EC2InventoryUtil { public static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){ Map<String,List<Address>> elasticIPMap = new LinkedHashMap<>(); AmazonEC2 ec2Client ; String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Address> elasticIPList = ec2Client.describeAddresses().getAddresses(); if(!elasticIPList.isEmpty() ) { log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Elastic IP "+ region.getName()+" >> " + elasticIPList.size()); elasticIPMap.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticIPList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"elasticip",e.getMessage()); } } return elasticIPMap; } }
EC2InventoryUtil { public static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){ Map<String,List<Address>> elasticIPMap = new LinkedHashMap<>(); AmazonEC2 ec2Client ; String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Address> elasticIPList = ec2Client.describeAddresses().getAddresses(); if(!elasticIPList.isEmpty() ) { log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Elastic IP "+ region.getName()+" >> " + elasticIPList.size()); elasticIPMap.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticIPList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"elasticip",e.getMessage()); } } return elasticIPMap; } private  EC2InventoryUtil(); }
EC2InventoryUtil { public static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){ Map<String,List<Address>> elasticIPMap = new LinkedHashMap<>(); AmazonEC2 ec2Client ; String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Address> elasticIPList = ec2Client.describeAddresses().getAddresses(); if(!elasticIPList.isEmpty() ) { log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Elastic IP "+ region.getName()+" >> " + elasticIPList.size()); elasticIPMap.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticIPList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"elasticip",e.getMessage()); } } return elasticIPMap; } private  EC2InventoryUtil(); static Map<String,List<RouteTable>> fetchRouteTables(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<NetworkAcl>> fetchNetworkACL(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<InternetGateway>> fetchInternetGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VpnGateway>> fetchVPNGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<EgressOnlyInternetGateway>> fetchEgressGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<DhcpOptions>> fetchDHCPOptions(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VpcPeeringConnection>> fetchPeeringConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<CustomerGateway>> fetchCustomerGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VpnConnection>> fetchVPNConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<ReservedInstances>> fetchReservedInstances(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<InstanceInformation>> fetchSSMInfo(BasicSessionCredentials temporaryCredentials, String skipRegions, String accountId,String accountName); }
EC2InventoryUtil { public static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){ Map<String,List<Address>> elasticIPMap = new LinkedHashMap<>(); AmazonEC2 ec2Client ; String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Address> elasticIPList = ec2Client.describeAddresses().getAddresses(); if(!elasticIPList.isEmpty() ) { log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Elastic IP "+ region.getName()+" >> " + elasticIPList.size()); elasticIPMap.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticIPList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"elasticip",e.getMessage()); } } return elasticIPMap; } private  EC2InventoryUtil(); static Map<String,List<RouteTable>> fetchRouteTables(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<NetworkAcl>> fetchNetworkACL(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<InternetGateway>> fetchInternetGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VpnGateway>> fetchVPNGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<EgressOnlyInternetGateway>> fetchEgressGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<DhcpOptions>> fetchDHCPOptions(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VpcPeeringConnection>> fetchPeeringConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<CustomerGateway>> fetchCustomerGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VpnConnection>> fetchVPNConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<ReservedInstances>> fetchReservedInstances(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<InstanceInformation>> fetchSSMInfo(BasicSessionCredentials temporaryCredentials, String skipRegions, String accountId,String accountName);  }