Skip to content

Commit

Permalink
Add env support
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Gopathi <[email protected]>
  • Loading branch information
RahulGopathi committed Sep 16, 2023
1 parent 61c75e6 commit 25e6555
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# database configs
DB_HOST=localhost
DB_USER=user
DB_PASSWORD=password
DB_NAME=aws_database

# offer names to process
OFFER_NAMES_TO_PROCESS=AmazonS3,AmazonRDS
3 changes: 2 additions & 1 deletion api/database.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from decouple import config

# Set up the database connection
DATABASE_URL = "mysql+mysqlconnector://aws:[email protected]/aws_database"
DATABASE_URL = f"mysql+mysqlconnector://{config('DB_USER')}:{config('DB_PASSWORD')}@{config('DB_HOST')}/{config('DB_NAME')}"
engine = create_engine(DATABASE_URL)

# Create a session to interact with the database
Expand Down
24 changes: 13 additions & 11 deletions load-data/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests
import mysql.connector
import sys
from decouple import config

# Check if a JSON file name was provided as a command-line argument
if len(sys.argv) != 2:
Expand All @@ -19,11 +20,7 @@
sys.exit(1)

# Define a list of offer names to process
offer_names_to_process = [
"AmazonS3",
"AmazonRDS",
# Add more offer names here as needed
]
offer_names_to_process = config('OFFER_NAMES_TO_PROCESS', cast=lambda v: [s.strip() for s in v.split(',')])


# Function to create the database and tables
Expand Down Expand Up @@ -135,10 +132,9 @@ def process_offer(offer_name, offer_details, cursor):

# Define MySQL database connection parameters
db_config = {
'host': '10.22.0.140',
'user': 'aws',
'password': 'aws',
'database': 'awsdb',
'host': config('DB_HOST'),
'user': config('DB_USER'),
'password': config('DB_PASSWORD'),
}

# Create a MySQL database connection
Expand All @@ -153,9 +149,15 @@ def process_offer(offer_name, offer_details, cursor):

print("Processing offers...")
# Iterate over the offers in the JSON data
for offer_name, offer_details in data['offers'].items():
if offer_name in offer_names_to_process:
if offer_names_to_process == ['*']:
for offer_name, offer_details in data['offers'].items():
process_offer(offer_name, offer_details, cursor)
else:
for offer_name in offer_names_to_process:
if offer_name in data['offers']:
process_offer(offer_name, data['offers'][offer_name], cursor)
else:
print(f"Offer '{offer_name}' not found in JSON data.")

# Commit changes and close the database connection
connection.commit()
Expand Down

0 comments on commit 25e6555

Please sign in to comment.