com.amazonaws.services.ec2.model.DescribeVpcsRequest Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.DescribeVpcsRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VpcTableProviderTest.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUpRead()
{
    when(mockEc2.describeVpcs(any(DescribeVpcsRequest.class))).thenAnswer((InvocationOnMock invocation) -> {
        DescribeVpcsRequest request = (DescribeVpcsRequest) invocation.getArguments()[0];

        assertEquals(getIdValue(), request.getVpcIds().get(0));
        DescribeVpcsResult mockResult = mock(DescribeVpcsResult.class);
        List<Vpc> values = new ArrayList<>();
        values.add(makeVpc(getIdValue()));
        values.add(makeVpc(getIdValue()));
        values.add(makeVpc("fake-id"));
        when(mockResult.getVpcs()).thenReturn(values);
        return mockResult;
    });
}
 
Example #2
Source File: VpcTableProvider.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * Calls DescribeVPCs on the AWS EC2 Client returning all VPCs that match the supplied predicate and attempting
 * to push down certain predicates (namely queries for specific VPCs) to EC2.
 *
 * @See TableProvider
 */
@Override
public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker)
{
    DescribeVpcsRequest request = new DescribeVpcsRequest();

    ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("id");
    if (idConstraint != null && idConstraint.isSingleValue()) {
        request.setVpcIds(Collections.singletonList(idConstraint.getSingleValue().toString()));
    }

    DescribeVpcsResult response = ec2.describeVpcs(request);
    for (Vpc vpc : response.getVpcs()) {
        instanceToRow(vpc, spiller);
    }
}
 
Example #3
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetVpcCidrs() {
    String cidr1 = "1.2.3.0/24";
    String cidr2 = "10.0.0.0/8";
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    CloudStack cloudStack = mock(CloudStack.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudStack.getNetwork()).thenReturn(new Network(new Subnet(null), Map.of("vpcId", "vpc-123")));
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region("eu-west1")));
    when(awsClient.createAccess(any(AwsCredentialView.class), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any(DescribeVpcsRequest.class)))
            .thenReturn(new DescribeVpcsResult().withVpcs(new Vpc()
                    .withCidrBlockAssociationSet(new VpcCidrBlockAssociation().withCidrBlock(cidr1), new VpcCidrBlockAssociation().withCidrBlock(cidr2))));

    List<String> vpcCidrs = underTest.getVpcCidrs(authenticatedContext, cloudStack);

    assertTrue(vpcCidrs.contains(cidr1));
    assertTrue(vpcCidrs.contains(cidr2));
}
 
Example #4
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNetworkCidrMoreThanOneAssociatedCidrOnOneVpcShouldReturn2Cidr() {
    String existingVpc = "vpc-1";
    String cidrBlock1 = "10.0.0.0/16";
    String cidrBlock2 = "10.23.0.0/16";

    Network network = new Network(null, Map.of(AwsNetworkView.VPC_ID, existingVpc, "region", "us-west-2"));
    CloudCredential credential = new CloudCredential();
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    DescribeVpcsResult describeVpcsResult = describeVpcsResult(cidrBlock1, cidrBlock2);

    when(awsClient.createAccess(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client);
    when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult);

    NetworkCidr result = underTest.getNetworkCidr(network, credential);
    assertEquals(cidrBlock1, result.getCidr());
    assertTrue(result.getCidrs().contains(cidrBlock1));
    assertTrue(result.getCidrs().contains(cidrBlock2));
}
 
Example #5
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNetworkCidrWithoutResult() {
    String existingVpc = "vpc-1";

    Network network = new Network(null, Map.of(AwsNetworkView.VPC_ID, existingVpc, "region", "us-west-2"));
    CloudCredential credential = new CloudCredential();
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    DescribeVpcsResult describeVpcsResult = describeVpcsResult();

    when(awsClient.createAccess(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client);
    when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult);

    thrown.expect(BadRequestException.class);
    thrown.expectMessage("VPC cidr could not fetch from AWS: " + existingVpc);

    underTest.getNetworkCidr(network, credential);
}
 
Example #6
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNetworkCidr() {
    String existingVpc = "vpc-1";
    String cidrBlock = "10.0.0.0/16";

    Network network = new Network(null, Map.of(AwsNetworkView.VPC_ID, existingVpc, "region", "us-west-2"));
    CloudCredential credential = new CloudCredential();
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    DescribeVpcsResult describeVpcsResult = describeVpcsResult(cidrBlock);

    when(awsClient.createAccess(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client);
    when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult);

    NetworkCidr result = underTest.getNetworkCidr(network, credential);
    assertEquals(cidrBlock, result.getCidr());
}
 
Example #7
Source File: AwsNetworkService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public List<String> getVpcCidrs(AuthenticatedContext ac, CloudStack stack) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    if (awsNetworkView.isExistingVPC()) {
        String region = ac.getCloudContext().getLocation().getRegion().value();
        AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);

        DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc());
        Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
        List<String> cidrBlockAssociationSet = vpc.getCidrBlockAssociationSet().stream()
                .map(VpcCidrBlockAssociation::getCidrBlock)
                .collect(Collectors.toList());
        LOGGER.info("VPC associated CIDR blocks: [{}]", cidrBlockAssociationSet);
        return cidrBlockAssociationSet;
    } else {
        return Collections.emptyList();
    }
}
 
Example #8
Source File: AwsNetworkService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);

    DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc());
    Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
    String vpcCidr = vpc.getCidrBlock();
    LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr);

    DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc())));
    List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets();
    List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList());
    LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs));

    return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs);
}
 
Example #9
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe VPCs.
 *
 * @return List of vpcs
 */
protected final List<Vpc> describeVpcs() {
    DescribeVpcsRequest req = new DescribeVpcsRequest();
    DescribeVpcsResult result = amazonEC2Client.describeVpcs(req);
    List<Vpc> vpcs = result.getVpcs();
    return vpcs;
}
 
Example #10
Source File: AwsNetworkConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) {
    AwsCredentialView awsCredentialView = new AwsCredentialView(credential);
    AmazonEC2Client awsClientAccess = awsClient.createAccess(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION));
    AwsNetworkView awsNetworkView = new AwsNetworkView(network);
    String existingVpc = awsNetworkView.getExistingVpc();
    DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc));
    List<String> vpcCidrs = new ArrayList<>();
    for (Vpc vpc : describeVpcsResult.getVpcs()) {
        if (vpc.getCidrBlockAssociationSet() != null) {
            LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId());
            List<String> cidrs = vpc.getCidrBlockAssociationSet()
                    .stream()
                    .map(VpcCidrBlockAssociation::getCidrBlock)
                    .collect(Collectors.toList());
            LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs);
            vpcCidrs.addAll(cidrs);
        } else {
            LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId());
            vpcCidrs.add(vpc.getCidrBlock());
        }
    }

    if (vpcCidrs.isEmpty()) {
        throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc);
    }
    if (vpcCidrs.size() > 1) {
        LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0));
    }
    return new NetworkCidr(vpcCidrs.get(0), vpcCidrs);
}
 
Example #11
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private DescribeVpcsRequest getDescribeVpcsRequestWIthFilters(Map<String, String> filters) {
    //create vpc filter view
    PlatformResourceVpcFilterView filter = new PlatformResourceVpcFilterView(filters);
    DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest();
    // If the filtervalue is provided then we should filter only for those vpc
    if (!Strings.isNullOrEmpty(filter.getVpcId())) {
        describeVpcsRequest.withVpcIds(filter.getVpcId());
    }
    return describeVpcsRequest;
}
 
Example #12
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value());
    try {
        LOGGER.debug("Describing route tables in region {}", region.getRegionName());
        DescribeRouteTablesResult describeRouteTablesResult = ec2Client.describeRouteTables();
        DescribeVpcsRequest describeVpcsRequest = getDescribeVpcsRequestWIthFilters(filters);
        Set<CloudNetwork> cloudNetworks = new HashSet<>();

        DescribeVpcsResult describeVpcsResult = null;
        boolean first = true;
        while (first || !isNullOrEmpty(describeVpcsResult.getNextToken())) {
            LOGGER.debug("Getting VPC list in region {}{}", region.getRegionName(), first ? "" : " (continuation)");
            first = false;
            describeVpcsRequest.setNextToken(describeVpcsResult == null ? null : describeVpcsResult.getNextToken());
            describeVpcsResult = ec2Client.describeVpcs(describeVpcsRequest);
            Set<CloudNetwork> partialNetworks = getCloudNetworks(ec2Client, describeRouteTablesResult, describeVpcsResult);
            cloudNetworks.addAll(partialNetworks);
        }
        Map<String, Set<CloudNetwork>> result = new HashMap<>();
        result.put(region.value(), cloudNetworks);
        return new CloudNetworks(result);
    } catch (SdkClientException e) {
        LOGGER.error(String.format("Unable to enumerate networks in region '%s'. Check exception for details.", region.getRegionName()), e);
        throw e;
    }
}
 
Example #13
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public VpcCollection getVpcs(DescribeVpcsRequest request) {
    ResourceCollectionImpl result = service.getCollection("Vpcs", request);
    if (result == null) return null;
    return new VpcCollectionImpl(result);
}
 
Example #14
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public VpcCollection getVpcs() {
    return getVpcs((DescribeVpcsRequest)null);
}
 
Example #15
Source File: VpcImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public boolean load(DescribeVpcsRequest request,
        ResultCapture<DescribeVpcsResult> extractor) {

    return resource.load(request, extractor);
}
 
Example #16
Source File: VpcImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public boolean load(DescribeVpcsRequest request) {
    return load(request, null);
}
 
Example #17
Source File: Ec2Utils.java    From pacbot with Apache License 2.0 3 votes vote down vote up
/**
    * Gets the vpcs for region.
    *
    * @param ec2ServiceClient the ec 2 service client
    * @param region the region
    * @param request the request
    * @return the vpcs for region
    */
public static List<Vpc> getVpcsForRegion(AmazonEC2 ec2ServiceClient,
		Region region, DescribeVpcsRequest request) {
	ec2ServiceClient.setRegion(region);
	DescribeVpcsResult describeVpcsResult =  ec2ServiceClient.describeVpcs(request);
	return  describeVpcsResult.getVpcs();
	
}
 
Example #18
Source File: EC2.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves the Vpcs collection referenced by this resource.
 */
VpcCollection getVpcs(DescribeVpcsRequest request);
 
Example #19
Source File: Vpc.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Makes a call to the service to load this resource's attributes if they
 * are not loaded yet, and use a ResultCapture to retrieve the low-level
 * client response
 * The following request parameters will be populated from the data of this
 * <code>Vpc</code> resource, and any conflicting parameter value set in the
 * request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>VpcIds.0</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return Returns {@code true} if the resource is not yet loaded when this
 *         method was invoked, which indicates that a service call has been
 *         made to retrieve the attributes.
 * @see DescribeVpcsRequest
 */
boolean load(DescribeVpcsRequest request, ResultCapture<DescribeVpcsResult>
        extractor);
 
Example #20
Source File: Vpc.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Makes a call to the service to load this resource's attributes if they
 * are not loaded yet.
 * The following request parameters will be populated from the data of this
 * <code>Vpc</code> resource, and any conflicting parameter value set in the
 * request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>VpcIds.0</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return Returns {@code true} if the resource is not yet loaded when this
 *         method was invoked, which indicates that a service call has been
 *         made to retrieve the attributes.
 * @see DescribeVpcsRequest
 */
boolean load(DescribeVpcsRequest request);