From 25e655558b4bbcce14b189debd3ffeba4ec7771e Mon Sep 17 00:00:00 2001 From: Rahul Gopathi Date: Sat, 16 Sep 2023 07:21:01 +0530 Subject: [PATCH] Add env support Signed-off-by: Rahul Gopathi --- .env.example | 8 ++++++++ api/database.py | 3 ++- load-data/main.py | 24 +++++++++++++----------- 3 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d5bf62c --- /dev/null +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/api/database.py b/api/database.py index ac00543..0454921 100644 --- a/api/database.py +++ b/api/database.py @@ -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:aws@10.22.0.140/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 diff --git a/load-data/main.py b/load-data/main.py index f3eaa81..2cb2d81 100644 --- a/load-data/main.py +++ b/load-data/main.py @@ -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: @@ -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 @@ -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 @@ -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()