Glue: Add expression param to existing get_tables glue api (#5495)

This commit is contained in:
rafcio19 2022-09-28 11:44:01 +01:00 committed by GitHub
parent aa7d68a688
commit e11e4c3551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 16 deletions

View File

@ -2875,13 +2875,13 @@
## glue
<details>
<summary>17% implemented</summary>
<summary>20% implemented</summary>
- [ ] batch_create_partition
- [ ] batch_delete_connection
- [ ] batch_delete_partition
- [ ] batch_delete_table
- [ ] batch_delete_table_version
- [X] batch_create_partition
- [X] batch_delete_connection
- [X] batch_delete_partition
- [X] batch_delete_table
- [X] batch_delete_table_version
- [ ] batch_get_blueprints
- [ ] batch_get_crawlers
- [ ] batch_get_custom_entity_types

View File

@ -25,11 +25,11 @@ glue
|start-h3| Implemented features for this service |end-h3|
- [ ] batch_create_partition
- [ ] batch_delete_connection
- [ ] batch_delete_partition
- [ ] batch_delete_table
- [ ] batch_delete_table_version
- [X] batch_create_partition
- [X] batch_delete_connection
- [X] batch_delete_partition
- [X] batch_delete_table
- [X] batch_delete_table_version
- [ ] batch_get_blueprints
- [ ] batch_get_crawlers
- [ ] batch_get_custom_entity_types

View File

@ -1,6 +1,7 @@
import time
from collections import OrderedDict
from datetime import datetime
import re
from typing import List
from moto.core import BaseBackend, BaseModel
@ -136,9 +137,16 @@ class GlueBackend(BaseBackend):
except KeyError:
raise TableNotFoundException(table_name)
def get_tables(self, database_name):
def get_tables(self, database_name, expression):
database = self.get_database(database_name)
return [table for table_name, table in database.tables.items()]
if expression:
return [
table
for table_name, table in database.tables.items()
if re.match(expression, table_name)
]
else:
return [table for table_name, table in database.tables.items()]
def delete_table(self, database_name, table_name):
database = self.get_database(database_name)

View File

@ -106,7 +106,8 @@ class GlueResponse(BaseResponse):
def get_tables(self):
database_name = self.parameters.get("DatabaseName")
tables = self.glue_backend.get_tables(database_name)
expression = self.parameters.get("Expression")
tables = self.glue_backend.get_tables(database_name, expression)
return json.dumps({"TableList": [table.as_dict() for table in tables]})
def delete_table(self):

View File

@ -66,8 +66,11 @@ def get_table(client, database_name, table_name):
return client.get_table(DatabaseName=database_name, Name=table_name)
def get_tables(client, database_name):
return client.get_tables(DatabaseName=database_name)
def get_tables(client, database_name, expression=None):
if expression:
return client.get_tables(DatabaseName=database_name, Expression=expression)
else:
return client.get_tables(DatabaseName=database_name)
def get_table_versions(client, database_name, table_name):

View File

@ -227,6 +227,44 @@ def test_get_tables():
table["PartitionKeys"].should.equal(table_inputs[table_name]["PartitionKeys"])
@mock_glue
def test_get_tables_expression():
client = boto3.client("glue", region_name="us-east-1")
database_name = "myspecialdatabase"
helpers.create_database(client, database_name)
table_names = [
"mytableprefix_123",
"mytableprefix_something_test",
"something_mytablepostfix",
"test_catchthis123_test",
"asduas6781catchthisasdas",
"fakecatchthisfake",
]
table_inputs = {}
for table_name in table_names:
table_input = helpers.create_table_input(database_name, table_name)
table_inputs[table_name] = table_input
helpers.create_table(client, database_name, table_name, table_input)
prefix_expression = "mytableprefix_\\w+"
postfix_expression = "\\w+_mytablepostfix"
string_expression = "\\w+catchthis\\w+"
response_prefix = helpers.get_tables(client, database_name, prefix_expression)
response_postfix = helpers.get_tables(client, database_name, postfix_expression)
response_string_match = helpers.get_tables(client, database_name, string_expression)
tables_prefix = response_prefix["TableList"]
tables_postfix = response_postfix["TableList"]
tables_string_match = response_string_match["TableList"]
tables_prefix.should.have.length_of(2)
tables_postfix.should.have.length_of(1)
tables_string_match.should.have.length_of(3)
@mock_glue
def test_get_table_versions():
client = boto3.client("glue", region_name="us-east-1")