Skip to content

Commit 3d534fc

Browse files
committed
Changed getConnectionString to incorporate SRV fomrat
1 parent 001fa79 commit 3d534fc

12 files changed

+119
-19
lines changed

docs/MongoDB-batchsink.md

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ automatically generated.
2626

2727
**Password:** Password to use to connect to the specified database.
2828

29+
**Connect Using SRV String:** Toggle to determine whether to use an SRV connection string for MongoDB. It can be
30+
enabled if the MongoDB deployment supports SRV DNS records for connection resolution.
31+
2932
**Connection Arguments:** A list of arbitrary string key/value pairs as connection arguments. See
3033
[Connection String Options] for a full description of these arguments.
3134

docs/MongoDB-batchsource.md

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ and use the [MongoDB extended JSON format] to represent non-native JSON data typ
2828

2929
**Password:** Password to use to connect to the specified database.
3030

31+
**Connect Using SRV String:** Toggle to determine whether to use an SRV connection string for MongoDB. It can be
32+
enabled if the MongoDB deployment supports SRV DNS records for connection resolution.
33+
3134
**Authentication Connection String:** Optional MongoDB connection string to connect to the 'config' database of a
3235
sharded cluster. It can be omitted if username and password do not differ from the previously provided ones or if
3336
'config' database does not require authentication.

src/main/java/io/cdap/plugin/MongoDBConfig.java

+34-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class MongoDBConfig extends PluginConfig {
5050
@Name(MongoDBConstants.PORT)
5151
@Description("Port that MongoDB is listening to.")
5252
@Macro
53-
private int port;
53+
@Nullable
54+
private Integer port;
5455

5556
@Name(MongoDBConstants.DATABASE)
5657
@Description("MongoDB database name.")
@@ -76,21 +77,29 @@ public class MongoDBConfig extends PluginConfig {
7677
@Nullable
7778
private String password;
7879

80+
@Name(MongoDBConstants.CONNECT_USING_SRV_STRING)
81+
@Description("Toggle to determine whether to use an SRV connection string for MongoDB. It can be " +
82+
"enabled if the MongoDB deployment supports SRV DNS records for connection resolution.")
83+
@Macro
84+
@Nullable
85+
private boolean connectUsingSRVString;
86+
7987
@Name(MongoDBConstants.CONNECTION_ARGUMENTS)
8088
@Description("A list of arbitrary string key/value pairs as connection arguments.")
8189
@Macro
8290
@Nullable
8391
private String connectionArguments;
8492

8593
public MongoDBConfig(String referenceName, String host, int port, String database, String collection, String user,
86-
String password, String connectionArguments) {
94+
String password, boolean connectUsingSRVString, String connectionArguments) {
8795
this.referenceName = referenceName;
8896
this.host = host;
8997
this.port = port;
9098
this.database = database;
9199
this.collection = collection;
92100
this.user = user;
93101
this.password = password;
102+
this.connectUsingSRVString = connectUsingSRVString;
94103
this.connectionArguments = connectionArguments;
95104
}
96105

@@ -102,7 +111,8 @@ public String getHost() {
102111
return host;
103112
}
104113

105-
public int getPort() {
114+
@Nullable
115+
public Integer getPort() {
106116
return port;
107117
}
108118

@@ -124,6 +134,10 @@ public String getPassword() {
124134
return password;
125135
}
126136

137+
public boolean connectUsingSRVString() {
138+
return connectUsingSRVString;
139+
}
140+
127141
@Nullable
128142
public String getConnectionArguments() {
129143
return connectionArguments;
@@ -146,7 +160,8 @@ public void validate() {
146160
if (!containsMacro(MongoDBConstants.HOST) && Strings.isNullOrEmpty(host)) {
147161
throw new InvalidConfigPropertyException("Host must be specified", MongoDBConstants.HOST);
148162
}
149-
if (!containsMacro(MongoDBConstants.PORT)) {
163+
if ((!containsMacro(MongoDBConstants.CONNECT_USING_SRV_STRING) && !connectUsingSRVString) &&
164+
!containsMacro(MongoDBConstants.PORT)) {
150165
if (port < 1) {
151166
throw new InvalidConfigPropertyException("Port number must be greater than 0", MongoDBConstants.PORT);
152167
}
@@ -161,24 +176,32 @@ public void validate() {
161176

162177
/**
163178
* Constructs a connection string such as: "mongodb://admin:password@localhost:27017/admin.analytics?key=value;"
164-
* using host, port, username, password, database, collection and optional connection properties. In the case when
165-
* username or password is not provided the connection string will not contain credentials:
179+
* using host, port, username, password, database, collection and optional connection properties.
180+
* If SRV is enabled, the connection string will use the "mongodb+srv://" protocol instead of "mongodb://".
181+
* In the case when username or password is not provided, the connection string will not contain credentials:
166182
* "mongodb://localhost:27017/admin.analytics?key=value;"
183+
* When SRV is not used, the port will be included in the connection string.
167184
*
168185
* @return connection string.
169186
*/
170187
public String getConnectionString() {
171-
StringBuilder connectionStringBuilder = new StringBuilder("mongodb://");
188+
StringBuilder connectionStringBuilder = new StringBuilder();
189+
if (connectUsingSRVString()) {
190+
connectionStringBuilder.append("mongodb+srv://");
191+
} else {
192+
connectionStringBuilder.append("mongodb://");
193+
}
172194
if (!Strings.isNullOrEmpty(user) || !Strings.isNullOrEmpty(password)) {
173195
connectionStringBuilder.append(user).append(":").append(password).append("@");
174196
}
175-
connectionStringBuilder.append(host).append(":").append(port).append("/")
176-
.append(database).append(".").append(collection);
177-
197+
connectionStringBuilder.append(host);
198+
if (!connectUsingSRVString()) {
199+
connectionStringBuilder.append(":").append(port);
200+
}
201+
connectionStringBuilder.append("/").append(database).append(".").append(collection);
178202
if (!Strings.isNullOrEmpty(connectionArguments)) {
179203
connectionStringBuilder.append("?").append(connectionArguments);
180204
}
181-
182205
return connectionStringBuilder.toString();
183206
}
184207

src/main/java/io/cdap/plugin/MongoDBConstants.java

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ private MongoDBConstants() {
7979
*/
8080
public static final String PASSWORD = "password";
8181

82+
/**
83+
* Configuration property name used to specify whether to use an SRV Connection string for MongoDB.
84+
*/
85+
public static final String CONNECT_USING_SRV_STRING = "connectUsingSRVString";
86+
8287
/**
8388
* Configuration property name used to specify auxiliary MongoDB connection string to authenticate against when
8489
* constructing splits.

src/main/java/io/cdap/plugin/batch/sink/MongoDBBatchSink.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ public static class MongoDBSinkConfig extends MongoDBConfig {
171171
private String idField;
172172

173173
public MongoDBSinkConfig(String referenceName, String host, int port, String database, String collection,
174-
String user, String password, String connectionArguments, String idField) {
175-
super(referenceName, host, port, database, collection, user, password, connectionArguments);
174+
String user, String password, boolean connectUsingSRVString,
175+
String connectionArguments, String idField) {
176+
super(referenceName, host, port, database, collection, user, password,
177+
connectUsingSRVString, connectionArguments);
176178
this.idField = idField;
177179
}
178180

src/main/java/io/cdap/plugin/batch/source/MongoDBBatchSource.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ public static class MongoDBSourceConfig extends MongoDBConfig {
190190
private String authConnectionString;
191191

192192
public MongoDBSourceConfig(String referenceName, String host, int port, String database, String collection,
193-
String user, String password, String connectionArguments, String schema,
193+
String user, String password, boolean connectUsingSRVString,
194+
String connectionArguments, String schema,
194195
String inputQuery, String onError, String authConnectionString) {
195-
super(referenceName, host, port, database, collection, user, password, connectionArguments);
196+
super(referenceName, host, port, database, collection, user, password,
197+
connectUsingSRVString, connectionArguments);
196198
this.schema = schema;
197199
this.inputQuery = inputQuery;
198200
this.onError = onError;

src/test/java/io/cdap/plugin/batch/MongoDBConfigBuilder.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class MongoDBConfigBuilder<T extends MongoDBConfigBuilder> {
2929
protected String collection;
3030
protected String user;
3131
protected String password;
32+
protected boolean connectUsingSRVString;
3233
protected String connectionArguments;
3334

3435
public static MongoDBConfigBuilder builder() {
@@ -82,12 +83,18 @@ public T setPassword(String password) {
8283
return (T) this;
8384
}
8485

86+
public T setConnectUsingSRVString(boolean connectUsingSRVString) {
87+
this.connectUsingSRVString = connectUsingSRVString;
88+
return (T) this;
89+
}
90+
8591
public T setConnectionArguments(String connectionArguments) {
8692
this.connectionArguments = connectionArguments;
8793
return (T) this;
8894
}
8995

9096
public MongoDBConfig build() {
91-
return new MongoDBConfig(referenceName, host, port, database, collection, user, password, connectionArguments);
97+
return new MongoDBConfig(referenceName, host, port, database, collection, user, password,
98+
connectUsingSRVString, connectionArguments);
9299
}
93100
}

src/test/java/io/cdap/plugin/batch/MongoDBConfigTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class MongoDBConfigTest {
3636
.setCollection("analytics")
3737
.setUser("admin")
3838
.setPassword("password")
39+
.setConnectUsingSRVString(false)
3940
.setConnectionArguments("key=value;")
4041
.build();
4142

@@ -45,6 +46,16 @@ public void testConfigConnectionString() {
4546
VALID_CONFIG.getConnectionString());
4647
}
4748

49+
@Test
50+
public void testConfigConnectionStringWithSRV() {
51+
String connectionString = MongoDBConfigBuilder.builder(VALID_CONFIG)
52+
.setConnectUsingSRVString(true)
53+
.build()
54+
.getConnectionString();
55+
56+
Assert.assertEquals("mongodb+srv://admin:password@localhost/admin.analytics?key=value;", connectionString);
57+
}
58+
4859
@Test
4960
public void testConfigConnectionStringNoCreds() {
5061
String connectionString = MongoDBConfigBuilder.builder(VALID_CONFIG)
@@ -56,6 +67,18 @@ public void testConfigConnectionStringNoCreds() {
5667
Assert.assertEquals("mongodb://localhost:27017/admin.analytics?key=value;", connectionString);
5768
}
5869

70+
@Test
71+
public void testConfigConnectionStringWithSRVNoCreds() {
72+
String connectionString = MongoDBConfigBuilder.builder(VALID_CONFIG)
73+
.setConnectUsingSRVString(true)
74+
.setUser(null)
75+
.setPassword(null)
76+
.build()
77+
.getConnectionString();
78+
79+
Assert.assertEquals("mongodb+srv://localhost/admin.analytics?key=value;", connectionString);
80+
}
81+
5982
@Test
6083
public void testValidateValid() {
6184
VALID_CONFIG.validate();

src/test/java/io/cdap/plugin/batch/sink/MongoDBSinkConfigBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ public MongoDBSinkConfigBuilder setIdField(String idField) {
4949

5050
public MongoDBBatchSink.MongoDBSinkConfig build() {
5151
return new MongoDBBatchSink.MongoDBSinkConfig(referenceName, host, port, database, collection, user, password,
52-
connectionArguments, idField);
52+
connectUsingSRVString, connectionArguments, idField);
5353
}
5454
}

src/test/java/io/cdap/plugin/batch/source/MongoDBSourceConfigBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public MongoDBSourceConfigBuilder setAuthConnectionString(String authConnectionS
7070

7171
public MongoDBBatchSource.MongoDBSourceConfig build() {
7272
return new MongoDBBatchSource.MongoDBSourceConfig(referenceName, host, port, database, collection, user, password,
73-
connectionArguments, schema, inputQuery, onError,
74-
authConnectionString);
73+
connectUsingSRVString, connectionArguments, schema, inputQuery,
74+
onError, authConnectionString);
7575
}
7676
}

widgets/MongoDB-batchsink.json

+16
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@
5757
"widget-type": "password",
5858
"label": "Password",
5959
"name": "password"
60+
},
61+
{
62+
"widget-type": "toggle",
63+
"label": "Connect Using SRV String",
64+
"name": "connectUsingSRVString",
65+
"widget-attributes": {
66+
"on": {
67+
"value": "true",
68+
"label": "True"
69+
},
70+
"off": {
71+
"value": "false",
72+
"label": "False"
73+
},
74+
"default": "false"
75+
}
6076
}
6177
]
6278
},

widgets/MongoDB-batchsource.json

+16
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@
6161
"label": "Password",
6262
"name": "password"
6363
},
64+
{
65+
"widget-type": "toggle",
66+
"label": "Connect Using SRV String",
67+
"name": "connectUsingSRVString",
68+
"widget-attributes": {
69+
"on": {
70+
"value": "true",
71+
"label": "True"
72+
},
73+
"off": {
74+
"value": "false",
75+
"label": "False"
76+
},
77+
"default": "false"
78+
}
79+
},
6480
{
6581
"widget-type": "textbox",
6682
"label": "Authentication Connection String",

0 commit comments

Comments
 (0)