Search in sources :

Example 1 with InternetGateway

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

the class AWSRemoteCleanup method deleteInternetGateways.

private void deleteInternetGateways(String vpcId, AmazonEC2 usEastEc2Client) {
    DescribeInternetGatewaysRequest internetGatewaysRequest = new DescribeInternetGatewaysRequest().withFilters(new Filter(ATTACHMENT_VPC_KEY, Collections.singletonList(vpcId)));
    DescribeInternetGatewaysResult internetGatewaysResult = usEastEc2Client.describeInternetGateways(internetGatewaysRequest);
    internetGatewaysResult.getInternetGateways().forEach(internetGateway -> {
        DetachInternetGatewayRequest detachInternetGatewayRequest = new DetachInternetGatewayRequest().withInternetGatewayId(internetGateway.getInternetGatewayId());
        detachInternetGatewayRequest.setVpcId(vpcId);
        usEastEc2Client.detachInternetGateway(detachInternetGatewayRequest);
        DeleteInternetGatewayRequest deleteInternetGatewayRequest = new DeleteInternetGatewayRequest().withInternetGatewayId(internetGateway.getInternetGatewayId());
        this.host.log("Terminating stale internet gateway: %s", internetGateway.getInternetGatewayId());
        usEastEc2Client.deleteInternetGateway(deleteInternetGatewayRequest);
    });
}
Also used : DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) DeleteInternetGatewayRequest(com.amazonaws.services.ec2.model.DeleteInternetGatewayRequest) Filter(com.amazonaws.services.ec2.model.Filter) DetachInternetGatewayRequest(com.amazonaws.services.ec2.model.DetachInternetGatewayRequest) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)

Example 2 with InternetGateway

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

the class AWSNetworkClient method getInternetGateway.

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);
}
Also used : DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)

Example 3 with InternetGateway

use of com.amazonaws.services.ec2.model.InternetGateway in project cloudbreak by hortonworks.

the class AwsSetup method validateExistingIGW.

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()));
            }
        }
    }
}
Also used : DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)

Example 4 with InternetGateway

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

the class TestAWSNetworkService method testEnvironmentCreation.

/*
     * Test covers the necessary elements for a successful environment creation
     * These environmental elements are necessary before any VM instances can be
     * created
     *
     * - Internet Gateway
     * - VPC
     * - Subnet
     * - Route to IG
     *
     */
@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);
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) Test(org.junit.Test)

Example 5 with InternetGateway

use of com.amazonaws.services.ec2.model.InternetGateway in project cloudbreak by hortonworks.

the class AwsPlatformResources method gateways.

@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);
    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);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CloudGateWays(com.sequenceiq.cloudbreak.cloud.model.CloudGateWays) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest) CloudGateWay(com.sequenceiq.cloudbreak.cloud.model.CloudGateWay) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

DescribeInternetGatewaysRequest (com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)4 DescribeInternetGatewaysResult (com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult)4 InternetGateway (com.amazonaws.services.ec2.model.InternetGateway)4 InternetGatewayAttachment (com.amazonaws.services.ec2.model.InternetGatewayAttachment)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 DeleteInternetGatewayRequest (com.amazonaws.services.ec2.model.DeleteInternetGatewayRequest)1 DetachInternetGatewayRequest (com.amazonaws.services.ec2.model.DetachInternetGatewayRequest)1 Filter (com.amazonaws.services.ec2.model.Filter)1 RouteTable (com.amazonaws.services.ec2.model.RouteTable)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)1 CloudGateWay (com.sequenceiq.cloudbreak.cloud.model.CloudGateWay)1 CloudGateWays (com.sequenceiq.cloudbreak.cloud.model.CloudGateWays)1 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)1 Region (com.sequenceiq.cloudbreak.cloud.model.Region)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1