Search in sources :

Example 1 with DatabaseServer

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

the class GcpDatabaseServerLaunchService method getDatabaseInstance.

private DatabaseInstance getDatabaseInstance(DatabaseStack stack, String deploymentName, Compute compute, String projectId) throws java.io.IOException {
    DatabaseServer databaseServer = stack.getDatabaseServer();
    GcpDatabaseServerView databaseServerView = new GcpDatabaseServerView(databaseServer);
    GcpDatabaseNetworkView databaseNetworkView = new GcpDatabaseNetworkView(stack.getNetwork());
    Subnetwork subnetworkForRedbeams;
    if (Strings.isNullOrEmpty(databaseNetworkView.getSharedProjectId())) {
        subnetworkForRedbeams = compute.subnetworks().get(projectId, databaseServerView.getLocation(), databaseNetworkView.getSubnetId()).execute();
    } else {
        subnetworkForRedbeams = compute.subnetworks().get(databaseNetworkView.getSharedProjectId(), databaseServerView.getLocation(), databaseNetworkView.getSubnetId()).execute();
    }
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setCurrentDiskSize(databaseServerView.getAllocatedStorageInMb());
    databaseInstance.setName(deploymentName);
    databaseInstance.setInstanceType("CLOUD_SQL_INSTANCE");
    databaseInstance.setBackendType("SECOND_GEN");
    databaseInstance.setRegion(databaseServerView.getLocation());
    databaseInstance.setRootPassword(databaseServerView.getAdminPassword());
    databaseInstance.setConnectionName(databaseServerView.getAdminLoginName());
    databaseInstance.setGceZone(databaseNetworkView.getAvailabilityZone());
    databaseInstance.setDatabaseVersion(databaseServerView.getDatabaseVersion());
    databaseInstance.setSettings(getSettings(stack, databaseServerView, subnetworkForRedbeams));
    String keyName = databaseServer.getStringParameter(VOLUME_ENCRYPTION_KEY_ID);
    if (keyName != null && !keyName.isEmpty()) {
        DiskEncryptionConfiguration diskEncryptionConfiguration = new DiskEncryptionConfiguration();
        diskEncryptionConfiguration.setKmsKeyName(keyName);
        databaseInstance.setDiskEncryptionConfiguration(diskEncryptionConfiguration);
    }
    return databaseInstance;
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) Subnetwork(com.google.api.services.compute.model.Subnetwork) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) DiskEncryptionConfiguration(com.google.api.services.sqladmin.model.DiskEncryptionConfiguration) GcpDatabaseNetworkView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseNetworkView) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance)

Example 2 with DatabaseServer

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

the class AwsStackRequestHelper method getStackParameters.

@VisibleForTesting
Collection<Parameter> getStackParameters(AuthenticatedContext ac, DatabaseStack stack, boolean deleteProtection) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    DatabaseServer databaseServer = stack.getDatabaseServer();
    AwsRdsInstanceView awsRdsInstanceView = new AwsRdsInstanceView(databaseServer);
    AwsRdsDbSubnetGroupView awsRdsDbSubnetGroupView = new AwsRdsDbSubnetGroupView(databaseServer);
    AwsRdsVpcSecurityGroupView awsRdsVpcSecurityGroupView = new AwsRdsVpcSecurityGroupView(databaseServer);
    AwsRdsDbParameterGroupView awsRdsDbParameterGroupView = new AwsRdsDbParameterGroupView(databaseServer);
    List<Parameter> parameters = new ArrayList<>(asList(new Parameter().withParameterKey("DBInstanceClassParameter").withParameterValue(awsRdsInstanceView.getDBInstanceClass()), new Parameter().withParameterKey("DBInstanceIdentifierParameter").withParameterValue(awsRdsInstanceView.getDBInstanceIdentifier()), new Parameter().withParameterKey("DBSubnetGroupNameParameter").withParameterValue(awsRdsDbSubnetGroupView.getDBSubnetGroupName()), new Parameter().withParameterKey("DBSubnetGroupSubnetIdsParameter").withParameterValue(String.join(",", awsNetworkView.getSubnetList())), new Parameter().withParameterKey("EngineParameter").withParameterValue(awsRdsInstanceView.getEngine()), new Parameter().withParameterKey("MasterUsernameParameter").withParameterValue(awsRdsInstanceView.getMasterUsername()), new Parameter().withParameterKey("MasterUserPasswordParameter").withParameterValue(awsRdsInstanceView.getMasterUserPassword()), new Parameter().withParameterKey("DeletionProtectionParameter").withParameterValue(deleteProtection ? "true" : "false")));
    addParameterIfNotNull(parameters, "AllocatedStorageParameter", awsRdsInstanceView.getAllocatedStorage());
    addParameterIfNotNull(parameters, "BackupRetentionPeriodParameter", awsRdsInstanceView.getBackupRetentionPeriod());
    addParameterIfNotNull(parameters, "EngineVersionParameter", awsRdsInstanceView.getEngineVersion());
    addParameterIfNotNull(parameters, "MultiAZParameter", awsRdsInstanceView.getMultiAZ());
    addParameterIfNotNull(parameters, "StorageTypeParameter", awsRdsInstanceView.getStorageType());
    addParameterIfNotNull(parameters, "PortParameter", databaseServer.getPort());
    boolean useSslEnforcement = databaseServer.isUseSslEnforcement();
    addParameterIfNotNull(parameters, "DBParameterGroupNameParameter", useSslEnforcement ? awsRdsDbParameterGroupView.getDBParameterGroupName() : null);
    addParameterIfNotNull(parameters, "DBParameterGroupFamilyParameter", useSslEnforcement ? awsRdsDbParameterGroupView.getDBParameterGroupFamily() : null);
    addParameterIfNotNull(parameters, "SslCertificateIdentifierParameter", useSslEnforcement && awsRdsInstanceView.isSslCertificateIdentifierDefined() ? awsRdsInstanceView.getSslCertificateIdentifier() : null);
    if (awsRdsInstanceView.getVPCSecurityGroups().isEmpty()) {
        // VPC-id and VPC cidr should be filled in
        parameters.addAll(asList(new Parameter().withParameterKey("VPCIdParameter").withParameterValue(String.valueOf(awsNetworkView.getExistingVpc())), new Parameter().withParameterKey("DBSecurityGroupNameParameter").withParameterValue(awsRdsVpcSecurityGroupView.getDBSecurityGroupName())));
    } else {
        parameters.add(new Parameter().withParameterKey("VPCSecurityGroupsParameter").withParameterValue(String.join(",", awsRdsInstanceView.getVPCSecurityGroups())));
    }
    return parameters;
}
Also used : AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsRdsDbParameterGroupView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsDbParameterGroupView) ArrayList(java.util.ArrayList) Parameter(com.amazonaws.services.cloudformation.model.Parameter) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView) AwsRdsDbSubnetGroupView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsDbSubnetGroupView) AwsRdsVpcSecurityGroupView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsVpcSecurityGroupView) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer 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 4 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer 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 5 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer 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)

Aggregations

DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)36 Test (org.junit.jupiter.api.Test)27 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)23 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)18 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)17 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)16 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)13 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)13 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Network (com.sequenceiq.cloudbreak.cloud.model.Network)8 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)7 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)7 HashMap (java.util.HashMap)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)6 Settings (com.google.api.services.sqladmin.model.Settings)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)4 Compute (com.google.api.services.compute.Compute)4