@@ -18,54 +18,22 @@ class sra_dynamodb:
1818 UNEXPECTED = "Unexpected!"
1919
2020 LOGGER = logging .getLogger (__name__ )
21- log_level : str = os .environ .get ("LOG_LEVEL" , "DEBUG " )
21+ log_level : str = os .environ .get ("LOG_LEVEL" , "INFO " )
2222 LOGGER .setLevel (log_level )
2323
24- # DEBUG STUFF
25- import sys
26- system_path = []
27- for path in sys .path :
28- system_path .append (path )
29- LOGGER .debug (f"Python import paths: { system_path } " )
30-
31- import pkgutil
32- packages_installed = []
33- for module in pkgutil .iter_modules ():
34- packages_installed .append (module .name )
35- LOGGER .debug (f"Installed packages: { packages_installed } " )
36-
37- # try:
38- # from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource
39- # LOGGER.info("Successfully imported DynamoDBServiceResource.")
40- # except ModuleNotFoundError as e:
41- # LOGGER.error(f"Failed to import DynamoDBServiceResource: {e}")
42- # except Exception as e:
43- # LOGGER.error(f"Unexpected error during import: {e}")
44-
45- # try:
46- # from mypy_boto3_dynamodb.client import DynamoDBClient
47- # LOGGER.info("Successfully imported DynamoDBClient.")
48- # except ModuleNotFoundError as e:
49- # LOGGER.error(f"Failed to import DynamoDBClient: {e}")
50- # except Exception as e:
51- # LOGGER.error(f"Unexpected error during import: {e}")
52- # END DEBUG STUFF
53-
5424 try :
55- MANAGEMENT_ACCOUNT_SESSION : Session = boto3 .Session ()
56- except Exception :
57- LOGGER .exception (UNEXPECTED )
58- raise ValueError ("Unexpected error executing Lambda function. Review CloudWatch logs for details. " ) from None
25+ MANAGEMENT_ACCOUNT_SESSION : Session = boto3 .Session ()
26+ except Exception as error :
27+ LOGGER .exception (f"Error creating boto3 session: { error } " )
28+ raise ValueError (f"Error creating boto3 session: { error } " ) from None
5929
6030 try :
61- # Use string-based type annotations
6231 DYNAMODB_CLIENT : "DynamoDBClient" = MANAGEMENT_ACCOUNT_SESSION .client ("dynamodb" )
6332 DYNAMODB_RESOURCE : "DynamoDBServiceResource" = MANAGEMENT_ACCOUNT_SESSION .resource ("dynamodb" )
64- # DYNAMODB_RESOURCE: DynamoDBServiceResource = MANAGEMENT_ACCOUNT_SESSION.resource("dynamodb")
65- # DYNAMODB_CLIENT: DynamoDBClient = MANAGEMENT_ACCOUNT_SESSION.client("dynamodb")
6633 LOGGER .info ("DynamoDB resource and client created successfully." )
6734 except Exception as error :
68- LOGGER .warning (f"Error creating boto3 dymanodb resource and client: { error } " )
35+ LOGGER .info (f"Error creating boto3 dymanodb resource and/or client: { error } " )
36+ raise ValueError (f"Error creating boto3 dymanodb resource and/or client: { error } " ) from None
6937
7038
7139 def __init__ (self , profile = "default" ) -> None :
@@ -76,12 +44,13 @@ def __init__(self, profile="default") -> None:
7644 else :
7745 self .MANAGEMENT_ACCOUNT_SESSION = boto3 .Session ()
7846
79- # self.DYNAMODB_RESOURCE = self.MANAGEMENT_ACCOUNT_SESSION.resource("dynamodb")
47+ self .DYNAMODB_RESOURCE = self .MANAGEMENT_ACCOUNT_SESSION .resource ("dynamodb" )
48+ self .DYNAMODB_CLIENT = self .MANAGEMENT_ACCOUNT_SESSION .client ("dynamodb" )
8049 except Exception :
8150 self .LOGGER .exception (self .UNEXPECTED )
8251 raise ValueError ("Unexpected error!" ) from None
8352
84- def create_table (self , table_name , dynamodb_client ):
53+ def create_table (self , table_name , dynamodb_client = DYNAMODB_CLIENT ):
8554 # Define table schema
8655 key_schema = [
8756 {"AttributeName" : "solution_name" , "KeyType" : "HASH" },
@@ -112,7 +81,7 @@ def create_table(self, table_name, dynamodb_client):
11281 # TODO(liamschn): need to add a maximum retry mechanism here
11382 sleep (5 )
11483
115- def table_exists (self , table_name , dynamodb_client ):
84+ def table_exists (self , table_name , dynamodb_client = DYNAMODB_CLIENT ):
11685 # Check if table exists
11786 try :
11887 dynamodb_client .describe_table (TableName = table_name )
@@ -130,7 +99,7 @@ def get_date_time(self):
13099 now = datetime .now ()
131100 return now .strftime ("%Y%m%d%H%M%S" )
132101
133- def insert_item (self , table_name , dynamodb_resource , solution_name ):
102+ def insert_item (self , table_name , solution_name , dynamodb_resource = DYNAMODB_RESOURCE ):
134103 table = dynamodb_resource .Table (table_name )
135104 record_id = self .generate_id ()
136105 date_time = self .get_date_time ()
@@ -144,7 +113,7 @@ def insert_item(self, table_name, dynamodb_resource, solution_name):
144113 # self.LOGGER.info({"insert_record_response": response})
145114 return record_id , date_time
146115
147- def update_item (self , table_name , dynamodb_resource , solution_name , record_id , attributes_and_values ):
116+ def update_item (self , table_name , solution_name , record_id , attributes_and_values , dynamodb_resource = DYNAMODB_RESOURCE ):
148117 self .LOGGER .info (f"Updating { table_name } dynamodb table with { attributes_and_values } " )
149118 table = dynamodb_resource .Table (table_name )
150119 update_expression = ""
@@ -167,7 +136,7 @@ def update_item(self, table_name, dynamodb_resource, solution_name, record_id, a
167136 )
168137 return response
169138
170- def find_item (self , table_name , dynamodb_resource , solution_name , additional_attributes ) -> tuple [bool , dict ]:
139+ def find_item (self , table_name , solution_name , additional_attributes , dynamodb_resource = DYNAMODB_RESOURCE ) -> tuple [bool , dict ]:
171140 """Find an item in the dynamodb table based on the solution name and additional attributes.
172141
173142 Args:
@@ -213,7 +182,7 @@ def get_unique_values_from_list(self, list_of_values):
213182 unique_values .append (value )
214183 return unique_values
215184
216- def get_distinct_solutions_and_accounts (self , table_name , dynamodb_resource ):
185+ def get_distinct_solutions_and_accounts (self , table_name , dynamodb_resource = DYNAMODB_RESOURCE ):
217186 table = dynamodb_resource .Table (table_name )
218187 response = table .scan ()
219188 solution_names = [item ["solution_name" ] for item in response ["Items" ]]
@@ -222,7 +191,7 @@ def get_distinct_solutions_and_accounts(self, table_name, dynamodb_resource):
222191 accounts = self .get_unique_values_from_list (accounts )
223192 return solution_names , accounts
224193
225- def get_resources_for_solutions_by_account (self , table_name , dynamodb_resource , solutions , account ):
194+ def get_resources_for_solutions_by_account (self , table_name , solutions , account , dynamodb_resource = DYNAMODB_RESOURCE ):
226195 table = dynamodb_resource .Table (table_name )
227196 query_results = {}
228197 for solution in solutions :
@@ -240,7 +209,7 @@ def get_resources_for_solutions_by_account(self, table_name, dynamodb_resource,
240209 query_results [solution ] = response
241210 return query_results
242211
243- def delete_item (self , table_name , dynamodb_resource , solution_name , record_id ):
212+ def delete_item (self , table_name , solution_name , record_id , dynamodb_resource = DYNAMODB_RESOURCE ):
244213 """Delete an item from the dynamodb table
245214
246215 Args:
0 commit comments