canonical / paas-app-charmer

2 stars 2 forks source link

Parse all components of a database url #26

Closed javierdelapuente closed 4 months ago

javierdelapuente commented 4 months ago

Applicable spec:

Overview

We want to parse all the components of a database URL, so Flask and Django applications do not have to do it.

Besides the current environment variable with the full connection string, new environment variables will be added to the workload.

As an example for POSTGRESQL these will be all the environment variables (besides POSTGRESQL, similar variables will be created for REDIS, MONGODB and MYSQL):

POSTGRESQL_DB_CONNECT_STRING. This environment variable is the full URL (as it was before this PR)

From urllib.parse, and using the same converntions for default values (see https://docs.python.org/3/library/urllib.parse.html#url-parsing) then next environment variables will be added when the parsed component is not None: POSTGRESQL_DB_SCHEME POSTGRESQL_DB_NETLOC POSTGRESQL_DB_PATH POSTGRESQL_DB_PARAMS POSTGRESQL_DB_QUERY POSTGRESQL_DB_FRAGMENT POSTGRESQL_DB_USERNAME POSTGRESQL_DB_PASSWORD POSTGRESQL_DB_HOSTNAME POSTGRESQL_DB_PORT

Besides, if the variable POSTGRESQL_DB_PATH contains a possible database name, the next variable will be added (see https://www.prisma.io/dataguide/postgresql/short-guides/connection-uris#a-quick-overview for where the database name is located in the url): POSTGRESQL_DB_NAME

Rationale

Currently the user of paas-app-charmer has to split the components in the workload. With this PR the user just has to use the environment variables.

Juju Events Changes

Module Changes

Library Changes

Checklist

javierdelapuente commented 4 months ago

@erinecon this PR adds new environment variables

github-actions[bot] commented 4 months ago

Test coverage for f86da83c48dcef734be171edf1ea63a773001670

Name                                            Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------------------------------------------
paas_app_charmer/__init__.py                       29     14      0      0    52%   13-14, 19-20, 26-27, 33-34, 40-41, 47-48, 54-55
paas_app_charmer/_gunicorn/__init__.py              0      0      0      0   100%
paas_app_charmer/_gunicorn/charm.py               189     24     46      4    87%   32-33, 40-41, 160-161, 163-164, 185->exit, 197-201, 254-256, 323-324, 329, 334, 339, 349, 354, 359, 364, 369
paas_app_charmer/_gunicorn/charm_state.py         106      2     20      2    97%   176, 260
paas_app_charmer/_gunicorn/charm_utils.py          23      0      0      0   100%
paas_app_charmer/_gunicorn/observability.py        13      0      2      0   100%
paas_app_charmer/_gunicorn/secret_storage.py       13      0      0      0   100%
paas_app_charmer/_gunicorn/webserver.py            75      4     14      1    94%   156, 168-174
paas_app_charmer/_gunicorn/workload_config.py      19      0      0      0   100%
paas_app_charmer/_gunicorn/wsgi_app.py            102      0     42      2    99%   84->86, 114->exit
paas_app_charmer/database_migration.py             35      0      2      0   100%
paas_app_charmer/databases.py                      25      2     11      1    92%   89-90
paas_app_charmer/django/__init__.py                 1      0      0      0   100%
paas_app_charmer/django/charm.py                   45      6      6      1    86%   73-77, 99, 114-115
paas_app_charmer/exceptions.py                      5      0      0      0   100%
paas_app_charmer/flask/__init__.py                  1      0      0      0   100%
paas_app_charmer/flask/charm.py                    37      0      2      0   100%
paas_app_charmer/secret_storage.py                 39      3     16      5    85%   50, 54->53, 55->57, 85, 104
paas_app_charmer/utils.py                          11      0     12      0   100%
-------------------------------------------------------------------------------------------
TOTAL                                             768     55    173     16    92%

Static code analysis report

Run started:2024-07-04 15:36:15.883718

Test results:
    No issues identified.

Code scanned:
    Total lines of code: 1652
    Total lines skipped (#nosec): 0
    Total potential issues skipped due to specifically being disabled (e.g., #nosec BXXX): 0

Run metrics:
    Total issues (by severity):
        Undefined: 0
        Low: 0
        Medium: 0
        High: 0
    Total issues (by confidence):
        Undefined: 0
        Low: 0
        Medium: 0
        High: 0
Files skipped (0):
javierdelapuente commented 4 months ago

LGTM. Should this be documented somewhere?

Yes, @erinecon is working on new doc for the flask-framework. I will add it there. Thanks for the review