Search in sources :

Example 16 with EnvironmentCreationDto

use of com.sequenceiq.environment.environment.dto.EnvironmentCreationDto in project cloudbreak by hortonworks.

the class EnvironmentApiConverterTest method testAzureResourceEncryptionParametersAndAzureRequest.

@Test
void testAzureResourceEncryptionParametersAndAzureRequest() {
    EnvironmentRequest request = createEnvironmentRequest(AZURE);
    request.setAzure(AzureEnvironmentParameters.builder().withResourceEncryptionParameters(AzureResourceEncryptionParameters.builder().withEncryptionKeyUrl(KEY_URL).withEncryptionKeyResourceGroupName(KEY_URL_RESOURCE_GROUP).build()).build());
    FreeIpaCreationDto freeIpaCreationDto = mock(FreeIpaCreationDto.class);
    EnvironmentTelemetry environmentTelemetry = mock(EnvironmentTelemetry.class);
    EnvironmentBackup environmentBackup = mock(EnvironmentBackup.class);
    AccountTelemetry accountTelemetry = mock(AccountTelemetry.class);
    Features features = mock(Features.class);
    NetworkDto networkDto = mock(NetworkDto.class);
    when(credentialService.getCloudPlatformByCredential(anyString(), anyString(), any())).thenReturn(AZURE.name());
    when(freeIpaConverter.convert(request.getFreeIpa(), "id", CloudConstants.AWS)).thenReturn(freeIpaCreationDto);
    when(accountTelemetry.getFeatures()).thenReturn(features);
    when(accountTelemetryService.getOrDefault(any())).thenReturn(accountTelemetry);
    when(telemetryApiConverter.convert(eq(request.getTelemetry()), any(), anyString())).thenReturn(environmentTelemetry);
    when(backupConverter.convert(eq(request.getBackup()))).thenReturn(environmentBackup);
    when(tunnelConverter.convert(request.getTunnel())).thenReturn(request.getTunnel());
    when(networkRequestToDtoConverter.convert(request.getNetwork())).thenReturn(networkDto);
    EnvironmentCreationDto actual = testInitCreationDto(request);
    assertEquals(KEY_URL, actual.getParameters().getAzureParametersDto().getAzureResourceEncryptionParametersDto().getEncryptionKeyUrl());
    assertEquals(KEY_URL_RESOURCE_GROUP, actual.getParameters().getAzureParametersDto().getAzureResourceEncryptionParametersDto().getEncryptionKeyResourceGroupName());
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) EnvironmentTelemetry(com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry) FreeIpaCreationDto(com.sequenceiq.environment.environment.dto.FreeIpaCreationDto) EnvironmentBackup(com.sequenceiq.environment.environment.dto.EnvironmentBackup) AccountTelemetry(com.sequenceiq.environment.telemetry.domain.AccountTelemetry) EnvironmentRequest(com.sequenceiq.environment.api.v1.environment.model.request.EnvironmentRequest) Features(com.sequenceiq.common.api.telemetry.model.Features) ExperimentalFeatures(com.sequenceiq.environment.environment.domain.ExperimentalFeatures) EnvironmentCreationDto(com.sequenceiq.environment.environment.dto.EnvironmentCreationDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with EnvironmentCreationDto

use of com.sequenceiq.environment.environment.dto.EnvironmentCreationDto in project cloudbreak by hortonworks.

the class EnvironmentCreationServiceTest method testEncryptionKeyValidationError.

@Test
void testEncryptionKeyValidationError() {
    final EnvironmentCreationDto environmentCreationDto = EnvironmentCreationDto.builder().withName(ENVIRONMENT_NAME).withCloudPlatform("GCP").withCreator(CRN).withAccountId(ACCOUNT_ID).withAuthentication(AuthenticationDto.builder().build()).build();
    final Environment environment = new Environment();
    environment.setName(ENVIRONMENT_NAME);
    environment.setId(1L);
    environment.setAccountId(ACCOUNT_ID);
    Credential credential = new Credential();
    credential.setCloudPlatform("GCP");
    ValidationResultBuilder validationResultBuilder = new ValidationResultBuilder();
    validationResultBuilder.error("error");
    when(validatorService.validateEncryptionKeyUrl(any(), any())).thenReturn(validationResultBuilder.build());
    when(environmentService.isNameOccupied(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID))).thenReturn(false);
    when(environmentDtoConverter.creationDtoToEnvironment(eq(environmentCreationDto))).thenReturn(environment);
    when(environmentResourceService.getCredentialFromRequest(any(), any())).thenReturn(credential);
    when(validatorService.validateParentChildRelation(any(), any())).thenReturn(ValidationResult.builder().build());
    when(validatorService.validateNetworkCreation(any(), any())).thenReturn(ValidationResult.builder());
    when(validatorService.validateFreeIpaCreation(any())).thenReturn(ValidationResult.builder().build());
    when(authenticationDtoConverter.dtoToAuthentication(any())).thenReturn(new EnvironmentAuthentication());
    when(entitlementService.azureEnabled(eq(ACCOUNT_ID))).thenReturn(true);
    when(environmentService.save(any())).thenReturn(environment);
    assertThrows(BadRequestException.class, () -> environmentCreationServiceUnderTest.create(environmentCreationDto));
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential) EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder) Environment(com.sequenceiq.environment.environment.domain.Environment) EnvironmentCreationDto(com.sequenceiq.environment.environment.dto.EnvironmentCreationDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with EnvironmentCreationDto

use of com.sequenceiq.environment.environment.dto.EnvironmentCreationDto in project cloudbreak by hortonworks.

the class EnvironmentCreationServiceTest method testCreateAzureDisabled.

@Test
void testCreateAzureDisabled() {
    ParametersDto parametersDto = ParametersDto.builder().withAwsParameters(AwsParametersDto.builder().withDynamoDbTableName("dynamo").build()).build();
    final EnvironmentCreationDto environmentCreationDto = EnvironmentCreationDto.builder().withName(ENVIRONMENT_NAME).withCloudPlatform("AZURE").withCreator(CRN).withAccountId(ACCOUNT_ID).withAuthentication(AuthenticationDto.builder().build()).withParameters(parametersDto).withLocation(LocationDto.builder().withName("test").withDisplayName("test").withLatitude(0.1).withLongitude(0.1).build()).build();
    final Environment environment = new Environment();
    environment.setName(ENVIRONMENT_NAME);
    environment.setId(1L);
    environment.setAccountId(ACCOUNT_ID);
    Credential credential = new Credential();
    credential.setCloudPlatform("AZURE");
    when(environmentService.isNameOccupied(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID))).thenReturn(false);
    when(environmentDtoConverter.creationDtoToEnvironment(eq(environmentCreationDto))).thenReturn(environment);
    when(environmentResourceService.getCredentialFromRequest(any(), any())).thenReturn(credential);
    when(validatorService.validateParentChildRelation(any(), any())).thenReturn(ValidationResult.builder().build());
    when(validatorService.validateNetworkCreation(any(), any())).thenReturn(ValidationResult.builder());
    when(validatorService.validateFreeIpaCreation(any())).thenReturn(ValidationResult.builder().build());
    when(authenticationDtoConverter.dtoToAuthentication(any())).thenReturn(new EnvironmentAuthentication());
    when(environmentService.getRegionsByEnvironment(eq(environment))).thenReturn(getCloudRegions());
    when(environmentService.save(any())).thenReturn(environment);
    when(entitlementService.azureEnabled(eq(ACCOUNT_ID))).thenReturn(false);
    assertThrows(BadRequestException.class, () -> environmentCreationServiceUnderTest.create(environmentCreationDto));
    verify(validatorService, Mockito.times(1)).validatePublicKey(any());
    verify(environmentService, never()).save(any());
    verify(environmentResourceService, never()).createAndSetNetwork(any(), any(), any(), any(), any());
    verify(reactorFlowManager, never()).triggerCreationFlow(anyLong(), eq(ENVIRONMENT_NAME), eq(USER), anyString());
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential) EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) Environment(com.sequenceiq.environment.environment.domain.Environment) AwsDiskEncryptionParametersDto(com.sequenceiq.environment.parameter.dto.AwsDiskEncryptionParametersDto) ParametersDto(com.sequenceiq.environment.parameter.dto.ParametersDto) AzureParametersDto(com.sequenceiq.environment.parameter.dto.AzureParametersDto) AzureResourceEncryptionParametersDto(com.sequenceiq.environment.parameter.dto.AzureResourceEncryptionParametersDto) AwsParametersDto(com.sequenceiq.environment.parameter.dto.AwsParametersDto) EnvironmentCreationDto(com.sequenceiq.environment.environment.dto.EnvironmentCreationDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with EnvironmentCreationDto

use of com.sequenceiq.environment.environment.dto.EnvironmentCreationDto in project cloudbreak by hortonworks.

the class EnvironmentCreationServiceTest method testCreate.

@Test
void testCreate() {
    ParametersDto parametersDto = ParametersDto.builder().withAwsParameters(AwsParametersDto.builder().withDynamoDbTableName("dynamo").build()).build();
    String environmentCrn = "crn";
    final EnvironmentCreationDto environmentCreationDto = EnvironmentCreationDto.builder().withName(ENVIRONMENT_NAME).withCreator(CRN).withAccountId(ACCOUNT_ID).withCrn(environmentCrn).withAuthentication(AuthenticationDto.builder().build()).withParameters(parametersDto).withLocation(LocationDto.builder().withName("test").withDisplayName("test").withLatitude(0.1).withLongitude(0.1).build()).build();
    final Environment environment = new Environment();
    environment.setName(ENVIRONMENT_NAME);
    environment.setId(1L);
    environment.setAccountId(ACCOUNT_ID);
    Credential credential = new Credential();
    credential.setCloudPlatform("platform");
    when(environmentService.isNameOccupied(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID))).thenReturn(false);
    when(environmentDtoConverter.creationDtoToEnvironment(eq(environmentCreationDto))).thenReturn(environment);
    when(environmentResourceService.getCredentialFromRequest(any(), eq(ACCOUNT_ID))).thenReturn(credential);
    when(validatorService.validateParentChildRelation(any(), any())).thenReturn(ValidationResult.builder().build());
    when(validatorService.validateNetworkCreation(any(), any())).thenReturn(ValidationResult.builder());
    when(validatorService.validateFreeIpaCreation(any())).thenReturn(ValidationResult.builder().build());
    when(authenticationDtoConverter.dtoToAuthentication(any())).thenReturn(new EnvironmentAuthentication());
    when(environmentService.getRegionsByEnvironment(eq(environment))).thenReturn(getCloudRegions());
    when(environmentService.save(any())).thenReturn(environment);
    environmentCreationServiceUnderTest.create(environmentCreationDto);
    verify(validatorService, Mockito.times(1)).validatePublicKey(any());
    verify(environmentService).save(any());
    verify(parametersService).saveParameters(eq(environment), eq(parametersDto));
    verify(environmentResourceService).createAndSetNetwork(any(), any(), any(), any(), any());
    verify(reactorFlowManager).triggerCreationFlow(eq(1L), eq(ENVIRONMENT_NAME), eq(CRN), anyString());
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential) EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) Environment(com.sequenceiq.environment.environment.domain.Environment) AwsDiskEncryptionParametersDto(com.sequenceiq.environment.parameter.dto.AwsDiskEncryptionParametersDto) ParametersDto(com.sequenceiq.environment.parameter.dto.ParametersDto) AzureParametersDto(com.sequenceiq.environment.parameter.dto.AzureParametersDto) AzureResourceEncryptionParametersDto(com.sequenceiq.environment.parameter.dto.AzureResourceEncryptionParametersDto) AwsParametersDto(com.sequenceiq.environment.parameter.dto.AwsParametersDto) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EnvironmentCreationDto(com.sequenceiq.environment.environment.dto.EnvironmentCreationDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with EnvironmentCreationDto

use of com.sequenceiq.environment.environment.dto.EnvironmentCreationDto in project cloudbreak by hortonworks.

the class EnvironmentCreationService method validateEncryptionKey.

private ValidationResult validateEncryptionKey(EnvironmentCreationDto creationDto) {
    ValidationResultBuilder resultBuilder = ValidationResult.builder();
    String cloudPlatform = creationDto.getCloudPlatform().toLowerCase(Locale.ROOT);
    switch(cloudPlatform) {
        case "azure":
            String encryptionKeyUrl = Optional.ofNullable(creationDto.getParameters()).map(paramsDto -> paramsDto.getAzureParametersDto()).map(azureParamsDto -> azureParamsDto.getAzureResourceEncryptionParametersDto()).map(azureREParamsDto -> azureREParamsDto.getEncryptionKeyUrl()).orElse(null);
            if (encryptionKeyUrl != null) {
                resultBuilder.merge(validatorService.validateEncryptionKeyUrl(encryptionKeyUrl, creationDto.getAccountId()));
            }
            break;
        case "gcp":
            String encryptionKey = Optional.ofNullable(creationDto.getParameters()).map(parametersDto -> parametersDto.getGcpParametersDto()).map(gcpParametersDto -> gcpParametersDto.getGcpResourceEncryptionParametersDto()).map(gcpREParamsDto -> gcpREParamsDto.getEncryptionKey()).orElse(null);
            if (encryptionKey != null) {
                resultBuilder.merge(validatorService.validateEncryptionKey(encryptionKey, creationDto.getAccountId()));
            }
            break;
        case "aws":
            String encryptionKeyArn = Optional.ofNullable(creationDto.getParameters()).map(paramsDto -> paramsDto.getAwsParametersDto()).map(awsParamsDto -> awsParamsDto.getAwsDiskEncryptionParametersDto()).map(awsREparamsDto -> awsREparamsDto.getEncryptionKeyArn()).orElse(null);
            if (encryptionKeyArn != null) {
                resultBuilder.merge(validatorService.validateEncryptionKeyArn(encryptionKeyArn, creationDto.getAccountId()));
            }
            break;
        default:
            break;
    }
    return resultBuilder.build();
}
Also used : EnvironmentDtoConverter(com.sequenceiq.environment.environment.dto.EnvironmentDtoConverter) Environment(com.sequenceiq.environment.environment.domain.Environment) NullUtil.getIfNotNull(com.sequenceiq.cloudbreak.util.NullUtil.getIfNotNull) EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) AuthenticationDtoConverter(com.sequenceiq.environment.environment.dto.AuthenticationDtoConverter) Credential(com.sequenceiq.environment.credential.domain.Credential) LoggerFactory(org.slf4j.LoggerFactory) EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) EnvironmentTelemetry(com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry) LoadBalancerEntitlementService(com.sequenceiq.environment.network.service.LoadBalancerEntitlementService) StringUtils(org.apache.commons.lang3.StringUtils) Value(org.springframework.beans.factory.annotation.Value) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder) Locale(java.util.Locale) Service(org.springframework.stereotype.Service) PublicEndpointAccessGateway(com.sequenceiq.common.api.type.PublicEndpointAccessGateway) BadRequestException(javax.ws.rs.BadRequestException) ParametersService(com.sequenceiq.environment.parameters.service.ParametersService) ExperimentalFeatures(com.sequenceiq.environment.environment.domain.ExperimentalFeatures) EnvironmentStatus(com.sequenceiq.environment.environment.EnvironmentStatus) Tunnel(com.sequenceiq.common.api.type.Tunnel) Logger(org.slf4j.Logger) EnvironmentCreationDto(com.sequenceiq.environment.environment.dto.EnvironmentCreationDto) ParametersDto(com.sequenceiq.environment.parameter.dto.ParametersDto) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn) EnvironmentValidatorService(com.sequenceiq.environment.environment.validation.EnvironmentValidatorService) NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) Optional(java.util.Optional) GCP(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform.GCP) AZURE(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform.AZURE) ProxyConfig(com.sequenceiq.environment.proxy.domain.ProxyConfig) EnvironmentReactorFlowManager(com.sequenceiq.environment.environment.flow.EnvironmentReactorFlowManager) ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder)

Aggregations

EnvironmentCreationDto (com.sequenceiq.environment.environment.dto.EnvironmentCreationDto)21 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 Test (org.junit.jupiter.api.Test)16 Environment (com.sequenceiq.environment.environment.domain.Environment)11 Credential (com.sequenceiq.environment.credential.domain.Credential)10 EnvironmentAuthentication (com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)9 NetworkDto (com.sequenceiq.environment.network.dto.NetworkDto)9 EnvironmentRequest (com.sequenceiq.environment.api.v1.environment.model.request.EnvironmentRequest)8 ExperimentalFeatures (com.sequenceiq.environment.environment.domain.ExperimentalFeatures)8 FreeIpaCreationDto (com.sequenceiq.environment.environment.dto.FreeIpaCreationDto)8 EnvironmentTelemetry (com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry)8 ValidationResultBuilder (com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder)7 Features (com.sequenceiq.common.api.telemetry.model.Features)7 ParametersDto (com.sequenceiq.environment.parameter.dto.ParametersDto)6 AccountTelemetry (com.sequenceiq.environment.telemetry.domain.AccountTelemetry)6 AwsDiskEncryptionParametersDto (com.sequenceiq.environment.parameter.dto.AwsDiskEncryptionParametersDto)5 AwsParametersDto (com.sequenceiq.environment.parameter.dto.AwsParametersDto)5 AzureParametersDto (com.sequenceiq.environment.parameter.dto.AzureParametersDto)5 AzureResourceEncryptionParametersDto (com.sequenceiq.environment.parameter.dto.AzureResourceEncryptionParametersDto)5 EnvironmentBackup (com.sequenceiq.environment.environment.dto.EnvironmentBackup)4