Search in sources :

Example 1 with RouteTable

use of com.amazonaws.services.ec2.model.RouteTable in project photon-model by vmware.

the class AWSSubnetTaskServiceTest method testCreateSubnetWithOutboundAccess.

@Test
public void testCreateSubnetWithOutboundAccess() throws Throwable {
    // provision a "public" subnet first
    SubnetState publicSubnetState = provisionSubnet(AWS_NON_EXISTING_PUBLIC_SUBNET_NAME, AWS_NON_EXISTING_PUBLIC_SUBNET_CIDR, null);
    assertNotNull(publicSubnetState.id);
    assertEquals(LifecycleState.READY, publicSubnetState.lifecycleState);
    SubnetState subnetState = provisionSubnet(AWS_NON_EXISTING_SUBNET_NAME, AWS_NON_EXISTING_SUBNET_CIDR, publicSubnetState.documentSelfLink);
    assertNotNull(subnetState.id);
    assertEquals(LifecycleState.READY, subnetState.lifecycleState);
    if (!this.isMock) {
        // Verify that the subnet was created.
        DescribeSubnetsRequest describeRequest = new DescribeSubnetsRequest().withSubnetIds(Collections.singletonList(subnetState.id));
        List<Subnet> subnets = this.client.describeSubnets(describeRequest).getSubnets();
        assertNotNull(subnets);
        assertEquals(1, subnets.size());
        // Verify that a NAT gateway was created
        assertNotNull(subnetState.customProperties);
        String natGatewayId = subnetState.customProperties.get(AWS_NAT_GATEWAY_ID);
        String routeTableId = subnetState.customProperties.get(AWS_ROUTE_TABLE_ID);
        String allocationId = subnetState.customProperties.get(AWS_ELASTIC_IP_ALLOCATION_ID);
        assertNotNull(natGatewayId);
        assertNotNull(routeTableId);
        assertNotNull(allocationId);
        DescribeNatGatewaysRequest describeNatGatewaysRequest = new DescribeNatGatewaysRequest().withNatGatewayIds(Collections.singletonList(natGatewayId));
        List<NatGateway> natGateways = this.client.describeNatGateways(describeNatGatewaysRequest).getNatGateways();
        assertNotNull(natGateways);
        assertEquals(1, natGateways.size());
        NatGateway natGateway = natGateways.get(0);
        assertEquals(publicSubnetState.id, natGateway.getSubnetId());
        assertNotNull(natGateway.getNatGatewayAddresses());
        assertEquals(1, natGateway.getNatGatewayAddresses().size());
        assertEquals(allocationId, natGateway.getNatGatewayAddresses().get(0).getAllocationId());
        assertEquals("available", natGateways.get(0).getState());
        // verify that a route table was created
        DescribeRouteTablesRequest describeRouteTablesRequest = new DescribeRouteTablesRequest().withRouteTableIds(Collections.singletonList(routeTableId));
        List<RouteTable> routeTables = this.client.describeRouteTables(describeRouteTablesRequest).getRouteTables();
        assertNotNull(routeTables);
        assertEquals(1, routeTables.size());
        RouteTable routeTable = routeTables.get(0);
        assertNotNull(routeTable.getAssociations());
        assertEquals(1, routeTable.getAssociations().size());
        assertEquals(subnetState.id, routeTable.getAssociations().get(0).getSubnetId());
        assertNotNull(routeTable.getRoutes());
        assertEquals(2, routeTable.getRoutes().size());
        boolean hasRouteToNatGateway = false;
        for (Route route : routeTable.getRoutes()) {
            if (route.getDestinationCidrBlock().equals("0.0.0.0/0") && route.getNatGatewayId() != null && route.getNatGatewayId().equals(natGatewayId)) {
                hasRouteToNatGateway = true;
                break;
            }
        }
        assertTrue(hasRouteToNatGateway);
        // Verify that an IP address allocation was created
        DescribeAddressesRequest describeAddressesRequest = new DescribeAddressesRequest().withAllocationIds(Collections.singletonList(allocationId));
        List<Address> addresses = this.client.describeAddresses(describeAddressesRequest).getAddresses();
        assertNotNull(addresses);
        assertEquals(1, addresses.size());
    }
    // delete the subnet
    kickOffSubnetProvision(InstanceRequestType.DELETE, subnetState, TaskStage.FINISHED);
    if (!this.isMock) {
        // Verify that the subnet was deleted.
        DescribeSubnetsRequest describeRequest = new DescribeSubnetsRequest().withSubnetIds(Collections.singletonList(subnetState.id));
        try {
            this.client.describeSubnets(describeRequest).getSubnets();
            fail("Subnet should not exist in AWS.");
        } catch (AmazonEC2Exception ex) {
            assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
        }
        // Verify that the NAT gateway was deleted
        String natGatewayId = subnetState.customProperties.get(AWS_NAT_GATEWAY_ID);
        String routeTableId = subnetState.customProperties.get(AWS_ROUTE_TABLE_ID);
        String allocationId = subnetState.customProperties.get(AWS_ELASTIC_IP_ALLOCATION_ID);
        DescribeNatGatewaysRequest describeNatGatewaysRequest = new DescribeNatGatewaysRequest().withNatGatewayIds(Collections.singletonList(natGatewayId));
        List<NatGateway> natGateways = this.client.describeNatGateways(describeNatGatewaysRequest).getNatGateways();
        assertNotNull(natGateways);
        assertEquals(1, natGateways.size());
        assertEquals("deleted", natGateways.get(0).getState());
        // Verify that the route table was deleted
        DescribeRouteTablesRequest describeRouteTablesRequest = new DescribeRouteTablesRequest().withRouteTableIds(Collections.singletonList(routeTableId));
        try {
            this.client.describeRouteTables(describeRouteTablesRequest).getRouteTables();
            fail("Route table should not exist in AWS.");
        } catch (AmazonEC2Exception ex) {
            assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
        }
        DescribeAddressesRequest describeAddressesRequest = new DescribeAddressesRequest().withAllocationIds(Collections.singletonList(allocationId));
        try {
            this.client.describeAddresses(describeAddressesRequest).getAddresses();
            fail("IP address allocation should not exist in AWS.");
        } catch (AmazonEC2Exception ex) {
            assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeNatGatewaysRequest(com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) NatGateway(com.amazonaws.services.ec2.model.NatGateway) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) RouteTable(com.amazonaws.services.ec2.model.RouteTable) DescribeRouteTablesRequest(com.amazonaws.services.ec2.model.DescribeRouteTablesRequest) Subnet(com.amazonaws.services.ec2.model.Subnet) Route(com.amazonaws.services.ec2.model.Route) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 2 with RouteTable

use of com.amazonaws.services.ec2.model.RouteTable in project photon-model by vmware.

the class AWSNetworkService method handleStages.

private void handleStages(AWSNetworkContext context) {
    try {
        switch(context.stage) {
            case NETWORK_TASK_STATE:
                getNetworkTaskState(context, AWSNetworkStage.NETWORK_STATE);
                break;
            case NETWORK_STATE:
                getNetworkState(context, AWSNetworkStage.CREDENTIALS);
                break;
            case CREDENTIALS:
                getCredentials(context, AWSNetworkStage.AWS_CLIENT);
                break;
            case AWS_CLIENT:
                this.clientManager.getOrCreateEC2ClientAsync(context.credentials, context.network.regionId, this).whenComplete((ec2Client, t) -> {
                    if (t != null) {
                        context.stage = AWSNetworkStage.FAILED;
                        context.error = t;
                        handleStages(context);
                        return;
                    }
                    context.client = new AWSNetworkClient(ec2Client);
                    if (context.networkRequest.requestType == NetworkInstanceRequest.InstanceRequestType.CREATE) {
                        context.stage = AWSNetworkStage.PROVISION_VPC;
                    } else {
                        context.stage = AWSNetworkStage.REMOVE_GATEWAY;
                    }
                    handleStages(context);
                });
                break;
            case PROVISION_VPC:
                String vpcID = context.client.createVPC(context.network.subnetCIDR);
                updateNetworkProperties(AWS_VPC_ID, vpcID, context, AWSNetworkStage.PROVISION_SUBNET);
                break;
            case PROVISION_SUBNET:
                Subnet subnet = context.client.createSubnet(context.network.subnetCIDR, getCustomProperty(context, AWS_VPC_ID));
                createSubnetState(subnet, context, AWSNetworkStage.PROVISION_GATEWAY);
                break;
            case PROVISION_GATEWAY:
                String gatewayID = context.client.createInternetGateway();
                context.client.attachInternetGateway(getCustomProperty(context, AWS_VPC_ID), gatewayID);
                updateNetworkProperties(AWS_GATEWAY_ID, gatewayID, context, AWSNetworkStage.PROVISION_ROUTE);
                break;
            case PROVISION_ROUTE:
                RouteTable routeTable = context.client.getMainRouteTable(context.network.customProperties.get(AWS_VPC_ID));
                context.client.createInternetRoute(getCustomProperty(context, AWS_GATEWAY_ID), routeTable.getRouteTableId(), ROUTE_DEST_ALL);
                updateNetworkProperties(AWS_VPC_ROUTE_TABLE_ID, routeTable.getRouteTableId(), context, AWSNetworkStage.FINISHED);
                break;
            case REMOVE_GATEWAY:
                context.client.detachInternetGateway(getCustomProperty(context, AWS_VPC_ID), getCustomProperty(context, AWS_GATEWAY_ID));
                context.client.deleteInternetGateway(getCustomProperty(context, AWS_GATEWAY_ID));
                updateNetworkProperties(AWS_GATEWAY_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.REMOVE_SUBNET);
                break;
            case REMOVE_SUBNET:
                // Iterate SubnetStates (page-by-page) and delete AWS Subnet and SubnetState
                deleteSubnetStates(context, AWSNetworkStage.REMOVE_ROUTE);
                break;
            case REMOVE_ROUTE:
                // only need to update the document, the AWS artifact will be
                // removed on VPC removal
                updateNetworkProperties(AWS_VPC_ROUTE_TABLE_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.REMOVE_VPC);
                break;
            case REMOVE_VPC:
                context.client.deleteVPC(getCustomProperty(context, AWS_VPC_ID));
                updateNetworkProperties(AWS_VPC_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.FINISHED);
                break;
            case FAILED:
                context.taskManager.patchTaskToFailure(context.error);
                break;
            case FINISHED:
                context.taskManager.finishTask();
                break;
            default:
                break;
        }
    } catch (Throwable error) {
        // Same as FAILED stage
        context.taskManager.patchTaskToFailure(error);
    }
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) Subnet(com.amazonaws.services.ec2.model.Subnet)

Example 3 with RouteTable

use of com.amazonaws.services.ec2.model.RouteTable in project photon-model by vmware.

the class AWSNetworkClient method createRouteTable.

/**
 * 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);
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) CreateRouteTableResult(com.amazonaws.services.ec2.model.CreateRouteTableResult) CreateRouteTableRequest(com.amazonaws.services.ec2.model.CreateRouteTableRequest)

Example 4 with RouteTable

use of com.amazonaws.services.ec2.model.RouteTable in project photon-model by vmware.

the class AWSNetworkClient method getMainRouteTable.

/**
 * 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);
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) DescribeRouteTablesRequest(com.amazonaws.services.ec2.model.DescribeRouteTablesRequest) Filter(com.amazonaws.services.ec2.model.Filter) ArrayList(java.util.ArrayList) DescribeRouteTablesResult(com.amazonaws.services.ec2.model.DescribeRouteTablesResult)

Example 5 with RouteTable

use of com.amazonaws.services.ec2.model.RouteTable in project photon-model by vmware.

the class TestAWSNetworkService method testGetMainRouteTable.

@Test
public void testGetMainRouteTable() throws Throwable {
    Vpc defVPC = this.netClient.getDefaultVPC();
    assertTrue(defVPC != null);
    RouteTable routeTable = this.netClient.getMainRouteTable(defVPC.getVpcId());
    assertTrue(routeTable != null);
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) Vpc(com.amazonaws.services.ec2.model.Vpc) Test(org.junit.Test)

Aggregations

RouteTable (com.amazonaws.services.ec2.model.RouteTable)6 Test (org.junit.Test)3 DescribeRouteTablesRequest (com.amazonaws.services.ec2.model.DescribeRouteTablesRequest)2 Subnet (com.amazonaws.services.ec2.model.Subnet)2 Address (com.amazonaws.services.ec2.model.Address)1 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 CreateRouteTableRequest (com.amazonaws.services.ec2.model.CreateRouteTableRequest)1 CreateRouteTableResult (com.amazonaws.services.ec2.model.CreateRouteTableResult)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeNatGatewaysRequest (com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest)1 DescribeRouteTablesResult (com.amazonaws.services.ec2.model.DescribeRouteTablesResult)1 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)1 Filter (com.amazonaws.services.ec2.model.Filter)1 InternetGateway (com.amazonaws.services.ec2.model.InternetGateway)1 InternetGatewayAttachment (com.amazonaws.services.ec2.model.InternetGatewayAttachment)1 NatGateway (com.amazonaws.services.ec2.model.NatGateway)1 Route (com.amazonaws.services.ec2.model.Route)1 Vpc (com.amazonaws.services.ec2.model.Vpc)1 AWSNetworkClient (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient)1 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)1