Search in sources :

Example 1 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class AzureDatabaseResourceService method buildDatabaseResourcesForLaunch.

public List<CloudResourceStatus> buildDatabaseResourcesForLaunch(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier persistenceNotifier) {
    CloudContext cloudContext = ac.getCloudContext();
    AzureClient client = ac.getParameter(AzureClient.class);
    String stackName = azureUtils.getStackName(cloudContext);
    String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, stack);
    ResourceGroupUsage resourceGroupUsage = azureResourceGroupMetadataProvider.getResourceGroupUsage(stack);
    String template = azureDatabaseTemplateBuilder.build(cloudContext, stack);
    if (!client.resourceGroupExists(resourceGroupName)) {
        if (resourceGroupUsage != ResourceGroupUsage.MULTIPLE) {
            LOGGER.warn("Resource group with name {} does not exist", resourceGroupName);
            throw new CloudConnectorException(String.format("Resource group with name %s does not exist!", resourceGroupName));
        } else {
            LOGGER.debug("Resource group with name {} does not exist, creating it now..", resourceGroupName);
            String region = ac.getCloudContext().getLocation().getRegion().value();
            client.createResourceGroup(resourceGroupName, region, stack.getTags());
        }
    }
    createResourceGroupResource(persistenceNotifier, cloudContext, resourceGroupName);
    createTemplateResource(persistenceNotifier, cloudContext, stackName);
    Deployment deployment;
    try {
        String parametersMapAsString = new Json(Map.of()).getValue();
        client.createTemplateDeployment(resourceGroupName, stackName, template, parametersMapAsString);
    } catch (CloudException e) {
        throw azureUtils.convertToCloudConnectorException(e, "Database stack provisioning");
    } catch (Exception e) {
        throw new CloudConnectorException(String.format("Error in provisioning database stack %s: %s", stackName, e.getMessage()), e);
    } finally {
        deployment = client.getTemplateDeployment(resourceGroupName, stackName);
        if (deployment != null) {
            List<CloudResource> cloudResources = azureCloudResourceService.getDeploymentCloudResources(deployment);
            cloudResources.forEach(cloudResource -> persistenceNotifier.notifyAllocation(cloudResource, cloudContext));
        }
    }
    String fqdn = (String) ((Map) ((Map) deployment.outputs()).get(DATABASE_SERVER_FQDN)).get("value");
    List<CloudResource> databaseResources = createCloudResources(fqdn);
    databaseResources.forEach(dbr -> persistenceNotifier.notifyAllocation(dbr, cloudContext));
    return databaseResources.stream().map(resource -> new CloudResourceStatus(resource, ResourceStatus.CREATED)).collect(Collectors.toList());
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ARM_TEMPLATE(com.sequenceiq.common.api.type.ResourceType.ARM_TEMPLATE) AzureDatabaseTemplateProvider(com.sequenceiq.cloudbreak.cloud.azure.AzureDatabaseTemplateProvider) LoggerFactory(org.slf4j.LoggerFactory) ResourceGroupUsage(com.sequenceiq.cloudbreak.cloud.azure.ResourceGroupUsage) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AZURE_PRIVATE_ENDPOINT(com.sequenceiq.common.api.type.ResourceType.AZURE_PRIVATE_ENDPOINT) Lists(com.google.common.collect.Lists) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AzureDatabaseTemplateBuilder(com.sequenceiq.cloudbreak.cloud.azure.AzureDatabaseTemplateBuilder) Service(org.springframework.stereotype.Service) Map(java.util.Map) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) AzureUtils(com.sequenceiq.cloudbreak.cloud.azure.AzureUtils) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) ResourceType(com.sequenceiq.common.api.type.ResourceType) AZURE_RESOURCE_GROUP(com.sequenceiq.common.api.type.ResourceType.AZURE_RESOURCE_GROUP) Deployment(com.microsoft.azure.management.resources.Deployment) Logger(org.slf4j.Logger) AzureResourceGroupMetadataProvider(com.sequenceiq.cloudbreak.cloud.azure.AzureResourceGroupMetadataProvider) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors(java.util.stream.Collectors) AzureCloudResourceService(com.sequenceiq.cloudbreak.cloud.azure.AzureCloudResourceService) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) Json(com.sequenceiq.cloudbreak.common.json.Json) AZURE_DATABASE(com.sequenceiq.common.api.type.ResourceType.AZURE_DATABASE) RDS_HOSTNAME(com.sequenceiq.common.api.type.ResourceType.RDS_HOSTNAME) List(java.util.List) AzureTransientDeploymentService(com.sequenceiq.cloudbreak.cloud.azure.template.AzureTransientDeploymentService) CloudException(com.microsoft.azure.CloudException) Optional(java.util.Optional) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Deployment(com.microsoft.azure.management.resources.Deployment) Json(com.sequenceiq.cloudbreak.common.json.Json) CloudException(com.microsoft.azure.CloudException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudException(com.microsoft.azure.CloudException) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) ResourceGroupUsage(com.sequenceiq.cloudbreak.cloud.azure.ResourceGroupUsage) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Map(java.util.Map)

Example 2 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class DatabaseServerSslCertificatePrescriptionServiceTest method setUp.

@BeforeEach
void setUp() {
    dbStack = new DBStack();
    databaseServer = DatabaseServer.builder().build();
    databaseStack = new DatabaseStack(null, databaseServer, Map.of(), "");
    region = Region.region("myregion");
    location = Location.location(region);
}
Also used : DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class AllocateDatabaseServerHandlerTest method setUp.

@BeforeEach
void setUp() {
    dbStack = new DBStack();
    dbStack.setCloudPlatform(CloudPlatform.AWS.name());
    lenient().when(dbStackService.getById(anyLong())).thenReturn(dbStack);
    databaseStack = new DatabaseStack(null, null, Map.of(), "");
}
Also used : DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class DBStackToDatabaseStackConverterTest method testConversionAzureWithMultipleResourceGroups.

@Test
public void testConversionAzureWithMultipleResourceGroups() {
    Network network = new Network();
    network.setAttributes(new Json(NETWORK_ATTRIBUTES));
    dbStack.setNetwork(network);
    dbStack.setCloudPlatform(CLOUD_PLATFORM);
    dbStack.setParameters(new HashMap<>());
    DatabaseServer server = new DatabaseServer();
    server.setDatabaseVendor(DatabaseVendor.POSTGRES);
    server.setAttributes(new Json(DATABASE_SERVER_ATTRIBUTES));
    dbStack.setDatabaseServer(server);
    dbStack.setTags(new Json(STACK_TAGS));
    dbStack.setTemplate("template");
    DetailedEnvironmentResponse environmentResponse = new DetailedEnvironmentResponse();
    environmentResponse.setCloudPlatform(CLOUD_PLATFORM);
    environmentResponse.setAzure(AzureEnvironmentParameters.builder().withAzureResourceGroup(AzureResourceGroup.builder().withResourceGroupUsage(ResourceGroupUsage.MULTIPLE).build()).build());
    when(environmentService.getByCrn(anyString())).thenReturn(environmentResponse);
    DatabaseStack convertedStack = underTest.convert(dbStack);
    Map<String, Object> parameters = convertedStack.getDatabaseServer().getParameters();
    assertThat(parameters.containsKey(RESOURCE_GROUP_NAME_PARAMETER)).isFalse();
    assertThat(parameters.containsKey(RESOURCE_GROUP_USAGE_PARAMETER)).isFalse();
    assertThat(parameters.size()).isEqualTo(2);
}
Also used : Network(com.sequenceiq.redbeams.domain.stack.Network) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.redbeams.domain.stack.DatabaseServer) Json(com.sequenceiq.cloudbreak.common.json.Json) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 5 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class DBStackToDatabaseStackConverterTest method testConversionAzureWithAzureEncryptionResourcesPresentAndSingleResourceGroup.

@Test
public void testConversionAzureWithAzureEncryptionResourcesPresentAndSingleResourceGroup() {
    Network network = new Network();
    network.setAttributes(new Json(NETWORK_ATTRIBUTES));
    dbStack.setNetwork(network);
    dbStack.setCloudPlatform(CLOUD_PLATFORM);
    dbStack.setParameters(new HashMap<>());
    DatabaseServer server = new DatabaseServer();
    server.setDatabaseVendor(DatabaseVendor.POSTGRES);
    server.setAttributes(new Json(DATABASE_SERVER_ATTRIBUTES));
    dbStack.setDatabaseServer(server);
    dbStack.setTags(new Json(STACK_TAGS));
    dbStack.setTemplate("template");
    DetailedEnvironmentResponse environmentResponse = new DetailedEnvironmentResponse();
    environmentResponse.setCloudPlatform(CLOUD_PLATFORM);
    environmentResponse.setAzure(AzureEnvironmentParameters.builder().withAzureResourceGroup(AzureResourceGroup.builder().withResourceGroupUsage(ResourceGroupUsage.SINGLE).withName(RESOURCE_GROUP).build()).withResourceEncryptionParameters(AzureResourceEncryptionParameters.builder().withEncryptionKeyUrl(KEY_URL).withEncryptionKeyResourceGroupName(RESOURCE_GROUP).build()).build());
    when(environmentService.getByCrn(anyString())).thenReturn(environmentResponse);
    DatabaseStack convertedStack = underTest.convert(dbStack);
    Map<String, Object> parameters = convertedStack.getDatabaseServer().getParameters();
    assertThat(parameters.get(RESOURCE_GROUP_NAME_PARAMETER).toString()).isEqualTo(RESOURCE_GROUP);
    assertThat(parameters.get(RESOURCE_GROUP_USAGE_PARAMETER).toString()).isEqualTo(ResourceGroupUsage.SINGLE.name());
    assertThat(parameters.get(ENCRYPTION_KEY_URL).toString()).isEqualTo(KEY_URL);
    assertThat(parameters.get(ENCRYPTION_KEY_RESOURCE_GROUP_NAME).toString()).isEqualTo(RESOURCE_GROUP);
    assertThat(parameters.size()).isEqualTo(6);
}
Also used : Network(com.sequenceiq.redbeams.domain.stack.Network) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.redbeams.domain.stack.DatabaseServer) Json(com.sequenceiq.cloudbreak.common.json.Json) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)48 Test (org.junit.jupiter.api.Test)34 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)24 DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)24 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)22 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)16 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)13 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)12 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)12 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)11 DatabaseServer (com.sequenceiq.redbeams.domain.stack.DatabaseServer)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)10 Network (com.sequenceiq.cloudbreak.cloud.model.Network)9 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)8 Location (com.sequenceiq.cloudbreak.cloud.model.Location)8 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)8 Json (com.sequenceiq.cloudbreak.common.json.Json)8 DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)7 Network (com.sequenceiq.redbeams.domain.stack.Network)7