Skip to content

Commit 401d6ac

Browse files
committed
Add remotehq control room endpoitns
1 parent b85010c commit 401d6ac

File tree

11 files changed

+477
-54
lines changed

11 files changed

+477
-54
lines changed

api/control_rooms.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import requests
2+
from datetime import datetime, timezone
3+
4+
from http import HTTPStatus
5+
6+
from api.helpers import _get_body
7+
from api.helpers import http_response
8+
from api.remotehq_helpers import ddb_get_room
9+
from api.remotehq_helpers import ddb_get_all_rooms
10+
from api.remotehq_helpers import ddb_put_room
11+
from api.remotehq_helpers import ddb_delete_room
12+
from api.remotehq_helpers import create_new_room
13+
from api.remotehq_helpers import delete_room
14+
from api.remotehq_helpers import edit_room_configuration
15+
from api.remotehq_helpers import restart_control_room
16+
from api.remotehq_helpers import Room
17+
18+
### Endpoints
19+
20+
def get_control_room(event, context):
21+
site = event['pathParameters']['site']
22+
23+
# Return the existing room if it exists
24+
room = Room.from_dynamodb(site)
25+
if room is not None:
26+
return http_response(HTTPStatus.OK, room.get_data())
27+
28+
# Otherwise, create a new room
29+
room = Room.new_site_control_room(site)
30+
if room is not None:
31+
return http_response(HTTPStatus.OK, room.get_data())
32+
33+
return http_response(HTTPStatus.INTERNAL_SERVER_ERROR, 'failed to get room')
34+
35+
def modify_room(event, context):
36+
pass
37+
38+
def restart_control_room_handler(event, context):
39+
site = event['pathParameters']['site']
40+
41+
# verify that the room already exists.
42+
if site not in [room['site'] for room in ddb_get_all_rooms()]:
43+
return http_response(HTTPStatus.NOT_FOUND, f'no control room found for site {site}')
44+
45+
# delete and recreate the room
46+
room = Room.new_site_control_room(site)
47+
48+
# verify that it worked
49+
if room is not None:
50+
return http_response(HTTPStatus.OK, 'control room restarted successfully')
51+
else:
52+
response_content = {
53+
"message": f"Problem modifying room config",
54+
"site": site,
55+
"room_data": room.get_data(),
56+
}
57+
return http_response(HTTPStatus.INTERNAL_SERVER_ERROR, response_content)
58+
59+
def restart_all_rooms_handler(event, context):
60+
# utc hour when restart should happen
61+
# 3pm local site time
62+
restart_times = {
63+
"mrc": 22,
64+
"mrc2": 22,
65+
"sqa": 22,
66+
"sro": 22,
67+
"saf": 23,
68+
"dht": 22,
69+
"tst": 12,
70+
"tst001": 12,
71+
}
72+
current_utc_hour = datetime.now(timezone.utc).timetuple().tm_hour
73+
74+
all_rooms = ddb_get_all_rooms()
75+
for room_data in all_rooms:
76+
site = room_data["site"]
77+
if site not in restart_times.keys():
78+
print(f'Missing restart time for site {site}')
79+
continue
80+
if restart_times[site] == current_utc_hour:
81+
print(f'...restarting room {site}')
82+
room = Room.new_site_control_room(site)
83+
if room is None:
84+
print(f'Problem reloading site {site}')
85+
else:
86+
print(f'Successfully restarted site {site}')
87+
else:
88+
hours_until_restart = (restart_times[site] - current_utc_hour) % 24
89+
print(f'...room f{site} not due to restart for {hours_until_restart} hours')
90+
91+
92+
def delete_control_room(event, context):
93+
site = event['pathParameters']['site']
94+
95+
# Get room details from dynamodb
96+
room = Room.from_dynamodb(site)
97+
if room is not None:
98+
room.delete_room()
99+
return http_response(HTTPStatus.OK, 'room has been deleted')
100+
101+
else:
102+
return http_response(HTTPStatus.OK, 'no such room found')

api/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
1616
from sqlalchemy.exc import ArgumentError
1717

18-
from api.helpers import _get_secret, http_response
18+
from api.helpers import get_secret, http_response
1919
from api.helpers import get_s3_image_path, get_s3_file_url
2020

2121
from api.site_configs import get_all_sites
@@ -28,7 +28,7 @@
2828
logger.setLevel(logging.DEBUG)
2929

3030
Base = declarative_base()
31-
DB_ADDRESS = _get_secret('db-url')
31+
DB_ADDRESS = get_secret('db-url')
3232

3333
@contextmanager
3434
def get_session(db_address):

api/events_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from api.events import get_moon_riseset_illum
1717
from helpers import BUCKET_NAME, REGION, S3_PUT_TTL, S3_GET_TTL
1818
from helpers import dynamodb_r, ssm_c
19-
from helpers import DecimalEncoder, http_response, _get_body, _get_secret, get_db_connection
19+
from helpers import DecimalEncoder, http_response, _get_body, get_secret, get_db_connection
2020

2121
log = logging.getLogger()
2222
log.setLevel(logging.INFO)

api/handler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313

1414
from api.helpers import BUCKET_NAME, REGION, S3_PUT_TTL, S3_GET_TTL
1515
from api.helpers import dynamodb_r
16-
from api.helpers import DecimalEncoder, http_response, _get_body, _get_secret, get_db_connection
16+
from api.helpers import DecimalEncoder, http_response, _get_body, get_secret, get_db_connection
1717
from api.helpers import get_base_filename_from_full_filename
1818
from api.helpers import get_s3_file_url
1919
from api.s3_helpers import save_tiff_to_s3
2020

2121
from api.db import get_files_within_date_range
2222

23-
db_host = _get_secret('db-host')
24-
db_database = _get_secret('db-database')
25-
db_user = _get_secret('db-user')
26-
db_password = _get_secret('db-password')
23+
db_host = get_secret('db-host')
24+
db_database = get_secret('db-database')
25+
db_user = get_secret('db-user')
26+
db_password = get_secret('db-password')
2727

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

40-
return http_response(HTTPStatus.OK, "auth successful")
41+
return http_response(HTTPStatus.OK, "successful authorization")
4142

4243

4344
def default(event, context):

api/helpers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def http_response(status_code, body):
3939
"Access-Control-Allow-Origin": "*",
4040
# Required for cookies, authorization headers with HTTPS
4141
"Access-Control-Allow-Credentials": "true",
42-
"Access-Control-Allow-Headers": "*",
43-
"Content-Type": "application/json",
42+
#"Access-Control-Allow-Headers": "*",
43+
#"Content-Type": "application/json",
4444
},
4545
"body": body
4646
}
@@ -52,7 +52,7 @@ def _get_body(event):
5252
log.exception("event body could not be JSON decoded.")
5353
return {}
5454

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

6666
def get_db_connection():
6767
connection_params = {
68-
'host': _get_secret('db-host'),
69-
'database': _get_secret('db-database'),
70-
'user': _get_secret('db-user'),
71-
'password': _get_secret('db-password')
68+
'host': get_secret('db-host'),
69+
'database': get_secret('db-database'),
70+
'user': get_secret('db-user'),
71+
'password': get_secret('db-password')
7272
}
7373
connection = psycopg2.connect(**connection_params)
7474
return connection

api/info_images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
from http import HTTPStatus
55

6-
from api.helpers import _get_secret, http_response, get_s3_file_url
6+
from api.helpers import get_secret, http_response, get_s3_file_url
77

88

99
ddb = boto3.resource('dynamodb', os.getenv('REGION'))

0 commit comments

Comments
 (0)