use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class AzureNetworkConnector method getNetworkCidr.
@Override
public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) {
AzureClient azureClient = azureClientService.getClient(credential);
String resourceGroupName = azureUtils.getCustomResourceGroupName(network);
String networkId = azureUtils.getCustomNetworkId(network);
com.microsoft.azure.management.network.Network networkByResourceGroup = azureClient.getNetworkByResourceGroup(resourceGroupName, networkId);
if (networkByResourceGroup == null || networkByResourceGroup.addressSpaces().isEmpty()) {
throw new BadRequestException(String.format("Network could not be fetched from Azure with Resource Group name: %s and VNET id: %s. " + "Please make sure that the name of the VNET is correct and is present in the Resource Group specified.", resourceGroupName, networkId));
}
List<String> networkCidrs = networkByResourceGroup.addressSpaces();
if (networkCidrs.size() > 1) {
LOGGER.info("More than one network CIDRs for Resource Group name: {} and network id: {}. We will use the first one: {}", resourceGroupName, networkId, networkCidrs.get(0));
}
return new NetworkCidr(networkCidrs.get(0), networkCidrs);
}
use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class GcpNetworkConnector method getNetworkCidr.
@Override
public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) {
String subnetId = network.getStringParameter(GcpStackUtil.SUBNET_ID);
String region = network.getStringParameter(GcpStackUtil.REGION);
String sharedProjectId = network.getStringParameter(GcpStackUtil.SHARED_PROJECT_ID);
LOGGER.debug("Getting network cidrs for subnet {} in region {}", subnetId, region);
Compute compute = gcpComputeFactory.buildCompute(credential);
String projectId = gcpStackUtil.getProjectId(credential);
Subnetwork subnet = null;
try {
if (Strings.isNullOrEmpty(sharedProjectId)) {
LOGGER.info("We will query the subnets from the {} project in the {} region (NOT Shared VPC scenario)", projectId, region);
SubnetworkList ownProjectSubnets = compute.subnetworks().list(projectId, region).execute();
Set<Subnetwork> collect = new HashSet<>();
if (ownProjectSubnets.getItems() != null) {
collect = ownProjectSubnets.getItems().stream().filter(e -> e.getName().equals(subnetId) || e.getId().toString().equals(subnetId)).collect(Collectors.toSet());
}
subnet = collect.stream().findFirst().orElse(null);
} else {
LOGGER.info("We will query the subnet from the {} project in the {} region (Shared VPC scenario)", sharedProjectId, region);
subnet = compute.subnetworks().get(sharedProjectId, region, subnetId).execute();
}
if (subnet == null) {
throw new GcpResourceException(String.format("Subnet with id %s did not found.", subnetId));
}
String ipCidrRange = subnet.getIpCidrRange();
return new NetworkCidr(ipCidrRange, secondaryRanges(subnet));
} catch (TokenResponseException e) {
throw gcpStackUtil.getMissingServiceAccountKeyError(e, projectId);
} catch (GoogleJsonResponseException e) {
throw exceptionHandlerWithThrow(e, subnetId, GCP_NETWORK);
} catch (IOException e) {
throw new GcpResourceException("Describe subnets failed due to IO exception" + e.getMessage(), GCP_NETWORK, subnetId);
}
}
use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class EnvironmentInitHandler method initEnvironment.
private void initEnvironment(Environment environment) {
generateDomainForEnvironment(environment);
String environmentCrnForVirtualGroups = getEnvironmentCrnForVirtualGroups(environment);
if (!createVirtualGroups(environment, environmentCrnForVirtualGroups)) {
// To keep backward compatibility, if somebody passes the group name, then we shall just use it
environmentService.setAdminGroupName(environment, environment.getAdminGroupName());
}
if (environment.getNetwork() != null && RegistrationType.EXISTING.equals(environment.getNetwork().getRegistrationType())) {
EnvironmentNetworkConverter environmentNetworkConverter = environmentNetworkConverterMap.get(CloudPlatform.valueOf(environment.getCloudPlatform()));
if (environmentNetworkConverter != null) {
Network network = environmentNetworkConverter.convertToNetwork(environment.getNetwork());
NetworkCidr networkCidr = environmentNetworkService.getNetworkCidr(network, environment.getCloudPlatform(), environment.getCredential());
environment.getNetwork().setNetworkCidr(networkCidr.getCidr());
environment.getNetwork().setNetworkCidrs(StringUtils.join(networkCidr.getCidrs(), ","));
}
}
environmentService.assignEnvironmentAdminRole(environment.getCreator(), environmentCrnForVirtualGroups);
setLocationAndRegions(environment);
environmentService.save(environment);
}
use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class NetworkService method refreshMetadataFromCloudProvider.
public BaseNetwork refreshMetadataFromCloudProvider(BaseNetwork originalNetwork, EnvironmentEditDto editDto, Environment environment) {
EnvironmentNetworkConverter environmentNetworkConverter = environmentNetworkConverterMap.get(CloudPlatform.valueOf(environment.getCloudPlatform()));
NetworkDto originalNetworkDto = environmentNetworkConverter.convertToDto(originalNetwork);
NetworkDto cloneNetworkDto = NetworkDto.builder(originalNetworkDto).withSubnetMetas(editDto.getNetworkDto().getSubnetMetas()).build();
try {
Map<String, CloudSubnet> subnetMetadatas = cloudNetworkService.retrieveSubnetMetadata(environment, cloneNetworkDto);
originalNetwork.setSubnetMetas(subnetMetadatas.values().stream().collect(toMap(c -> getId(environment.getCloudPlatform(), c), c -> c)));
Map<String, CloudSubnet> endpointGatewaySubnetMetadatas = cloudNetworkService.retrieveEndpointGatewaySubnetMetadata(environment, cloneNetworkDto);
originalNetwork.setEndpointGatewaySubnetMetas(endpointGatewaySubnetMetadatas.values().stream().collect(toMap(c -> getId(environment.getCloudPlatform(), c), c -> c)));
Network network = environmentNetworkConverter.convertToNetwork(originalNetwork);
NetworkCidr networkCidr = environmentNetworkService.getNetworkCidr(network, environment.getCloudPlatform(), environment.getCredential());
originalNetwork.setNetworkCidr(networkCidr.getCidr());
originalNetwork.setNetworkCidrs(StringUtils.join(networkCidr.getCidrs(), ","));
} catch (NetworkConnectorNotFoundException connectorNotFoundException) {
throw new BadRequestException(connectorNotFoundException.getMessage());
}
return originalNetwork;
}
use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class NetworkServiceTest method testRefreshMetadataFromAwsCloudProviderMustUseSubnetId.
@Test
public void testRefreshMetadataFromAwsCloudProviderMustUseSubnetId() {
NetworkDto networkDto = mock(NetworkDto.class);
AuthenticationDto authenticationDto = mock(AuthenticationDto.class);
EnvironmentTelemetry environmentTelemetry = mock(EnvironmentTelemetry.class);
EnvironmentBackup environmentBackup = mock(EnvironmentBackup.class);
SecurityAccessDto securityAccessDto = mock(SecurityAccessDto.class);
ParametersDto parametersDto = mock(ParametersDto.class);
EnvironmentNetworkConverter environmentNetworkConverter = mock(EnvironmentNetworkConverter.class);
Network network = mock(Network.class);
Credential credential = mock(Credential.class);
BaseNetwork baseNetwork = new GcpNetwork();
baseNetwork.setRegistrationType(RegistrationType.EXISTING);
Environment environment = new Environment();
environment.setCloudPlatform("AWS");
environment.setCredential(credential);
EnvironmentEditDto environmentEditDto = new EnvironmentEditDto("description", "accountId", networkDto, authenticationDto, environmentTelemetry, environmentBackup, securityAccessDto, Tunnel.CCMV2, IdBrokerMappingSource.MOCK, CloudStorageValidation.ENABLED, "adminGroupName", parametersDto);
when(environmentNetworkConverterMap.get(any(CloudPlatform.class))).thenReturn(environmentNetworkConverter);
when(environmentNetworkConverter.convertToDto(baseNetwork)).thenReturn(networkDto);
when(cloudNetworkService.retrieveSubnetMetadata(any(Environment.class), any(NetworkDto.class))).thenReturn(Map.of("s1", cloudSubnet("s1", "subnet1")));
when(cloudNetworkService.retrieveEndpointGatewaySubnetMetadata(any(Environment.class), any(NetworkDto.class))).thenReturn(Map.of("s1", cloudSubnet("s1", "subnet1")));
when(environmentNetworkConverter.convertToNetwork(any(BaseNetwork.class))).thenReturn(network);
when(environmentNetworkService.getNetworkCidr(any(Network.class), anyString(), any(Credential.class))).thenReturn(new NetworkCidr("10.0.0.0", new ArrayList<>()));
BaseNetwork result = underTest.refreshMetadataFromCloudProvider(baseNetwork, environmentEditDto, environment);
Assertions.assertEquals(result.getSubnetMetas().keySet().stream().findFirst().get(), "s1");
Assertions.assertEquals(result.getSubnetMetas().keySet().size(), 1);
}
Aggregations