1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guillermoandrae\DynamoDb\Operation; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Exception\DynamoDbException; |
7
|
|
|
use Aws\DynamoDb\Marshaler; |
8
|
|
|
use Guillermoandrae\DynamoDb\Constant\BillingModes; |
9
|
|
|
use Guillermoandrae\DynamoDb\Constant\KeyTypes; |
10
|
|
|
use Guillermoandrae\DynamoDb\Contract\AbstractTableAwareOperation; |
11
|
|
|
use Guillermoandrae\DynamoDb\Exception; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#createtable |
16
|
|
|
*/ |
17
|
|
|
final class CreateTableOperation extends AbstractTableAwareOperation |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array Attributes describing the key schema. |
21
|
|
|
*/ |
22
|
|
|
private $attributeDefinitions = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array The primary key. |
26
|
|
|
*/ |
27
|
|
|
private $keySchema = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int The maximum number of strongly consistent reads consumed per second. |
31
|
|
|
*/ |
32
|
|
|
private $readCapacityUnits = 5; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int The maximum number of writes consumed per second. |
36
|
|
|
*/ |
37
|
|
|
private $writeCapacityUnits = 5; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string The billing mode. |
41
|
|
|
*/ |
42
|
|
|
private $billingMode = BillingModes::PROVISIONED; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array The server-side encryption settings. |
46
|
|
|
*/ |
47
|
|
|
private $sseSpecification = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array The tags. |
51
|
|
|
*/ |
52
|
|
|
private $tags = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* CreateTableRequest constructor. |
56
|
|
|
* |
57
|
|
|
* @param DynamoDbClient $client The DynamoDb client. |
58
|
|
|
* @param Marshaler $marshaler The Marshaler. |
59
|
|
|
* @param string $tableName The table name. |
60
|
|
|
* @param array $keySchema OPTIONAL The key schema. |
61
|
|
|
*/ |
62
|
16 |
|
public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName, array $keySchema = []) |
63
|
|
|
{ |
64
|
16 |
|
parent::__construct($client, $marshaler, $tableName); |
65
|
16 |
|
if (!empty($keySchema)) { |
66
|
13 |
|
$this->setKeySchema($keySchema); |
67
|
|
|
} |
68
|
16 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Registers the partition key. |
72
|
|
|
* |
73
|
|
|
* @param string $name The name of the partition key. |
74
|
|
|
* @param string $attributeType The attribute type. |
75
|
|
|
* @return CreateTableOperation This object. |
76
|
|
|
*/ |
77
|
1 |
|
public function setPartitionKey(string $name, string $attributeType): CreateTableOperation |
78
|
|
|
{ |
79
|
1 |
|
$this->setKeySchema([ |
80
|
1 |
|
$name => [ |
81
|
1 |
|
'attributeType' => $attributeType, |
82
|
|
|
'keyType' => KeyTypes::HASH |
83
|
|
|
] |
84
|
|
|
]); |
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Registers the sort key. |
90
|
|
|
* |
91
|
|
|
* @param string $name The name of the sort key. |
92
|
|
|
* @param string $attributeType The attribute type. |
93
|
|
|
* @return CreateTableOperation This object. |
94
|
|
|
*/ |
95
|
1 |
|
public function setSortKey(string $name, string $attributeType): CreateTableOperation |
96
|
|
|
{ |
97
|
1 |
|
$this->setKeySchema([ |
98
|
1 |
|
$name => [ |
99
|
1 |
|
'attributeType' => $attributeType, |
100
|
|
|
'keyType' => KeyTypes::RANGE |
101
|
|
|
] |
102
|
|
|
]); |
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Registers the key schema and attribute definitions. |
108
|
|
|
* |
109
|
|
|
* The key schema argument should be an associative array with the following keys: |
110
|
|
|
* |
111
|
|
|
* $keySchema = [ |
112
|
|
|
* 'MyAttribute' => [ // this is the name of your attribute |
113
|
|
|
* 'attributeType' => 'S', // this can be one of the AttributeTypes constants |
114
|
|
|
* 'keyType' => 'HASH' // this can be either 'HASH' or 'RANGE' (or one of the KeyTypes constants) |
115
|
|
|
* ] |
116
|
|
|
* ]; |
117
|
|
|
* |
118
|
|
|
* This method will use the information available in the provided array to build the 'KeySchema' and |
119
|
|
|
* 'AttributeDefinitions' arrays needed for table creation requests. |
120
|
|
|
* |
121
|
|
|
* @param array $keySchema The key schema. |
122
|
|
|
* @return CreateTableOperation This object. |
123
|
|
|
*/ |
124
|
15 |
|
public function setKeySchema(array $keySchema): CreateTableOperation |
125
|
|
|
{ |
126
|
15 |
|
foreach ($keySchema as $name => $data) { |
127
|
15 |
|
$this->keySchema[] = [ |
128
|
15 |
|
'AttributeName' => $name, |
129
|
15 |
|
'KeyType' => $data['keyType'] |
130
|
|
|
]; |
131
|
15 |
|
$this->attributeDefinitions[] = [ |
132
|
15 |
|
'AttributeName' => $name, |
133
|
15 |
|
'AttributeType' => $data['attributeType'] |
134
|
|
|
]; |
135
|
|
|
} |
136
|
15 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Registers the maximum number of strongly consistent reads consumed per second. |
141
|
|
|
* |
142
|
|
|
* @param integer $readCapacityUnits The maximum number of strongly consistent reads consumed per second. |
143
|
|
|
* @return CreateTableOperation This object. |
144
|
|
|
*/ |
145
|
1 |
|
public function setReadCapacityUnits(int $readCapacityUnits): CreateTableOperation |
146
|
|
|
{ |
147
|
1 |
|
$this->readCapacityUnits = $readCapacityUnits; |
148
|
1 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Registers the maximum number of writes consumed per second. |
153
|
|
|
* |
154
|
|
|
* @param integer $writeCapacityUnits The maximum number of writes consumed per second. |
155
|
|
|
* @return CreateTableOperation This object. |
156
|
|
|
*/ |
157
|
1 |
|
public function setWriteCapacityUnits(int $writeCapacityUnits): CreateTableOperation |
158
|
|
|
{ |
159
|
1 |
|
$this->writeCapacityUnits = $writeCapacityUnits; |
160
|
1 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Registers the billing mode. |
165
|
|
|
* |
166
|
|
|
* @param string $billingMode The billing mode. |
167
|
|
|
* @return CreateTableOperation This object. |
168
|
|
|
*/ |
169
|
1 |
|
public function setBillingMode(string $billingMode) |
170
|
|
|
{ |
171
|
1 |
|
$this->billingMode = $billingMode; |
172
|
1 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Registers the server-side encryption settings. |
177
|
|
|
* |
178
|
|
|
* @param bool $isEnabled Whether or not SSE is enabled. |
179
|
|
|
* @param string $masterKeyId OPTIONAL The ID of the master key. |
180
|
|
|
* @return CreateTableOperation This object. |
181
|
|
|
*/ |
182
|
1 |
|
public function setSSESpecification(bool $isEnabled, string $masterKeyId = '') |
183
|
|
|
{ |
184
|
1 |
|
$sseSpecification = []; |
185
|
1 |
|
if ($isEnabled) { |
186
|
|
|
$sseSpecification = [ |
187
|
1 |
|
'Enabled' => $isEnabled, |
188
|
1 |
|
'SSEType' => 'KMS' |
189
|
|
|
]; |
190
|
1 |
|
if (!empty($masterKeyId)) { |
191
|
1 |
|
$sseSpecification['KMSMasterKeyId'] = $masterKeyId; |
192
|
|
|
} |
193
|
|
|
} |
194
|
1 |
|
$this->sseSpecification = $sseSpecification; |
195
|
1 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Registers a tag. |
200
|
|
|
* |
201
|
|
|
* @param string $key The tag key. |
202
|
|
|
* @param string $value The tag value. |
203
|
|
|
* @return CreateTableOperation This object. |
204
|
|
|
*/ |
205
|
1 |
|
public function addTag(string $key, string $value) |
206
|
|
|
{ |
207
|
1 |
|
$this->tags[] = [ |
208
|
1 |
|
'Key' => $key, |
209
|
1 |
|
'Value' => $value |
210
|
|
|
]; |
211
|
1 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritDoc} |
216
|
|
|
*/ |
217
|
16 |
|
public function toArray(): array |
218
|
|
|
{ |
219
|
16 |
|
$query = parent::toArray(); |
220
|
16 |
|
$query['KeySchema'] = $this->keySchema; |
221
|
16 |
|
$query['AttributeDefinitions'] = $this->attributeDefinitions; |
222
|
16 |
|
$query['BillingMode'] = $this->billingMode; |
223
|
16 |
|
$query['ProvisionedThroughput'] = [ |
224
|
16 |
|
'ReadCapacityUnits' => $this->readCapacityUnits, |
225
|
16 |
|
'WriteCapacityUnits' => $this->writeCapacityUnits, |
226
|
|
|
]; |
227
|
16 |
|
if (!empty($this->sseSpecification)) { |
228
|
1 |
|
$query['SSESpecification'] = $this->sseSpecification; |
229
|
|
|
} |
230
|
16 |
|
if (!empty($this->tags)) { |
231
|
1 |
|
$query['Tags'] = $this->tags; |
232
|
|
|
} |
233
|
16 |
|
return $query; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritDoc} |
238
|
|
|
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#createtable |
239
|
|
|
*/ |
240
|
9 |
|
public function execute(): bool |
241
|
|
|
{ |
242
|
|
|
try { |
243
|
9 |
|
$this->client->createTable($this->toArray()); |
244
|
|
|
//$this->client->waitUntil('TableExists', ['TableName' => $this->toArray()['TableName']]); |
245
|
7 |
|
return true; |
246
|
2 |
|
} catch (DynamoDbException $ex) { |
247
|
1 |
|
throw new Exception($ex->getMessage()); |
248
|
1 |
|
} catch (InvalidArgumentException $ex) { |
249
|
1 |
|
throw new Exception('Bad key schema: ' . $ex->getMessage()); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|