com.amazonaws.services.ec2.model.InternetGateway Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.InternetGateway. 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: FileManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Generate internet gateway files.
 *
 * @param internetGatewayMap the internet gateway map
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void generateInternetGatewayFiles(Map<String, List<InternetGateway>> internetGatewayMap) throws IOException {
	String fieldNames;
	String keys;
	fieldNames = "internetGatewayId";
	keys = "discoverydate`accountid`accountname`region`internetgatewayid";
	FileGenerator.generateJson(internetGatewayMap, fieldNames, "aws-internetgateway.data",keys);

	fieldNames = "internetGatewayId`attachments.vpcId`attachments.state";
	keys = "discoverydate`accountid`accountname`region`internetgatewayid`vpcid`state";
	FileGenerator.generateJson(internetGatewayMap, fieldNames, "aws-internetgateway-attachments.data",keys);

	fieldNames = "internetGatewayId`tags.key`tags.value";
	keys = "discoverydate`accountid`accountname`region`internetgatewayid`key`value";
	FileGenerator.generateJson(internetGatewayMap, fieldNames, "aws-internetgateway-tags.data",keys);
}
 
Example #2
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch internet gateway.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<InternetGateway>> fetchInternetGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	
	Map<String,List<InternetGateway>> internetGatewayMap = new LinkedHashMap<>();
	AmazonEC2 ec2Client ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"internetgateway\" , \"region\":\"" ;

	for(Region region : RegionUtils.getRegions()) { 
		try{
			if(!skipRegions.contains(region.getName())){ 
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				List<InternetGateway> internetGatewayList = ec2Client.describeInternetGateways().getInternetGateways();
				
				if(!internetGatewayList.isEmpty() ) {
					log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Internet Gateway "+ region.getName()+" >> " + internetGatewayList.size());
					internetGatewayMap.put(accountId+delimiter+accountName+delimiter+region.getName(), internetGatewayList);
				}
		   	}
		}catch(Exception e){
	   		log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"internetgateway",e.getMessage());
	   	}
	}
	return internetGatewayMap;
}
 
Example #3
Source File: EC2InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch internet gateway test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchInternetGatewayTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeInternetGatewaysResult describeInternetGatewaysResult = new DescribeInternetGatewaysResult();
    List<InternetGateway> internetGatewayList = new ArrayList<>();
    internetGatewayList.add(new InternetGateway());
    describeInternetGatewaysResult.setInternetGateways(internetGatewayList);
    when(ec2Client.describeInternetGateways()).thenReturn(describeInternetGatewaysResult);
    assertThat(ec2InventoryUtil.fetchInternetGateway(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #4
Source File: AwsSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void validateExistingIGW(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    if (awsNetworkView.isExistingIGW()) {
        DescribeInternetGatewaysRequest describeInternetGatewaysRequest = new DescribeInternetGatewaysRequest();
        describeInternetGatewaysRequest.withInternetGatewayIds(awsNetworkView.getExistingIgw());
        DescribeInternetGatewaysResult describeInternetGatewaysResult = amazonEC2Client.describeInternetGateways(describeInternetGatewaysRequest);
        if (describeInternetGatewaysResult.getInternetGateways().size() < 1) {
            throw new CloudConnectorException(String.format(IGW_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingIgw()));
        } else {
            InternetGateway internetGateway = describeInternetGatewaysResult.getInternetGateways().get(0);
            InternetGatewayAttachment attachment = internetGateway.getAttachments().get(0);
            if (attachment != null && !attachment.getVpcId().equals(awsNetworkView.getExistingVpc())) {
                throw new CloudConnectorException(String.format(IGWVPC_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingIgw(),
                        awsNetworkView.getExistingVpc()));
            }
        }
    }
}
 
Example #5
Source File: Ec2NetworkTest.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Test describing vpcs.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void describeAllNetworksTest() {
    log.info("Start describing vpcs test");
    List<Vpc> vpcs = describeVpcs();

    Assert.assertNotNull("vpcs should not be null", vpcs);
    Assert.assertNotNull("vpc id should not be null", vpcs.get(0).getVpcId());
    log.info("Vpc Sizes " + vpcs.size());

    log.info("Start describing vpcs test");
    List<Subnet> subnets = getSubnets();

    Assert.assertNotNull("vpcs should not be null", subnets);
    Assert.assertNotNull("vpc id should not be null", subnets.get(0).getSubnetId());
    log.info("Subnets Sizes " + subnets.size());

    log.info("Start describing vpcs test");
    List<InternetGateway> internetGateways = getInternetGateways();

    Assert.assertNotNull("vpcs should not be null", internetGateways);
    Assert.assertNotNull("vpc id should not be null", internetGateways.get(0).getInternetGatewayId());
    log.info("Subnets Sizes " + internetGateways.size());

}
 
Example #6
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudGateWays gateways(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    AmazonEC2Client ec2Client = awsClient.createAccess(cloudCredential);

    Map<String, Set<CloudGateWay>> resultCloudGateWayMap = new HashMap<>();
    CloudRegions regions = regions(cloudCredential, region, filters, true);

    for (Entry<Region, List<AvailabilityZone>> regionListEntry : regions.getCloudRegions().entrySet()) {
        if (region == null || Strings.isNullOrEmpty(region.value()) || regionListEntry.getKey().value().equals(region.value())) {
            ec2Client.setRegion(RegionUtils.getRegion(regionListEntry.getKey().value()));

            DescribeInternetGatewaysRequest describeInternetGatewaysRequest = new DescribeInternetGatewaysRequest();
            DescribeInternetGatewaysResult describeInternetGatewaysResult = ec2Client.describeInternetGateways(describeInternetGatewaysRequest);

            Set<CloudGateWay> gateWays = new HashSet<>();
            for (InternetGateway internetGateway : describeInternetGatewaysResult.getInternetGateways()) {
                CloudGateWay cloudGateWay = new CloudGateWay();
                cloudGateWay.setId(internetGateway.getInternetGatewayId());
                cloudGateWay.setName(internetGateway.getInternetGatewayId());
                Collection<String> vpcs = new ArrayList<>();
                for (InternetGatewayAttachment internetGatewayAttachment : internetGateway.getAttachments()) {
                    vpcs.add(internetGatewayAttachment.getVpcId());
                }
                Map<String, Object> properties = new HashMap<>();
                properties.put("attachment", vpcs);
                cloudGateWay.setProperties(properties);
                gateWays.add(cloudGateWay);
            }
            for (AvailabilityZone availabilityZone : regionListEntry.getValue()) {
                resultCloudGateWayMap.put(availabilityZone.value(), gateWays);
            }
        }
    }
    return new CloudGateWays(resultCloudGateWayMap);
}
 
Example #7
Source File: Ec2NetworkTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Test describing internet gateway.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void describeInternetGatewayTest() {
    log.info("Start describing internet gateway test");

    createInternetGatewayTest();
    InternetGateway internetGateway = getInternetGateway();

    Assert.assertNotNull("internet gateway should not be null", internetGateway);
    Assert.assertNotNull("internet gateway id should not be null",
            internetGateway.getInternetGatewayId());
    Assert.assertTrue("internet gateway should be deleted", deleteInternetGateway(internetGateway.getInternetGatewayId()));
}
 
Example #8
Source File: Ec2NetworkTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Test create internet gateway.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void createInternetGatewayTest() {
    log.info("create internet gateway test");

    InternetGateway internetGateway = createInternetGateway();

    Assert.assertNotNull("internet gateway should not be null", internetGateway);
    Assert.assertNotNull("internet gateway id should not be null",
            internetGateway.getInternetGatewayId());
}
 
Example #9
Source File: Ec2NetworkTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Test attach internet gateway.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void attachInternetGatewayTest() {
    log.info("Attach internet gateway test");
    
    Vpc vpc = createVpc(MOCK_CIDR_BLOCK, PROPERTY_TENANCY);
    
    InternetGateway internetGateway = createInternetGateway();
    
    Assert.assertTrue("internet gateway should be attached to vpc", attachInternetGateway(internetGateway.getInternetGatewayId(), vpc.getVpcId()));
}
 
Example #10
Source File: Ec2NetworkTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Test delete internet gateway.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void deleteInternetGatewayTest() {
    log.info("delete internet gateway test");

    InternetGateway internetGateway = createInternetGateway();

    Assert.assertNotNull("internet gateway should not be null", internetGateway);
    Assert.assertNotNull("internet gateway id should not be null",
            internetGateway.getInternetGatewayId());
    Assert.assertTrue("internet gateway should be deleted", deleteInternetGateway(internetGateway.getInternetGatewayId()));
}
 
Example #11
Source File: Ec2NetworkTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Test create Volumes.
 */
@Test(timeout = TIMEOUT_LEVEL1)
public final void createNetworkResourcesTest() {
    
    //Create VPCs
    for(int i =0 ; i < 2 ; i++)
    {
        createVpcTest(); 
    }
    
    List<Vpc> vpcs = describeVpcs();
    
    // Create Subnet
    for(Vpc vpc : vpcs) {
        
        for(int j=0; j<2; j++)
        {
            Subnet subnet = createSubnet(MOCK_CIDR_BLOCK, vpc.getVpcId());
            RouteTable routeTable = createRouteTable(vpc.getVpcId());
            InternetGateway internetGateway = createInternetGateway();

            createRoute(routeTable.getRouteTableId(), internetGateway.getInternetGatewayId(), MOCK_CIDR_BLOCK);
            
            attachInternetGateway(internetGateway.getInternetGatewayId(), vpc.getVpcId());
        }
    }
}
 
Example #12
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe internet gateway.
 *
 * @return InternetGateway
 */
protected final InternetGateway getInternetGateway() {
    InternetGateway internetGateway = null;

    DescribeInternetGatewaysRequest req = new DescribeInternetGatewaysRequest();
    DescribeInternetGatewaysResult result = amazonEC2Client.describeInternetGateways(req);
    if (result != null && !result.getInternetGateways().isEmpty()) {
        internetGateway = result.getInternetGateways().get(0);
    }

    return internetGateway;
}
 
Example #13
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
* Describe internet gateways.
*
* @return List of InternetGateway
*/
protected final List<InternetGateway> getInternetGateways() {
   List<InternetGateway> internetGateways = null;
   DescribeInternetGatewaysRequest req = new DescribeInternetGatewaysRequest();
   DescribeInternetGatewaysResult result = amazonEC2Client.describeInternetGateways(req);
   if (result != null && !result.getInternetGateways().isEmpty()) {
       internetGateways = result.getInternetGateways();
   }

   return internetGateways;
}
 
Example #14
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Create internet gateway.
 *
 * @return InternetGateway
 */
protected final InternetGateway createInternetGateway() {
    InternetGateway internetGateway = null;

    CreateInternetGatewayRequest req = new CreateInternetGatewayRequest();
    CreateInternetGatewayResult result = amazonEC2Client.createInternetGateway(req);
    if (result != null) {
        internetGateway = result.getInternetGateway();
    }

    return internetGateway;
}