@Override protected void consumeSuccess(DescribeVpcsRequest request, DescribeVpcsResult result) { URI adapterUri = AdapterUriUtil.buildAdapterUri(getHost(), AWSUriPaths.AWS_NETWORK_ADAPTER); for (Vpc resultVPC : result.getVpcs()) { NetworkState networkState = mapVPCToNetworkState(resultVPC, this.context.request.regionId, this.context.request.request.resourcePoolLink, this.context.request.request.endpointLink, this.context.request.endpointAuth.documentSelfLink, this.context.request.parentComputeLink, this.context.request.tenantLinks, adapterUri); if (networkState.subnetCIDR == null) { logWarning(() -> String.format("AWS did not return CIDR information for VPC %s", resultVPC.toString())); } this.context.awsVpcs.put(resultVPC.getVpcId(), resultVPC); this.context.vpcs.put(resultVPC.getVpcId(), networkState); } }
@Override @Cacheable(value = CachingConfiguration.VPC_CACHE, key = "#vpcId", condition = "#bypassCache == false") public Vpc getVpcInRegion(final 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 VPC {} in region {} ({})", vpcId, region, bypassCache); DescribeVpcsRequest request = new DescribeVpcsRequest() .withVpcIds(vpcId); DescribeVpcsResult result = getClientForRegion(region).describeVpcs(request); List<Vpc> results = result.getVpcs(); if (results.size() != 1) { throw new IllegalArgumentException("Did not get expected result"); } return results.get(0); }
/** * Gets the default VPC */ public static Vpc getDefaultVPC(AWSInstanceContext aws) { DescribeVpcsResult result = aws.amazonEC2Client.describeVpcs(); List<Vpc> vpcs = result.getVpcs(); for (Vpc vpc : vpcs) { if (vpc.isDefault()) { return vpc; } } return null; }
public Vpc getVPC(String vpcId) { DescribeVpcsRequest req = new DescribeVpcsRequest().withVpcIds(vpcId); DescribeVpcsResult result = this.client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); if (vpcs != null && vpcs.size() == 1) { return vpcs.get(0); } return null; }
/** * Get the default VPC - return null if no default specified */ public Vpc getDefaultVPC() { DescribeVpcsRequest req = new DescribeVpcsRequest(); DescribeVpcsResult result = this.client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); for (Vpc vpc : vpcs) { if (vpc.isDefault()) { return vpc; } } return null; }
@Override @Cacheable(value = CachingConfiguration.VPC_LISTS_CACHE, key = "#region", condition = "#bypassCache == false") public List<Vpc> getVpcsInRegion(final String region, boolean bypassCache) { Preconditions.checkArgument(StringUtils.isNotBlank(region), "region may not be null or blank"); LOG.info("Retrieving all VPCs in region {} ({})", region, bypassCache); DescribeVpcsResult result = getClientForRegion(region).describeVpcs(); return result.getVpcs(); }
public Vpc createVPC() throws InterruptedException { logger.info("create VPC"); String vpcId = ec2.createVpc(new CreateVpcRequest().withCidrBlock("10.0.0.0/16")).getVpc().getVpcId(); while (true) { Threads.sleepRoughly(Duration.ofSeconds(20)); DescribeVpcsResult result = ec2.describeVpcs(new DescribeVpcsRequest().withVpcIds(vpcId)); Vpc remoteVPC = result.getVpcs().get(0); if ("available".equals(remoteVPC.getState())) { enableVPCDNS(vpcId); return remoteVPC; } } }
@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<?>> describeVpcs(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeVpcsRequest req = new DescribeVpcsRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing vpcs for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeVpcsResult res = ec2.describeVpcs(req); return converter.toVpcVpcs(res.getVpcs(), account.getId(), region, dt); }
/** * Describe VPCs. * * @return List of vpcs */ protected final List<Vpc> describeVpcs() { DescribeVpcsRequest req = new DescribeVpcsRequest(); DescribeVpcsResult result = amazonEC2Client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); return vpcs; }
@Override public DescribeVpcsResult describeVpcs(DescribeVpcsRequest describeVpcsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Override public DescribeVpcsResult describeVpcs() throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Test public void cleanUpAWSEC2() { if (this.isMock) { return; } for (AmazonEC2 ec2Client : this.ec2Clients.values()) { try { DescribeVpcsResult describeVpcsRequest = ec2Client.describeVpcs(); List<Vpc> vpcs = describeVpcsRequest.getVpcs(); List<String> enumTestVpcIds = new ArrayList<>(); List<String> instanceIdsToBeDeleted = new ArrayList<>(); vpcs.stream() .forEach(vpc -> { vpc.getTags().stream() .forEach(tag -> { if (tag.getKey().equalsIgnoreCase("name") && tag.getValue() .equalsIgnoreCase("enumtest-vpc")) { enumTestVpcIds.add(vpc.getVpcId()); } }); }); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(); List<Reservation> reservations = describeInstancesResult.getReservations(); for (Reservation reservation : reservations) { List<Instance> instances = reservation.getInstances(); for (Instance instance : instances) { long instanceLaunchTimeMicros = TimeUnit.MILLISECONDS .toMicros(instance.getLaunchTime().getTime()); long timeDifference = Utils.getNowMicrosUtc() - instanceLaunchTimeMicros; if (timeDifference > TimeUnit.HOURS.toMicros(1) && enumTestVpcIds.contains(instance.getVpcId()) && shouldDelete(instance)) { instanceIdsToBeDeleted.add(instance.getInstanceId()); } } } if (instanceIdsToBeDeleted.isEmpty()) { continue; } TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(instanceIdsToBeDeleted); TerminateInstancesResult terminateInstancesResult = ec2Client .terminateInstances(terminateInstancesRequest); terminateInstancesResult.getTerminatingInstances().stream() .forEach(instanceStateChange -> { this.host.log("Terminating stale instance: %s", instanceStateChange.getInstanceId()); }); } catch (Exception e) { this.host.log(Level.INFO, e.getMessage()); continue; } } }
public Vpc describeVPC(String vpcId) { logger.info("describe vpc, vpcId={}", vpcId); DescribeVpcsResult result = ec2.describeVpcs(new DescribeVpcsRequest().withVpcIds(vpcId)); return result.getVpcs().get(0); }
@Override public boolean load(DescribeVpcsRequest request, ResultCapture<DescribeVpcsResult> 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); }
@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()); 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(cloudContext.getName()).thenReturn(new String(new 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); }
@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()); 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(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); }
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet, and use a ResultCapture to retrieve the low-level * client response * The following request parameters will be populated from the data of this * <code>Vpc</code> resource, and any conflicting parameter value set in the * request will be overridden: * <ul> * <li> * <b><code>VpcIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeVpcsRequest */ boolean load(DescribeVpcsRequest request, ResultCapture<DescribeVpcsResult> extractor);