-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
cosmos_client.py
485 lines (430 loc) · 23.7 KB
/
cosmos_client.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
# The MIT License (MIT)
# Copyright (c) 2014 Microsoft Corporation
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Create, read, and delete databases in the Azure Cosmos DB SQL API service.
"""
from typing import Any, Dict, Optional, Union, cast, Iterable, List # pylint: disable=unused-import
import warnings
from azure.core.tracing.decorator import distributed_trace # type: ignore
from ._cosmos_client_connection import CosmosClientConnection
from ._base import build_options, _set_throughput_options
from .offer import ThroughputProperties
from ._retry_utility import ConnectionRetryPolicy
from .database import DatabaseProxy
from .documents import ConnectionPolicy, DatabaseAccount
from .exceptions import CosmosResourceNotFoundError
__all__ = ("CosmosClient",)
def _parse_connection_str(conn_str, credential):
# type: (str, Optional[Any]) -> Dict[str, str]
conn_str = conn_str.rstrip(";")
conn_settings = dict( # type: ignore # pylint: disable=consider-using-dict-comprehension
s.split("=", 1) for s in conn_str.split(";")
)
if 'AccountEndpoint' not in conn_settings:
raise ValueError("Connection string missing setting 'AccountEndpoint'.")
if not credential and 'AccountKey' not in conn_settings:
raise ValueError("Connection string missing setting 'AccountKey'.")
return conn_settings
def _build_auth(credential):
# type: (Any) -> Dict[str, Any]
auth = {}
if isinstance(credential, str):
auth['masterKey'] = credential
elif isinstance(credential, dict):
if any(k for k in credential.keys() if k in ['masterKey', 'resourceTokens', 'permissionFeed']):
return credential # Backwards compatible
auth['resourceTokens'] = credential # type: ignore
elif hasattr(credential, '__iter__'):
auth['permissionFeed'] = credential
elif hasattr(credential, 'get_token'):
auth['clientSecretCredential'] = credential
else:
raise TypeError(
"Unrecognized credential type. Please supply the master key as a string "
"or a dictionary, or resource tokens, or a list of permissions, or any instance of a class implementing"
" TokenCredential (see azure.identity module for specific implementations such as ClientSecretCredential).")
return auth
def _build_connection_policy(kwargs):
# type: (Dict[str, Any]) -> ConnectionPolicy
# pylint: disable=protected-access
policy = kwargs.pop('connection_policy', None) or ConnectionPolicy()
# Connection config
policy.RequestTimeout = kwargs.pop('request_timeout', None) or \
kwargs.pop('connection_timeout', None) or \
policy.RequestTimeout
policy.ConnectionMode = kwargs.pop('connection_mode', None) or policy.ConnectionMode
policy.ProxyConfiguration = kwargs.pop('proxy_config', None) or policy.ProxyConfiguration
policy.EnableEndpointDiscovery = kwargs.pop('enable_endpoint_discovery') \
if 'enable_endpoint_discovery' in kwargs.keys() else policy.EnableEndpointDiscovery
policy.PreferredLocations = kwargs.pop('preferred_locations', None) or policy.PreferredLocations
policy.UseMultipleWriteLocations = kwargs.pop('multiple_write_locations', None) or \
policy.UseMultipleWriteLocations
# SSL config
verify = kwargs.pop('connection_verify', None)
policy.DisableSSLVerification = not bool(verify if verify is not None else True)
ssl = kwargs.pop('ssl_config', None) or policy.SSLConfiguration
if ssl:
ssl.SSLCertFile = kwargs.pop('connection_cert', None) or ssl.SSLCertFile
ssl.SSLCaCerts = verify or ssl.SSLCaCerts
policy.SSLConfiguration = ssl
# Retry config
retry_options = kwargs.pop('retry_options', None)
if retry_options is not None:
warnings.warn("This option has been deprecated and will be removed from the SDK in a future release.",
DeprecationWarning)
retry_options = policy.RetryOptions
total_retries = kwargs.pop('retry_total', None)
retry_options._max_retry_attempt_count = total_retries or retry_options._max_retry_attempt_count
retry_options._fixed_retry_interval_in_milliseconds = kwargs.pop('retry_fixed_interval', None) or \
retry_options._fixed_retry_interval_in_milliseconds
max_backoff = kwargs.pop('retry_backoff_max', None)
retry_options._max_wait_time_in_seconds = max_backoff or retry_options._max_wait_time_in_seconds
policy.RetryOptions = retry_options
connection_retry = kwargs.pop('connection_retry_policy', None)
if connection_retry is not None:
warnings.warn("This option has been deprecated and will be removed from the SDK in a future release.",
DeprecationWarning)
connection_retry = policy.ConnectionRetryConfiguration
if not connection_retry:
connection_retry = ConnectionRetryPolicy(
retry_total=total_retries,
retry_connect=kwargs.pop('retry_connect', None),
retry_read=kwargs.pop('retry_read', None),
retry_status=kwargs.pop('retry_status', None),
retry_backoff_max=max_backoff,
retry_on_status_codes=kwargs.pop('retry_on_status_codes', []),
retry_backoff_factor=kwargs.pop('retry_backoff_factor', 0.8),
)
policy.ConnectionRetryConfiguration = connection_retry
return policy
class CosmosClient(object): # pylint: disable=client-accepts-api-version-keyword
"""A client-side logical representation of an Azure Cosmos DB account.
Use this client to configure and execute requests to the Azure Cosmos DB service.
:param str url: The URL of the Cosmos DB account.
:param credential: Can be the account key, or a dictionary of resource tokens.
:type credential: Union[str, Dict[str, str], ~azure.core.credentials.TokenCredential]
:param str consistency_level: Consistency level to use for the session. The default value is None (Account level).
More on consistency levels and possible values: https://aka.ms/cosmos-consistency-levels
:keyword int timeout: An absolute timeout in seconds, for the combined HTTP request and response processing.
:keyword int request_timeout: The HTTP request timeout in milliseconds.
:keyword str connection_mode: The connection mode for the client - currently only supports 'Gateway'.
:keyword proxy_config: Connection proxy configuration.
:paramtype proxy_config: ~azure.cosmos.ProxyConfiguration
:keyword ssl_config: Connection SSL configuration.
:paramtype ssl_config: ~azure.cosmos.SSLConfiguration
:keyword bool connection_verify: Whether to verify the connection, default value is True.
:keyword str connection_cert: An alternative certificate to verify the connection.
:keyword int retry_total: Maximum retry attempts.
:keyword int retry_backoff_max: Maximum retry wait time in seconds.
:keyword int retry_fixed_interval: Fixed retry interval in milliseconds.
:keyword int retry_read: Maximum number of socket read retry attempts.
:keyword int retry_connect: Maximum number of connection error retry attempts.
:keyword int retry_status: Maximum number of retry attempts on error status codes.
:keyword list[int] retry_on_status_codes: A list of specific status codes to retry on.
:keyword float retry_backoff_factor: Factor to calculate wait time between retry attempts.
:keyword bool enable_endpoint_discovery: Enable endpoint discovery for
geo-replicated database accounts. (Default: True)
:keyword list[str] preferred_locations: The preferred locations for geo-replicated database accounts.
:keyword bool enable_diagnostics_logging: Enable the CosmosHttpLogging policy.
Must be used along with a logger to work.
:keyword ~logging.Logger logger: Logger to be used for collecting request diagnostics. Can be passed in at client
level (to log all requests) or at a single request level. Requests will be logged at INFO level.
.. admonition:: Example:
.. literalinclude:: ../samples/examples.py
:start-after: [START create_client]
:end-before: [END create_client]
:language: python
:dedent: 0
:caption: Create a new instance of the Cosmos DB client:
:name: create_client
"""
def __init__(self, url, credential, consistency_level=None, **kwargs):
# type: (str, Any, Optional[str], Any) -> None
"""Instantiate a new CosmosClient."""
auth = _build_auth(credential)
connection_policy = _build_connection_policy(kwargs)
self.client_connection = CosmosClientConnection(
url, auth=auth, consistency_level=consistency_level, connection_policy=connection_policy, **kwargs
)
def __repr__(self): # pylint:disable=client-method-name-no-double-underscore
# type () -> str
return "<CosmosClient [{}]>".format(self.client_connection.url_connection)[:1024]
def __enter__(self):
self.client_connection.pipeline_client.__enter__()
return self
def __exit__(self, *args):
return self.client_connection.pipeline_client.__exit__(*args)
@classmethod
def from_connection_string(cls, conn_str, credential=None, consistency_level=None, **kwargs):
# type: (str, Optional[Any], Optional[str], Any) -> CosmosClient
"""Create a CosmosClient instance from a connection string.
This can be retrieved from the Azure portal.For full list of optional
keyword arguments, see the CosmosClient constructor.
:param str conn_str: The connection string.
:param credential: Alternative credentials to use instead of the key
provided in the connection string.
:type credential: str or dict(str, str)
:param Optional[str] consistency_level:
Consistency level to use for the session. The default value is None (Account level).
"""
settings = _parse_connection_str(conn_str, credential)
return cls(
url=settings['AccountEndpoint'],
credential=credential or settings['AccountKey'],
consistency_level=consistency_level,
**kwargs
)
@staticmethod
def _get_database_link(database_or_id):
# type: (Union[DatabaseProxy, str, Dict[str, str]]) -> str
if isinstance(database_or_id, str):
return "dbs/{}".format(database_or_id)
try:
return cast("DatabaseProxy", database_or_id).database_link
except AttributeError:
pass
database_id = cast("Dict[str, str]", database_or_id)["id"]
return "dbs/{}".format(database_id)
@distributed_trace
def create_database( # pylint: disable=redefined-builtin
self,
id, # type: str
populate_query_metrics=None, # type: Optional[bool]
offer_throughput=None, # type: Optional[Union[int, ThroughputProperties]]
**kwargs # type: Any
):
# type: (...) -> DatabaseProxy
"""
Create a new database with the given ID (name).
:param id: ID (name) of the database to create.
:param offer_throughput: The provisioned throughput for this offer.
:paramtype offer_throughput: int or ~azure.cosmos.ThroughputProperties.
:keyword str session_token: Token for use with Session consistency.
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
:keyword str etag: An ETag value, or the wildcard character (*). Used to check if the resource
has changed, and act according to the condition specified by the `match_condition` parameter.
:keyword ~azure.core.MatchConditions match_condition: The match condition to use upon the etag.
:keyword Callable response_hook: A callable invoked with the response metadata.
:returns: A DatabaseProxy instance representing the new database.
:rtype: ~azure.cosmos.DatabaseProxy
:raises ~azure.cosmos.exceptions.CosmosResourceExistsError: Database with the given ID already exists.
.. admonition:: Example:
.. literalinclude:: ../samples/examples.py
:start-after: [START create_database]
:end-before: [END create_database]
:language: python
:dedent: 0
:caption: Create a database in the Cosmos DB account:
:name: create_database
"""
request_options = build_options(kwargs)
response_hook = kwargs.pop('response_hook', None)
if populate_query_metrics is not None:
warnings.warn(
"the populate_query_metrics flag does not apply to this method and will be removed in the future",
UserWarning,
)
request_options["populateQueryMetrics"] = populate_query_metrics
_set_throughput_options(offer=offer_throughput, request_options=request_options)
result = self.client_connection.CreateDatabase(database=dict(id=id), options=request_options, **kwargs)
if response_hook:
response_hook(self.client_connection.last_response_headers)
return DatabaseProxy(self.client_connection, id=result["id"], properties=result)
@distributed_trace
def create_database_if_not_exists( # pylint: disable=redefined-builtin
self,
id, # type: str
populate_query_metrics=None, # type: Optional[bool]
offer_throughput=None, # type: Optional[Union[int, ThroughputProperties]]
**kwargs # type: Any
):
# type: (...) -> DatabaseProxy
"""
Create the database if it does not exist already.
If the database already exists, the existing settings are returned.
..note::
This function does not check or update existing database settings or
offer throughput if they differ from what is passed in.
:param id: ID (name) of the database to read or create.
:param bool populate_query_metrics: Enable returning query metrics in response headers.
:param offer_throughput: The provisioned throughput for this offer.
:type offer_throughput: int or ~azure.cosmos.ThroughputProperties.
:keyword str session_token: Token for use with Session consistency.
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
:keyword str etag: An ETag value, or the wildcard character (*). Used to check if the resource
has changed, and act according to the condition specified by the `match_condition` parameter.
:keyword ~azure.core.MatchConditions match_condition: The match condition to use upon the etag.
:keyword Callable response_hook: A callable invoked with the response metadata.
:returns: A DatabaseProxy instance representing the database.
:rtype: ~azure.cosmos.DatabaseProxy
:raises ~azure.cosmos.exceptions.CosmosHttpResponseError: The database read or creation failed.
"""
try:
database_proxy = self.get_database_client(id)
database_proxy.read(
populate_query_metrics=populate_query_metrics,
**kwargs
)
return database_proxy
except CosmosResourceNotFoundError:
return self.create_database(
id,
populate_query_metrics=populate_query_metrics,
offer_throughput=offer_throughput,
**kwargs
)
def get_database_client(self, database):
# type: (Union[str, DatabaseProxy, Dict[str, Any]]) -> DatabaseProxy
"""Retrieve an existing database with the ID (name) `id`.
:param database: The ID (name), dict representing the properties or
`DatabaseProxy` instance of the database to read.
:type database: str or dict(str, str) or ~azure.cosmos.DatabaseProxy
:returns: A `DatabaseProxy` instance representing the retrieved database.
:rtype: ~azure.cosmos.DatabaseProxy
"""
if isinstance(database, DatabaseProxy):
id_value = database.id
else:
try:
id_value = database["id"]
except TypeError:
id_value = database
return DatabaseProxy(self.client_connection, id_value)
@distributed_trace
def list_databases(
self,
max_item_count=None, # type: Optional[int]
populate_query_metrics=None, # type: Optional[bool]
**kwargs # type: Any
):
# type: (...) -> Iterable[Dict[str, Any]]
"""List the databases in a Cosmos DB SQL database account.
:param int max_item_count: Max number of items to be returned in the enumeration operation.
:keyword str session_token: Token for use with Session consistency.
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
:keyword Callable response_hook: A callable invoked with the response metadata.
:returns: An Iterable of database properties (dicts).
:rtype: Iterable[dict[str, str]]
"""
feed_options = build_options(kwargs)
response_hook = kwargs.pop('response_hook', None)
if max_item_count is not None:
feed_options["maxItemCount"] = max_item_count
if populate_query_metrics is not None:
warnings.warn(
"the populate_query_metrics flag does not apply to this method and will be removed in the future",
UserWarning,
)
feed_options["populateQueryMetrics"] = populate_query_metrics
result = self.client_connection.ReadDatabases(options=feed_options, **kwargs)
if response_hook:
response_hook(self.client_connection.last_response_headers)
return result
@distributed_trace
def query_databases(
self,
query=None, # type: Optional[str]
parameters=None, # type: Optional[List[str]]
enable_cross_partition_query=None, # type: Optional[bool]
max_item_count=None, # type: Optional[int]
populate_query_metrics=None, # type: Optional[bool]
**kwargs # type: Any
):
# type: (...) -> Iterable[Dict[str, Any]]
"""Query the databases in a Cosmos DB SQL database account.
:param str query: The Azure Cosmos DB SQL query to execute.
:param list[str] parameters: Optional array of parameters to the query. Ignored if no query is provided.
:param bool enable_cross_partition_query: Allow scan on the queries which couldn't be
served as indexing was opted out on the requested paths.
:param int max_item_count: Max number of items to be returned in the enumeration operation.
:keyword str session_token: Token for use with Session consistency.
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
:keyword Callable response_hook: A callable invoked with the response metadata.
:returns: An Iterable of database properties (dicts).
:rtype: Iterable[dict[str, str]]
"""
feed_options = build_options(kwargs)
response_hook = kwargs.pop('response_hook', None)
if enable_cross_partition_query is not None:
feed_options["enableCrossPartitionQuery"] = enable_cross_partition_query
if max_item_count is not None:
feed_options["maxItemCount"] = max_item_count
if populate_query_metrics is not None:
warnings.warn(
"the populate_query_metrics flag does not apply to this method and will be removed in the future",
UserWarning,
)
feed_options["populateQueryMetrics"] = populate_query_metrics
if query:
# This is currently eagerly evaluated in order to capture the headers
# from the call.
# (just returning a generator did not initiate the first network call, so
# the headers were misleading)
# This needs to change for "real" implementation
query = query if parameters is None else dict(query=query, parameters=parameters) # type: ignore
result = self.client_connection.QueryDatabases(query=query, options=feed_options, **kwargs)
else:
result = self.client_connection.ReadDatabases(options=feed_options, **kwargs)
if response_hook:
response_hook(self.client_connection.last_response_headers)
return result
@distributed_trace
def delete_database(
self,
database, # type: Union[str, DatabaseProxy, Dict[str, Any]]
populate_query_metrics=None, # type: Optional[bool]
**kwargs # type: Any
):
# type: (...) -> None
"""Delete the database with the given ID (name).
:param database: The ID (name), dict representing the properties or :class:`DatabaseProxy`
instance of the database to delete.
:type database: str or dict(str, str) or ~azure.cosmos.DatabaseProxy
:keyword str session_token: Token for use with Session consistency.
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
:keyword str etag: An ETag value, or the wildcard character (*). Used to check if the resource
has changed, and act according to the condition specified by the `match_condition` parameter.
:keyword ~azure.core.MatchConditions match_condition: The match condition to use upon the etag.
:keyword Callable response_hook: A callable invoked with the response metadata.
:raises ~azure.cosmos.exceptions.CosmosHttpResponseError: If the database couldn't be deleted.
:rtype: None
"""
request_options = build_options(kwargs)
response_hook = kwargs.pop('response_hook', None)
if populate_query_metrics is not None:
warnings.warn(
"the populate_query_metrics flag does not apply to this method and will be removed in the future",
UserWarning,
)
request_options["populateQueryMetrics"] = populate_query_metrics
database_link = self._get_database_link(database)
self.client_connection.DeleteDatabase(database_link, options=request_options, **kwargs)
if response_hook:
response_hook(self.client_connection.last_response_headers)
@distributed_trace
def get_database_account(self, **kwargs):
# type: (Any) -> DatabaseAccount
"""Retrieve the database account information.
:keyword Callable response_hook: A callable invoked with the response metadata.
:returns: A `DatabaseAccount` instance representing the Cosmos DB Database Account.
:rtype: ~azure.cosmos.DatabaseAccount
"""
response_hook = kwargs.pop('response_hook', None)
result = self.client_connection.GetDatabaseAccount(**kwargs)
if response_hook:
response_hook(self.client_connection.last_response_headers)
return result