Skip to content

Commit

Permalink
Add remotehq control room endpoitns
Browse files Browse the repository at this point in the history
  • Loading branch information
timbeccue committed May 26, 2022
1 parent b85010c commit 401d6ac
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 54 deletions.
102 changes: 102 additions & 0 deletions api/control_rooms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import requests
from datetime import datetime, timezone

from http import HTTPStatus

from api.helpers import _get_body
from api.helpers import http_response
from api.remotehq_helpers import ddb_get_room
from api.remotehq_helpers import ddb_get_all_rooms
from api.remotehq_helpers import ddb_put_room
from api.remotehq_helpers import ddb_delete_room
from api.remotehq_helpers import create_new_room
from api.remotehq_helpers import delete_room
from api.remotehq_helpers import edit_room_configuration
from api.remotehq_helpers import restart_control_room
from api.remotehq_helpers import Room

### Endpoints

def get_control_room(event, context):
site = event['pathParameters']['site']

# Return the existing room if it exists
room = Room.from_dynamodb(site)
if room is not None:
return http_response(HTTPStatus.OK, room.get_data())

# Otherwise, create a new room
room = Room.new_site_control_room(site)
if room is not None:
return http_response(HTTPStatus.OK, room.get_data())

return http_response(HTTPStatus.INTERNAL_SERVER_ERROR, 'failed to get room')

def modify_room(event, context):
pass

def restart_control_room_handler(event, context):
site = event['pathParameters']['site']

# verify that the room already exists.
if site not in [room['site'] for room in ddb_get_all_rooms()]:
return http_response(HTTPStatus.NOT_FOUND, f'no control room found for site {site}')

# delete and recreate the room
room = Room.new_site_control_room(site)

# verify that it worked
if room is not None:
return http_response(HTTPStatus.OK, 'control room restarted successfully')
else:
response_content = {
"message": f"Problem modifying room config",
"site": site,
"room_data": room.get_data(),
}
return http_response(HTTPStatus.INTERNAL_SERVER_ERROR, response_content)

def restart_all_rooms_handler(event, context):
# utc hour when restart should happen
# 3pm local site time
restart_times = {
"mrc": 22,
"mrc2": 22,
"sqa": 22,
"sro": 22,
"saf": 23,
"dht": 22,
"tst": 12,
"tst001": 12,
}
current_utc_hour = datetime.now(timezone.utc).timetuple().tm_hour

all_rooms = ddb_get_all_rooms()
for room_data in all_rooms:
site = room_data["site"]
if site not in restart_times.keys():
print(f'Missing restart time for site {site}')
continue
if restart_times[site] == current_utc_hour:
print(f'...restarting room {site}')
room = Room.new_site_control_room(site)
if room is None:
print(f'Problem reloading site {site}')
else:
print(f'Successfully restarted site {site}')
else:
hours_until_restart = (restart_times[site] - current_utc_hour) % 24
print(f'...room f{site} not due to restart for {hours_until_restart} hours')


def delete_control_room(event, context):
site = event['pathParameters']['site']

# Get room details from dynamodb
room = Room.from_dynamodb(site)
if room is not None:
room.delete_room()
return http_response(HTTPStatus.OK, 'room has been deleted')

else:
return http_response(HTTPStatus.OK, 'no such room found')
4 changes: 2 additions & 2 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from sqlalchemy.exc import ArgumentError

from api.helpers import _get_secret, http_response
from api.helpers import get_secret, http_response
from api.helpers import get_s3_image_path, get_s3_file_url

from api.site_configs import get_all_sites
Expand All @@ -28,7 +28,7 @@
logger.setLevel(logging.DEBUG)

Base = declarative_base()
DB_ADDRESS = _get_secret('db-url')
DB_ADDRESS = get_secret('db-url')

@contextmanager
def get_session(db_address):
Expand Down
2 changes: 1 addition & 1 deletion api/events_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from api.events import get_moon_riseset_illum
from helpers import BUCKET_NAME, REGION, S3_PUT_TTL, S3_GET_TTL
from helpers import dynamodb_r, ssm_c
from helpers import DecimalEncoder, http_response, _get_body, _get_secret, get_db_connection
from helpers import DecimalEncoder, http_response, _get_body, get_secret, get_db_connection

log = logging.getLogger()
log.setLevel(logging.INFO)
Expand Down
13 changes: 7 additions & 6 deletions api/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@

from api.helpers import BUCKET_NAME, REGION, S3_PUT_TTL, S3_GET_TTL
from api.helpers import dynamodb_r
from api.helpers import DecimalEncoder, http_response, _get_body, _get_secret, get_db_connection
from api.helpers import DecimalEncoder, http_response, _get_body, get_secret, get_db_connection
from api.helpers import get_base_filename_from_full_filename
from api.helpers import get_s3_file_url
from api.s3_helpers import save_tiff_to_s3

from api.db import get_files_within_date_range

db_host = _get_secret('db-host')
db_database = _get_secret('db-database')
db_user = _get_secret('db-user')
db_password = _get_secret('db-password')
db_host = get_secret('db-host')
db_database = get_secret('db-database')
db_user = get_secret('db-user')
db_password = get_secret('db-password')

log = logging.getLogger()
log.setLevel(logging.INFO)
Expand All @@ -36,8 +36,9 @@
def dummy_requires_auth(event, context):
""" No purpose other than testing auth functionality """
log.info(json.dumps(event, indent=2))
log.info("Testing individual method deployment")

return http_response(HTTPStatus.OK, "auth successful")
return http_response(HTTPStatus.OK, "successful authorization")


def default(event, context):
Expand Down
14 changes: 7 additions & 7 deletions api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def http_response(status_code, body):
"Access-Control-Allow-Origin": "*",
# Required for cookies, authorization headers with HTTPS
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "*",
"Content-Type": "application/json",
#"Access-Control-Allow-Headers": "*",
#"Content-Type": "application/json",
},
"body": body
}
Expand All @@ -52,7 +52,7 @@ def _get_body(event):
log.exception("event body could not be JSON decoded.")
return {}

def _get_secret(key):
def get_secret(key):
"""
Some parameters are stored in AWS Systems Manager Parameter Store.
This replaces the .env variables we used to use with flask.
Expand All @@ -65,10 +65,10 @@ def _get_secret(key):

def get_db_connection():
connection_params = {
'host': _get_secret('db-host'),
'database': _get_secret('db-database'),
'user': _get_secret('db-user'),
'password': _get_secret('db-password')
'host': get_secret('db-host'),
'database': get_secret('db-database'),
'user': get_secret('db-user'),
'password': get_secret('db-password')
}
connection = psycopg2.connect(**connection_params)
return connection
Expand Down
2 changes: 1 addition & 1 deletion api/info_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from http import HTTPStatus

from api.helpers import _get_secret, http_response, get_s3_file_url
from api.helpers import get_secret, http_response, get_s3_file_url


ddb = boto3.resource('dynamodb', os.getenv('REGION'))
Expand Down
Loading

0 comments on commit 401d6ac

Please sign in to comment.