11import logging
22import boto3
3- from boto3 .dynamodb .conditions import Key , Attr , ConditionBase
3+ from boto3 .dynamodb .conditions import Key , Attr
44import os
55import random
66import string
77from datetime import datetime
88from time import sleep
99import botocore
10- from botocore .exceptions import ClientError
1110from boto3 .session import Session
12- from typing import TYPE_CHECKING , Any , Sequence , cast , Dict , Tuple , List
11+ from typing import TYPE_CHECKING , Any , Dict , Sequence , cast
1312if TYPE_CHECKING :
1413 from mypy_boto3_dynamodb .client import DynamoDBClient
1514 from mypy_boto3_dynamodb .service_resource import DynamoDBServiceResource
@@ -137,59 +136,44 @@ def update_item(self, table_name: str, solution_name: str, record_id: str, attri
137136 )
138137 return response
139138
140- def find_item (
141- self , table_name : str , solution_name : str , additional_attributes : Dict [str , Any ]
142- ) -> Tuple [bool , Dict [str , Any ]]:
143- """Find an item in the DynamoDB table based on the solution name and additional attributes.
139+ def find_item (self , table_name : str , solution_name : str , additional_attributes : dict ) -> tuple [bool , dict ]:
140+ """Find an item in the dynamodb table based on the solution name and additional attributes.
144141
145142 Args:
146- table_name: DynamoDB table name.
147- solution_name: Solution name to search for.
148- additional_attributes: Additional attributes to search for.
143+ table_name: dynamodb table name
144+ dynamodb_resource: dynamodb resource
145+ solution_name: solution name
146+ additional_attributes: additional attributes to search for
149147
150148 Returns:
151- True and the item if found, otherwise False and empty dict.
149+ True and the item if found, otherwise False and empty dict
152150 """
153- self .LOGGER .info (f"Searching for { additional_attributes } in { table_name } DynamoDB table" )
154-
155- # Get the DynamoDB table
151+ self .LOGGER .info (f"Searching for { additional_attributes } in { table_name } dynamodb table" )
156152 table = self .DYNAMODB_RESOURCE .Table (table_name )
153+ expression_attribute_values = {":solution_name" : solution_name }
157154
158- # Prepare query parameters
159- expression_attribute_values : Dict [str , Any ] = {":solution_name" : solution_name }
155+ filter_expression = " AND " .join ([f"{ attr } = :{ attr } " for attr in additional_attributes .keys ()])
160156
161- # Build the filter expression
162- filter_conditions : ConditionBase = Attr (list (additional_attributes .keys ())[0 ]).eq (
163- additional_attributes [list (additional_attributes .keys ())[0 ]]
164- )
165- for attr , value in list (additional_attributes .items ())[1 :]:
166- filter_conditions &= Attr (attr ).eq (value )
157+ expression_attribute_values .update ({f":{ attr } " : value for attr , value in additional_attributes .items ()})
158+
159+ query_params : Dict [str , Any ] = {}
167160
168- query_params : Dict [ str , Any ] = {
169- "KeyConditionExpression" : Key ( "solution_name" ). eq ( solution_name ) ,
161+ query_params = {
162+ "KeyConditionExpression" : "solution_name = : solution_name" ,
170163 "ExpressionAttributeValues" : expression_attribute_values ,
171- "FilterExpression" : filter_conditions ,
164+ "FilterExpression" : filter_expression ,
172165 }
173166
174- try :
175- response = table .query (** query_params )
176- except ClientError as e :
177- self .LOGGER .error (f"Error querying DynamoDB table { table_name } : { e } " )
178- return False , {}
167+ response = table .query (** query_params )
179168
180- # Handle the response
181- items = response .get ("Items" , [])
182- if len (items ) > 1 :
169+ if len (response ["Items" ]) > 1 :
183170 self .LOGGER .info (
184- f"Found more than one record that matched solution name { solution_name } : { additional_attributes } . "
185- f"Review { table_name } DynamoDB table to determine the cause."
171+ f"Found more than one record that matched solution name { solution_name } : { additional_attributes } Review { table_name } dynamodb table to determine cause."
186172 )
187- elif not items :
173+ elif len ( response [ "Items" ]) < 1 :
188174 return False , {}
189-
190- self .LOGGER .info (f"Found record id { items [0 ]} " )
191- return True , items [0 ]
192-
175+ self .LOGGER .info (f"Found record id { response ['Items' ][0 ]} " )
176+ return True , response ["Items" ][0 ]
193177
194178 def get_unique_values_from_list (self , list_of_values : list ) -> list :
195179 unique_values = []
@@ -207,45 +191,20 @@ def get_distinct_solutions_and_accounts(self, table_name: str) -> tuple[list, li
207191 accounts = self .get_unique_values_from_list (accounts )
208192 return solution_names , accounts
209193
210- def get_resources_for_solutions_by_account (
211- self , table_name : str , solutions : List [str ], account : str
212- ) -> Dict [str , Any ]:
213- """
214- Retrieve resources for the specified solutions and account from a DynamoDB table.
215-
216- Args:
217- table_name: Name of the DynamoDB table.
218- solutions: List of solutions to query.
219- account: Account to filter by.
220-
221- Returns:
222- Dictionary of solutions and their corresponding query results.
223- """
194+ def get_resources_for_solutions_by_account (self , table_name : str , solutions : list , account : str ) -> dict :
224195 table = self .DYNAMODB_RESOURCE .Table (table_name )
225- query_results : Dict [str , Any ] = {}
226-
196+ query_results = {}
227197 for solution in solutions :
228- # Build the query parameters
229- key_condition : ConditionBase = Key ("solution_name" ).eq (solution )
230- filter_condition : ConditionBase = Attr ("account" ).eq (account )
231-
232198 query_params : Dict [str , Any ] = {
233- "KeyConditionExpression" : key_condition ,
234- "ExpressionAttributeValues" : {
235- ":solution_name" : solution ,
236- ":account" : account ,
237- },
238- "FilterExpression" : filter_condition ,
199+ "KeyConditionExpression" : "solution_name = :solution_name" ,
200+ "ExpressionAttributeValues" : {":solution_name" : solution , ":account" : account },
201+ "FilterExpression" : "account = :account" ,
239202 }
240-
241- # Perform the query
242203 response = table .query (** query_params )
243204 self .LOGGER .info (f"response: { response } " )
244205 query_results [solution ] = response
245-
246206 return query_results
247207
248-
249208 def delete_item (self , table_name : str , solution_name : str , record_id : str ) -> Any :
250209 """Delete an item from the dynamodb table
251210
0 commit comments