From e39a6cc032bc809a9671ad378fab18dbbcad1a72 Mon Sep 17 00:00:00 2001 From: Christopher Lacasse Date: Thu, 20 Jul 2023 10:46:15 -0400 Subject: [PATCH] Implement delete table --- handler/deletetable.go | 21 ++++++++++++++++++++- itest/integration_test.go | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/handler/deletetable.go b/handler/deletetable.go index 101804c..eb092bb 100644 --- a/handler/deletetable.go +++ b/handler/deletetable.go @@ -2,10 +2,29 @@ package handler import ( "context" + "encoding/json" + "fmt" "github.com/aws/aws-sdk-go/service/dynamodb" ) func DeleteTable(ctx context.Context, s *state, input *dynamodb.DeleteTableInput) (*dynamodb.DeleteTableOutput, error) { - return &dynamodb.DeleteTableOutput{}, nil + key := fmt.Sprintf("$table:%s", *input.TableName) + jsonValue, err := s.kv.Get(ctx, []byte(key)) + if err != nil { + return nil, err + } + + var td dynamodb.TableDescription + if err = json.Unmarshal(jsonValue, &td); err != nil { + return nil, err + } + + if err = s.kv.Delete(ctx, []byte(key)); err != nil { + return nil, err + } + + return &dynamodb.DeleteTableOutput{ + TableDescription: &td, + }, nil } diff --git a/itest/integration_test.go b/itest/integration_test.go index 4a6eb10..aa62192 100644 --- a/itest/integration_test.go +++ b/itest/integration_test.go @@ -70,22 +70,42 @@ func TestInvalidCreateTable(t *testing.T) { require.Error(t, err) } -func TestListTables(t *testing.T) { +func TestDeleteTable(t *testing.T) { ddbSvc := getDDBService(t) - _, err := ddbSvc.ListTables(&dynamodb.ListTablesInput{ - Limit: aws.Int64(5), + testTableName := "TestTable" + _, err := ddbSvc.CreateTable(&dynamodb.CreateTableInput{ + TableName: aws.String(testTableName), + AttributeDefinitions: []*dynamodb.AttributeDefinition{ + { + AttributeName: aws.String("id"), + AttributeType: aws.String(dynamodb.ScalarAttributeTypeS), + }, + { + AttributeName: aws.String("value"), + AttributeType: aws.String(dynamodb.ScalarAttributeTypeS), + }, + }, + KeySchema: []*dynamodb.KeySchemaElement{ + { + AttributeName: aws.String("id"), + KeyType: aws.String(dynamodb.KeyTypeHash), + }, + }, }) require.NoError(t, err) -} - -func TestDeleteTable(t *testing.T) { - ddbSvc := getDDBService(t) - _, err := ddbSvc.DeleteTable(&dynamodb.DeleteTableInput{ - TableName: aws.String("TestTable"), + response, err := ddbSvc.DeleteTable(&dynamodb.DeleteTableInput{ + TableName: aws.String(testTableName), }) require.NoError(t, err) + require.NotNil(t, response.TableDescription) + require.Equal(t, *response.TableDescription.TableName, testTableName) + + _, err = ddbSvc.DescribeTable(&dynamodb.DescribeTableInput{ + TableName: aws.String(testTableName), + }) + require.Error(t, err) } func TestPutItem(t *testing.T) {