Skip to content

Commit f3a1a6b

Browse files
authored
Add Waiters on S3 and DynamoDB (#332)
1 parent b3ad65b commit f3a1a6b

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/DynamoDbClient.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use AsyncAws\DynamoDb\Result\PutItemOutput;
2424
use AsyncAws\DynamoDb\Result\QueryOutput;
2525
use AsyncAws\DynamoDb\Result\ScanOutput;
26+
use AsyncAws\DynamoDb\Result\TableExistsWaiter;
2627
use AsyncAws\DynamoDb\Result\UpdateItemOutput;
2728
use AsyncAws\DynamoDb\Result\UpdateTableOutput;
2829

@@ -255,6 +256,23 @@ public function scan($input): ScanOutput
255256
return new ScanOutput($response, $this, $input);
256257
}
257258

259+
/**
260+
* Check status of operation describeTable.
261+
*
262+
* @see describeTable
263+
*
264+
* @param array{
265+
* TableName: string,
266+
* }|DescribeTableInput $input
267+
*/
268+
public function tableExists($input): TableExistsWaiter
269+
{
270+
$input = DescribeTableInput::create($input);
271+
$response = $this->getResponse($input->request());
272+
273+
return new TableExistsWaiter($response, $this, $input);
274+
}
275+
258276
/**
259277
* Edits an existing item's attributes, or adds a new item to the table if it does not already exist. You can put,
260278
* 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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 TableExistsWaiter 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 (200 === $response->getStatusCode() && 'ACTIVE' === ($response->toArray()['Table']['TableStatus'] ?? null)) {
19+
return self::STATE_SUCCESS;
20+
}
21+
22+
if (null !== $exception) {
23+
return self::STATE_PENDING;
24+
}
25+
26+
/** @psalm-suppress TypeDoesNotContainType */
27+
return null === $exception ? self::STATE_PENDING : self::STATE_FAILURE;
28+
}
29+
30+
protected function refreshState(): Waiter
31+
{
32+
if (!$this->awsClient instanceof DynamoDbClient) {
33+
throw new \InvalidArgumentException('missing client injected in waiter result');
34+
}
35+
if (!$this->input instanceof DescribeTableInput) {
36+
throw new \InvalidArgumentException('missing last request injected in waiter result');
37+
}
38+
39+
return $this->awsClient->TableExists($this->input);
40+
}
41+
}

tests/Integration/DynamoDbClientTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ public function testUpdateTable(): void
277277
self::assertEquals(8, $result->getTableDescription()->getProvisionedThroughput()->getWriteCapacityUnits());
278278
}
279279

280+
public function testTableExists(): void
281+
{
282+
$client = $this->getClient();
283+
284+
$input = new DescribeTableInput([
285+
'TableName' => $this->tableName,
286+
]);
287+
288+
self::assertTrue($client->tableExists($input)->isSuccess());
289+
self::assertFalse($client->tableExists(['TableName' => 'does-not-exists'])->isSuccess());
290+
}
291+
280292
private function getClient(): DynamoDbClient
281293
{
282294
if ($this->client instanceof DynamoDbClient) {

0 commit comments

Comments
 (0)