Skip to content

Commit

Permalink
set up cloud sql python connector (#3114)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolyachevets authored Oct 23, 2024
1 parent 780f622 commit 3e1bb85
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 12 deletions.
86 changes: 85 additions & 1 deletion queue_services/account-mailer/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions queue_services/account-mailer/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ zipp = "3.19.1"
auth-api = { git = "https://github.com/bcgov/sbc-auth.git", rev = "feature-gcp-migration", subdirectory = "auth-api" }
simple-cloudevent = { git = "https://github.com/daxiom/simple-cloudevent.py.git" }
build-deps = { git = "https://github.com/bcgov/sbc-auth.git", rev = "feature-gcp-migration", subdirectory = "build-deps" }
cloud-sql-python-connector = "^1.13.0"

[tool.poetry.group.dev.dependencies]
psycopg2 = "^2.9.9"
Expand Down
71 changes: 61 additions & 10 deletions queue_services/account-mailer/src/account_mailer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""The Events Listener service.
This module is the service worker for applying filings to the Business Database structure.
"""
from __future__ import annotations

import os
from dataclasses import dataclass

from auth_api.exceptions import ExceptionHandler
from auth_api.models import db
Expand All @@ -26,29 +28,78 @@
from auth_api.services.gcp_queue import queue
from auth_api.utils.cache import cache
from flask import Flask
from google.cloud.sql.connector import Connector

from account_mailer import config
from account_mailer import config as app_config
from account_mailer.resources.worker import bp as worker_endpoint


def register_endpoints(app: Flask):
"""Register endpoints with the flask application."""
# Allow base route to match with, and without a trailing slash
app.url_map.strict_slashes = False
@dataclass
class DBConfig:
"""Database configuration settings."""

unix_sock: str
database: str
user: str
password: str


app.register_blueprint(
url_prefix='/',
blueprint=worker_endpoint,
def getconn(connector: Connector, db_config: DBConfig) -> object:
"""Create a database connection.
Args:
connector (Connector): The Google Cloud SQL connector instance.
db_config (DBConfig): The database configuration.
Returns:
object: A connection object to the database.
"""
return connector.connect(
instance_connection_string=db_config.unix_sock.replace('/cloudsql/', ''),
ip_type='private',
user=db_config.user,
password=db_config.password,
db=db_config.database,
driver='pg8000',
)


def register_endpoints(app: Flask) -> None:
"""Register endpoints with the Flask application.
Args:
app (Flask): The Flask application instance.
"""
app.url_map.strict_slashes = False
app.register_blueprint(worker_endpoint, url_prefix='/')
app.register_blueprint(ops_bp)


def create_app(run_mode=os.getenv('DEPLOYMENT_ENV', 'production')) -> Flask:
"""Return a configured Flask App using the Factory method."""
"""Return a configured Flask App using the Factory method.
Args:
run_mode (str): The running mode of the application (e.g., production, development).
Returns:
Flask: The configured Flask application instance.
"""
app = Flask(__name__)
app.config.from_object(config.get_named_config(run_mode))
app.config.from_object(app_config.get_named_config(run_mode))
app.config['ENV'] = run_mode

if app.config.get('DB_UNIX_SOCKET'):
connector = Connector(refresh_strategy='lazy')
db_config = DBConfig(
unix_sock=app.config.get('DB_UNIX_SOCKET'),
database=app.config.get('DB_NAME'),
user=app.config.get('DB_USER'),
password=app.config.get('DB_PASSWORD')
)
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'creator': lambda: getconn(connector, db_config)
}

db.init_app(app)
flags.init_app(app)
cache.init_app(app)
Expand Down
2 changes: 1 addition & 1 deletion queue_services/account-mailer/src/account_mailer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class _Config(): # pylint: disable=too-few-public-methods
DB_HOST = os.getenv('DATABASE_HOST', '')
DB_PORT = os.getenv('DATABASE_PORT', '5432')
if DB_UNIX_SOCKET := os.getenv('DATABASE_UNIX_SOCKET', None):
SQLALCHEMY_DATABASE_URI = f'postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}' # noqa: E231, E501
SQLALCHEMY_DATABASE_URI = 'postgresql+pg8000://'
else:
SQLALCHEMY_DATABASE_URI = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}' # noqa: E231, E501

Expand Down

0 comments on commit 3e1bb85

Please sign in to comment.