Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13053: Remove Connection URI config MongoDB #14584

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ingestion/src/metadata/examples/workflows/mongodb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ source:
config:
type: MongoDB
databaseName: custom_database_name
connectionDetails:
connectionURI: mongodb+srv://user:pass@localhost:27017
scheme: mongodb+srv
username: username
password: password
hostPort: localhost:27017
sourceConfig:
config:
type: DatabaseMetadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
Workflow as AutomationWorkflow,
)
from metadata.generated.schema.entity.services.connections.database.mongoDBConnection import (
MongoConnectionString,
MongoDBConnection,
)
from metadata.ingestion.connections.builders import get_connection_url_common
Expand All @@ -36,10 +35,7 @@ def get_connection(connection: MongoDBConnection):
"""
Create connection
"""
if isinstance(connection.connectionDetails, MongoConnectionString):
mongo_url = connection.connectionDetails.connectionURI
else:
mongo_url = get_connection_url_common(connection.connectionDetails)
mongo_url = get_connection_url_common(connection)
return MongoClient(mongo_url)


Expand Down
6 changes: 3 additions & 3 deletions ingestion/tests/unit/topology/database/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
"serviceConnection": {
"config": {
"type": "MongoDB",
"connectionDetails": {
"connectionURI": "mongodb://ulixius:dummy_password@localhost:27017",
},
"username": "ulixius",
"password": "dummy_password",
"hostPort": "localhost:27017",
},
},
"sourceConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ To fetch the metadata from MongoDB to OpenMetadata, the MongoDB user must have a

#### Connection Details

- **MongoDB Connection Details**: Choose between MongoDB Connection String and MongoDB Connection Values to authenticate with your mongodb cluster.
- **Connection URI**: MongoDB connection string is a concise string of parameters used to establish a connection between an OpenMetadata and a MongoDB database. For ex. `mongodb://username:password@mongodb0.example.com:27017`.
- **Username**: Username to connect to Mongodb. This user must have access to perform `find` operation on collection and `listCollection` operations on database available in MongoDB.
- **Password**: Password to connect to MongoDB.
- **Host Port**: The hostPort parameter specifies the host and port of the MongoDB. This should be specified as a string in the format `hostname:port`. E.g., `localhost:27017`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ This is a sample config for MongoDB:

{% /codeInfo %}

{% codeInfo srNumber=5 %}

**connectionURI**: MongoDB connection string is a concise string of parameters used to establish a connection between an OpenMetadata and a MongoDB database. For ex. `mongodb://username:password@mongodb0.example.com:27017`.

{% /codeInfo %}

{% codeInfo srNumber=6 %}

**databaseName**: Optional name to give to the database in OpenMetadata. If left blank, we will use default as the database name.
Expand Down Expand Up @@ -132,23 +126,19 @@ source:
serviceConnection:
config:
type: MongoDB
connectionDetails:
```
```yaml {% srNumber=1 %}
username: username
username: username
```
```yaml {% srNumber=2 %}
password: password
password: password
```
```yaml {% srNumber=3 %}
hostPort: localhost:27017
```
```yaml {% srNumber=5 %}
# connectionURI: mongodb://username:password@mongodb0.example.com:27017
hostPort: localhost:27017
```
```yaml {% srNumber=7 %}
# connectionOptions:
# key: value
# connectionOptions:
# key: value
```
```yaml {% srNumber=6 %}
database: custom_database_name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.openmetadata.service.migration.mysql.v130;

import static org.openmetadata.service.migration.utils.v130.MigrationUtil.migrateMongoDBConnStr;

import lombok.SneakyThrows;
import org.jdbi.v3.core.Handle;
import org.openmetadata.service.migration.api.MigrationProcessImpl;
import org.openmetadata.service.migration.utils.MigrationFile;

public class Migration extends MigrationProcessImpl {

private Handle handle;

public Migration(MigrationFile migrationFile) {
super(migrationFile);
}

@Override
public void initialize(Handle handle) {
super.initialize(handle);
this.handle = handle;
}

@Override
@SneakyThrows
public void runDataMigration() {
String updateSqlQuery =
"UPDATE dbservice_entity de SET json = :json "
+ "WHERE serviceType = 'MongoDB' "
+ "AND id = :id";
migrateMongoDBConnStr(handle, updateSqlQuery);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.openmetadata.service.migration.postgres.v130;

import static org.openmetadata.service.migration.utils.v130.MigrationUtil.migrateMongoDBConnStr;

import lombok.SneakyThrows;
import org.jdbi.v3.core.Handle;
import org.openmetadata.service.migration.api.MigrationProcessImpl;
import org.openmetadata.service.migration.utils.MigrationFile;

public class Migration extends MigrationProcessImpl {

private Handle handle;

public Migration(MigrationFile migrationFile) {
super(migrationFile);
}

@Override
public void initialize(Handle handle) {
super.initialize(handle);
this.handle = handle;
}

@Override
@SneakyThrows
public void runDataMigration() {
String updateSqlQuery =
"UPDATE dbservice_entity de SET json = :json::jsonb "
+ "WHERE serviceType = 'MongoDB' "
+ "AND id = :id";
migrateMongoDBConnStr(handle, updateSqlQuery);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.openmetadata.service.migration.utils.v130;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.jdbi.v3.core.Handle;
import org.openmetadata.schema.entity.services.DatabaseService;
import org.openmetadata.service.util.JsonUtils;

public class MigrationUtil {

private static final String GET_MONGO_DB_SERVICES =
"SELECT id, json from " + "dbservice_entity de WHERE " + "serviceType = 'MongoDB'";

private MigrationUtil() {
/* Cannot create object util class*/
}

private static Map extractConnectionURIDetails(String connectionString) {
Map connectionDetailsMap = new LinkedHashMap();
try {
// Parse the MongoDB connection string
URI uri = new URI(connectionString);

// Extract components
String username = uri.getUserInfo().split(":")[0];
String password = uri.getUserInfo().split(":")[1];
String host = uri.getHost();
String scheme = uri.getScheme();
int port = uri.getPort();
String query = uri.getQuery();
Map queryMap = new HashMap<>();
if (query != null) {
String[] queryParams = query.split("&");
System.out.println("Query Parameters:");
for (String param : queryParams) {
queryMap.put(param.split("=")[0], param.split("=")[1]);
}
}

// populate connection details map the extracted components
connectionDetailsMap.put("username", username);
connectionDetailsMap.put("password", password);
connectionDetailsMap.put("hostPort", host + ":" + port);
connectionDetailsMap.put("scheme", scheme);
connectionDetailsMap.put("connectionOptions", queryMap);

} catch (URISyntaxException e) {
e.printStackTrace();
}
return connectionDetailsMap;
}

public static void migrateMongoDBConnStr(Handle handle, String updateSqlQuery) {
handle
.createQuery(GET_MONGO_DB_SERVICES)
.mapToMap()
.forEach(
row -> {
DatabaseService mongoService =
JsonUtils.readValue(row.get("json").toString(), DatabaseService.class);
String id = row.get("id").toString();
Map mongoDBConnection = (LinkedHashMap) mongoService.getConnection().getConfig();
Map connDetails = (LinkedHashMap) mongoDBConnection.get("connectionDetails");

Map finalConnectionDetails;
if (connDetails.get("connectionURI") != null) {
String connectionURI = connDetails.get("connectionURI").toString();
finalConnectionDetails = extractConnectionURIDetails(connectionURI);
} else {
finalConnectionDetails = connDetails;
}
mongoDBConnection.putAll(finalConnectionDetails);
mongoDBConnection.remove("connectionDetails");
String json = JsonUtils.pojoToJson(mongoService);

handle.createUpdate(updateSqlQuery).bind("json", json).bind("id", id).execute();
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
],
"default": "MongoDB"
},
"MongoConnectionString": {
"type": "object",
"javaType": "org.openmetadata.schema.services.connections.database.mongo.MongoConnectionString",
"title": "Mongo Connection String",
"properties": {
"connectionURI": {
"title": "Connection URI",
"description": "Connection URI to connect to your MongoDB cluster",
"type": "string"
}
}
"mongoDBScheme": {
"description": "Mongo connection scheme options.",
"type": "string",
"enum": [
"mongodb",
"mongodb+srv"
],
"default": "mongodb"
}

},
Expand All @@ -35,17 +32,31 @@
"$ref": "#/definitions/mongoDBType",
"default": "MongoDB"
},
"connectionDetails": {
"title": "MongoDB Connection Details",
"description": "MongoDB Connection Details.",
"oneOf": [
{
"$ref": "mongoDB/mongoDBValues.json"
},
{
"$ref": "#/definitions/MongoConnectionString"
}
]
"scheme": {
"title": "Connection Scheme",
"description": "Mongo connection scheme options.",
"$ref": "#/definitions/mongoDBScheme",
"default": "mongodb"
},
"username": {
"title": "Username",
"description": "Username to connect to MongoDB. This user should have privileges to read all the metadata in MongoDB.",
"type": "string"
},
"password": {
"title": "Password",
"description": "Password to connect to MongoDB.",
"type": "string",
"format": "password"
},
"hostPort": {
"title": "Host and Port",
"description": "Host and port of the MongoDB service.",
"type": "string"
},
"connectionOptions": {
"title": "Connection Options",
"$ref": "../connectionBasicType.json#/definitions/connectionOptions"
},
"databaseName": {
"title": "Database Name",
Expand All @@ -57,5 +68,6 @@
"$ref": "../connectionBasicType.json#/definitions/supportsMetadataExtraction"
}
},
"required": ["hostPort"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ You can find further information on the Hive connector in the [here](https://doc

## Connection Details

$$section
### MongoDB Connection Details $(id="connectionDetails")

Choose between MongoDB Connection String and MongoDB Connection Values to authenticate with your mongodb cluster.
$$

$$section
### Connection URI $(id="connectionURI")

MongoDB connection string is a concise string of parameters used to establish a connection between an OpenMetadata and a MongoDB database. For ex. `mongodb://username:password@mongodb0.example.com:27017`
$$

$$section
### Username $(id="username")
Username to connect to Mongodb. This user must have access to perform `find` operation on collection and `listCollection` operations on database available in MongoDB.
Expand Down
Loading