diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index 5d45a41bf..ecbbce210 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -2875,13 +2875,13 @@ ## glue
-17% implemented +20% implemented -- [ ] 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 diff --git a/docs/docs/services/glue.rst b/docs/docs/services/glue.rst index a2c7ae521..287b0f91e 100644 --- a/docs/docs/services/glue.rst +++ b/docs/docs/services/glue.rst @@ -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 diff --git a/moto/glue/models.py b/moto/glue/models.py index a2afeac2d..71158b89d 100644 --- a/moto/glue/models.py +++ b/moto/glue/models.py @@ -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) diff --git a/moto/glue/responses.py b/moto/glue/responses.py index 81bb7ca15..ae99e8ada 100644 --- a/moto/glue/responses.py +++ b/moto/glue/responses.py @@ -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): diff --git a/tests/test_glue/helpers.py b/tests/test_glue/helpers.py index a524e356d..a868f7fd0 100644 --- a/tests/test_glue/helpers.py +++ b/tests/test_glue/helpers.py @@ -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): diff --git a/tests/test_glue/test_datacatalog.py b/tests/test_glue/test_datacatalog.py index 4f32af14c..460fbfac6 100644 --- a/tests/test_glue/test_datacatalog.py +++ b/tests/test_glue/test_datacatalog.py @@ -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")