-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathbase.py
1228 lines (1130 loc) · 50.1 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Lowest level connection
"""
import logging
import uuid
from threading import local
from typing import Any, Dict, List, Mapping, Optional, Sequence, cast
import botocore.client
import botocore.exceptions
from botocore.client import ClientError
from botocore.exceptions import BotoCoreError
from botocore.session import get_session
from pynamodb.connection._botocore_private import BotocoreBaseClientPrivate
from pynamodb._util import bin_decode_attr
from pynamodb.constants import (
RETURN_CONSUMED_CAPACITY_VALUES, RETURN_ITEM_COLL_METRICS_VALUES,
RETURN_ITEM_COLL_METRICS, RETURN_CONSUMED_CAPACITY, RETURN_VALUES_VALUES,
EXCLUSIVE_START_KEY, SCAN_INDEX_FORWARD, ATTR_DEFINITIONS,
BATCH_WRITE_ITEM, CONSISTENT_READ, DESCRIBE_TABLE, KEY_CONDITION_EXPRESSION,
BATCH_GET_ITEM, DELETE_REQUEST, SELECT_VALUES, RETURN_VALUES, REQUEST_ITEMS,
PROJECTION_EXPRESSION, SERVICE_NAME, DELETE_ITEM, PUT_REQUEST, UPDATE_ITEM, TABLE_NAME,
INDEX_NAME, KEY_SCHEMA, ATTR_NAME, ATTR_TYPE, TABLE_KEY, KEY_TYPE, GET_ITEM, UPDATE,
PUT_ITEM, SELECT, LIMIT, QUERY, SCAN, ITEM, LOCAL_SECONDARY_INDEXES,
KEYS, KEY, SEGMENT, TOTAL_SEGMENTS, CREATE_TABLE, PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS,
WRITE_CAPACITY_UNITS, GLOBAL_SECONDARY_INDEXES, PROJECTION, EXCLUSIVE_START_TABLE_NAME, TOTAL,
DELETE_TABLE, UPDATE_TABLE, LIST_TABLES, GLOBAL_SECONDARY_INDEX_UPDATES, ATTRIBUTES,
CONSUMED_CAPACITY, CAPACITY_UNITS, ATTRIBUTE_TYPES,
ITEMS, LAST_EVALUATED_KEY, RESPONSES, UNPROCESSED_KEYS,
UNPROCESSED_ITEMS, STREAM_SPECIFICATION, STREAM_VIEW_TYPE, STREAM_ENABLED,
EXPRESSION_ATTRIBUTE_NAMES, EXPRESSION_ATTRIBUTE_VALUES,
CONDITION_EXPRESSION, FILTER_EXPRESSION,
TRANSACT_WRITE_ITEMS, TRANSACT_GET_ITEMS, CLIENT_REQUEST_TOKEN, TRANSACT_ITEMS, TRANSACT_CONDITION_CHECK,
TRANSACT_GET, TRANSACT_PUT, TRANSACT_DELETE, TRANSACT_UPDATE, UPDATE_EXPRESSION,
RETURN_VALUES_ON_CONDITION_FAILURE_VALUES, RETURN_VALUES_ON_CONDITION_FAILURE,
AVAILABLE_BILLING_MODES, DEFAULT_BILLING_MODE, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE,
PROVISIONED_BILLING_MODE, TIME_TO_LIVE_SPECIFICATION, ENABLED, UPDATE_TIME_TO_LIVE, TAGS, VALUE
)
from pynamodb.exceptions import (
TableError, QueryError, PutError, DeleteError, UpdateError, GetError, ScanError, TableDoesNotExist,
VerboseClientError,
TransactGetError, TransactWriteError, CancellationReason,
)
from pynamodb.expressions.condition import Condition
from pynamodb.expressions.operand import Path
from pynamodb.expressions.projection import create_projection_expression
from pynamodb.expressions.update import Action, Update
from pynamodb.settings import get_settings_value
from pynamodb.signals import pre_dynamodb_send, post_dynamodb_send
from pynamodb.types import HASH, RANGE
BOTOCORE_EXCEPTIONS = (BotoCoreError, ClientError)
RATE_LIMITING_ERROR_CODES = ['ProvisionedThroughputExceededException', 'ThrottlingException']
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class MetaTable(object):
"""
A pythonic wrapper around table metadata
"""
def __init__(self, data: Optional[Dict]) -> None:
self.data = data or {}
self._range_keyname = None
self._hash_keyname = None
def __repr__(self) -> str:
if self.data:
return "MetaTable<{}>".format(self.data.get(TABLE_NAME))
return ""
@property
def table_name(self) -> str:
"""
Returns the table name
"""
return self.data[TABLE_NAME]
@property
def range_keyname(self) -> Optional[str]:
"""
Returns the name of this table's range key
"""
if self._range_keyname is None:
for attr in self.data.get(KEY_SCHEMA, []):
if attr.get(KEY_TYPE) == RANGE:
self._range_keyname = attr.get(ATTR_NAME)
return self._range_keyname
@property
def hash_keyname(self) -> str:
"""
Returns the name of this table's hash key
"""
if self._hash_keyname is None:
for attr in self.data.get(KEY_SCHEMA, []):
if attr.get(KEY_TYPE) == HASH:
self._hash_keyname = attr.get(ATTR_NAME)
break
if self._hash_keyname is None:
raise ValueError("No hash_key found in key schema")
return self._hash_keyname
def get_key_names(self, index_name=None):
"""
Returns the names of the primary key attributes and index key attributes (if index_name is specified)
"""
key_names = [self.hash_keyname]
if self.range_keyname:
key_names.append(self.range_keyname)
if index_name is not None:
index_hash_keyname = self.get_index_hash_keyname(index_name)
if index_hash_keyname not in key_names:
key_names.append(index_hash_keyname)
index_range_keyname = self.get_index_range_keyname(index_name)
if index_range_keyname is not None and index_range_keyname not in key_names:
key_names.append(index_range_keyname)
return key_names
def has_index_name(self, index_name):
"""
Returns True if the base table has a global or local secondary index with index_name
"""
global_indexes = self.data.get(GLOBAL_SECONDARY_INDEXES)
local_indexes = self.data.get(LOCAL_SECONDARY_INDEXES)
indexes = (global_indexes or []) + (local_indexes or [])
return any(index.get(INDEX_NAME) == index_name for index in indexes)
def get_index_hash_keyname(self, index_name: str) -> str:
"""
Returns the name of the hash key for a given index
"""
global_indexes = self.data.get(GLOBAL_SECONDARY_INDEXES)
local_indexes = self.data.get(LOCAL_SECONDARY_INDEXES)
indexes = []
if local_indexes:
indexes += local_indexes
if global_indexes:
indexes += global_indexes
for index in indexes:
if index.get(INDEX_NAME) == index_name:
for schema_key in index.get(KEY_SCHEMA):
if schema_key.get(KEY_TYPE) == HASH:
return schema_key.get(ATTR_NAME)
raise ValueError("No hash key attribute for index: {}".format(index_name))
def get_index_range_keyname(self, index_name):
"""
Returns the name of the hash key for a given index
"""
global_indexes = self.data.get(GLOBAL_SECONDARY_INDEXES)
local_indexes = self.data.get(LOCAL_SECONDARY_INDEXES)
indexes = []
if local_indexes:
indexes += local_indexes
if global_indexes:
indexes += global_indexes
for index in indexes:
if index.get(INDEX_NAME) == index_name:
for schema_key in index.get(KEY_SCHEMA):
if schema_key.get(KEY_TYPE) == RANGE:
return schema_key.get(ATTR_NAME)
return None
def get_item_attribute_map(self, attributes: Dict, item_key=ITEM, pythonic_key: bool = True):
"""
Builds up a dynamodb compatible AttributeValue map
"""
if pythonic_key:
item_key = item_key
attr_map: Dict[str, Dict] = {
item_key: {}
}
for key, value in attributes.items():
# In this case, the user provided a mapping
# {'key': {'S': 'value'}}
if isinstance(value, dict):
attr_map[item_key][key] = value
else:
attr_map[item_key][key] = {
self.get_attribute_type(key): value
}
return attr_map
def get_attribute_type(self, attribute_name: str, value: Optional[Any] = None) -> str:
"""
Returns the proper attribute type for a given attribute name
"""
for attr in self.data.get(ATTR_DEFINITIONS, []):
if attr.get(ATTR_NAME) == attribute_name:
return attr.get(ATTR_TYPE)
if value is not None and isinstance(value, dict):
for key in ATTRIBUTE_TYPES:
if key in value:
return key
attr_names = [attr.get(ATTR_NAME) for attr in self.data.get(ATTR_DEFINITIONS, [])]
raise ValueError("No attribute {} in {}".format(attribute_name, attr_names))
def get_identifier_map(self, hash_key: str, range_key: Optional[str] = None, key: str = KEY):
"""
Builds the identifier map that is common to several operations
"""
kwargs: Dict[str, Any] = {
key: {
self.hash_keyname: {
self.get_attribute_type(self.hash_keyname): hash_key
}
}
}
if range_key is not None and self.range_keyname is not None:
kwargs[key][self.range_keyname] = {
self.get_attribute_type(self.range_keyname): range_key
}
return kwargs
def get_exclusive_start_key_map(self, exclusive_start_key):
"""
Builds the exclusive start key attribute map
"""
if isinstance(exclusive_start_key, dict) and self.hash_keyname in exclusive_start_key:
# This is useful when paginating results, as the LastEvaluatedKey returned is already
# structured properly
return {
EXCLUSIVE_START_KEY: exclusive_start_key
}
else:
return {
EXCLUSIVE_START_KEY: {
self.hash_keyname: {
self.get_attribute_type(self.hash_keyname): exclusive_start_key
}
}
}
class Connection(object):
"""
A higher level abstraction over botocore
"""
def __init__(self,
region: Optional[str] = None,
host: Optional[str] = None,
read_timeout_seconds: Optional[float] = None,
connect_timeout_seconds: Optional[float] = None,
max_retry_attempts: Optional[int] = None,
max_pool_connections: Optional[int] = None,
extra_headers: Optional[Mapping[str, str]] = None,
aws_access_key_id: Optional[str] = None,
aws_secret_access_key: Optional[str] = None,
aws_session_token: Optional[str] = None):
self._tables: Dict[str, MetaTable] = {}
self.host = host
self._local = local()
self._client: Optional[BotocoreBaseClientPrivate] = None
self._convert_to_request_dict__endpoint_url = False
if region:
self.region = region
else:
self.region = get_settings_value('region')
if connect_timeout_seconds is not None:
self._connect_timeout_seconds = connect_timeout_seconds
else:
self._connect_timeout_seconds = get_settings_value('connect_timeout_seconds')
if read_timeout_seconds is not None:
self._read_timeout_seconds = read_timeout_seconds
else:
self._read_timeout_seconds = get_settings_value('read_timeout_seconds')
if max_retry_attempts is not None:
self._max_retry_attempts_exception = max_retry_attempts
else:
self._max_retry_attempts_exception = get_settings_value('max_retry_attempts')
if max_pool_connections is not None:
self._max_pool_connections = max_pool_connections
else:
self._max_pool_connections = get_settings_value('max_pool_connections')
if extra_headers is not None:
self._extra_headers = extra_headers
else:
self._extra_headers = get_settings_value('extra_headers')
self._aws_access_key_id = aws_access_key_id
self._aws_secret_access_key = aws_secret_access_key
self._aws_session_token = aws_session_token
def __repr__(self) -> str:
return "Connection<{}>".format(self.client.meta.endpoint_url)
def dispatch(self, operation_name: str, operation_kwargs: Dict) -> Dict:
"""
Dispatches `operation_name` with arguments `operation_kwargs`
Raises TableDoesNotExist if the specified table does not exist
"""
if operation_name not in [DESCRIBE_TABLE, LIST_TABLES, UPDATE_TABLE, UPDATE_TIME_TO_LIVE, DELETE_TABLE, CREATE_TABLE]:
if RETURN_CONSUMED_CAPACITY not in operation_kwargs:
operation_kwargs.update(self.get_consumed_capacity_map(TOTAL))
log.debug("Calling %s with arguments %s", operation_name, operation_kwargs)
table_name = operation_kwargs.get(TABLE_NAME)
req_uuid = uuid.uuid4()
self.send_pre_boto_callback(operation_name, req_uuid, table_name)
data = self._make_api_call(operation_name, operation_kwargs)
self.send_post_boto_callback(operation_name, req_uuid, table_name)
if data and CONSUMED_CAPACITY in data:
capacity = data.get(CONSUMED_CAPACITY)
if isinstance(capacity, dict) and CAPACITY_UNITS in capacity:
capacity = capacity.get(CAPACITY_UNITS)
log.debug("%s %s consumed %s units", data.get(TABLE_NAME, ''), operation_name, capacity)
return data
def send_post_boto_callback(self, operation_name, req_uuid, table_name):
try:
post_dynamodb_send.send(self, operation_name=operation_name, table_name=table_name, req_uuid=req_uuid)
except Exception:
log.exception("post_boto callback threw an exception.")
def send_pre_boto_callback(self, operation_name, req_uuid, table_name):
try:
pre_dynamodb_send.send(self, operation_name=operation_name, table_name=table_name, req_uuid=req_uuid)
except Exception:
log.exception("pre_boto callback threw an exception.")
def _before_send(self, request, **_) -> None:
if self._extra_headers is not None:
request.headers.update(self._extra_headers)
def _make_api_call(self, operation_name: str, operation_kwargs: Dict) -> Dict:
try:
return self.client._make_api_call(operation_name, operation_kwargs)
except ClientError as e:
resp_metadata = e.response.get('ResponseMetadata', {}).get('HTTPHeaders', {})
cancellation_reasons = e.response.get('CancellationReasons', [])
item = e.response.get('Item')
botocore_props = {'Error': e.response.get('Error', {})}
if item:
botocore_props['Item'] = item
verbose_props = {
'request_id': resp_metadata.get('x-amzn-requestid', ''),
'table_name': self._get_table_name_for_error_context(operation_kwargs),
}
raise VerboseClientError(
botocore_props,
operation_name,
verbose_props,
cancellation_reasons=(
(
CancellationReason(
code=d['Code'],
message=d.get('Message'),
raw_item=cast(Optional[Dict[str, Dict[str, Any]]], d.get('Item')),
) if d['Code'] != 'None' else None
)
for d in cancellation_reasons
),
) from e
def _get_table_name_for_error_context(self, operation_kwargs) -> str:
# First handle the two multi-table cases: batch and transaction operations
if REQUEST_ITEMS in operation_kwargs:
return ','.join(operation_kwargs[REQUEST_ITEMS])
elif TRANSACT_ITEMS in operation_kwargs:
table_names = []
for item in operation_kwargs[TRANSACT_ITEMS]:
for op in item.values():
table_names.append(op[TABLE_NAME])
return ",".join(table_names)
return operation_kwargs.get(TABLE_NAME)
@property
def session(self) -> botocore.session.Session:
"""
Returns a valid botocore session
"""
# botocore client creation is not thread safe as of v1.2.5+ (see issue #153)
if getattr(self._local, 'session', None) is None:
self._local.session = get_session()
if self._aws_access_key_id and self._aws_secret_access_key:
self._local.session.set_credentials(self._aws_access_key_id,
self._aws_secret_access_key,
self._aws_session_token)
return self._local.session
@property
def client(self) -> BotocoreBaseClientPrivate:
"""
Returns a botocore dynamodb client
"""
# botocore has a known issue where it will cache empty credentials
# https://github.com/boto/botocore/blob/4d55c9b4142/botocore/credentials.py#L1016-L1021
# if the client does not have credentials, we create a new client
# otherwise the client is permanently poisoned in the case of metadata service flakiness when using IAM roles
if not self._client or (self._client._request_signer and not self._client._request_signer._credentials):
config = botocore.client.Config(
parameter_validation=False, # Disable unnecessary validation for performance
connect_timeout=self._connect_timeout_seconds,
read_timeout=self._read_timeout_seconds,
max_pool_connections=self._max_pool_connections,
retries={
'total_max_attempts': 1 + self._max_retry_attempts_exception,
'mode': 'standard',
}
)
self._client = cast(BotocoreBaseClientPrivate, self.session.create_client(SERVICE_NAME, self.region, endpoint_url=self.host, config=config))
self._client.meta.events.register_first('before-send.*.*', self._before_send)
return self._client
def add_meta_table(self, meta_table: MetaTable) -> None:
"""
Adds information about the table's schema.
"""
if meta_table.table_name in self._tables:
raise ValueError(f"Meta-table for '{meta_table.table_name}' already added")
self._tables[meta_table.table_name] = meta_table
def get_meta_table(self, table_name: str) -> MetaTable:
"""
Returns information about the table's schema.
"""
try:
return self._tables[table_name]
except KeyError:
raise TableError(f"Meta-table for '{table_name}' not initialized") from None
def create_table(
self,
table_name: str,
attribute_definitions: Optional[Any] = None,
key_schema: Optional[Any] = None,
read_capacity_units: Optional[int] = None,
write_capacity_units: Optional[int] = None,
global_secondary_indexes: Optional[Any] = None,
local_secondary_indexes: Optional[Any] = None,
stream_specification: Optional[Dict] = None,
billing_mode: str = DEFAULT_BILLING_MODE,
tags: Optional[Dict[str, str]] = None,
) -> Dict:
"""
Performs the CreateTable operation
"""
operation_kwargs: Dict[str, Any] = {
TABLE_NAME: table_name,
BILLING_MODE: billing_mode,
PROVISIONED_THROUGHPUT: {
READ_CAPACITY_UNITS: read_capacity_units,
WRITE_CAPACITY_UNITS: write_capacity_units,
}
}
attrs_list = []
if attribute_definitions is None:
raise ValueError("attribute_definitions argument is required")
for attr in attribute_definitions:
attrs_list.append({
ATTR_NAME: attr.get(ATTR_NAME) or attr['attribute_name'],
ATTR_TYPE: attr.get(ATTR_TYPE) or attr['attribute_type']
})
operation_kwargs[ATTR_DEFINITIONS] = attrs_list
if billing_mode not in AVAILABLE_BILLING_MODES:
raise ValueError("incorrect value for billing_mode, available modes: {}".format(AVAILABLE_BILLING_MODES))
if billing_mode == PAY_PER_REQUEST_BILLING_MODE:
del operation_kwargs[PROVISIONED_THROUGHPUT]
elif billing_mode == PROVISIONED_BILLING_MODE:
del operation_kwargs[BILLING_MODE]
if global_secondary_indexes:
global_secondary_indexes_list = []
for index in global_secondary_indexes:
index_kwargs = {
INDEX_NAME: index.get('index_name'),
KEY_SCHEMA: sorted(index.get('key_schema'), key=lambda x: x.get(KEY_TYPE)),
PROJECTION: index.get('projection'),
PROVISIONED_THROUGHPUT: index.get('provisioned_throughput')
}
if billing_mode == PAY_PER_REQUEST_BILLING_MODE:
del index_kwargs[PROVISIONED_THROUGHPUT]
global_secondary_indexes_list.append(index_kwargs)
operation_kwargs[GLOBAL_SECONDARY_INDEXES] = global_secondary_indexes_list
if key_schema is None:
raise ValueError("key_schema is required")
key_schema_list = []
for item in key_schema:
key_schema_list.append({
ATTR_NAME: item.get(ATTR_NAME) or item['attribute_name'],
KEY_TYPE: str(item.get(KEY_TYPE) or item['key_type']).upper()
})
operation_kwargs[KEY_SCHEMA] = sorted(key_schema_list, key=lambda x: x.get(KEY_TYPE))
local_secondary_indexes_list = []
if local_secondary_indexes:
for index in local_secondary_indexes:
local_secondary_indexes_list.append({
INDEX_NAME: index.get('index_name'),
KEY_SCHEMA: sorted(index.get('key_schema'), key=lambda x: x.get(KEY_TYPE)),
PROJECTION: index.get('projection'),
})
operation_kwargs[LOCAL_SECONDARY_INDEXES] = local_secondary_indexes_list
if stream_specification:
operation_kwargs[STREAM_SPECIFICATION] = {
STREAM_ENABLED: stream_specification['stream_enabled'],
STREAM_VIEW_TYPE: stream_specification['stream_view_type']
}
if tags:
operation_kwargs[TAGS] = [
{
KEY: k,
VALUE: v
} for k, v in tags.items()
]
try:
data = self.dispatch(CREATE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to create table: {}".format(e), e)
return data
def update_time_to_live(self, table_name: str, ttl_attribute_name: str) -> Dict:
"""
Performs the UpdateTimeToLive operation
"""
operation_kwargs = {
TABLE_NAME: table_name,
TIME_TO_LIVE_SPECIFICATION: {
ATTR_NAME: ttl_attribute_name,
ENABLED: True,
}
}
try:
return self.dispatch(UPDATE_TIME_TO_LIVE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to update TTL on table: {}".format(e), e)
def delete_table(self, table_name: str) -> Dict:
"""
Performs the DeleteTable operation
"""
operation_kwargs = {
TABLE_NAME: table_name
}
try:
data = self.dispatch(DELETE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to delete table: {}".format(e), e)
return data
def update_table(
self,
table_name: str,
read_capacity_units: Optional[int] = None,
write_capacity_units: Optional[int] = None,
global_secondary_index_updates: Optional[Any] = None,
) -> Dict:
"""
Performs the UpdateTable operation
"""
operation_kwargs: Dict[str, Any] = {
TABLE_NAME: table_name
}
if read_capacity_units and not write_capacity_units or write_capacity_units and not read_capacity_units:
raise ValueError("read_capacity_units and write_capacity_units are required together")
if read_capacity_units and write_capacity_units:
operation_kwargs[PROVISIONED_THROUGHPUT] = {
READ_CAPACITY_UNITS: read_capacity_units,
WRITE_CAPACITY_UNITS: write_capacity_units
}
if global_secondary_index_updates:
global_secondary_indexes_list = []
for index in global_secondary_index_updates:
global_secondary_indexes_list.append({
UPDATE: {
INDEX_NAME: index.get('index_name'),
PROVISIONED_THROUGHPUT: {
READ_CAPACITY_UNITS: index.get('read_capacity_units'),
WRITE_CAPACITY_UNITS: index.get('write_capacity_units')
}
}
})
operation_kwargs[GLOBAL_SECONDARY_INDEX_UPDATES] = global_secondary_indexes_list
try:
return self.dispatch(UPDATE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to update table: {}".format(e), e)
def list_tables(
self,
exclusive_start_table_name: Optional[str] = None,
limit: Optional[int] = None,
) -> Dict:
"""
Performs the ListTables operation
"""
operation_kwargs: Dict[str, Any] = {}
if exclusive_start_table_name:
operation_kwargs.update({
EXCLUSIVE_START_TABLE_NAME: exclusive_start_table_name
})
if limit is not None:
operation_kwargs.update({
LIMIT: limit
})
try:
return self.dispatch(LIST_TABLES, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Unable to list tables: {}".format(e), e)
def describe_table(self, table_name: str) -> Dict:
"""
Performs the DescribeTable operation
"""
operation_kwargs = {
TABLE_NAME: table_name
}
try:
data = self.dispatch(DESCRIBE_TABLE, operation_kwargs)
table_data = data.get(TABLE_KEY)
# For compatibility with existing code which uses Connection directly,
# we can let DescribeTable set the meta table.
if table_data:
meta_table = MetaTable(table_data)
if meta_table.table_name not in self._tables:
self.add_meta_table(meta_table)
return table_data
except BotoCoreError as e:
raise TableError("Unable to describe table: {}".format(e), e)
except ClientError as e:
if 'ResourceNotFound' in e.response['Error']['Code']:
raise TableDoesNotExist(e.response['Error']['Message'])
else:
raise
def get_item_attribute_map(
self,
table_name: str,
attributes: Any,
item_key: str = ITEM,
pythonic_key: bool = True,
) -> Dict:
"""
Builds up a dynamodb compatible AttributeValue map
"""
tbl = self.get_meta_table(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_item_attribute_map(
attributes,
item_key=item_key,
pythonic_key=pythonic_key)
def parse_attribute(
self,
attribute: Any,
return_type: bool = False
) -> Any:
"""
Returns the attribute value, where the attribute can be
a raw attribute value, or a dictionary containing the type:
{'S': 'String value'}
"""
if isinstance(attribute, dict):
for key in ATTRIBUTE_TYPES:
if key in attribute:
if return_type:
return key, attribute.get(key)
return attribute.get(key)
raise ValueError("Invalid attribute supplied: {}".format(attribute))
else:
if return_type:
return None, attribute
return attribute
def get_attribute_type(
self,
table_name: str,
attribute_name: str,
value: Optional[Any] = None
) -> str:
"""
Returns the proper attribute type for a given attribute name
:param value: The attribute value an be supplied just in case the type is already included
"""
tbl = self.get_meta_table(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_attribute_type(attribute_name, value=value)
def get_identifier_map(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
key: str = KEY
) -> Dict:
"""
Builds the identifier map that is common to several operations
"""
tbl = self.get_meta_table(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_identifier_map(hash_key, range_key=range_key, key=key)
def get_consumed_capacity_map(self, return_consumed_capacity: str) -> Dict:
"""
Builds the consumed capacity map that is common to several operations
"""
if return_consumed_capacity.upper() not in RETURN_CONSUMED_CAPACITY_VALUES:
raise ValueError("{} must be one of {}".format(RETURN_ITEM_COLL_METRICS, RETURN_CONSUMED_CAPACITY_VALUES))
return {
RETURN_CONSUMED_CAPACITY: str(return_consumed_capacity).upper()
}
def get_return_values_map(self, return_values: str) -> Dict:
"""
Builds the return values map that is common to several operations
"""
if return_values.upper() not in RETURN_VALUES_VALUES:
raise ValueError("{} must be one of {}".format(RETURN_VALUES, RETURN_VALUES_VALUES))
return {
RETURN_VALUES: str(return_values).upper()
}
def get_return_values_on_condition_failure_map(
self,
return_values_on_condition_failure: str
) -> Dict:
"""
Builds the return values map that is common to several operations
"""
if return_values_on_condition_failure.upper() not in RETURN_VALUES_VALUES:
raise ValueError("{} must be one of {}".format(
RETURN_VALUES_ON_CONDITION_FAILURE,
RETURN_VALUES_ON_CONDITION_FAILURE_VALUES
))
return {
RETURN_VALUES_ON_CONDITION_FAILURE: str(return_values_on_condition_failure).upper()
}
def get_item_collection_map(self, return_item_collection_metrics: str) -> Dict:
"""
Builds the item collection map
"""
if return_item_collection_metrics.upper() not in RETURN_ITEM_COLL_METRICS_VALUES:
raise ValueError("{} must be one of {}".format(RETURN_ITEM_COLL_METRICS, RETURN_ITEM_COLL_METRICS_VALUES))
return {
RETURN_ITEM_COLL_METRICS: str(return_item_collection_metrics).upper()
}
def get_exclusive_start_key_map(self, table_name: str, exclusive_start_key: str) -> Dict:
"""
Builds the exclusive start key attribute map
"""
tbl = self.get_meta_table(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_exclusive_start_key_map(exclusive_start_key)
def get_operation_kwargs(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
key: str = KEY,
attributes: Optional[Any] = None,
attributes_to_get: Optional[Any] = None,
actions: Optional[Sequence[Action]] = None,
condition: Optional[Condition] = None,
consistent_read: Optional[bool] = None,
return_values: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
return_values_on_condition_failure: Optional[str] = None
) -> Dict:
self._check_condition('condition', condition)
operation_kwargs: Dict[str, Any] = {}
name_placeholders: Dict[str, str] = {}
expression_attribute_values: Dict[str, Any] = {}
operation_kwargs[TABLE_NAME] = table_name
operation_kwargs.update(self.get_identifier_map(table_name, hash_key, range_key, key=key))
if attributes and operation_kwargs.get(ITEM) is not None:
attrs = self.get_item_attribute_map(table_name, attributes)
operation_kwargs[ITEM].update(attrs[ITEM])
if attributes_to_get is not None:
projection_expression = create_projection_expression(attributes_to_get, name_placeholders)
operation_kwargs[PROJECTION_EXPRESSION] = projection_expression
if condition is not None:
condition_expression = condition.serialize(name_placeholders, expression_attribute_values)
operation_kwargs[CONDITION_EXPRESSION] = condition_expression
if consistent_read is not None:
operation_kwargs[CONSISTENT_READ] = consistent_read
if return_values is not None:
operation_kwargs.update(self.get_return_values_map(return_values))
if return_values_on_condition_failure is not None:
operation_kwargs.update(self.get_return_values_on_condition_failure_map(return_values_on_condition_failure))
if return_consumed_capacity is not None:
operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
if return_item_collection_metrics is not None:
operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
if actions is not None:
update_expression = Update(*actions)
operation_kwargs[UPDATE_EXPRESSION] = update_expression.serialize(
name_placeholders,
expression_attribute_values
)
if name_placeholders:
operation_kwargs[EXPRESSION_ATTRIBUTE_NAMES] = self._reverse_dict(name_placeholders)
if expression_attribute_values:
operation_kwargs[EXPRESSION_ATTRIBUTE_VALUES] = expression_attribute_values
return operation_kwargs
def delete_item(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
condition: Optional[Condition] = None,
return_values: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
) -> Dict:
"""
Performs the DeleteItem operation and returns the result
"""
operation_kwargs = self.get_operation_kwargs(
table_name,
hash_key,
range_key=range_key,
condition=condition,
return_values=return_values,
return_consumed_capacity=return_consumed_capacity,
return_item_collection_metrics=return_item_collection_metrics
)
try:
return self.dispatch(DELETE_ITEM, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise DeleteError("Failed to delete item: {}".format(e), e)
def update_item(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
actions: Optional[Sequence[Action]] = None,
condition: Optional[Condition] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
return_values: Optional[str] = None,
) -> Dict:
"""
Performs the UpdateItem operation
"""
if not actions:
raise ValueError("'actions' cannot be empty")
operation_kwargs = self.get_operation_kwargs(
table_name=table_name,
hash_key=hash_key,
range_key=range_key,
actions=actions,
condition=condition,
return_values=return_values,
return_consumed_capacity=return_consumed_capacity,
return_item_collection_metrics=return_item_collection_metrics,
)
try:
return self.dispatch(UPDATE_ITEM, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise UpdateError("Failed to update item: {}".format(e), e)
def put_item(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
attributes: Optional[Any] = None,
condition: Optional[Condition] = None,
return_values: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
return_values_on_condition_failure: Optional[str] = None,
) -> Dict:
"""
Performs the PutItem operation and returns the result
"""
operation_kwargs = self.get_operation_kwargs(
table_name=table_name,
hash_key=hash_key,
range_key=range_key,
key=ITEM,
attributes=attributes,
condition=condition,
return_values=return_values,
return_consumed_capacity=return_consumed_capacity,
return_item_collection_metrics=return_item_collection_metrics,
return_values_on_condition_failure=return_values_on_condition_failure
)
try:
return self.dispatch(PUT_ITEM, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
response = getattr(e, 'response', {})
raise PutError("Failed to put item: {}".format(e), e, response.get('Item'))
def _get_transact_operation_kwargs(
self,
client_request_token: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None
) -> Dict:
operation_kwargs = {}
if client_request_token is not None:
operation_kwargs[CLIENT_REQUEST_TOKEN] = client_request_token
if return_consumed_capacity is not None:
operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
if return_item_collection_metrics is not None:
operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
return operation_kwargs
def transact_write_items(
self,
condition_check_items: Sequence[Dict],
delete_items: Sequence[Dict],
put_items: Sequence[Dict],
update_items: Sequence[Dict],
client_request_token: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
) -> Dict:
"""
Performs the TransactWrite operation and returns the result
"""
transact_items: List[Dict] = []
transact_items.extend(
{TRANSACT_CONDITION_CHECK: item} for item in condition_check_items
)
transact_items.extend(
{TRANSACT_DELETE: item} for item in delete_items
)
transact_items.extend(
{TRANSACT_PUT: item} for item in put_items
)
transact_items.extend(
{TRANSACT_UPDATE: item} for item in update_items
)
operation_kwargs = self._get_transact_operation_kwargs(
client_request_token=client_request_token,
return_consumed_capacity=return_consumed_capacity,
return_item_collection_metrics=return_item_collection_metrics
)
operation_kwargs[TRANSACT_ITEMS] = transact_items
try:
return self.dispatch(TRANSACT_WRITE_ITEMS, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TransactWriteError("Failed to write transaction items", e)
def transact_get_items(
self,
get_items: Sequence[Dict],
return_consumed_capacity: Optional[str] = None,
) -> Dict:
"""
Performs the TransactGet operation and returns the result
"""
operation_kwargs = self._get_transact_operation_kwargs(return_consumed_capacity=return_consumed_capacity)
operation_kwargs[TRANSACT_ITEMS] = [
{TRANSACT_GET: item} for item in get_items
]
try:
return self.dispatch(TRANSACT_GET_ITEMS, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TransactGetError("Failed to get transaction items", e)
def batch_write_item(
self,
table_name: str,
put_items: Optional[Any] = None,
delete_items: Optional[Any] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
) -> Dict:
"""
Performs the batch_write_item operation