From df493ea18de22b4533073eee3e296686019911c2 Mon Sep 17 00:00:00 2001 From: Don Kuntz Date: Mon, 10 Jun 2019 14:14:30 -0500 Subject: [PATCH] Add glue.batch_delete_table, and fix glue.batch_create_partition to respond correctly (#2233) * Fix glue.batch_create_partition to only respond with Errors if Errors occurred * Add glue.batch_delete_table endpoint * Remove unused variable --- moto/glue/responses.py | 31 +++++++++++++++++++++++++++-- tests/test_glue/test_datacatalog.py | 20 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/moto/glue/responses.py b/moto/glue/responses.py index 4531b3b5..5c001cac 100644 --- a/moto/glue/responses.py +++ b/moto/glue/responses.py @@ -5,7 +5,8 @@ import json from moto.core.responses import BaseResponse from .models import glue_backend from .exceptions import ( - PartitionAlreadyExistsException + PartitionAlreadyExistsException, + TableNotFoundException ) @@ -93,6 +94,28 @@ class GlueResponse(BaseResponse): resp = self.glue_backend.delete_table(database_name, table_name) return json.dumps(resp) + def batch_delete_table(self): + database_name = self.parameters.get('DatabaseName') + + errors = [] + for table_name in self.parameters.get('TablesToDelete'): + try: + self.glue_backend.delete_table(database_name, table_name) + except TableNotFoundException: + errors.append({ + "TableName": table_name, + "ErrorDetail": { + "ErrorCode": "EntityNotFoundException", + "ErrorMessage": "Table not found" + } + }) + + out = {} + if errors: + out["Errors"] = errors + + return json.dumps(out) + def get_partitions(self): database_name = self.parameters.get('DatabaseName') table_name = self.parameters.get('TableName') @@ -145,7 +168,11 @@ class GlueResponse(BaseResponse): } }) - return json.dumps({"Errors": errors_output}) + out = {} + if errors_output: + out["Errors"] = errors_output + + return json.dumps(out) def update_partition(self): database_name = self.parameters.get('DatabaseName') diff --git a/tests/test_glue/test_datacatalog.py b/tests/test_glue/test_datacatalog.py index 237859a3..a0226656 100644 --- a/tests/test_glue/test_datacatalog.py +++ b/tests/test_glue/test_datacatalog.py @@ -229,6 +229,26 @@ def test_delete_table(): exc.exception.response['Error']['Code'].should.equal('EntityNotFoundException') exc.exception.response['Error']['Message'].should.match('Table myspecialtable not found') +@mock_glue +def test_batch_delete_table(): + client = boto3.client('glue', region_name='us-east-1') + database_name = 'myspecialdatabase' + helpers.create_database(client, database_name) + + table_name = 'myspecialtable' + table_input = helpers.create_table_input(database_name, table_name) + helpers.create_table(client, database_name, table_name, table_input) + + result = client.batch_delete_table(DatabaseName=database_name, TablesToDelete=[table_name]) + result['ResponseMetadata']['HTTPStatusCode'].should.equal(200) + + # confirm table is deleted + with assert_raises(ClientError) as exc: + helpers.get_table(client, database_name, table_name) + + exc.exception.response['Error']['Code'].should.equal('EntityNotFoundException') + exc.exception.response['Error']['Message'].should.match('Table myspecialtable not found') + @mock_glue def test_get_partitions_empty():