public RouteTableDTO(final RouteTable routeTable) { this.routeTableId = routeTable.getRouteTableId(); this.vpcId = routeTable.getVpcId(); this.routes.addAll( routeTable.getRoutes() .stream() .map(RouteDTO::new) .collect(Collectors.toList())); this.tags.addAll( routeTable.getTags() .stream() .map(TagDTO::new) .collect(Collectors.toList())); this.associations.addAll( routeTable.getAssociations() .stream() .map(RouteTableAssociationDTO::new) .collect(Collectors.toList())); this.propagatingVgws.addAll( routeTable.getPropagatingVgws() .stream() .map(PropagatingVgwDTO::new) .collect(Collectors.toList())); }
/** * Get the main route table for a given VPC */ public RouteTable getMainRouteTable(String vpcId) { // build filter list List<Filter> filters = new ArrayList<>(); filters.add(AWSUtils.getFilter(AWSUtils.AWS_FILTER_VPC_ID, vpcId)); filters.add(AWSUtils.getFilter(AWS_MAIN_ROUTE_ASSOCIATION, "true")); DescribeRouteTablesRequest req = new DescribeRouteTablesRequest() .withFilters(filters); DescribeRouteTablesResult res = this.client.describeRouteTables(req); List<RouteTable> routeTables = res.getRouteTables(); return routeTables.isEmpty() ? null : routeTables.get(0); }
/** * Create a route table */ public DeferredResult<String> createRouteTable(String vpcId) { CreateRouteTableRequest req = new CreateRouteTableRequest() .withVpcId(vpcId); String message = "Create AWS Route Table on VPC [" + vpcId + "]."; AWSDeferredResultAsyncHandler<CreateRouteTableRequest, CreateRouteTableResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.createRouteTableAsync(req, handler); return handler.toDeferredResult() .thenApply(CreateRouteTableResult::getRouteTable) .thenApply(RouteTable::getRouteTableId); }
/** * Update the main route table information for the VPC that is being mapped to a network * state. Query AWS for the main route tables with a list of VPCs. From the result set find * the relevant route table Id and upda */ @Override protected void consumeSuccess(DescribeRouteTablesRequest request, DescribeRouteTablesResult result) { for (RouteTable routeTable : result.getRouteTables()) { if (this.context.vpcs.containsKey(routeTable.getVpcId())) { NetworkState networkStateToUpdate = this.context.vpcs .get(routeTable.getVpcId()); networkStateToUpdate.customProperties.put(AWS_VPC_ROUTE_TABLE_ID, routeTable.getRouteTableId()); this.context.vpcs.put(routeTable.getVpcId(), networkStateToUpdate); } } }
@Test public void testGetMainRouteTable() throws Throwable { Vpc defVPC = this.netClient.getDefaultVPC(); assertTrue(defVPC != null); RouteTable routeTable = this.netClient.getMainRouteTable(defVPC.getVpcId()); assertTrue(routeTable != null); }
@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); }
public VpcDetailDTO(final Vpc vpc, final List<Subnet> subnets, final List<RouteTable> routeTables) { super(vpc); final Map<String, SubnetDetailDTO> subnetDetails = new HashMap<>(); subnetDetails.putAll( subnets.stream() .map(SubnetDetailDTO::new) .collect(Collectors.toMap(s -> s.getSubnetId(), identity()))); LOG.trace("Details map: {}", subnetDetails); routeTables.stream() .map(RouteTableDTO::new) .forEach(rt -> rt.getAssociations().forEach(assoc -> { SubnetDetailDTO dto = subnetDetails.get(assoc.getSubnetId()); if (dto == null) { if (LOG.isTraceEnabled()) { LOG.trace("RT: {}, Assoc.SubnetID: {}, Assocs: {}", rt.getRouteTableId(), assoc.getSubnetId(), rt.getAssociations()); } return; } dto.setRouteTableId(rt.getRouteTableId()); dto.getRoutes().addAll(rt.getRoutes()); })); this.subnets.addAll(subnetDetails.values()); }
public SubnetDetailDTO(final Subnet subnet, final RouteTable routeTable) { super(subnet); routeTableId = routeTable.getRouteTableId(); routes.addAll( routeTable.getRoutes() .stream() .map(RouteDTO::new) .collect(Collectors.toList())); }
@Override @Cacheable(value = CachingConfiguration.ROUTE_TABLE_CACHE, key = "#vpcId", condition = "#bypassCache == false") public List<RouteTable> getRouteTablesForVpcInRegion(final String vpcId, final String region, boolean bypassCache) { LOG.info("Retrieving route tables for VPC {} in region {} ({})", vpcId, region, bypassCache); DescribeRouteTablesRequest request = new DescribeRouteTablesRequest() .withFilters(new Filter() .withName("vpc-id") .withValues(vpcId)); DescribeRouteTablesResult result = getClientForRegion(region).describeRouteTables(request); return result.getRouteTables(); }
public List<AbstractResource<?>> toVpcRouteTables(List<RouteTable> routeTables, String accountId, Region region, DateTime dt) { List<AbstractResource<?>> resources = new ArrayList<>(); for (RouteTable routeTable : routeTables) { VpcRouteTable vpcRouteTable = new VpcRouteTable(); conf(vpcRouteTable, accountId, region, dt); vpcRouteTable.setResource(routeTable); resources.add(vpcRouteTable); } log.debug("{} route tables found via api and converted to VpcRouteTable", resources.size()); return resources; }
/** * Checks if routes between the selected VPCs already exist * * @param vpnEndpoints * @return */ private boolean checkIfRoutesExist(List<VPNEndpoint> vpnEndpoints) { boolean routesExist = false; for (VPNEndpoint vpnEndpoint : vpnEndpoints) { ec2Client.setEndpoint(vpnEndpoint.getRegion().getEndpoint()); DescribeRouteTablesResult descRouteTableResult = ec2Client.describeRouteTables(); List<RouteTable> routeTables = descRouteTableResult.getRouteTables(); for (RouteTable routeTable : routeTables) { if (routeTable.getVpcId().equals(vpnEndpoint.getVpc().getVpcId())) { List<Route> routes = routeTable.getRoutes(); for (Route route : routes) { for (VPNEndpoint extVpnEndpoint : vpnEndpoints) { if (!vpnEndpoint.equals(extVpnEndpoint)) { LOG.debug("Checking if route allows requested traffic: " + route); if (route.getDestinationCidrBlock().endsWith(extVpnEndpoint.getVpc().getCidrBlock())) { routesExist = true; LOG.error("A route already exists between " + vpnEndpoint.getVpc().getCidrBlock() + " and " + extVpnEndpoint.getVpc().getCidrBlock()); } } } } } } } return routesExist; }
/** * Create routes * * @param vpnEndpoints */ private void createAndAssociateRoutes(List<VPNEndpoint> vpnEndpoints) { for (VPNEndpoint vpnEndpoint : vpnEndpoints) { ec2Client.setEndpoint(vpnEndpoint.getRegion().getEndpoint()); for (VPNEndpoint extVpnEndpoint : vpnEndpoints) { if (!vpnEndpoint.equals(extVpnEndpoint)) { // Get route tables DescribeRouteTablesResult descRouteTablesResult = ec2Client.describeRouteTables(); List<RouteTable> routeTables = descRouteTablesResult.getRouteTables(); for (RouteTable routeTable : routeTables) { if (routeTable.getVpcId().equals(vpnEndpoint.getVpc().getVpcId())) { // Create the route CreateRouteRequest createRouteReq = new CreateRouteRequest(); createRouteReq.setDestinationCidrBlock(extVpnEndpoint.getVpc().getCidrBlock()); createRouteReq.setInstanceId(vpnEndpoint.getInstance().getInstanceId()); createRouteReq.setRouteTableId(routeTable.getRouteTableId()); LOG.debug("About to create a route in " + vpnEndpoint.getVpc().getVpcId() + " to " + extVpnEndpoint.getVpc().getVpcId() + " in route table: " + routeTable.getRouteTableId()); ec2Client.createRoute(createRouteReq); LOG.debug("Created route in " + vpnEndpoint.getVpc().getVpcId() + " to " + extVpnEndpoint.getVpc().getVpcId() + " in route table: " + routeTable.getRouteTableId()); } } } } } }
public HashMap<Region, List> listRegionRouteTables(AWSCredentials awsCreds) { AmazonEC2Client ec2Client = new AmazonEC2Client(awsCreds); List<Region> regions = new ArrayList(); DescribeRegionsResult descRegionsResult = ec2Client.describeRegions(); if (descRegionsResult != null) { regions = descRegionsResult.getRegions(); } HashMap<Region, List> regionRouteTablesMap = new HashMap(); ExecutorService listRouteTablesExecutor = Executors.newFixedThreadPool(8); for (Region region : regions) { List<RouteTable> routeTables = new ArrayList(); regionRouteTablesMap.put(region, routeTables); Runnable worker = new ListRouteTableRunnable(awsCreds, region, routeTables); listRouteTablesExecutor.execute(worker); } listRouteTablesExecutor.shutdown(); try { listRouteTablesExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.error("Caught InterruptedException: " + e.getMessage()); } return regionRouteTablesMap; }
public ListRouteTableRunnable(AWSCredentials awsCreds, Region region, List<RouteTable> routeTables) { this.region = region; this.routeTables = routeTables; ec2Client = new AmazonEC2Client(awsCreds); ec2Client.setEndpoint(region.getEndpoint()); LOG.debug("Set endpoint to " + region.getEndpoint()); }
private void visitRouteTable(VPCDiagramBuilder vpcDiagram, RouteTable routeTable) throws CfnAssistException { logger.debug("visit routetable " + routeTable.getRouteTableId()); List<Route> routes = routeTable.getRoutes(); List<RouteTableAssociation> usersOfTable = routeTable.getAssociations(); for (RouteTableAssociation usedBy : usersOfTable) { String subnetId = usedBy.getSubnetId(); // can subnet ever be null in an association? if (subnetId!=null) { vpcDiagram.addAsssociatedRouteTable(routeTable, subnetId); // possible duplication if route table reused? for (Route route : routes) { vpcDiagram.addRoute(routeTable.getRouteTableId(), subnetId, route); } } } }
public void addRouteTable(RouteTable routeTable) throws CfnAssistException { String name = AmazonVPCFacade.getNameFromTags(routeTable.getTags()); String routeTableId = routeTable.getRouteTableId(); String label = AmazonVPCFacade.createLabelFromNameAndID(routeTableId, name); String diagramIdForTable = formRouteTableIdForDiagram(id, routeTableId); networkChildDiagram.addRouteTable(diagramIdForTable, label); }
/** * Test describing route table. */ @Test(timeout = TIMEOUT_LEVEL1) public final void describeRouteTableTest() { log.info("Start describing route table test"); createRouteTableTest(); RouteTable routeTable = getRouteTable(); Assert.assertNotNull("route table should not be null", routeTable); Assert.assertNotNull("route table id should not be null", routeTable.getRouteTableId()); Assert.assertTrue("route table should be deleted", deleteRouteTable(routeTable.getRouteTableId())); }
/** * Test create route table. */ @Test(timeout = TIMEOUT_LEVEL1) public final void createRouteTableTest() { log.info("Start create route table test"); Vpc vpc = createVpc(MOCK_CIDR_BLOCK, PROPERTY_TENANCY); RouteTable routeTable = createRouteTable(vpc.getVpcId()); Assert.assertNotNull("route table should not be null", routeTable); Assert.assertNotNull("route table id should not be null", routeTable.getRouteTableId()); }
/** * Test Delete route table. */ @Test(timeout = TIMEOUT_LEVEL1) public final void deleteRouteTableTest() { log.info("Start delete route table test"); Vpc vpc = createVpc(MOCK_CIDR_BLOCK, PROPERTY_TENANCY); RouteTable routeTable = createRouteTable(vpc.getVpcId()); Assert.assertNotNull("route table should not be null", routeTable); Assert.assertNotNull("route table id should not be null", routeTable.getRouteTableId()); Assert.assertTrue("route table should be deleted", deleteRouteTable(routeTable.getRouteTableId())); }
/** * 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 route table. * * @return RouteTable */ protected final RouteTable getRouteTable() { RouteTable routeTable = null; DescribeRouteTablesRequest req = new DescribeRouteTablesRequest(); DescribeRouteTablesResult result = amazonEC2Client.describeRouteTables(req); if (result != null && !result.getRouteTables().isEmpty()) { routeTable = result.getRouteTables().get(0); } return routeTable; }
/** * Create route table. * * @param vpcId the vpc id * @return RouteTable */ protected final RouteTable createRouteTable(final String vpcId) { RouteTable routeTable = null; CreateRouteTableRequest req = new CreateRouteTableRequest(); req.setVpcId(vpcId); CreateRouteTableResult result = amazonEC2Client.createRouteTable(req); if (result != null) { routeTable = result.getRouteTable(); } return routeTable; }
public List<RouteTable> describeRouteTables(Collection<String> routeTableIds) { logger.info("describe route tables, routeTableIds={}", routeTableIds); return ec2.describeRouteTables(new DescribeRouteTablesRequest().withRouteTableIds(routeTableIds)).getRouteTables(); }
public List<RouteTable> getRouteTablesFor(String vpcId) { return cloudRepository.getRouteTablesForVPC(vpcId); }
public void addAsssociatedRouteTable(RouteTable routeTable, String subnetId) throws CfnAssistException { SubnetDiagramBuilder subnetDiagram = subnetDiagramBuilders.get(subnetId); subnetDiagram.addRouteTable(routeTable); }
@Test public void shouldWalkVPCAndAddItemsForDiagram() throws CfnAssistException { Vpc vpc = vpcBuilder.setFacadeVisitExpections(awsFacade); String instanceSubnetId = vpcBuilder.getSubnetId(); Subnet instanceSubnet = vpcBuilder.getSubnet(); String dbSubnetId = vpcBuilder.getDbSubnetId(); Subnet dbSubnet = vpcBuilder.getDbSubnet(); Address eip = vpcBuilder.getEip(); LoadBalancerDescription elb = vpcBuilder.getElb(); DBInstance dbInstance = vpcBuilder.getDbInstance(); Instance instance = vpcBuilder.getInstance(); String instanceId = instance.getInstanceId(); RouteTable routeTable = vpcBuilder.getRouteTable(); NetworkAcl acl = vpcBuilder.getAcl(); NetworkAclEntry outboundEntry = vpcBuilder.getOutboundEntry(); NetworkAclEntry inboundEntry = vpcBuilder.getInboundEntry(); SecurityGroup instanceSecurityGroup = vpcBuilder.getInstanceSecurityGroup(); IpPermission instanceIpPermsInbound = vpcBuilder.getInstanceIpPermsInbound(); IpPermission instanceIpPermsOutbound = vpcBuilder.getInstanceIpPermsOutbound(); SecurityGroup dbSecurityGroup = vpcBuilder.getDBSecurityGroup(); IpPermission dbIpPermsInbound = vpcBuilder.getDbIpPermsInbound(); IpPermission dbIpPermsOutbound = vpcBuilder.getDbIpPermsOutbound(); SecurityGroup elbSecurityGroup = vpcBuilder.getElbSecurityGroup(); EasyMock.expect(diagramFactory.createVPCDiagramBuilder(vpc)).andReturn(vpcDiagramBuilder); EasyMock.expect(diagramFactory.createSubnetDiagramBuilder(vpcDiagramBuilder, instanceSubnet)).andReturn(subnetDiagramBuilder); EasyMock.expect(diagramFactory.createSubnetDiagramBuilder(vpcDiagramBuilder, dbSubnet)).andReturn(dbSubnetDiagramBuilder); subnetDiagramBuilder.add(instance); vpcDiagramBuilder.add(instanceSubnetId, subnetDiagramBuilder); vpcDiagramBuilder.add(dbSubnetId, dbSubnetDiagramBuilder); // route table & routes vpcDiagramBuilder.addAsssociatedRouteTable(routeTable, instanceSubnetId); vpcDiagramBuilder.addRoute(routeTable.getRouteTableId(), instanceSubnetId, vpcBuilder.getRouteA()); vpcDiagramBuilder.addRoute(routeTable.getRouteTableId(), instanceSubnetId, vpcBuilder.getRouteB()); vpcDiagramBuilder.addRoute(routeTable.getRouteTableId(), instanceSubnetId, vpcBuilder.getRouteC()); vpcDiagramBuilder.addAsssociatedRouteTable(routeTable, dbSubnetId); vpcDiagramBuilder.addRoute(routeTable.getRouteTableId(), dbSubnetId, vpcBuilder.getRouteA()); vpcDiagramBuilder.addRoute(routeTable.getRouteTableId(), dbSubnetId, vpcBuilder.getRouteB()); vpcDiagramBuilder.addRoute(routeTable.getRouteTableId(), dbSubnetId, vpcBuilder.getRouteC()); // eip vpcDiagramBuilder.addEIP(eip); vpcDiagramBuilder.linkEIPToInstance(eip.getPublicIp(), instanceId); // elb vpcDiagramBuilder.addELB(elb); vpcDiagramBuilder.associateELBToInstance(elb, instanceId); vpcDiagramBuilder.associateELBToSubnet(elb, instanceSubnetId); vpcDiagramBuilder.associateELBToSubnet(elb, dbSubnetId); vpcDiagramBuilder.addSecurityGroup(elbSecurityGroup); vpcDiagramBuilder.associateInstanceWithSecGroup(elb.getDNSName(), elbSecurityGroup); vpcDiagramBuilder.addSecGroupInboundPerms("secElbGroupId", vpcBuilder.getElbIpPermsInbound()); vpcDiagramBuilder.addSecGroupOutboundPerms("secElbGroupId", vpcBuilder.getElbIpPermsOutbound()); // db vpcDiagramBuilder.addDBInstance(dbInstance); vpcDiagramBuilder.associateDBWithSubnet(dbInstance, dbSubnetId); vpcDiagramBuilder.addSecurityGroup(dbSecurityGroup); vpcDiagramBuilder.associateInstanceWithSecGroup(dbInstance.getDBInstanceIdentifier(), dbSecurityGroup); vpcDiagramBuilder.addSecGroupInboundPerms("secDbGroupId",dbIpPermsInbound); vpcDiagramBuilder.addSecGroupOutboundPerms("secDbGroupId",dbIpPermsOutbound); // acl vpcDiagramBuilder.addAcl(acl); vpcDiagramBuilder.associateAclWithSubnet(acl, instanceSubnetId); vpcDiagramBuilder.addACLOutbound("aclId",outboundEntry, instanceSubnetId); vpcDiagramBuilder.addACLInbound("aclId", inboundEntry, instanceSubnetId); // sec group vpcDiagramBuilder.addSecurityGroup(instanceSecurityGroup, instanceSubnetId); vpcDiagramBuilder.associateInstanceWithSecGroup(instanceId, instanceSecurityGroup); vpcDiagramBuilder.addSecGroupInboundPerms("secGroupId",instanceIpPermsInbound, instanceSubnetId); vpcDiagramBuilder.addSecGroupOutboundPerms("secGroupId",instanceIpPermsOutbound, instanceSubnetId); diagramBuilder.add(vpcDiagramBuilder); replayAll(); VPCVisitor visitor = new VPCVisitor(diagramBuilder, awsFacade, diagramFactory); visitor.visit(vpc); verifyAll(); }
/** * Gets the route tables that exist in the given VPC. * * @param vpcId the identifier of the VPC to retrieve route tables for * @param region the AWS region the VPC exists in * @param bypassCache whether or not the cache should be ignored * @return the non-null list of route tables in the VPC */ List<RouteTable> getRouteTablesForVpcInRegion(@NotBlank String vpcId, @NotBlank String region, boolean bypassCache);