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

CSS-12576 add support for mysql catalogs #67

Merged
merged 2 commits into from
Dec 6, 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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Adding a catalog to Trino requires user or service account credentiials. For thi
### Juju secrets
Juju secrets are used to manage connector credentials. The format of these differ by connector type. Note: the same secret can be shared by multiple trino catalogs.

For PostgreSQL (`postgresql-user-creds.yaml`):
For PostgreSQL or MySQL (`postgresql-user-creds.yaml` or `mysql-user-creds.yaml`):
```
rw:
user: trino
Expand Down Expand Up @@ -117,6 +117,7 @@ For Google sheets (`gsheets-service-accounts.yaml`):
These secrets can be created by running the following:
```
juju add-secret postgresql-credentials replicas#file=postgresql-user-creds.yaml cert#file=certificates.yaml
juju add-secret mysql-credentials replicas#file=mysql-user-creds.yaml
juju add-secret bigquery-service-accounts service-accounts#file=bigquery-service-accounts.yaml
juju add-secret gsheets-service-accounts service-accounts#file=gsheets-service-accounts.yaml
```
Expand All @@ -135,6 +136,9 @@ catalogs:
backend: dwh
database: example
secret-id: crt7gpnmp25c760ji150
mysql_example:
backend: mysql
secret-id: crt7gpnmp25c760ji150
ge_bigquery:
backend: bigquery
project: <project-id>
Expand All @@ -152,6 +156,14 @@ backends:
case-insensitive-name-matching=true
decimal-mapping=allow_overflow
decimal-rounding-mode=HALF_UP
mysql:
connector: mysql
url: jdbc:mysql://<database-host>:3306
params: sslMode=REQUIRED
config: |
case-insensitive-name-matching=true
decimal-mapping=allow_overflow
decimal-rounding-mode=HALF_UP
bigquery:
connector: bigquery
config: |
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ jsonschema==4.17.3
bcrypt==4.0.1
cosl==0.0.10
cerberus==1.3.5
# 0.28.x introduced some issues: https://github.com/gtsystem/lightkube/issues/78
httpx==0.27.2
lightkube==0.15.4
lightkube-models==1.28.1.4
14 changes: 7 additions & 7 deletions src/catalog_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from literals import (
BIGQUERY_BACKEND_SCHEMA,
GSHEETS_BACKEND_SCHEMA,
POSTGRESQL_BACKEND_SCHEMA,
REPLICA_SCHEMA,
SQL_BACKEND_SCHEMA,
)
from utils import add_cert_to_truststore, validate_keys

Expand Down Expand Up @@ -151,24 +151,24 @@ def configure_catalogs(self):
raise


class PostgresqlCatalog(CatalogBase):
"""Class for handling the PostgreSQL connector."""
class SqlCatalog(CatalogBase):
"""Class for handling the PostgreSQL and MySQL connectors."""

def _get_credentials(self):
"""Handle PostgreSQL catalog configuration.
"""Handle PostgreSQL/MySQL catalog configuration.

Returns:
replicas: the database replica configuration.
"""
validate_keys(self.backend, POSTGRESQL_BACKEND_SCHEMA)
validate_keys(self.backend, SQL_BACKEND_SCHEMA)
secret = self._get_secret_content(self.info["secret-id"])
replicas = yaml.safe_load(secret["replicas"])
certs = yaml.safe_load(secret.get("cert", ""))
self._add_certs(certs)
return replicas

def _create_properties(self, replicas):
"""Create the PostgreSQL connector catalog files.
"""Create the PostgreSQL/MySQL connector catalog files.

Args:
replicas: the database replica configuration.
Expand All @@ -184,7 +184,7 @@ def _create_properties(self, replicas):
suffix = replica_info.get("suffix", "")

catalog_name = f"{self.name}{suffix}"
url = f"{self.backend['url']}/{self.info['database']}"
url = f"{self.backend['url']}/{self.info.get('database','')}"
if self.backend.get("params"):
url = f"{url}?{self.backend['params']}"

Expand Down
5 changes: 3 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
from ops.pebble import CheckStatus, ExecError

from catalog_manager import BigqueryCatalog, GsheetCatalog, PostgresqlCatalog
from catalog_manager import BigqueryCatalog, GsheetCatalog, SqlCatalog
from literals import (
CATALOG_DIR,
CATALOG_SCHEMA,
Expand Down Expand Up @@ -467,7 +467,8 @@ def _create_catalog_instance(self, truststore_pwd, name, info, backend):
ValueError: in case the backend type is not supported.
"""
catalog_map = {
"postgresql": PostgresqlCatalog,
"postgresql": SqlCatalog,
"mysql": SqlCatalog,
"bigquery": BigqueryCatalog,
"gsheets": GsheetCatalog,
}
Expand Down
2 changes: 1 addition & 1 deletion src/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"secret-id": {"type": "string"},
}

POSTGRESQL_BACKEND_SCHEMA = {
SQL_BACKEND_SCHEMA = {
"connector": {"type": "string"},
"url": {"type": "string"},
"params": {"type": "string"},
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
password: pwd2
""" # nosec

MYSQL_REPLICA_SECRET = """\
ro:
user: trino_ro
password: pwd3
""" # nosec

CATALOG_QUERY = "SHOW CATALOGS"
TRINO_USER = "trino"

Expand Down Expand Up @@ -338,6 +344,8 @@ async def add_juju_secret(ops_test: OpsTest, connector_type: str):
"""
if connector_type == "postgresql":
data_args = [f"replicas={POSTGRESQL_REPLICA_SECRET}"] # nosec
elif connector_type == "mysql":
data_args = [f"replicas=={MYSQL_REPLICA_SECRET}"] # nosec
elif connector_type == "bigquery":
data_args = [f"service-accounts={BIGQUERY_SECRET}"] # nosec
elif connector_type == "gsheets":
Expand All @@ -355,6 +363,7 @@ async def add_juju_secret(ops_test: OpsTest, connector_type: str):

async def create_catalog_config(
postgresql_secret_id,
mysql_secret_id,
bigquery_secret_id,
gsheets_secret_id,
include_bigquery=True,
Expand All @@ -363,6 +372,7 @@ async def create_catalog_config(

Args:
postgresql_secret_id: the juju secret id for postgresql
mysql_secret_id: the juju secret id for postgresql
bigquery_secret_id: the juju secret id for bigquery
gsheets_secret_id: the juju secret id for gsheets
include_bigquery: flag to indicate if bigquery configuration should be included.
Expand All @@ -377,6 +387,9 @@ async def create_catalog_config(
backend: dwh
database: example
secret-id: {postgresql_secret_id}
mysql:
backend: mysql
secret-id: {mysql_secret_id}
bigquery:
backend: bigquery
project: project-12345
Expand All @@ -394,6 +407,14 @@ async def create_catalog_config(
case-insensitive-name-matching=true
decimal-mapping=allow_overflow
decimal-rounding-mode=HALF_UP
mysql:
connector: mysql
url: jdbc:mysql://mysql.com:3306
params: sslMode=REQUIRED
config: |
case-insensitive-name-matching=true
decimal-mapping=allow_overflow
decimal-rounding-mode=HALF_UP
bigquery:
connector: bigquery
config: |
Expand All @@ -408,6 +429,9 @@ async def create_catalog_config(
backend: dwh
database: example
secret-id: {postgresql_secret_id}
mysql:
backend: mysql
secret-id: {mysql_secret_id}
gsheets-1:
backend: gsheets
metasheet-id: 1Es4HhWALUQjoa-bQh4a8B5HROz7dpGMfq_HbfoaW5LM
Expand All @@ -421,6 +445,14 @@ async def create_catalog_config(
case-insensitive-name-matching=true
decimal-mapping=allow_overflow
decimal-rounding-mode=HALF_UP
mysql:
connector: mysql
url: jdbc:mysql://mysql.com:3306
params: sslMode=REQUIRED
config: |
case-insensitive-name-matching=true
decimal-mapping=allow_overflow
decimal-rounding-mode=HALF_UP
gsheets:
connector: gsheets
"""
Expand Down
16 changes: 14 additions & 2 deletions tests/integration/test_charm.py
gtato marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,39 @@ async def test_basic_client(self, ops_test: OpsTest):
async def test_catalog_config(self, ops_test: OpsTest):
"""Adds a PostgreSQL and BigQuery connector and asserts catalogs added."""
postgresql_secret_id = await add_juju_secret(ops_test, "postgresql")
mysql_secret_id = await add_juju_secret(ops_test, "mysql")
bigquery_secret_id = await add_juju_secret(ops_test, "bigquery")
gsheet_secret_id = await add_juju_secret(ops_test, "gsheets")

for app in ["trino-k8s", "trino-k8s-worker"]:
await ops_test.model.grant_secret("postgresql-secret", app)
await ops_test.model.grant_secret("mysql-secret", app)
await ops_test.model.grant_secret("bigquery-secret", app)
await ops_test.model.grant_secret("gsheets-secret", app)

catalog_config = await create_catalog_config(
postgresql_secret_id, bigquery_secret_id, gsheet_secret_id, True
postgresql_secret_id,
mysql_secret_id,
bigquery_secret_id,
gsheet_secret_id,
True,
)
catalogs = await update_catalog_config(
ops_test, catalog_config, TRINO_USER
)

# Verify that both catalogs have been added.
assert "postgresql-1" in str(catalogs)
assert "mysql" in str(catalogs)
assert "bigquery" in str(catalogs)
assert "gsheets-1" in str(catalogs)

updated_catalog_config = await create_catalog_config(
postgresql_secret_id, bigquery_secret_id, gsheet_secret_id, False
postgresql_secret_id,
mysql_secret_id,
bigquery_secret_id,
gsheet_secret_id,
False,
)

catalogs = await update_catalog_config(
Expand All @@ -72,6 +83,7 @@ async def test_catalog_config(self, ops_test: OpsTest):

# Verify that only the bigquery catalog has been removed.
assert "postgresql-1" in str(catalogs)
assert "mysql" in str(catalogs)
assert "bigquery" not in str(catalogs)
assert "gsheets-1" in str(catalogs)

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
user: trino_ro
password: pwd2
""" # nosec
MYSQL_REPLICA_SECRET = """\
ro:
user: trino_ro
password: pwd3
""" # nosec
POSTGRESQL_REPLICA_CERT = """\
cert: |
-----BEGIN CERTIFICATE-----
Expand All @@ -61,6 +66,7 @@
POSTGRESQL_2_CATALOG_PATH = (
"/usr/lib/trino/etc/catalog/postgresql-2.properties"
)
MYSQL_CATALOG_PATH = "/usr/lib/trino/etc/catalog/mysql.properties"
BIGQUERY_CATALOG_PATH = "/usr/lib/trino/etc/catalog/bigquery.properties"
RANGER_PROPERTIES_PATH = "/usr/lib/ranger/install.properties"
POLICY_MGR_URL = "http://ranger-k8s:6080"
Expand Down
Loading
Loading