/** * Update the Internet gateway information for the VPC in question. For the list of Internet * gateways received based on the vpc filter work through the list of attachments and VPCs * and update the Internet gateway information in the network state that maps to the VPC. */ @Override protected void consumeSuccess(DescribeInternetGatewaysRequest request, DescribeInternetGatewaysResult result) { for (InternetGateway resultGateway : result.getInternetGateways()) { for (InternetGatewayAttachment attachment : resultGateway.getAttachments()) { if (this.context.vpcs.containsKey(attachment.getVpcId())) { NetworkState networkStateToUpdate = this.context.vpcs .get(attachment.getVpcId()); networkStateToUpdate.customProperties.put(AWS_GATEWAY_ID, resultGateway.getInternetGatewayId()); this.context.vpcs.put(attachment.getVpcId(), networkStateToUpdate); } } } }
private void validateExistingIGW(AwsNetworkView awsNetworkView, AmazonEC2Client 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())); } } } }
/** * 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()); }
public InternetGateway getInternetGateway(String resourceId) { DescribeInternetGatewaysRequest req = new DescribeInternetGatewaysRequest() .withInternetGatewayIds(resourceId); DescribeInternetGatewaysResult res = this.client.describeInternetGateways(req); List<InternetGateway> internetGateways = res.getInternetGateways(); return internetGateways.isEmpty() ? null : internetGateways.get(0); }
@Test public void testEnvironmentCreation() throws Throwable { boolean attached = false; String gatewayID = this.netClient.createInternetGateway(); assertTrue(gatewayID != null); String vpcID = this.netClient.createVPC(AWS_DEFAULT_SUBNET_CIDR); assertTrue(vpcID != null); String subnetID = this.netClient.createSubnet(AWS_DEFAULT_SUBNET_CIDR, vpcID).getSubnetId(); this.netClient.attachInternetGateway(vpcID, gatewayID); InternetGateway gw = this.netClient.getInternetGateway(gatewayID); List<InternetGatewayAttachment> attachments = gw.getAttachments(); // ensure we are attached to newly created vpc for (InternetGatewayAttachment attachment : attachments) { if (attachment.getVpcId().equalsIgnoreCase(vpcID)) { attached = true; break; } } assertTrue(attached); RouteTable routeTable = this.netClient.getMainRouteTable(vpcID); this.netClient.createInternetRoute(gatewayID, routeTable.getRouteTableId(), "0.0.0.0/0"); //remove resources this.netClient.detachInternetGateway(vpcID, gatewayID); this.netClient.deleteInternetGateway(gatewayID); this.netClient.deleteSubnet(subnetID); this.netClient.deleteVPC(vpcID); }
@Override public CloudGateWays gateways(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception { AmazonEC2Client ec2Client = awsClient.createAccess(cloudCredential); Map<String, Set<CloudGateWay>> resultCloudGateWayMap = new HashMap<>(); CloudRegions regions = regions(cloudCredential, region, filters); for (Map.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()); List<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); }
public List<AbstractResource<?>> toVpcInternetGateways(List<InternetGateway> internetGateways, String accountId, Region region, DateTime dt) { List<AbstractResource<?>> resources = new ArrayList<>(); for (InternetGateway internetGateway : internetGateways) { VpcInternetGateway vpcInternetGateway = new VpcInternetGateway(); conf(vpcInternetGateway, accountId, region, dt); vpcInternetGateway.setResource(internetGateway); resources.add(vpcInternetGateway); } log.debug("{} internet gateways found via api and converted to VpcInternetGateway", resources.size()); return resources; }
/** * 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())); }
/** * 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()); }
/** * 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())); }
/** * 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())); }
/** * 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()); } } }
/** * 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; }
/** * 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; }
/** * 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; }
public InternetGateway createInternetGateway(String vpcId) { logger.info("create internet gateway, vpcId={}", vpcId); InternetGateway internetGateway = ec2.createInternetGateway().getInternetGateway(); ec2.attachInternetGateway(new AttachInternetGatewayRequest().withVpcId(vpcId).withInternetGatewayId(internetGateway.getInternetGatewayId())); return internetGateway; }