Search in sources :

Example 1 with NetworkCidr

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);
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) BadRequestException(javax.ws.rs.BadRequestException)

Example 2 with NetworkCidr

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);
    }
}
Also used : SubnetworkList(com.google.api.services.compute.model.SubnetworkList) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Subnetwork(com.google.api.services.compute.model.Subnetwork) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) Compute(com.google.api.services.compute.Compute) IOException(java.io.IOException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) HashSet(java.util.HashSet)

Example 3 with NetworkCidr

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);
}
Also used : NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) Network(com.sequenceiq.cloudbreak.cloud.model.Network) EnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)

Example 4 with NetworkCidr

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;
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) Network(com.sequenceiq.cloudbreak.cloud.model.Network) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) EnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)

Example 5 with NetworkCidr

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);
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) CloudPlatform(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform) EnvironmentTelemetry(com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry) Credential(com.sequenceiq.environment.credential.domain.Credential) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) EnvironmentBackup(com.sequenceiq.environment.environment.dto.EnvironmentBackup) GcpNetwork(com.sequenceiq.environment.network.dao.domain.GcpNetwork) ArrayList(java.util.ArrayList) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) EnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) GcpNetwork(com.sequenceiq.environment.network.dao.domain.GcpNetwork) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) Environment(com.sequenceiq.environment.environment.domain.Environment) ParametersDto(com.sequenceiq.environment.parameter.dto.ParametersDto) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

NetworkCidr (com.sequenceiq.cloudbreak.cloud.network.NetworkCidr)17 Network (com.sequenceiq.cloudbreak.cloud.model.Network)14 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)9 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)8 BaseNetwork (com.sequenceiq.environment.network.dao.domain.BaseNetwork)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)5 NetworkDto (com.sequenceiq.environment.network.dto.NetworkDto)5 EnvironmentNetworkConverter (com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 DescribeVpcsRequest (com.amazonaws.services.ec2.model.DescribeVpcsRequest)4 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)4 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)4 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)4 Credential (com.sequenceiq.environment.credential.domain.Credential)4 Environment (com.sequenceiq.environment.environment.domain.Environment)4 ArrayList (java.util.ArrayList)4 CloudPlatform (com.sequenceiq.cloudbreak.common.mappable.CloudPlatform)3