-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathput_pointers_from_files.py
More file actions
53 lines (42 loc) · 1.67 KB
/
put_pointers_from_files.py
File metadata and controls
53 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# Put pointers from the provided files into the pointers table
# This will overwrite the pointer if it already exists in the table
import json
import os
import fire
from aws_session_assume import get_boto_session
from nrlf.core.dynamodb.model import DocumentPointer
from nrlf.core.logger import logger
from nrlf.producer.fhir.r4.model import DocumentReference
logger.setLevel("ERROR")
SKIP_PROD_WARNING = os.getenv("SKIP_PROD_WARNING", "false")
def _put_pointers_from_files(
*filenames, env: str = "dev", table_name: str | None = None
):
if env == "prod" and SKIP_PROD_WARNING != "true":
confirmation = input(
"\nWARNING - This command will modify the PROD environment. Continue? [y/n] "
)
if confirmation != "y":
return "Exiting at user request"
docrefs: list[DocumentReference] = []
print("Reading docrefs from files...")
for filename in filenames:
with open(filename) as f:
docref_json = json.load(f)
docref = DocumentReference.model_validate(docref_json)
docrefs.append(docref)
session = get_boto_session(env)
dynamodb = session.resource("dynamodb")
if not table_name:
table_name = f"nhsd-nrlf--{env}-pointers-table"
table = dynamodb.Table(table_name)
for docref in docrefs:
try:
print(f"Putting {docref.id}....")
pointer = DocumentPointer.from_document_reference(docref)
table.put_item(Item=pointer.model_dump())
except Exception as e:
print(f"Unable to put pointer for {docref.id}. Error: {e}")
if __name__ == "__main__":
fire.Fire(_put_pointers_from_files)