Search in sources :

Example 11 with DatabaseStack

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

the class AwsRdsLaunchService method launch.

public List<CloudResourceStatus> launch(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier resourceNotifier) {
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    DatabaseServer databaseServer = stack.getDatabaseServer();
    boolean useSslEnforcement = databaseServer.isUseSslEnforcement();
    try {
        cfClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException exception) {
        // all subnets desired for DB subnet group are in the stack
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        LOGGER.warn("API call failed with this error:", exception);
        if (!existingSubnet) {
            throw new CloudConnectorException("Can only create RDS instance with existing subnets", exception);
        }
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        RDSModelContext rdsModelContext = new RDSModelContext().withTemplate(stack.getTemplate()).withNetworkCidrs(awsNetworkView.getExistingVpcCidrs()).withHasPort(databaseServer.getPort() != null).withUseSslEnforcement(useSslEnforcement).withSslCertificateIdentifierDefined(new AwsRdsInstanceView(databaseServer).isSslCertificateIdentifierDefined()).withHasSecurityGroup(!databaseServer.getSecurity().getCloudSecurityIds().isEmpty()).withIsKmsCustom(stack.getDatabaseServer().getParameters().containsKey("key")).withGetKmsKey(getKmsKey(stack));
        String cfTemplate = cloudFormationTemplateBuilder.build(rdsModelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, cfTemplate));
    }
    LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("RDS CloudFormation stack %s creation failed", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, CREATE_FAILED));
    List<CloudResource> databaseResources = getCreatedOutputs(ac, stack, cFStackName, cfClient, resourceNotifier, useSslEnforcement);
    databaseResources.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
    // FIXME: For now, just return everything wrapped in a status object
    return databaseResources.stream().map(resource -> new CloudResourceStatus(resource, ResourceStatus.CREATED)).collect(Collectors.toList());
// FIXME check does nothing?!
// return awsResourceConnector.check(ac, databaseResources);
}
Also used : AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AwsCloudFormationErrorMessageProvider(com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) LoggerFactory(org.slf4j.LoggerFactory) CREATE_FAILED(com.amazonaws.services.cloudformation.model.ResourceStatus.CREATE_FAILED) ArrayList(java.util.ArrayList) RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Service(org.springframework.stereotype.Service) Map(java.util.Map) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) Waiter(com.amazonaws.waiters.Waiter) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) AwsStackRequestHelper(com.sequenceiq.cloudbreak.cloud.aws.AwsStackRequestHelper) AmazonServiceException(com.amazonaws.AmazonServiceException) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors(java.util.stream.Collectors) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) List(java.util.List) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AmazonServiceException(com.amazonaws.AmazonServiceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView)

Example 12 with DatabaseStack

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

the class AzureTemplateBuilderDbTest method buildTestWhenDataEncryptionParametersPresentAndKeyVersionError.

@Test
void buildTestWhenDataEncryptionParametersPresentAndKeyVersionError() {
    Template template = Optional.ofNullable(factoryBean.getObject()).map(config -> {
        try {
            return config.getTemplate("templates/arm-dbstack.ftl", "UTF-8");
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }).orElseThrow();
    Subnet subnet = new Subnet(SUBNET_CIDR);
    Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
    network.putParameter("subnets", FULL_SUBNET_ID);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("dbVersion", "10");
    params.put(KEY_URL, "https://dummyVault.vault.azure.net/keys/dummyKey");
    params.put(KEY_VAULT_RESOURCE_GROUP_NAME, "dummyResourceGroup");
    DatabaseServer databaseServer = DatabaseServer.builder().serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(params).build();
    DatabaseStack databaseStack = new DatabaseStack(network, databaseServer, Collections.emptyMap(), template.toString());
    assertThrows(IllegalArgumentException.class, () -> underTest.build(cloudContext, databaseStack));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Spy(org.mockito.Spy) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Template(freemarker.template.Template) MethodSource(org.junit.jupiter.params.provider.MethodSource) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) IOException(java.io.IOException) UUID(java.util.UUID) FreeMarkerTemplateUtils(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Optional(java.util.Optional) OutboundInternetTraffic(com.sequenceiq.common.api.type.OutboundInternetTraffic) Collections(java.util.Collections) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) HashMap(java.util.HashMap) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) IOException(java.io.IOException) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with DatabaseStack

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

the class AzureTemplateBuilderDbTest method createDatabaseStack.

private DatabaseStack createDatabaseStack(boolean useSslEnforcement, String template) {
    Subnet subnet = new Subnet(SUBNET_CIDR);
    Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
    network.putParameter("subnets", FULL_SUBNET_ID);
    DatabaseServer databaseServer = DatabaseServer.builder().useSslEnforcement(useSslEnforcement).serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(Map.of("dbVersion", "10")).build();
    return new DatabaseStack(network, databaseServer, Collections.emptyMap(), template);
}
Also used : Network(com.sequenceiq.cloudbreak.cloud.model.Network) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet)

Example 14 with DatabaseStack

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

the class AzureTemplateBuilderDbTest method buildTestWhenDataEncryptionParametersPresent.

@Test
void buildTestWhenDataEncryptionParametersPresent() {
    Template template = Optional.ofNullable(factoryBean.getObject()).map(config -> {
        try {
            return config.getTemplate("templates/arm-dbstack.ftl", "UTF-8");
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }).orElseThrow();
    Subnet subnet = new Subnet(SUBNET_CIDR);
    Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
    network.putParameter("subnets", FULL_SUBNET_ID);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("dbVersion", "10");
    params.put(KEY_URL, "https://dummyVault.vault.azure.net/keys/dummyKey/dummyVersion");
    params.put(KEY_VAULT_RESOURCE_GROUP_NAME, "dummyResourceGroup");
    DatabaseServer databaseServer = DatabaseServer.builder().serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(params).build();
    DatabaseStack databaseStack = new DatabaseStack(network, databaseServer, Collections.emptyMap(), template.toString());
    Mockito.when(azureDatabaseTemplateProvider.getTemplate(databaseStack)).thenReturn(template);
    Mockito.when(azureUtils.encodeString(SUBNET_ID)).thenReturn("hash");
    String result = underTest.build(cloudContext, databaseStack);
    assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
    assertThat(result).contains("\"keyVaultName\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyVault\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Key vault name where the key to use is stored\"\n" + "              }\n" + "        },\n" + "        \"keyVaultResourceGroupName\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyResourceGroup\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Key vault resource group name where it is stored\"\n" + "              }\n" + "        },\n" + "        \"keyName\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyKey\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Key name in the key vault to use as encryption protector\"\n" + "              }\n" + "        },\n" + "        \"keyVersion\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyVersion\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Version of the key in the key vault to use as encryption protector\"\n" + "              }\n" + "        }");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Spy(org.mockito.Spy) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Template(freemarker.template.Template) MethodSource(org.junit.jupiter.params.provider.MethodSource) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) IOException(java.io.IOException) UUID(java.util.UUID) FreeMarkerTemplateUtils(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Optional(java.util.Optional) OutboundInternetTraffic(com.sequenceiq.common.api.type.OutboundInternetTraffic) Collections(java.util.Collections) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) HashMap(java.util.HashMap) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) IOException(java.io.IOException) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with DatabaseStack

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted.

@Test
public void testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    when(instancesListResponse.isEmpty()).thenReturn(false);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("test");
    databaseInstance.setState("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("NOT_ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.STOPPED, check);
}
Also used : InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) Settings(com.google.api.services.sqladmin.model.Settings) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) 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