Skip to content

Commit

Permalink
Add edison.mongo.uri property to use mongo connection string
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Geweke <[email protected]>
  • Loading branch information
MediaMarco committed May 3, 2022
1 parent 516b531 commit 88b8d2a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 2.6.9
* **[edison-mongo]**
* Support mongoURI to connect to MongoDB server(s) with property: `edison.mongo.uri`

## 2.6.8
* **[all]**
* Update to Spring Boot 2.6.7, Spring Framework 5.3.19 and further dependency updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ public class MongoProperties {
private static final Logger LOG = getLogger(MongoProperties.class);

/**
* The MongoDB servers. Comma-separated list of host:port pairs.
* The MongoDB servers. Comma-separated list of host:port pairs. Can be overridden by specifying uri or uriPattern
*/
@NotEmpty
private String[] host = {"localhost"};

/**
* The MongoDB connection uri pattern
* The MongoDB connection uri pattern (will be converted to a MongoUri after replacing %s:%s@ with username and password)
*/
private String uriPattern;

/**
* The MongoURI
*/
private String uri;

/**
* The MongoDB database.
*/
Expand Down Expand Up @@ -184,6 +189,14 @@ public void setUriPattern(final String uriPattern) {
this.uriPattern = uriPattern;
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri;
}

public String getAuthenticationDb() {
return authenticationDb;
}
Expand Down Expand Up @@ -295,11 +308,18 @@ public MongoClientSettings toMongoClientSettings(final CodecRegistry codecRegist
.applyConnectionString(getConnectionString())
.applyToClusterSettings(cluster -> cluster
.serverSelectionTimeout(serverSelectionTimeout, MILLISECONDS));
} else if (nonNull(uri) && !uri.isBlank()) {
clientOptionsBuilder
.applyConnectionString(new ConnectionString(uri))
.applyToClusterSettings(cluster -> cluster
.serverSelectionTimeout(serverSelectionTimeout, MILLISECONDS));
if (useAuthorizedConnection()) {
clientOptionsBuilder.credential(getMongoCredentials());
}
} else {
clientOptionsBuilder.applyToClusterSettings(cluster -> cluster
.hosts(getServers())
.serverSelectionTimeout(serverSelectionTimeout, MILLISECONDS));

if (useAuthorizedConnection()) {
clientOptionsBuilder.credential(getMongoCredentials());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.mongodb.AuthenticationMechanism;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCompressor;
import com.mongodb.ServerAddress;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
Expand Down Expand Up @@ -78,4 +80,23 @@ void shouldUseConnectionStringWhenUriPatternWithScramIsProvided() {
assertThat(mongoClientOptions.getCredential().getPassword(), is("somePassword".toCharArray()));
assertThat(mongoClientOptions.getCredential().getSource(), is("admin"));
}

@Test
void shouldUseMongoURI() {
//given
final MongoProperties props = new MongoProperties();
props.setUser("aUsername");
props.setPassword("somePassword");
props.setUri("mongodb://my.database.com:5432,my.database2.com:9876");

//when
MongoClientSettings mongoClientOptions = props.toMongoClientSettings(MongoClientSettings.getDefaultCodecRegistry(), null, null);

//then
assertThat(mongoClientOptions.getCredential(), is(notNullValue()));
assertThat(mongoClientOptions.getCredential().getUserName(), is("aUsername"));
assertThat(mongoClientOptions.getCredential().getPassword(), is("somePassword".toCharArray()));
assertThat(mongoClientOptions.getClusterSettings().getHosts(),
is(List.of(new ServerAddress("my.database.com", 5432), new ServerAddress("my.database2.com", 9876))));
}
}

0 comments on commit 88b8d2a

Please sign in to comment.