"""Helper for DynamoDB requests.""" import boto3 from boto3.dynamodb.conditions import Key # instantiate at module level to allow boto3 to reuse connections HASH_KEY = 'orchard-identity-id' RANGE_KEY = 'tenant' DYNAMO_CLIENT = boto3.resource("dynamodb") TABLE_NAME = 'dev-permissions' def get_table(): """Get table instance.""" return DYNAMO_CLIENT.Table(TABLE_NAME) def _query_by_hash_key(hash_key, table): """Query DynamoDB for a set of matching given hash_key.""" response = table.query( KeyConditionExpression=Key(HASH_KEY).eq(hash_key), ) return response.get("Items", []) # hash_key is a value of the key (i.e. a specific orchard_identity_id) def query_by_hash_key(hash_key): """Query DynamoDB for a set of matching given hash_key. Args: hash_key (str): Key to match Returns: An array of matching items """ table = get_table() return _query_by_hash_key( hash_key, table ) response = query_by_hash_key(HASH_KEY)