/** * 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); }
public void deleteAwsSubnet() { if (this.isMock) { return; } DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest() .withFilters( new Filter(AWS_VPC_ID_FILTER, singletonList(AWS_DEFAULT_VPC_ID))) .withFilters( new Filter(AWS_SUBNET_CIDR_FILTER, singletonList(AWS_NON_EXISTING_SUBNET_CIDR))); DescribeSubnetsResult subnetResult = this.client.describeSubnets(subnetRequest); subnetResult.getSubnets().forEach(subnet -> { DeleteSubnetRequest deleteRequest = new DeleteSubnetRequest(subnet.getSubnetId()); this.client.deleteSubnet(deleteRequest); }); }
public void deleteAwsPublicSubnet() { if (this.isMock) { return; } DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest() .withFilters( new Filter(AWS_VPC_ID_FILTER, singletonList(AWS_DEFAULT_VPC_ID))) .withFilters( new Filter(AWS_SUBNET_CIDR_FILTER, singletonList(AWS_NON_EXISTING_PUBLIC_SUBNET_CIDR))); DescribeSubnetsResult subnetResult = this.client.describeSubnets(subnetRequest); subnetResult.getSubnets().forEach(subnet -> { DeleteSubnetRequest deleteRequest = new DeleteSubnetRequest(subnet.getSubnetId()); this.client.deleteSubnet(deleteRequest); }); }
@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; }
private void validateExistingSubnet(AwsNetworkView awsNetworkView, AmazonEC2Client 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())); } } } } }
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)); }
public Subnet getSubnet(String subnetId) { DescribeSubnetsRequest req = new DescribeSubnetsRequest() .withSubnetIds(subnetId); DescribeSubnetsResult subnetResult = this.client.describeSubnets(req); List<Subnet> subnets = subnetResult.getSubnets(); return subnets.isEmpty() ? null : subnets.get(0); }
/** * Update the Subnet information for the VPC in question. */ @Override protected void consumeSuccess(DescribeSubnetsRequest request, DescribeSubnetsResult result) { for (Subnet subnet : result.getSubnets()) { if (!this.context.vpcs.containsKey(subnet.getVpcId())) { logWarning(() -> String.format("AWS returned Subnet [%s] with VCP [%s] that is" + " missing locally.", subnet.getSubnetId(), subnet.getVpcId())); continue; } SubnetState subnetState = mapSubnetToSubnetState(subnet, this.context.request.tenantLinks, this.context.request.regionId, this.context.request.parentComputeLink, this.context.request.request.endpointLink); if (subnetState.subnetCIDR == null) { logWarning(() -> String.format("AWS did not return CIDR information for Subnet" + " %s", subnet.toString())); } this.context.awsSubnets.put(subnet.getSubnetId(), subnet); this.context.subnets.put( subnet.getSubnetId(), new AWSNetworkStateCreationContext.SubnetStateWithParentVpcId( subnet.getVpcId(), subnetState)); } }
/** * Creates a Subnet if not exist and return the Subnet id. */ public static String createOrGetSubnet(AmazonEC2AsyncClient client, String subnetCidr, String vpcId, String zoneId) { List<Filter> filters = new ArrayList<>(); Filter cidrBlockFilter = new Filter(); cidrBlockFilter.withName("cidrBlock"); cidrBlockFilter.withValues(subnetCidr); filters.add(cidrBlockFilter); if (zoneId != null) { Filter azFilter = new Filter(); azFilter.withName("availabilityZone"); azFilter.withValues(zoneId); filters.add(azFilter); } DescribeSubnetsResult result = client.describeSubnets(new DescribeSubnetsRequest() .withFilters(filters)); if (result.getSubnets() != null && !result.getSubnets().isEmpty()) { return result.getSubnets().get(0).getSubnetId(); } else { CreateSubnetRequest req = new CreateSubnetRequest() .withCidrBlock(subnetCidr) .withVpcId(vpcId); if (zoneId != null) { req.withAvailabilityZone(zoneId); } CreateSubnetResult res = client.createSubnet(req); return res.getSubnet().getSubnetId(); } }
/** * Validates the configured subnet ID. * * @param client the EC2 client * @param configuration the configuration to be validated * @param accumulator the exception condition accumulator * @param localizationContext the localization context * * @return the vpc id to subnet id mapping */ @VisibleForTesting Map<String, String> checkSubnetId(AmazonEC2Client client, Configured configuration, PluginExceptionConditionAccumulator accumulator, LocalizationContext localizationContext) { String subnetId = configuration.getConfigurationValue(SUBNET_ID, localizationContext); LOG.info(">> Describing subnet '{}'", subnetId); try { DescribeSubnetsResult result = client.describeSubnets( new DescribeSubnetsRequest().withSubnetIds(subnetId)); checkCount(accumulator, SUBNET_ID, localizationContext, "Subnet", result.getSubnets()); if (result.getSubnets().size() == 1) { return ImmutableMap.of(Iterables.getOnlyElement(result.getSubnets()).getVpcId(), subnetId); } } catch (AmazonServiceException e) { if (e.getErrorCode().startsWith(INVALID_SUBNET_ID)) { addError(accumulator, SUBNET_ID, localizationContext, null, INVALID_SUBNET_MSG, subnetId); } else { throw Throwables.propagate(e); } } return ImmutableMap.of(); }
/** * Get the availability zone from a Subnet ID. * * @param subnetId the id of the subnet * @return the availability zone of the subnet */ private String getAvailabilityZoneFromSubnetId(String subnetId) { DescribeSubnetsRequest request = new DescribeSubnetsRequest().withSubnetIds(subnetId); DescribeSubnetsResult result = client.describeSubnets(request); Subnet subnet = Iterables.getOnlyElement(result.getSubnets()); return subnet.getAvailabilityZone(); }
/** * 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; } } }
@Override @Cacheable(value = CachingConfiguration.SUBNET_CACHE, key = "#vpcId", condition = "#bypassCache == false") public List<Subnet> getSubnetsForVpcInRegion(String vpcId, final String region, boolean bypassCache) { Preconditions.checkArgument(StringUtils.isNotBlank(vpcId), "vpcId may not be null or blank"); Preconditions.checkArgument(StringUtils.isNotBlank(region), "region may not be null or blank"); LOG.info("Retrieving subnets for VPC {} in region {} ({})", vpcId, region, bypassCache); DescribeSubnetsRequest request = new DescribeSubnetsRequest() .withFilters(new Filter() .withName("vpc-id") .withValues(vpcId)); DescribeSubnetsResult result = getClientForRegion(region).describeSubnets(request); return result.getSubnets(); }
/** * {@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; }
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; }
private boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2Client 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; }
@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()); 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()); 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); }
@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()); 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()); 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); }
@Override public List<AbstractResource<?>> describeSubnets(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeSubnetsRequest req = new DescribeSubnetsRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing subnets for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeSubnetsResult res = ec2.describeSubnets(req); return converter.toVpcSubnets(res.getSubnets(), account.getId(), region, dt); }
private Subnet getSubnetDetails(String physicalId) { DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest(); Collection<String> subnetIds = new LinkedList<>(); subnetIds.add(physicalId); describeSubnetsRequest.setSubnetIds(subnetIds); DescribeSubnetsResult result = ec2Client.describeSubnets(describeSubnetsRequest); assertEquals(1, result.getSubnets().size()); return result.getSubnets().get(0); }
/** * 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; }
/** * 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; }
@Override public DescribeSubnetsResult describeSubnets(DescribeSubnetsRequest describeSubnetsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Override public DescribeSubnetsResult describeSubnets() throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Override public DescribeSubnetsResult describeSubnets(AmazonEC2Client ec2Client, DescribeSubnetsRequest describeSubnetsRequest) { return ec2Client.describeSubnets(describeSubnetsRequest); }
/** * In-memory implementation of describeSubnets. Returns the subnets from the pre-configured subnets. The method can be ordered to throw an * AmazonServiceException with a specified error code by specifying a subnet ID with prefix "throw." followed by a string indicating the error code to * throw. The exception thrown in this manner will always set the status code to 500. */ @Override public DescribeSubnetsResult describeSubnets(AmazonEC2Client ec2Client, DescribeSubnetsRequest describeSubnetsRequest) { List<Subnet> subnets = new ArrayList<>(); List<String> requestedSubnetIds = describeSubnetsRequest.getSubnetIds(); // add all subnets if request is empty (this is AWS behavior) if (requestedSubnetIds.isEmpty()) { requestedSubnetIds.addAll(mockSubnets.keySet()); } for (String requestedSubnetId : requestedSubnetIds) { MockSubnet mockSubnet = mockSubnets.get(requestedSubnetId); // Throw exception if any of the subnet ID do not exist if (mockSubnet == null) { AmazonServiceException amazonServiceException; if (requestedSubnetId.startsWith("throw.")) { String errorCode = requestedSubnetId.substring("throw.".length()); amazonServiceException = new AmazonServiceException(errorCode); amazonServiceException.setErrorCode(errorCode); amazonServiceException.setStatusCode(500); } else { amazonServiceException = new AmazonServiceException("The subnet ID '" + requestedSubnetId + "' does not exist"); amazonServiceException.setErrorCode(Ec2DaoImpl.ERROR_CODE_SUBNET_ID_NOT_FOUND); amazonServiceException.setStatusCode(400); } throw amazonServiceException; } subnets.add(mockSubnet.toAwsObject()); } DescribeSubnetsResult describeSubnetsResult = new DescribeSubnetsResult(); describeSubnetsResult.setSubnets(subnets); return describeSubnetsResult; }
private void loadRemoteSubnets(Map<String, Subnet> remoteSubnets) { DescribeSubnetsResult result = AWS.vpc.ec2.describeSubnets(new DescribeSubnetsRequest().withSubnetIds(remoteSubnets.keySet())); for (com.amazonaws.services.ec2.model.Subnet remoteSubnet : result.getSubnets()) { remoteSubnets.get(remoteSubnet.getSubnetId()).remoteSubnets.add(remoteSubnet); } }
@Override public boolean load(DescribeSubnetsRequest request, ResultCapture<DescribeSubnetsResult> extractor) { return resource.load(request, extractor); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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) 200, (byte) 200, (byte) 200, (byte) 200, (byte) 200, (byte) 200, (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); }
@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()); 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()); 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[]{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); }
@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()); 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()); 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) 200, (byte) 200, (byte) 200, (byte) 172})); 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); }