0
votes

I'm using micronaut framework and I'm trying to configure cassandra data access from application.yml For a standard test use case I'm able to configure the datastax driver

cassandra:
    default:
        clusterName: "Test Cluster"
        contactPoint: "192.168.99.100"
        port: 9042
        maxSchemaAgreementWaitSeconds: 20
        ssl: false

However I can't find a way to provide the configuration to be used with the method .withCredentials

I see that the implementation in https://github.com/micronaut-projects/micronaut-core/blob/dc8c423be1979817c9c8f53440f3b87e775523b2/configurations/cassandra/src/main/java/io/micronaut/configuration/cassandra/CassandraConfiguration.java

do the following

 @ConfigurationBuilder(allowZeroArgs = true, prefixes = { "with", "add" })
    Cluster.Builder builder = Cluster.builder();

however withCredentials method requires 2 parameters https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Cluster.Builder.html#withCredentials-java.lang.String-java.lang.String-

public Cluster.Builder withCredentials(String username,
                                       String password)

What would be the yaml way to provide the configuration to this method?

1
We should probably improve CassandraConfiguration to make the builder accessible to programatic customisation. Please feel free to report an issue for this. - Graeme Rocher
Thank you it works like charm, and your implementation was super fast! - Javier Abrego

1 Answers

0
votes

With the new feature added by @graeme-rocher in https://github.com/micronaut-projects/micronaut-core/issues/1106 I was able to do it as follows:

import com.datastax.driver.core.Cluster
import io.micronaut.context.ApplicationContext
import io.micronaut.context.event.BeanCreatedEvent
import io.micronaut.context.event.BeanCreatedEventListener
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.inject.Singleton

@Singleton
class ClusterBuilderListener implements BeanCreatedEventListener<Cluster.Builder> {
    private static final Logger LOG = LoggerFactory.getLogger(ClusterBuilderListener.class)

    @Override
    Cluster.Builder onCreated(BeanCreatedEvent<Cluster.Builder> event) {
        def builder = event.getBean()
        ApplicationContext applicationContext = (ApplicationContext) event.getSource()

        if(applicationContext.getEnvironment().getActiveNames().contains('pro') ){
            builder.withCredentials("username", "password")
        }
        return builder
    }
}