Skip to content

Commit 4fa15e0

Browse files
Add operation UpdateTimeToLiveInput (#618)
* Add operation UpdateTimeToLiveInput * Apply suggestions from code review Co-authored-by: guillaumesmo <[email protected]> Co-authored-by: guillaumesmo <[email protected]>
1 parent f84aca7 commit 4fa15e0

File tree

9 files changed

+347
-0
lines changed

9 files changed

+347
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## NOT RELEASED
4+
5+
### Added
6+
7+
- Added operation `UpdateTimeToLiveInput`
8+
39
## 0.2.1
410

511
### Fixed

src/DynamoDbClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use AsyncAws\DynamoDb\Input\ScanInput;
1818
use AsyncAws\DynamoDb\Input\UpdateItemInput;
1919
use AsyncAws\DynamoDb\Input\UpdateTableInput;
20+
use AsyncAws\DynamoDb\Input\UpdateTimeToLiveInput;
2021
use AsyncAws\DynamoDb\Result\CreateTableOutput;
2122
use AsyncAws\DynamoDb\Result\DeleteItemOutput;
2223
use AsyncAws\DynamoDb\Result\DeleteTableOutput;
@@ -30,6 +31,7 @@
3031
use AsyncAws\DynamoDb\Result\TableNotExistsWaiter;
3132
use AsyncAws\DynamoDb\Result\UpdateItemOutput;
3233
use AsyncAws\DynamoDb\Result\UpdateTableOutput;
34+
use AsyncAws\DynamoDb\Result\UpdateTimeToLiveOutput;
3335
use AsyncAws\DynamoDb\ValueObject\AttributeValue;
3436

3537
class DynamoDbClient extends AbstractApi
@@ -376,6 +378,28 @@ public function updateTable($input): UpdateTableOutput
376378
return new UpdateTableOutput($response);
377379
}
378380

381+
/**
382+
* The `UpdateTimeToLive` method enables or disables Time to Live (TTL) for the specified table. A successful
383+
* `UpdateTimeToLive` call returns the current `TimeToLiveSpecification`. It can take up to one hour for the change to
384+
* fully process. Any additional `UpdateTimeToLive` calls for the same table during this one hour duration result in a
385+
* `ValidationException`.
386+
*
387+
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#updatetimetolive
388+
*
389+
* @param array{
390+
* TableName: string,
391+
* TimeToLiveSpecification: \AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification|array,
392+
* @region?: string,
393+
* }|UpdateTimeToLiveInput $input
394+
*/
395+
public function updateTimeToLive($input): UpdateTimeToLiveOutput
396+
{
397+
$input = UpdateTimeToLiveInput::create($input);
398+
$response = $this->getResponse($input->request(), new RequestContext(['operation' => 'UpdateTimeToLive', 'region' => $input->getRegion()]));
399+
400+
return new UpdateTimeToLiveOutput($response);
401+
}
402+
379403
protected function getEndpointMetadata(?string $region): array
380404
{
381405
if (null === $region) {

src/Input/UpdateTimeToLiveInput.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace AsyncAws\DynamoDb\Input;
4+
5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
use AsyncAws\Core\Input;
7+
use AsyncAws\Core\Request;
8+
use AsyncAws\Core\Stream\StreamFactory;
9+
use AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification;
10+
11+
final class UpdateTimeToLiveInput extends Input
12+
{
13+
/**
14+
* The name of the table to be configured.
15+
*
16+
* @required
17+
*
18+
* @var string|null
19+
*/
20+
private $TableName;
21+
22+
/**
23+
* Represents the settings used to enable or disable Time to Live for the specified table.
24+
*
25+
* @required
26+
*
27+
* @var TimeToLiveSpecification|null
28+
*/
29+
private $TimeToLiveSpecification;
30+
31+
/**
32+
* @param array{
33+
* TableName?: string,
34+
* TimeToLiveSpecification?: \AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification|array,
35+
* @region?: string,
36+
* } $input
37+
*/
38+
public function __construct(array $input = [])
39+
{
40+
$this->TableName = $input['TableName'] ?? null;
41+
$this->TimeToLiveSpecification = isset($input['TimeToLiveSpecification']) ? TimeToLiveSpecification::create($input['TimeToLiveSpecification']) : null;
42+
parent::__construct($input);
43+
}
44+
45+
public static function create($input): self
46+
{
47+
return $input instanceof self ? $input : new self($input);
48+
}
49+
50+
public function getTableName(): ?string
51+
{
52+
return $this->TableName;
53+
}
54+
55+
public function getTimeToLiveSpecification(): ?TimeToLiveSpecification
56+
{
57+
return $this->TimeToLiveSpecification;
58+
}
59+
60+
/**
61+
* @internal
62+
*/
63+
public function request(): Request
64+
{
65+
// Prepare headers
66+
$headers = [
67+
'Content-Type' => 'application/x-amz-json-1.0',
68+
'X-Amz-Target' => 'DynamoDB_20120810.UpdateTimeToLive',
69+
];
70+
71+
// Prepare query
72+
$query = [];
73+
74+
// Prepare URI
75+
$uriString = '/';
76+
77+
// Prepare Body
78+
$bodyPayload = $this->requestBody();
79+
$body = empty($bodyPayload) ? '{}' : json_encode($bodyPayload);
80+
81+
// Return the Request
82+
return new Request('POST', $uriString, $query, $headers, StreamFactory::create($body));
83+
}
84+
85+
public function setTableName(?string $value): self
86+
{
87+
$this->TableName = $value;
88+
89+
return $this;
90+
}
91+
92+
public function setTimeToLiveSpecification(?TimeToLiveSpecification $value): self
93+
{
94+
$this->TimeToLiveSpecification = $value;
95+
96+
return $this;
97+
}
98+
99+
private function requestBody(): array
100+
{
101+
$payload = [];
102+
if (null === $v = $this->TableName) {
103+
throw new InvalidArgument(sprintf('Missing parameter "TableName" for "%s". The value cannot be null.', __CLASS__));
104+
}
105+
$payload['TableName'] = $v;
106+
if (null === $v = $this->TimeToLiveSpecification) {
107+
throw new InvalidArgument(sprintf('Missing parameter "TimeToLiveSpecification" for "%s". The value cannot be null.', __CLASS__));
108+
}
109+
$payload['TimeToLiveSpecification'] = $v->requestBody();
110+
111+
return $payload;
112+
}
113+
}

src/Result/UpdateTimeToLiveOutput.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace AsyncAws\DynamoDb\Result;
4+
5+
use AsyncAws\Core\Response;
6+
use AsyncAws\Core\Result;
7+
use AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification;
8+
9+
class UpdateTimeToLiveOutput extends Result
10+
{
11+
/**
12+
* Represents the output of an `UpdateTimeToLive` operation.
13+
*/
14+
private $TimeToLiveSpecification;
15+
16+
public function getTimeToLiveSpecification(): ?TimeToLiveSpecification
17+
{
18+
$this->initialize();
19+
20+
return $this->TimeToLiveSpecification;
21+
}
22+
23+
protected function populateResult(Response $response): void
24+
{
25+
$data = $response->toArray();
26+
27+
$this->TimeToLiveSpecification = empty($data['TimeToLiveSpecification']) ? null : new TimeToLiveSpecification([
28+
'Enabled' => filter_var($data['TimeToLiveSpecification']['Enabled'], \FILTER_VALIDATE_BOOLEAN),
29+
'AttributeName' => (string) $data['TimeToLiveSpecification']['AttributeName'],
30+
]);
31+
}
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace AsyncAws\DynamoDb\ValueObject;
4+
5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
7+
final class TimeToLiveSpecification
8+
{
9+
/**
10+
* Indicates whether TTL is to be enabled (true) or disabled (false) on the table.
11+
*/
12+
private $Enabled;
13+
14+
/**
15+
* The name of the TTL attribute used to store the expiration time for items in the table.
16+
*/
17+
private $AttributeName;
18+
19+
/**
20+
* @param array{
21+
* Enabled: bool,
22+
* AttributeName: string,
23+
* } $input
24+
*/
25+
public function __construct(array $input)
26+
{
27+
$this->Enabled = $input['Enabled'] ?? null;
28+
$this->AttributeName = $input['AttributeName'] ?? null;
29+
}
30+
31+
public static function create($input): self
32+
{
33+
return $input instanceof self ? $input : new self($input);
34+
}
35+
36+
public function getAttributeName(): string
37+
{
38+
return $this->AttributeName;
39+
}
40+
41+
public function getEnabled(): bool
42+
{
43+
return $this->Enabled;
44+
}
45+
46+
/**
47+
* @internal
48+
*/
49+
public function requestBody(): array
50+
{
51+
$payload = [];
52+
if (null === $v = $this->Enabled) {
53+
throw new InvalidArgument(sprintf('Missing parameter "Enabled" for "%s". The value cannot be null.', __CLASS__));
54+
}
55+
$payload['Enabled'] = (bool) $v;
56+
if (null === $v = $this->AttributeName) {
57+
throw new InvalidArgument(sprintf('Missing parameter "AttributeName" for "%s". The value cannot be null.', __CLASS__));
58+
}
59+
$payload['AttributeName'] = $v;
60+
61+
return $payload;
62+
}
63+
}

tests/Integration/DynamoDbClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
use AsyncAws\DynamoDb\Input\ScanInput;
1818
use AsyncAws\DynamoDb\Input\UpdateItemInput;
1919
use AsyncAws\DynamoDb\Input\UpdateTableInput;
20+
use AsyncAws\DynamoDb\Input\UpdateTimeToLiveInput;
2021
use AsyncAws\DynamoDb\ValueObject\AttributeDefinition;
2122
use AsyncAws\DynamoDb\ValueObject\KeySchemaElement;
2223
use AsyncAws\DynamoDb\ValueObject\LocalSecondaryIndex;
2324
use AsyncAws\DynamoDb\ValueObject\Projection;
2425
use AsyncAws\DynamoDb\ValueObject\ProvisionedThroughput;
2526
use AsyncAws\DynamoDb\ValueObject\Tag;
27+
use AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification;
2628

2729
class DynamoDbClientTest extends TestCase
2830
{
@@ -301,6 +303,25 @@ public function testTableNotExists(): void
301303
self::assertTrue($client->tableNotExists(['TableName' => 'does-not-exists'])->isSuccess());
302304
}
303305

306+
public function testUpdateTimeToLive(): void
307+
{
308+
$client = $this->getClient();
309+
310+
$input = new UpdateTimeToLiveInput([
311+
'TableName' => $this->tableName,
312+
'TimeToLiveSpecification' => new TimeToLiveSpecification([
313+
'Enabled' => true,
314+
'AttributeName' => 'attribute',
315+
]),
316+
]);
317+
$result = $client->updateTimeToLive($input);
318+
319+
$result->resolve();
320+
321+
self::assertTrue($result->getTimeToLiveSpecification()->getEnabled());
322+
self::assertSame('attribute', $result->getTimeToLiveSpecification()->getAttributeName());
323+
}
324+
304325
private function getClient(): DynamoDbClient
305326
{
306327
if ($this->client instanceof DynamoDbClient) {

tests/Unit/DynamoDbClientTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use AsyncAws\DynamoDb\Input\ScanInput;
1818
use AsyncAws\DynamoDb\Input\UpdateItemInput;
1919
use AsyncAws\DynamoDb\Input\UpdateTableInput;
20+
use AsyncAws\DynamoDb\Input\UpdateTimeToLiveInput;
2021
use AsyncAws\DynamoDb\Result\CreateTableOutput;
2122
use AsyncAws\DynamoDb\Result\DeleteItemOutput;
2223
use AsyncAws\DynamoDb\Result\DeleteTableOutput;
@@ -30,7 +31,9 @@
3031
use AsyncAws\DynamoDb\Result\TableNotExistsWaiter;
3132
use AsyncAws\DynamoDb\Result\UpdateItemOutput;
3233
use AsyncAws\DynamoDb\Result\UpdateTableOutput;
34+
use AsyncAws\DynamoDb\Result\UpdateTimeToLiveOutput;
3335
use AsyncAws\DynamoDb\ValueObject\KeySchemaElement;
36+
use AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification;
3437
use Symfony\Component\HttpClient\MockHttpClient;
3538

3639
class DynamoDbClientTest extends TestCase
@@ -216,4 +219,21 @@ public function testUpdateTable(): void
216219
self::assertInstanceOf(UpdateTableOutput::class, $result);
217220
self::assertFalse($result->info()['resolved']);
218221
}
222+
223+
public function testUpdateTimeToLive(): void
224+
{
225+
$client = new DynamoDbClient([], new NullProvider(), new MockHttpClient());
226+
227+
$input = new UpdateTimeToLiveInput([
228+
'TableName' => 'Foobar',
229+
'TimeToLiveSpecification' => new TimeToLiveSpecification([
230+
'Enabled' => false,
231+
'AttributeName' => 'attribute',
232+
]),
233+
]);
234+
$result = $client->updateTimeToLive($input);
235+
236+
self::assertInstanceOf(UpdateTimeToLiveOutput::class, $result);
237+
self::assertFalse($result->info()['resolved']);
238+
}
219239
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace AsyncAws\DynamoDb\Tests\Unit\Input;
4+
5+
use AsyncAws\Core\Test\TestCase;
6+
use AsyncAws\DynamoDb\Input\UpdateTimeToLiveInput;
7+
use AsyncAws\DynamoDb\ValueObject\TimeToLiveSpecification;
8+
9+
class UpdateTimeToLiveInputTest extends TestCase
10+
{
11+
public function testRequest(): void
12+
{
13+
$input = new UpdateTimeToLiveInput([
14+
'TableName' => 'table name',
15+
'TimeToLiveSpecification' => new TimeToLiveSpecification([
16+
'Enabled' => false,
17+
'AttributeName' => 'attribute',
18+
]),
19+
]);
20+
21+
// see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTimeToLive.html
22+
$expected = '
23+
POST / HTTP/1.0
24+
Content-Type: application/x-amz-json-1.0
25+
X-Amz-Target: DynamoDB_20120810.UpdateTimeToLive
26+
27+
{
28+
"TableName": "table name",
29+
"TimeToLiveSpecification": {
30+
"AttributeName": "attribute",
31+
"Enabled": false
32+
}
33+
}
34+
';
35+
36+
self::assertRequestEqualsHttpRequest($expected, $input->request());
37+
}
38+
}

0 commit comments

Comments
 (0)