use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.
the class DefaultSubnetCidrProvider method provide.
@Override
public Cidrs provide(String networkCidr, boolean privateSubnetEnabled) {
Set<NetworkSubnetRequest> result = new HashSet<>();
for (int i = 0; i < SUBNETS; i++) {
String subnet = calculateSubnet(networkCidr, result);
result.add(new NetworkSubnetRequest(subnet, SubnetType.PUBLIC));
}
return cidrs(result, Sets.newHashSet());
}
use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets.
@Test
public void testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets() {
String networkCidr = "0.0.0.0/16";
Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
Map<String, String> output = createOutput();
NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
List<SubnetRequest> subnetRequestList = createSubnetRequestList();
Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());
when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
when(cfClient.waiters()).thenReturn(cfWaiters);
when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
when(cfStackUtil.getOutputs(NETWORK_ID, cfClient)).thenReturn(output);
when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);
CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);
verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
verify(creationWaiter, times(1)).run(any());
verify(cfStackUtil).getOutputs(NETWORK_ID, cfClient);
verify(awsTaggingService, never()).prepareCloudformationTags(any(), any());
verify(cfClient, never()).createStack(any(CreateStackRequest.class));
assertEquals(VPC_ID, actual.getNetworkId());
assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets.
@Test
public void testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets() {
String networkCidr = "0.0.0.0/16";
Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
AmazonServiceException amazonServiceException = new AmazonServiceException("does not exist");
amazonServiceException.setStatusCode(400);
AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
when(cfClient.describeStacks(any(DescribeStacksRequest.class))).thenThrow(amazonServiceException);
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
Map<String, String> output = createOutput();
NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
List<SubnetRequest> subnetRequestList = createSubnetRequestList();
Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());
when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
when(cfClient.waiters()).thenReturn(cfWaiters);
when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
when(cfStackUtil.getOutputs(NETWORK_ID, cfClient)).thenReturn(output);
when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);
CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);
verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
verify(awsNetworkCfTemplateProvider).provide(networkCreationRequest, subnetRequestList);
verify(creationWaiter, times(1)).run(any());
verify(awsTaggingService).prepareCloudformationTags(any(), any());
verify(cfClient).createStack(any(CreateStackRequest.class));
verify(cfStackUtil).getOutputs(NETWORK_ID, cfClient);
assertEquals(VPC_ID, actual.getNetworkId());
assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.
the class ExtendedSubnetTypeProvider method updateCidrAndAddToList.
public void updateCidrAndAddToList(int offset, int count, int step, String[] ip, Set<NetworkSubnetRequest> subnetRequests, SubnetType subnetType, String subnetMask) {
for (int i = 0; i < count; i++) {
int newIpPart = i * step + offset;
ip[2] = String.valueOf(newIpPart);
String cidr = String.join(".", ip) + "/" + subnetMask;
subnetRequests.add(new NetworkSubnetRequest(cidr, subnetType));
}
}
use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.
the class DefaultSubnetCidrProvider method getSubnetCidrInRange.
private String getSubnetCidrInRange(String networkCidr, Iterable<NetworkSubnetRequest> subnetCidrs, int start, int end) {
SubnetUtils.SubnetInfo vpcInfo = new SubnetUtils(networkCidr).getInfo();
String lowProbe = incrementIp(vpcInfo.getLowAddress());
String highProbe = new SubnetUtils(toSubnetCidr(lowProbe)).getInfo().getHighAddress();
// start from the target subnet
for (int i = 0; i < start - 1; i++) {
lowProbe = incrementIp(lowProbe);
highProbe = incrementIp(highProbe);
}
boolean foundProbe = false;
for (int i = start; i < end; i++) {
boolean overlapping = false;
for (NetworkSubnetRequest subnetCidr : subnetCidrs) {
SubnetUtils.SubnetInfo subnetInfo = new SubnetUtils(subnetCidr.getCidr()).getInfo();
if (isInRange(lowProbe, subnetInfo) || isInRange(highProbe, subnetInfo)) {
overlapping = true;
break;
}
}
if (overlapping) {
lowProbe = incrementIp(lowProbe);
highProbe = incrementIp(highProbe);
} else {
foundProbe = true;
break;
}
}
if (foundProbe && isInRange(highProbe, vpcInfo)) {
String subnet = toSubnetCidr(lowProbe);
LOGGER.debug("The following subnet cidr found: {} for VPC: {}", subnet, networkCidr);
return subnet;
} else {
return null;
}
}
Aggregations