@SuppressWarnings("static-access") @Test public void fetchDirectConnectionsTest() throws Exception { mockStatic(AmazonDirectConnectClientBuilder.class); AmazonDirectConnectClient amazonDirectConnectClient = PowerMockito.mock(AmazonDirectConnectClient.class); AmazonDirectConnectClientBuilder amazonDirectConnectClientBuilder = PowerMockito.mock(AmazonDirectConnectClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonDirectConnectClientBuilder.standard()).thenReturn(amazonDirectConnectClientBuilder); when(amazonDirectConnectClientBuilder.withCredentials(anyObject())).thenReturn(amazonDirectConnectClientBuilder); when(amazonDirectConnectClientBuilder.withRegion(anyString())).thenReturn(amazonDirectConnectClientBuilder); when(amazonDirectConnectClientBuilder.build()).thenReturn(amazonDirectConnectClient); DescribeConnectionsResult describeConnectionsResult = new DescribeConnectionsResult(); List<Connection> connections = new ArrayList<>(); connections.add(new Connection()); describeConnectionsResult.setConnections(connections); when(amazonDirectConnectClient.describeConnections()).thenReturn(describeConnectionsResult); assertThat(directConnectionInventoryUtil.fetchDirectConnections(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
public static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Connection>> connectionMap = new LinkedHashMap<>(); String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Connection> connectionList = directConnectClient.describeConnections().getConnections(); if(!connectionList.isEmpty() ) { log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + connectionList.size()); connectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), connectionList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"directconnect",e.getMessage()); } } return connectionMap; }
DirectConnectionInventoryUtil { public static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Connection>> connectionMap = new LinkedHashMap<>(); String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Connection> connectionList = directConnectClient.describeConnections().getConnections(); if(!connectionList.isEmpty() ) { log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + connectionList.size()); connectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), connectionList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"directconnect",e.getMessage()); } } return connectionMap; } }
DirectConnectionInventoryUtil { public static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Connection>> connectionMap = new LinkedHashMap<>(); String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Connection> connectionList = directConnectClient.describeConnections().getConnections(); if(!connectionList.isEmpty() ) { log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + connectionList.size()); connectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), connectionList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"directconnect",e.getMessage()); } } return connectionMap; } private  DirectConnectionInventoryUtil(); }
DirectConnectionInventoryUtil { public static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Connection>> connectionMap = new LinkedHashMap<>(); String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Connection> connectionList = directConnectClient.describeConnections().getConnections(); if(!connectionList.isEmpty() ) { log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + connectionList.size()); connectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), connectionList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"directconnect",e.getMessage()); } } return connectionMap; } private  DirectConnectionInventoryUtil(); static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VirtualInterface>> fetchDirectConnectionsVirtualInterfaces(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); }
DirectConnectionInventoryUtil { public static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Connection>> connectionMap = new LinkedHashMap<>(); String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); List<Connection> connectionList = directConnectClient.describeConnections().getConnections(); if(!connectionList.isEmpty() ) { log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + connectionList.size()); connectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), connectionList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"directconnect",e.getMessage()); } } return connectionMap; } private  DirectConnectionInventoryUtil(); static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); static Map<String,List<VirtualInterface>> fetchDirectConnectionsVirtualInterfaces(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName);  }