Search in sources :

Example 1 with NatGateway

use of com.amazonaws.services.ec2.model.NatGateway 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 NatGateway

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

the class AWSNetworkClient method createNatGateway.

/**
 * Create a NAT Gateway
 * It waits for the NAT gateway to become available before returning the gateway id.
 */
public DeferredResult<String> createNatGateway(String publicSubnetId, String allocationId, TaskManager taskManager, long taskExpirationMicros) {
    CreateNatGatewayRequest req = new CreateNatGatewayRequest().withSubnetId(publicSubnetId).withAllocationId(allocationId);
    String message = "Create AWS NAT Gateway for subnet [" + publicSubnetId + "] with elastic IP allocation id [" + allocationId + "].";
    AWSDeferredResultAsyncHandler<CreateNatGatewayRequest, CreateNatGatewayResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
    this.client.createNatGatewayAsync(req, handler);
    return handler.toDeferredResult().thenApply(CreateNatGatewayResult::getNatGateway).thenApply(NatGateway::getNatGatewayId).thenCompose(natGatewayId -> waitForNatGatewayState(natGatewayId, taskManager, taskExpirationMicros, AWSTaskStatusChecker.AWS_AVAILABLE_NAME));
}
Also used : CreateNatGatewayRequest(com.amazonaws.services.ec2.model.CreateNatGatewayRequest) CreateNatGatewayResult(com.amazonaws.services.ec2.model.CreateNatGatewayResult)

Example 3 with NatGateway

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

the class AWSTaskStatusChecker method buildRequest.

private AmazonWebServiceRequest buildRequest(T type) {
    if (type instanceof Instance) {
        DescribeInstancesRequest descRequest = new DescribeInstancesRequest();
        List<String> instanceIdList = new ArrayList<>();
        instanceIdList.add(this.instanceId);
        descRequest.setInstanceIds(instanceIdList);
        return descRequest;
    } else if (type instanceof NatGateway) {
        DescribeNatGatewaysRequest descRequest = new DescribeNatGatewaysRequest();
        List<String> instanceIdList = new ArrayList<>();
        instanceIdList.add(this.instanceId);
        descRequest.setNatGatewayIds(instanceIdList);
        return descRequest;
    } else if (type instanceof Volume) {
        DescribeVolumesRequest descRequest = new DescribeVolumesRequest();
        List<String> volumeIdList = new ArrayList<>();
        volumeIdList.add(this.instanceId);
        descRequest.setVolumeIds(volumeIdList);
        return descRequest;
    } else {
        AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
        return null;
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) Volume(com.amazonaws.services.ec2.model.Volume) DescribeNatGatewaysRequest(com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest) ArrayList(java.util.ArrayList) NatGateway(com.amazonaws.services.ec2.model.NatGateway) ArrayList(java.util.ArrayList) List(java.util.List) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Example 4 with NatGateway

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

the class AWSTaskStatusChecker method buildHandler.

private AsyncHandler buildHandler(T type) {
    return new AsyncHandler<AmazonWebServiceRequest, AmazonWebServiceResult>() {

        @Override
        public void onError(Exception exception) {
            // particular instanceId.
            if (exception instanceof AmazonServiceException && ((AmazonServiceException) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_INSTANCE_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.service.logWarning("Could not retrieve status for instance %s. Retrying... Exception on AWS is %s", AWSTaskStatusChecker.this.instanceId, exception);
                AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                return;
            } else if (exception instanceof AmazonEC2Exception && ((AmazonEC2Exception) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.consumer.accept(null);
                return;
            }
            AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(exception);
            return;
        }

        @Override
        public void onSuccess(AmazonWebServiceRequest request, AmazonWebServiceResult result) {
            String status;
            Object instance;
            String failureMessage = null;
            String stateReason = null;
            if (result instanceof DescribeInstancesResult) {
                instance = ((DescribeInstancesResult) result).getReservations().get(0).getInstances().get(0);
                Instance vm = (Instance) instance;
                status = vm.getState().getName();
                stateReason = vm.getStateReason() != null ? vm.getStateReason().getMessage() : null;
            } else if (result instanceof DescribeNatGatewaysResult) {
                instance = ((DescribeNatGatewaysResult) result).getNatGateways().get(0);
                status = ((NatGateway) instance).getState();
                // if NAT gateway creation fails, the status is still "pending";
                // rather than keep checking for status and eventually time out, get the
                // failure message and fail the task
                failureMessage = ((NatGateway) instance).getFailureMessage();
            } else if (result instanceof DescribeVolumesResult) {
                instance = ((DescribeVolumesResult) result).getVolumes().get(0);
                status = ((Volume) instance).getState().toLowerCase();
            } else {
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + result));
                return;
            }
            if (failureMessage != null) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException(failureMessage));
                return;
            } else if (AWSTaskStatusChecker.this.failureStates.contains(status)) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException("Resource is state:[" + status + "]," + "reason:" + stateReason));
                return;
            } else if (!status.equals(AWSTaskStatusChecker.this.desiredState)) {
                AWSTaskStatusChecker.this.service.logInfo("Instance %s not yet in desired state %s. Current state %s, failure states %s, waiting 5s", AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, status, AWSTaskStatusChecker.this.failureStates);
                // if the instance is not in the desired state, schedule thread
                // to run again in 5 seconds
                AWSTaskStatusChecker.this.service.getHost().schedule(() -> {
                    AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                }, 5, TimeUnit.SECONDS);
                return;
            }
            AWSTaskStatusChecker.this.consumer.accept(instance);
            return;
        }
    };
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) Instance(com.amazonaws.services.ec2.model.Instance) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) NatGateway(com.amazonaws.services.ec2.model.NatGateway) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonWebServiceResult(com.amazonaws.AmazonWebServiceResult) Volume(com.amazonaws.services.ec2.model.Volume) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 5 with NatGateway

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

the class AWSTaskStatusChecker method runSearch.

private void runSearch(T type) {
    AmazonWebServiceRequest descRequest = buildRequest(type);
    AsyncHandler describeHandler = buildHandler(type);
    if (type instanceof Instance) {
        this.amazonEC2Client.describeInstancesAsync((DescribeInstancesRequest) descRequest, describeHandler);
    } else if (type instanceof NatGateway) {
        this.amazonEC2Client.describeNatGatewaysAsync((DescribeNatGatewaysRequest) descRequest, describeHandler);
    } else if (type instanceof Volume) {
        this.amazonEC2Client.describeVolumesAsync((DescribeVolumesRequest) descRequest, describeHandler);
    } else {
        AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
    }
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) Instance(com.amazonaws.services.ec2.model.Instance) Volume(com.amazonaws.services.ec2.model.Volume) DescribeNatGatewaysRequest(com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest) NatGateway(com.amazonaws.services.ec2.model.NatGateway) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest)

Aggregations

NatGateway (com.amazonaws.services.ec2.model.NatGateway)5 DescribeNatGatewaysRequest (com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 Instance (com.amazonaws.services.ec2.model.Instance)3 Volume (com.amazonaws.services.ec2.model.Volume)3 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2 CreateNatGatewayRequest (com.amazonaws.services.ec2.model.CreateNatGatewayRequest)2 CreateNatGatewayResult (com.amazonaws.services.ec2.model.CreateNatGatewayResult)2 DeleteNatGatewayRequest (com.amazonaws.services.ec2.model.DeleteNatGatewayRequest)2 DescribeNatGatewaysResult (com.amazonaws.services.ec2.model.DescribeNatGatewaysResult)2 Filter (com.amazonaws.services.ec2.model.Filter)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonWebServiceResult (com.amazonaws.AmazonWebServiceResult)1 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 Address (com.amazonaws.services.ec2.model.Address)1 AllocateAddressRequest (com.amazonaws.services.ec2.model.AllocateAddressRequest)1 AllocateAddressResult (com.amazonaws.services.ec2.model.AllocateAddressResult)1