Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 5 additions & 47 deletions execute_procedure.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,8 @@
import boto3
import pyodbc
from pub_oapi_tools_common import ucpms_db

# Get AWS session
session = boto3.Session()
conn = ucpms_db.get_connection(env="prod")

with conn.cursor() as cursor:
cursor.execute("EXEC UCOPreports.update_user_data_changes;")

def main():
creds = get_ssm_parameters(
folder="/pub-oapi-tools/elements-reporting-db/prod",
names=['driver', 'server', 'database', 'user', 'password'])

# Connect to Elements Reporting DB
conn = pyodbc.connect(
driver=creds['driver'],
server=(creds['server'] + ',1433'),
database=creds['database'],
uid=creds['user'],
pwd=creds['password'],
trustservercertificate='yes')

conn.autocommit = True

cursor = conn.cursor()
cursor.execute(
"EXEC UCOPreports.update_user_data_changes;")
conn.commit()

cursor.close()
conn.close()


# Parses data from parameter store
def get_ssm_parameters(folder, names):
print("Connect to SSM for parameters")
ssm_client = session.client(service_name='ssm', region_name='us-west-2')

param_names = [f"{folder}/{name}" for name in names]
response = ssm_client.get_parameters(Names=param_names, WithDecryption=True)

param_values = {
(param['Name'].split('/')[-1]): param['Value']
for param in response['Parameters']}

return param_values


# Stub for main
if __name__ == '__main__':
main()
conn.close()
78 changes: 21 additions & 57 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
from pub_oapi_tools_common import aws_lambda
from pub_oapi_tools_common import ucpms_db

from datetime import datetime
import subprocess
import pyodbc
import csv
import boto3
import requests

# Get AWS session
session = boto3.Session()


def main():
today_string = datetime.today().strftime('%Y-%m-%d')

# Get reporting DB creds
reporting_db_creds = get_ssm_parameters(
folder="/pub-oapi-tools/elements-reporting-db/prod",
names=['driver', 'server', 'database', 'user', 'password'])

# Get email addresses DB creds
emails = get_ssm_parameters(
folder="/pub-oapi-tools/emails",
names=['devin', 'alainna'])

# Load the sql file data from Github, replace a text string with today's date
pgd_change_query = get_sql_from_github()
pgd_change_query = pgd_change_query.replace('#REPLACE', today_string)

pdg_change_report = query_reporting_db(reporting_db_creds, pgd_change_query)
pdg_change_report = query_reporting_db(pgd_change_query)
pdg_change_report_file = write_csv_file(pdg_change_report, "pdg_change_report", today_string)

# Get email addresses
param_req = {"emails": {
"folder": "pub-oapi-tools/emails",
"names": ['devin', 'alainna']}}
param_return = aws_lambda.get_parameters(param_req=param_req)
emails = param_return['emails'].values()

# Set up the mail process with attachment and email recipients
subprocess_setup = ['mail',
'-s', 'TEST: PGD changes report',
'-s', 'PGD changes report',
'-a', pdg_change_report_file]
subprocess_setup += [emails['devin'], emails['alainna']]
subprocess_setup += emails

# Text in the email body
input_byte_string = b'''
The attached CSV includes users' primary group changes in the span of one month.
The attached CSV includes users' primary group changes in the span of one month.

Future-Proofing: This automated email was sent from the following program:
https://github.com/eScholarship/oapi-user-data-changes-report/
'''
Future-Proofing: This automated email was sent from the following program:https://github.com/eScholarship/oapi-user-data-changes-report/ '''

# Run the subprocess with EOT input to send
subprocess.run(
Expand All @@ -50,25 +43,12 @@ def main():
capture_output=True)


def query_reporting_db(creds, query):
# Connect to Elements reporting db
conn = pyodbc.connect(
driver=creds['driver'],
server=(creds['server'] + ',1433'),
database=creds['database'],
uid=creds['user'],
pwd=creds['password'],
trustservercertificate='yes')

print(f"Connected to Elements reporting DB, querying.")
conn.autocommit = True # Required when queries use TRANSACTION
cursor = conn.cursor()
cursor.execute(query)

# pyodbc doesn't return dicts automatically, we have to make them ourselves
columns = [column[0] for column in cursor.description]
rows = [dict(zip(columns, row)) for row in cursor.fetchall()]

def query_reporting_db(query):
conn = ucpms_db.get_connection(env="prod")
with conn.cursor() as cursor:
cursor.execute(query)
rows = ucpms_db.get_dict_list(cursor)
conn.close()
return rows


Expand All @@ -86,25 +66,9 @@ def write_csv_file(data, filename, today_string):
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)

return filename_with_date


# Parses data from parameter store
def get_ssm_parameters(folder, names):
print("Connect to SSM for parameters")
ssm_client = session.client(service_name='ssm', region_name='us-west-2')

param_names = [f"{folder}/{name}" for name in names]
response = ssm_client.get_parameters(Names=param_names, WithDecryption=True)

param_values = {
(param['Name'].split('/')[-1]): param['Value']
for param in response['Parameters']}

return param_values


# Stub for main
if __name__ == '__main__':
main()