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

The following examples show how to use com.amazonaws.services.ec2.model.DescribeVpcsResult. 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: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private Set<CloudNetwork> getCloudNetworks(AmazonEC2Client ec2Client,
        DescribeRouteTablesResult describeRouteTablesResult, DescribeVpcsResult describeVpcsResult) {

    Set<CloudNetwork> cloudNetworks = new HashSet<>();
    LOGGER.debug("Processing VPCs");
    for (Vpc vpc : describeVpcsResult.getVpcs()) {
        List<Subnet> awsSubnets = getSubnets(ec2Client, vpc);
        Set<CloudSubnet> subnets = convertAwsSubnetsToCloudSubnets(describeRouteTablesResult, awsSubnets);

        Map<String, Object> properties = prepareNetworkProperties(vpc);
        Optional<String> name = getName(vpc.getTags());
        if (name.isPresent()) {
            cloudNetworks.add(new CloudNetwork(name.get(), vpc.getVpcId(), subnets, properties));
        } else {
            cloudNetworks.add(new CloudNetwork(vpc.getVpcId(), vpc.getVpcId(), subnets, properties));
        }
    }
    return cloudNetworks;
}
 
Example #6
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 #7
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 #8
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 #9
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit24VpcEmptySubnet() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/24");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Collections.emptyList());

    thrown.expect(CloudConnectorException.class);
    thrown.expectMessage("The selected VPC has to be in a bigger CIDR range than /24");

    underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
}
 
Example #10
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit24Vpc() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/24");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(singletonList(subnet1));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24");

    thrown.expect(CloudConnectorException.class);
    thrown.expectMessage("The selected VPC has to be in a bigger CIDR range than /24");

    underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
}
 
Example #11
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 #12
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 #13
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit20Vpc2() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 16}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24");
    when(subnet2.getCidrBlock()).thenReturn("10.0.1.0/24");
    when(subnet3.getCidrBlock()).thenReturn("10.0.2.0/24");
    when(subnet4.getCidrBlock()).thenReturn("10.0.3.0/24");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.4.0/24", cidr);
}
 
Example #14
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch vpc info test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchVpcInfoTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeVpcsResult describeVpcsResult = new DescribeVpcsResult();
    List<Vpc> vpcList = new ArrayList<>();
    Vpc vpc = new Vpc();
    vpc.setVpcId("vpcId");
    vpcList.add(vpc);
    describeVpcsResult.setVpcs(vpcList);
    when(ec2Client.describeVpcs()).thenReturn(describeVpcsResult);
    
    DescribeVpcEndpointsResult describeVpcEndpointsResult = new DescribeVpcEndpointsResult();
    List<VpcEndpoint> vpcEndpoints = new ArrayList<VpcEndpoint>();
    VpcEndpoint vpcEndpoint = new VpcEndpoint();
    vpcEndpoint.setPolicyDocument("{\"Statement\":[{\"Effect\":\"Allow\",\"Resource\":\"*\"}]}");
    vpcEndpoints.add(vpcEndpoint);
    describeVpcEndpointsResult.setVpcEndpoints(vpcEndpoints);
    when(ec2Client.describeVpcEndpoints(anyObject())).thenReturn(describeVpcEndpointsResult);
    assertThat(inventoryUtil.fetchVpcInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
    
    describeVpcEndpointsResult = new DescribeVpcEndpointsResult();
    vpcEndpoints = new ArrayList<VpcEndpoint>();
    vpcEndpoint = new VpcEndpoint();
    vpcEndpoint.setPolicyDocument("{\"Statement\"[{\"Effect\":\"Allow\",\"Resource\":\"*\"}]}");
    vpcEndpoints.add(vpcEndpoint);
    describeVpcEndpointsResult.setVpcEndpoints(vpcEndpoints);
    when(ec2Client.describeVpcEndpoints(anyObject())).thenReturn(describeVpcEndpointsResult);
    assertThat(inventoryUtil.fetchVpcInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #15
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRForOneSpot() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn("");
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("172.14.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);

    List<com.amazonaws.services.ec2.model.Subnet> subnetList = new ArrayList<>();
    String startRange = "172.14.0.0";
    for (int i = 0; i < 254; i++) {
        startRange = incrementIp(startRange);
        com.amazonaws.services.ec2.model.Subnet subnetMock = mock(com.amazonaws.services.ec2.model.Subnet.class);
        when(subnetMock.getCidrBlock()).thenReturn(startRange + "/24");
        subnetList.add(subnetMock);
    }
    when(subnetsResult.getSubnets()).thenReturn(subnetList);

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("172.14.255.0/24", cidr);
}
 
Example #16
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRForFullVpc() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 7}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);

    List<com.amazonaws.services.ec2.model.Subnet> subnetList = new ArrayList<>();
    String startRange = "10.0.0.0";
    for (int i = 0; i < 255; i++) {
        startRange = incrementIp(startRange);
        com.amazonaws.services.ec2.model.Subnet subnetMock = mock(com.amazonaws.services.ec2.model.Subnet.class);
        when(subnetMock.getCidrBlock()).thenReturn(startRange + "/24");
        subnetList.add(subnetMock);
    }
    when(subnetsResult.getSubnets()).thenReturn(subnetList);

    thrown.expect(CloudConnectorException.class);
    thrown.expectMessage("Cannot find non-overlapping CIDR range");

    underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
}
 
Example #17
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit20Vpc1EmptyInTheMiddle() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet5 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet6 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet7 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet8 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 127, (byte) 127, (byte) 127, (byte) 127}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4, subnet5, subnet6, subnet7, subnet8));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/23");
    when(subnet2.getCidrBlock()).thenReturn("10.0.2.0/23");
    when(subnet3.getCidrBlock()).thenReturn("10.0.4.0/23");
    when(subnet4.getCidrBlock()).thenReturn("10.0.6.0/23");
    when(subnet5.getCidrBlock()).thenReturn("10.0.8.0/23");
    when(subnet6.getCidrBlock()).thenReturn("10.0.10.0/23");
    when(subnet7.getCidrBlock()).thenReturn("10.0.12.0/24");
    when(subnet8.getCidrBlock()).thenReturn("10.0.14.0/23");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.13.0/24", cidr);
}
 
Example #18
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit20Vpc1Empty2() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet5 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet6 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet7 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet8 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 4}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4, subnet5, subnet6, subnet7, subnet8));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/23");
    when(subnet2.getCidrBlock()).thenReturn("10.0.2.0/24");
    when(subnet3.getCidrBlock()).thenReturn("10.0.4.0/23");
    when(subnet4.getCidrBlock()).thenReturn("10.0.6.0/23");
    when(subnet5.getCidrBlock()).thenReturn("10.0.8.0/23");
    when(subnet6.getCidrBlock()).thenReturn("10.0.10.0/23");
    when(subnet7.getCidrBlock()).thenReturn("10.0.12.0/23");
    when(subnet8.getCidrBlock()).thenReturn("10.0.14.0/23");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.3.0/24", cidr);
}
 
Example #19
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit20Vpc1Empty() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet5 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet6 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet7 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet8 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 127, (byte) 127, (byte) 127, (byte) 127, (byte) 127, (byte) 127, (byte) 83}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4, subnet5, subnet6, subnet7, subnet8));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/23");
    when(subnet2.getCidrBlock()).thenReturn("10.0.2.0/23");
    when(subnet3.getCidrBlock()).thenReturn("10.0.4.0/23");
    when(subnet4.getCidrBlock()).thenReturn("10.0.6.0/23");
    when(subnet5.getCidrBlock()).thenReturn("10.0.8.0/23");
    when(subnet6.getCidrBlock()).thenReturn("10.0.10.0/23");
    when(subnet7.getCidrBlock()).thenReturn("10.0.12.0/23");
    when(subnet8.getCidrBlock()).thenReturn("10.0.14.0/24");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.15.0/24", cidr);
}
 
Example #20
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit20VpcFull() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet5 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet6 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet7 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet8 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 15}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4, subnet5, subnet6, subnet7, subnet8));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/23");
    when(subnet2.getCidrBlock()).thenReturn("10.0.2.0/23");
    when(subnet3.getCidrBlock()).thenReturn("10.0.4.0/23");
    when(subnet4.getCidrBlock()).thenReturn("10.0.6.0/23");
    when(subnet5.getCidrBlock()).thenReturn("10.0.8.0/23");
    when(subnet6.getCidrBlock()).thenReturn("10.0.10.0/23");
    when(subnet7.getCidrBlock()).thenReturn("10.0.12.0/23");
    when(subnet8.getCidrBlock()).thenReturn("10.0.14.0/23");

    thrown.expect(CloudConnectorException.class);
    thrown.expectMessage("Cannot find non-overlapping CIDR range");

    underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
}
 
Example #21
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit20Vpc() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 15}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24");
    when(subnet2.getCidrBlock()).thenReturn("10.0.1.0/24");
    when(subnet3.getCidrBlock()).thenReturn("10.0.2.0/24");
    when(subnet4.getCidrBlock()).thenReturn("10.0.3.0/24");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.15.0/24", cidr);
}
 
Example #22
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 #23
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWithNon24Subnets3() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 15}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
    when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
    when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/20");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.64.0/24", cidr);
}
 
Example #24
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWithNon24Subnets2() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 76}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
    when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
    when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/20");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.76.0/24", cidr);
}
 
Example #25
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWithNon24Subnets() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 23}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
    when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
    when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/24");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.49.0/24", cidr);
}
 
Example #26
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindNonOverLappingCIDR() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();

    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet5 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet6 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[]{(byte) 100}));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4, subnet5, subnet6));
    when(subnet1.getCidrBlock()).thenReturn("10.0.1.0/24");
    when(subnet2.getCidrBlock()).thenReturn("10.0.2.0/24");
    when(subnet3.getCidrBlock()).thenReturn("10.0.3.0/24");
    when(subnet4.getCidrBlock()).thenReturn("10.0.5.0/24");
    when(subnet5.getCidrBlock()).thenReturn("10.0.6.0/24");
    when(subnet6.getCidrBlock()).thenReturn("10.0.255.0/24");

    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);

    Assert.assertEquals("10.0.100.0/24", cidr);
}
 
Example #27
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 #28
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);