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

The following examples show how to use com.amazonaws.services.ec2.model.DescribeSubnetsResult. 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: SubnetTableProviderTest.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUpRead()
{
    when(mockEc2.describeSubnets(any(DescribeSubnetsRequest.class))).thenAnswer((InvocationOnMock invocation) -> {
        DescribeSubnetsRequest request = (DescribeSubnetsRequest) invocation.getArguments()[0];

        assertEquals(getIdValue(), request.getSubnetIds().get(0));
        DescribeSubnetsResult mockResult = mock(DescribeSubnetsResult.class);
        List<Subnet> values = new ArrayList<>();
        values.add(makeSubnet(getIdValue()));
        values.add(makeSubnet(getIdValue()));
        values.add(makeSubnet("fake-id"));
        when(mockResult.getSubnets()).thenReturn(values);

        return mockResult;
    });
}
 
Example #2
Source File: SubnetTableProvider.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * Calls DescribeSubnets on the AWS EC2 Client returning all subnets that match the supplied predicate and attempting
 * to push down certain predicates (namely queries for specific subnet) to EC2.
 *
 * @See TableProvider
 */
@Override
public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker)
{
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();

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

    DescribeSubnetsResult response = ec2.describeSubnets(request);
    for (Subnet subnet : response.getSubnets()) {
        instanceToRow(subnet, spiller);
    }
}
 
Example #3
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch subnets.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @param accountName the account name
 * @return the map
 */
public static Map<String,List<Subnet>> fetchSubnets(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
	Map<String,List<Subnet>> subnets = new LinkedHashMap<>();
	AmazonEC2 ec2Client ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Subnet\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				DescribeSubnetsResult rslt = ec2Client.describeSubnets();
				List<Subnet> subnetsTemp =rslt.getSubnets();
				if(! subnetsTemp.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Subnet "+region.getName() + " >> "+subnetsTemp.size());
					subnets.put(accountId+delimiter+accountName+delimiter+region.getName(),subnetsTemp);
				}

			}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"subnet",e.getMessage());
		}
	}

	return subnets;
}
 
Example #4
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch subnets test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchSubnetsTest() 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);
    
    DescribeSubnetsResult describeSubnetsResult = new DescribeSubnetsResult();
    List<Subnet> subnets = new ArrayList<>();
    subnets.add(new Subnet());
    describeSubnetsResult.setSubnets(subnets);
    when(ec2Client.describeSubnets()).thenReturn(describeSubnetsResult);
    assertThat(inventoryUtil.fetchSubnets(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #5
Source File: EC2Communication.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether exiting Subnet is present.
 * 
 * @param subnetString
 * @return <code>Subnet </code> if the matches one of the subnetString
 * 
 */
public Subnet resolveSubnet(String subnetString) throws APPlatformException {
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    DescribeSubnetsResult result = getEC2().describeSubnets(
            request.withSubnetIds(subnetString));
    List<Subnet> subnets = result.getSubnets();
    if (!subnets.isEmpty()) {
        LOGGER.debug(" number of subnets found: " + subnets.size());
        for (Subnet subnet : subnets) {
            LOGGER.debug("return subnet with id " + subnet.getSubnetId());
            return subnet;
        }

    }
    throw new APPlatformException(
            Messages.getAll("error_invalid_subnet_id") + subnetString);

}
 
Example #6
Source File: AwsIaasGatewayScriptService.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean hasSubnets(String vpcId) throws AutoException {
    if (StringUtils.isEmpty(vpcId)) {
        log.info(platform.getPlatformName() + " にvpcIdが有りません");
        System.out.println("VPCID_EMPTY");
        return false;
    }

    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    request.withFilters(new Filter().withName("vpc-id").withValues(vpcId));
    DescribeSubnetsResult result = ec2Client.describeSubnets(request);
    List<Subnet> subnets = result.getSubnets();

    if (subnets.isEmpty()) {
        log.info(platform.getPlatformName() + " にサブネットが有りません");
        System.out.println("SUBNET_EMPTY");
        return false;
    }

    return true;
}
 
Example #7
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private List<Subnet> getSubnets(AmazonEC2Client ec2Client, Vpc vpc) {
    List<Subnet> awsSubnets = new ArrayList<>();
    DescribeSubnetsResult describeSubnetsResult = null;
    boolean first = true;
    while (first || !isNullOrEmpty(describeSubnetsResult.getNextToken())) {
        LOGGER.debug("Describing subnets for VPC {}{}", vpc.getVpcId(), first ? "" : " (continuation)");
        first = false;
        DescribeSubnetsRequest describeSubnetsRequest = createSubnetsDescribeRequest(vpc, describeSubnetsResult == null
                ? null
                : describeSubnetsResult.getNextToken());
        describeSubnetsResult = ec2Client.describeSubnets(describeSubnetsRequest);
        awsSubnets.addAll(describeSubnetsResult.getSubnets());
        awsSubnets = awsSubnets.stream().filter(subnet -> !deniedAZs.contains(subnet.getAvailabilityZone()))
                .collect(Collectors.toList());
    }
    return awsSubnets;
}
 
Example #8
Source File: AwsSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void validateExistingSubnet(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    if (awsNetworkView.isExistingSubnet()) {
        DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
        describeSubnetsRequest.withSubnetIds(awsNetworkView.getSubnetList());
        DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
        if (describeSubnetsResult.getSubnets().size() < awsNetworkView.getSubnetList().size()) {
            throw new CloudConnectorException(String.format(SUBNET_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet()));
        } else {
            for (Subnet subnet : describeSubnetsResult.getSubnets()) {
                String vpcId = subnet.getVpcId();
                if (vpcId != null && !vpcId.equals(awsNetworkView.getExistingVpc())) {
                    throw new CloudConnectorException(String.format(SUBNETVPC_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet(),
                            awsNetworkView.getExistingVpc()));
                }
            }
        }
    }
}
 
Example #9
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe Subnets.
 * @return List of Subnet
 */
protected final List<Subnet> getSubnets() {
    List<Subnet> subnets = null;

    DescribeSubnetsRequest req = new DescribeSubnetsRequest();
    DescribeSubnetsResult result = amazonEC2Client.describeSubnets(req);
    if (result != null && !result.getSubnets().isEmpty()) {
        subnets = result.getSubnets();
    }

     return subnets;
  }
 
Example #10
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe Subnet.
 *
 * @return Subnet
 */
protected final Subnet getSubnet() {
    Subnet subnet = null;

    DescribeSubnetsRequest req = new DescribeSubnetsRequest();
    DescribeSubnetsResult result = amazonEC2Client.describeSubnets(req);
    if (result != null && !result.getSubnets().isEmpty()) {
        subnet = result.getSubnets().get(0);
    }

    return subnet;
}
 
Example #11
Source File: AwsLaunchTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void setupDescribeSubnetResponse() {
    when(amazonEC2Client.describeSubnets(any())).thenAnswer(
            (Answer<DescribeSubnetsResult>) invocation -> {
                DescribeSubnetsRequest request = (DescribeSubnetsRequest) invocation.getArgument(0);
                String subnetId = request.getSubnetIds().get(0);
                DescribeSubnetsResult result = new DescribeSubnetsResult()
                        .withSubnets(new com.amazonaws.services.ec2.model.Subnet()
                                .withSubnetId(subnetId)
                                .withAvailabilityZone(AVAILABILITY_ZONE));
                return result;
            }
    );
}
 
Example #12
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 #13
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 #14
Source File: AwsNetworkService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    boolean mapPublicIpOnLaunch = true;
    if (awsNetworkView.isExistingVPC() && awsNetworkView.isExistingSubnet()) {
        DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
        describeSubnetsRequest.setSubnetIds(awsNetworkView.getSubnetList());
        DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
        if (!describeSubnetsResult.getSubnets().isEmpty()) {
            mapPublicIpOnLaunch = describeSubnetsResult.getSubnets().get(0).isMapPublicIpOnLaunch();
        }
    }
    return mapPublicIpOnLaunch;
}
 
Example #15
Source File: AwsVolumeResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private String getAvailabilityZoneFromSubnet(AuthenticatedContext auth, CloudResource subnetResource) {
    AmazonEc2RetryClient amazonEC2Client = getAmazonEC2Client(auth);
    DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(new DescribeSubnetsRequest()
            .withSubnetIds(subnetResource.getName()));
    return describeSubnetsResult.getSubnets().stream()
            .filter(subnet -> subnetResource.getName().equals(subnet.getSubnetId()))
            .map(Subnet::getAvailabilityZone)
            .findFirst()
            .orElse(auth.getCloudContext().getLocation().getAvailabilityZone().value());
}
 
Example #16
Source File: EC2Mockup.java    From development with Apache License 2.0 5 votes vote down vote up
public void createDescribeSubnetsResult(String... subnetIds) {
    Collection<Subnet> subnets = new ArrayList<Subnet>();
    for (int i = 0; i < subnetIds.length; i++) {
        subnets.add(new Subnet().withSubnetId(subnetIds[i])
                .withVpcId(subnetIds[i]));
    }
    DescribeSubnetsResult subnetResult = new DescribeSubnetsResult()
            .withSubnets(subnets);
    doReturn(subnetResult).when(ec2)
            .describeSubnets(any(DescribeSubnetsRequest.class));
}
 
Example #17
Source File: AwsCommonProcess.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
public List<Subnet> describeSubnetsByVpcId(AwsProcessClient awsProcessClient, String vpcId) {
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    request.withFilters(new Filter().withName("vpc-id").withValues(vpcId));
    DescribeSubnetsResult result = awsProcessClient.getEc2Client().describeSubnets(request);
    List<Subnet> subnets = result.getSubnets();

    return subnets;
}
 
Example #18
Source File: AwsDescribeServiceImpl.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<Subnet> getSubnets(Long userNo, Long platformNo) {
    // VPCかどうかのチェック
    PlatformAws platformAws = platformAwsDao.read(platformNo);
    if (BooleanUtils.isNotTrue(platformAws.getVpc())) {
        // 非VPCの場合、サブネットはない
        return new ArrayList<Subnet>();
    }

    // サブネットを取得
    AwsProcessClient awsProcessClient = awsProcessClientFactory.createAwsProcessClient(userNo, platformNo);
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    request.withFilters(new Filter().withName("vpc-id").withValues(platformAws.getVpcId()));
    DescribeSubnetsResult result = awsProcessClient.getEc2Client().describeSubnets(request);
    List<Subnet> subnets = result.getSubnets();

    // プラットフォームにサブネットが指定されている場合、そのサブネットのみに制限する
    if (StringUtils.isNotEmpty(awsProcessClient.getPlatformAws().getSubnetId())) {
        List<String> subnetIds = new ArrayList<String>();
        for (String subnetId : StringUtils.split(awsProcessClient.getPlatformAws().getSubnetId(), ",")) {
            subnetIds.add(subnetId.trim());
        }

        List<Subnet> subnets2 = new ArrayList<Subnet>();
        for (Subnet subnet : subnets) {
            if (subnetIds.contains(subnet.getSubnetId())) {
                subnets2.add(subnet);
            }
        }
        subnets = subnets2;
    }

    // ソート
    Collections.sort(subnets, Comparators.COMPARATOR_SUBNET);

    return subnets;
}
 
Example #19
Source File: Ec2DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation uses the DescribeSubnets API.
 */
@Override
public List<Subnet> getSubnets(Collection<String> subnetIds, AwsParamsDto awsParamsDto)
{
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
    describeSubnetsRequest.setSubnetIds(subnetIds);
    try
    {
        DescribeSubnetsResult describeSubnetsResult = ec2Operations.describeSubnets(ec2Client, describeSubnetsRequest);
        return describeSubnetsResult.getSubnets();
    }
    catch (AmazonServiceException amazonServiceException)
    {
        /*
         * AWS throws a 400 error when any one of the specified subnet ID is not found.
         * We want to catch it and throw as an handled herd error as a 404 not found.
         */
        if (ERROR_CODE_SUBNET_ID_NOT_FOUND.equals(amazonServiceException.getErrorCode()))
        {
            throw new ObjectNotFoundException(amazonServiceException.getErrorMessage(), amazonServiceException);
        }
        // Any other type of error we throw as is because they are unexpected.
        else
        {
            throw amazonServiceException;
        }
    }
}
 
Example #20
Source File: SubnetImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public boolean load(DescribeSubnetsRequest request,
        ResultCapture<DescribeSubnetsResult> extractor) {

    return resource.load(request, extractor);
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
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 #29
Source File: Ec2OperationsImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public DescribeSubnetsResult describeSubnets(AmazonEC2Client ec2Client, DescribeSubnetsRequest describeSubnetsRequest)
{
    return ec2Client.describeSubnets(describeSubnetsRequest);
}
 
Example #30
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);
}