Skip to content

Commit

Permalink
Added logic to set db url from vcap services (#4082)
Browse files Browse the repository at this point in the history
  • Loading branch information
sambodeme authored Jul 15, 2024
1 parent 8f8bb19 commit 29eb254
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions backend/config/db_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.core.exceptions import ImproperlyConfigured


def get_db_url_from_vcap_services(
vcap,
):
database_url = None
for db_service in vcap.get("aws-rds", []):
if db_service.get("instance_name") == "fac-db":
database_url = db_service["credentials"]["uri"]
break

if not database_url:
raise ImproperlyConfigured(
"Database URL is not properly configured. Expected 'fac-db' URL."
)
return database_url
7 changes: 6 additions & 1 deletion backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import sys
import logging
import json
from .db_url import get_db_url_from_vcap_services
import environs
from cfenv import AppEnv
from audit.get_agency_names import get_agency_names, get_audit_info_lists

import dj_database_url
import newrelic.agent

newrelic.agent.initialize()
Expand Down Expand Up @@ -284,6 +285,10 @@
STATICFILES_STORAGE = "storages.backends.s3boto3.S3ManifestStaticStorage"
DEFAULT_FILE_STORAGE = "report_submission.storages.S3PrivateStorage"
vcap = json.loads(env.str("VCAP_SERVICES"))

DB_URL = get_db_url_from_vcap_services(vcap)
DATABASES = {"default": dj_database_url.parse(DB_URL)}

for service in vcap["s3"]:
if service["instance_name"] == "fac-public-s3":
# Public AWS S3 bucket for the app
Expand Down

0 comments on commit 29eb254

Please sign in to comment.