Skip to content

Commit f2e7b08

Browse files
authored
[DynamoDB] Add waiter for table not exists after delete (#338)
* Add waiter for table delete * WIP rework waiter generator & exception condition * Response::resolve() always throw exceptions, that can be caught * Fix error waiter for sqs * Fallback to full __type error code, like official sdk * Waiter::resolve() ignores http exceptions
1 parent f3a1a6b commit f2e7b08

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/DynamoDbClient.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use AsyncAws\DynamoDb\Result\QueryOutput;
2525
use AsyncAws\DynamoDb\Result\ScanOutput;
2626
use AsyncAws\DynamoDb\Result\TableExistsWaiter;
27+
use AsyncAws\DynamoDb\Result\TableNotExistsWaiter;
2728
use AsyncAws\DynamoDb\Result\UpdateItemOutput;
2829
use AsyncAws\DynamoDb\Result\UpdateTableOutput;
2930

@@ -273,6 +274,23 @@ public function tableExists($input): TableExistsWaiter
273274
return new TableExistsWaiter($response, $this, $input);
274275
}
275276

277+
/**
278+
* Check status of operation describeTable.
279+
*
280+
* @see describeTable
281+
*
282+
* @param array{
283+
* TableName: string,
284+
* }|DescribeTableInput $input
285+
*/
286+
public function tableNotExists($input): TableNotExistsWaiter
287+
{
288+
$input = DescribeTableInput::create($input);
289+
$response = $this->getResponse($input->request());
290+
291+
return new TableNotExistsWaiter($response, $this, $input);
292+
}
293+
276294
/**
277295
* Edits an existing item's attributes, or adds a new item to the table if it does not already exist. You can put,
278296
* delete, or add attribute values. You can also perform a conditional update on an existing item (insert a new

src/Result/TableExistsWaiter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function extractState(Response $response, ?HttpException $exception):
1919
return self::STATE_SUCCESS;
2020
}
2121

22-
if (null !== $exception) {
22+
if (null !== $exception && 'ResourceNotFoundException' === $exception->getAwsCode()) {
2323
return self::STATE_PENDING;
2424
}
2525

src/Result/TableNotExistsWaiter.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace AsyncAws\DynamoDb\Result;
4+
5+
use AsyncAws\Core\Exception\Http\HttpException;
6+
use AsyncAws\Core\Response;
7+
use AsyncAws\Core\Waiter;
8+
use AsyncAws\DynamoDb\DynamoDbClient;
9+
use AsyncAws\DynamoDb\Input\DescribeTableInput;
10+
11+
class TableNotExistsWaiter extends Waiter
12+
{
13+
protected const WAIT_TIMEOUT = 500.0;
14+
protected const WAIT_DELAY = 20.0;
15+
16+
protected function extractState(Response $response, ?HttpException $exception): string
17+
{
18+
if (null !== $exception && 'ResourceNotFoundException' === $exception->getAwsCode()) {
19+
return self::STATE_SUCCESS;
20+
}
21+
22+
/** @psalm-suppress TypeDoesNotContainType */
23+
return null === $exception ? self::STATE_PENDING : self::STATE_FAILURE;
24+
}
25+
26+
protected function refreshState(): Waiter
27+
{
28+
if (!$this->awsClient instanceof DynamoDbClient) {
29+
throw new \InvalidArgumentException('missing client injected in waiter result');
30+
}
31+
if (!$this->input instanceof DescribeTableInput) {
32+
throw new \InvalidArgumentException('missing last request injected in waiter result');
33+
}
34+
35+
return $this->awsClient->TableNotExists($this->input);
36+
}
37+
}

tests/Unit/DynamoDbClientTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use AsyncAws\DynamoDb\Result\PutItemOutput;
2727
use AsyncAws\DynamoDb\Result\QueryOutput;
2828
use AsyncAws\DynamoDb\Result\ScanOutput;
29+
use AsyncAws\DynamoDb\Result\TableExistsWaiter;
30+
use AsyncAws\DynamoDb\Result\TableNotExistsWaiter;
2931
use AsyncAws\DynamoDb\Result\UpdateItemOutput;
3032
use AsyncAws\DynamoDb\Result\UpdateTableOutput;
3133
use AsyncAws\DynamoDb\ValueObject\KeySchemaElement;
@@ -160,6 +162,34 @@ public function testScan(): void
160162
self::assertFalse($result->info()['resolved']);
161163
}
162164

165+
public function testTableExists(): void
166+
{
167+
$client = new DynamoDbClient([], new NullProvider(), new MockHttpClient());
168+
169+
$input = new DescribeTableInput([
170+
'TableName' => 'Foobar',
171+
172+
]);
173+
$result = $client->tableExists($input);
174+
175+
self::assertInstanceOf(TableExistsWaiter::class, $result);
176+
self::assertFalse($result->info()['resolved']);
177+
}
178+
179+
public function testTableNotExists(): void
180+
{
181+
$client = new DynamoDbClient([], new NullProvider(), new MockHttpClient());
182+
183+
$input = new DescribeTableInput([
184+
'TableName' => 'Foobar',
185+
186+
]);
187+
$result = $client->tableNotExists($input);
188+
189+
self::assertInstanceOf(TableNotExistsWaiter::class, $result);
190+
self::assertFalse($result->info()['resolved']);
191+
}
192+
163193
public function testUpdateItem(): void
164194
{
165195
$client = new DynamoDbClient([], new NullProvider(), new MockHttpClient());

0 commit comments

Comments
 (0)