/
_azure_appconfiguration_client.py
597 lines (514 loc) · 25.1 KB
/
_azure_appconfiguration_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
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
# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import binascii
from requests.structures import CaseInsensitiveDict
from azure.core import MatchConditions
from azure.core.pipeline import Pipeline
from azure.core.pipeline.policies import (
UserAgentPolicy,
DistributedTracingPolicy,
HttpLoggingPolicy,
BearerTokenCredentialPolicy,
)
from azure.core.tracing.decorator import distributed_trace
from azure.core.pipeline.transport import RequestsTransport
from azure.core.exceptions import (
HttpResponseError,
ClientAuthenticationError,
ResourceExistsError,
ResourceNotFoundError,
ResourceModifiedError,
ResourceNotModifiedError,
)
from ._azure_appconfiguration_error import ResourceReadOnlyError
from ._generated import AzureAppConfiguration
from ._generated.models import ErrorException
from ._generated._configuration import AzureAppConfigurationConfiguration
from ._models import ConfigurationSetting
from ._azure_appconfiguration_requests import AppConfigRequestsCredentialsPolicy
from ._azure_appconfiguration_credential import AppConfigConnectionStringCredential
from ._utils import (
get_endpoint_from_connection_string,
prep_if_match,
prep_if_none_match,
)
from ._sync_token import SyncTokenPolicy
from ._user_agent import USER_AGENT
try:
from typing import TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False
if TYPE_CHECKING:
# pylint:disable=unused-import,ungrouped-imports
from typing import Any, Optional
from azure.core.paging import ItemPaged
class AzureAppConfigurationClient:
"""Represents an client that calls restful API of Azure App Configuration service.
:param str base_url: base url of the service
:param credential: An object which can provide secrets for the app configuration service
:type credential: azure.AppConfigConnectionStringCredential
:keyword Pipeline pipeline: If omitted, the standard pipeline is used.
:keyword HttpTransport transport: If omitted, the standard pipeline is used.
:keyword list[HTTPPolicy] policies: If omitted, the standard pipeline is used.
"""
# pylint:disable=protected-access
def __init__(self, base_url, credential, **kwargs):
# type: (str, Any, dict) -> None
try:
if not base_url.lower().startswith("http"):
base_url = "https://" + base_url
except AttributeError:
raise ValueError("Base URL must be a string.")
if not credential:
raise ValueError("Missing credential")
self._config = AzureAppConfigurationConfiguration(
credential, base_url, **kwargs
)
self._config.user_agent_policy = UserAgentPolicy(
base_user_agent=USER_AGENT, **kwargs
)
self._sync_token_policy = SyncTokenPolicy()
self._sync_token_policy = None
pipeline = kwargs.get("pipeline")
if pipeline is None:
self._sync_token_policy = SyncTokenPolicy()
aad_mode = not isinstance(credential, AppConfigConnectionStringCredential)
pipeline = self._create_appconfig_pipeline(
credential=credential, aad_mode=aad_mode, base_url=base_url, **kwargs
)
self._impl = AzureAppConfiguration(
credentials=credential, endpoint=base_url, pipeline=pipeline
)
@classmethod
def from_connection_string(cls, connection_string, **kwargs):
# type: (str, dict) -> AzureAppConfigurationClient
"""Create AzureAppConfigurationClient from a Connection String.
:param connection_string: Connection String
(one of the access keys of the Azure App Configuration resource)
used to access the Azure App Configuration.
:type connection_string: str
:return: An AzureAppConfigurationClient authenticated with the connection string
:rtype: AzureAppConfigurationClient
Example
.. code-block:: python
from azure.appconfiguration import AzureAppConfigurationClient
connection_str = "<my connection string>"
client = AzureAppConfigurationClient.from_connection_string(connection_str)
"""
base_url = "https://" + get_endpoint_from_connection_string(connection_string)
return cls(
credential=AppConfigConnectionStringCredential(connection_string),
base_url=base_url,
**kwargs
)
def _create_appconfig_pipeline(
self, credential, base_url=None, aad_mode=False, **kwargs
):
transport = kwargs.get("transport")
policies = kwargs.get("policies")
if policies is None: # [] is a valid policy list
if aad_mode:
scope = base_url.strip("/") + "/.default"
if hasattr(credential, "get_token"):
credential_policy = BearerTokenCredentialPolicy(credential, scope)
else:
raise TypeError(
"Please provide an instance from azure-identity "
"or a class that implement the 'get_token protocol"
)
else:
credential_policy = AppConfigRequestsCredentialsPolicy(credential)
policies = [
self._config.headers_policy,
self._config.user_agent_policy,
self._config.retry_policy,
self._sync_token_policy,
credential_policy,
self._config.logging_policy, # HTTP request/response log
DistributedTracingPolicy(**kwargs),
HttpLoggingPolicy(**kwargs),
]
if not transport:
transport = RequestsTransport(**kwargs)
return Pipeline(transport, policies)
@distributed_trace
def list_configuration_settings(
self, key_filter=None, label_filter=None, **kwargs
): # type: (Optional[str], Optional[str], dict) -> ItemPaged[ConfigurationSetting]
"""List the configuration settings stored in the configuration service, optionally filtered by
label and accept_datetime
:param key_filter: filter results based on their keys. '*' can be
used as wildcard in the beginning or end of the filter
:type key_filter: str
:param label_filter: filter results based on their label. '*' can be
used as wildcard in the beginning or end of the filter
:type label_filter: str
:keyword datetime accept_datetime: filter out ConfigurationSetting created after this datetime
:keyword list[str] fields: specify which fields to include in the results. Leave None to include all fields
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request header
:return: An iterator of :class:`ConfigurationSetting`
:rtype: ~azure.core.paging.ItemPaged[ConfigurationSetting]
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`
Example
.. code-block:: python
from datetime import datetime, timedelta
accept_datetime = datetime.today() + timedelta(days=-1)
all_listed = client.list_configuration_settings()
for item in all_listed:
pass # do something
filtered_listed = client.list_configuration_settings(
label_filter="Labe*", key_filter="Ke*", accept_datetime=accept_datetime
)
for item in filtered_listed:
pass # do something
"""
select = kwargs.pop("fields", None)
if select:
select = ["locked" if x == "read_only" else x for x in select]
error_map = {401: ClientAuthenticationError}
try:
return self._impl.get_key_values(
label=label_filter,
key=key_filter,
select=select,
cls=lambda objs: [
ConfigurationSetting._from_generated(x) for x in objs
],
error_map=error_map,
**kwargs
)
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
@distributed_trace
def get_configuration_setting(
self,
key,
label=None,
etag="*",
match_condition=MatchConditions.Unconditionally,
**kwargs
): # type: (str, Optional[str], Optional[str], Optional[MatchConditions], dict) -> ConfigurationSetting
"""Get the matched ConfigurationSetting from Azure App Configuration service
:param key: key of the ConfigurationSetting
:type key: str
:param label: label of the ConfigurationSetting
:type label: str
:param etag: check if the ConfigurationSetting is changed. Set None to skip checking etag
:type etag: str or None
:param ~azure.core.MatchConditions match_condition: the match condition to use upon the etag
:keyword datetime accept_datetime: the retrieved ConfigurationSetting that created no later than this datetime
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request header
:return: The matched ConfigurationSetting object
:rtype: ~azure.appconfiguration.ConfigurationSetting
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`, \
:class:`ResourceNotFoundError`, :class:`ResourceModifiedError`, :class:`ResourceExistsError`
Example
.. code-block:: python
fetched_config_setting = client.get_configuration_setting(
key="MyKey", label="MyLabel"
)
"""
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError}
if match_condition == MatchConditions.IfNotModified:
error_map[412] = ResourceModifiedError
if match_condition == MatchConditions.IfModified:
error_map[304] = ResourceNotModifiedError
if match_condition == MatchConditions.IfPresent:
error_map[412] = ResourceNotFoundError
if match_condition == MatchConditions.IfMissing:
error_map[412] = ResourceExistsError
try:
key_value = self._impl.get_key_value(
key=key,
label=label,
if_match=prep_if_match(etag, match_condition),
if_none_match=prep_if_none_match(etag, match_condition),
error_map=error_map,
**kwargs
)
return ConfigurationSetting._from_generated(key_value)
except ResourceNotModifiedError:
return None
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
@distributed_trace
def add_configuration_setting(
self,
configuration_setting, # type: ConfigurationSetting
**kwargs # type: dict
):
# type: (...) -> ConfigurationSetting
"""Add a ConfigurationSetting instance into the Azure App Configuration service.
:param configuration_setting: the ConfigurationSetting object to be added
:type configuration_setting: :class:`ConfigurationSetting<azure.appconfiguration.ConfigurationSetting>`
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request header
:return: The ConfigurationSetting object returned from the App Configuration service
:rtype: ~azure.appconfiguration.ConfigurationSetting
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`, :class:`ResourceExistsError`
Example
.. code-block:: python
config_setting = ConfigurationSetting(
key="MyKey",
label="MyLabel",
value="my value",
content_type="my content type",
tags={"my tag": "my tag value"}
)
added_config_setting = client.add_configuration_setting(config_setting)
"""
key_value = configuration_setting._to_generated()
custom_headers = CaseInsensitiveDict(kwargs.get("headers"))
error_map = {401: ClientAuthenticationError, 412: ResourceExistsError}
try:
key_value_added = self._impl.put_key_value(
entity=key_value,
key=key_value.key,
label=key_value.label,
if_none_match="*",
headers=custom_headers,
error_map=error_map,
)
return ConfigurationSetting._from_generated(key_value_added)
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
@distributed_trace
def set_configuration_setting(
self,
configuration_setting,
match_condition=MatchConditions.Unconditionally,
**kwargs
): # type: (ConfigurationSetting, Optional[MatchConditions], dict) -> ConfigurationSetting
"""Add or update a ConfigurationSetting.
If the configuration setting identified by key and label does not exist, this is a create.
Otherwise this is an update.
:param configuration_setting: the ConfigurationSetting to be added (if not exists) \
or updated (if exists) to the service
:type configuration_setting: :class:`ConfigurationSetting`
:param ~azure.core.MatchConditions match_condition: the match condition to use upon the etag
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request header
:return: The ConfigurationSetting returned from the service
:rtype: ~azure.appconfiguration.ConfigurationSetting
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`, \
:class:`ResourceReadOnlyError`, :class:`ResourceModifiedError`, :class:`ResourceNotModifiedError`, \
:class:`ResourceNotFoundError`, :class:`ResourceExistsError`
Example
.. code-block:: python
config_setting = ConfigurationSetting(
key="MyKey",
label="MyLabel",
value="my set value",
content_type="my set content type",
tags={"my set tag": "my set tag value"}
)
returned_config_setting = client.set_configuration_setting(config_setting)
"""
key_value = configuration_setting._to_generated()
custom_headers = CaseInsensitiveDict(kwargs.get("headers"))
error_map = {401: ClientAuthenticationError, 409: ResourceReadOnlyError}
if match_condition == MatchConditions.IfNotModified:
error_map[412] = ResourceModifiedError
if match_condition == MatchConditions.IfModified:
error_map[412] = ResourceNotModifiedError
if match_condition == MatchConditions.IfPresent:
error_map[412] = ResourceNotFoundError
if match_condition == MatchConditions.IfMissing:
error_map[412] = ResourceExistsError
try:
key_value_set = self._impl.put_key_value(
entity=key_value,
key=key_value.key,
label=key_value.label,
if_match=prep_if_match(configuration_setting.etag, match_condition),
if_none_match=prep_if_none_match(
configuration_setting.etag, match_condition
),
headers=custom_headers,
error_map=error_map,
)
return ConfigurationSetting._from_generated(key_value_set)
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
@distributed_trace
def delete_configuration_setting(
self, key, label=None, **kwargs
): # type: (str, Optional[str], dict) -> ConfigurationSetting
"""Delete a ConfigurationSetting if it exists
:param key: key used to identify the ConfigurationSetting
:type key: str
:param label: label used to identify the ConfigurationSetting
:type label: str
:keyword str etag: check if the ConfigurationSetting is changed. Set None to skip checking etag
:keyword ~azure.core.MatchConditions match_condition: the match condition to use upon the etag
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request
:return: The deleted ConfigurationSetting returned from the service, or None if it doesn't exist.
:rtype: ~azure.appconfiguration.ConfigurationSetting
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`, \
:class:`ResourceReadOnlyError`, :class:`ResourceModifiedError`, :class:`ResourceNotModifiedError`, \
:class:`ResourceNotFoundError`, :class:`ResourceExistsError`
Example
.. code-block:: python
deleted_config_setting = client.delete_configuration_setting(
key="MyKey", label="MyLabel"
)
"""
etag = kwargs.pop("etag", None)
match_condition = kwargs.pop("match_condition", MatchConditions.Unconditionally)
custom_headers = CaseInsensitiveDict(kwargs.get("headers"))
error_map = {401: ClientAuthenticationError, 409: ResourceReadOnlyError}
if match_condition == MatchConditions.IfNotModified:
error_map[412] = ResourceModifiedError
if match_condition == MatchConditions.IfModified:
error_map[412] = ResourceNotModifiedError
if match_condition == MatchConditions.IfPresent:
error_map[412] = ResourceNotFoundError
if match_condition == MatchConditions.IfMissing:
error_map[412] = ResourceExistsError
try:
key_value_deleted = self._impl.delete_key_value(
key=key,
label=label,
if_match=prep_if_match(etag, match_condition),
headers=custom_headers,
error_map=error_map,
)
return ConfigurationSetting._from_generated(key_value_deleted)
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
@distributed_trace
def list_revisions(
self, key_filter=None, label_filter=None, **kwargs
): # type: (Optional[str], Optional[str], dict) -> ItemPaged[ConfigurationSetting]
"""
Find the ConfigurationSetting revision history.
:param key_filter: filter results based on their keys. '*' can be
used as wildcard in the beginning or end of the filter
:type key_filter: str
:param label_filter: filter results based on their label. '*' can be
used as wildcard in the beginning or end of the filter
:type label_filter: str
:keyword datetime accept_datetime: filter out ConfigurationSetting created after this datetime
:keyword list[str] fields: specify which fields to include in the results. Leave None to include all fields
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request header
:return: An iterator of :class:`ConfigurationSetting`
:rtype: ~azure.core.paging.ItemPaged[ConfigurationSetting]
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`
Example
.. code-block:: python
from datetime import datetime, timedelta
accept_datetime = datetime.today() + timedelta(days=-1)
all_revisions = client.list_revisions()
for item in all_revisions:
pass # do something
filtered_revisions = client.list_revisions(
label_filter="Labe*", key_filter="Ke*", accept_datetime=accept_datetime
)
for item in filtered_revisions:
pass # do something
"""
select = kwargs.pop("fields", None)
if select:
select = ["locked" if x == "read_only" else x for x in select]
error_map = {401: ClientAuthenticationError}
try:
return self._impl.get_revisions(
label=label_filter,
key=key_filter,
select=select,
cls=lambda objs: [
ConfigurationSetting._from_generated(x) for x in objs
],
error_map=error_map,
**kwargs
)
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
@distributed_trace
def set_read_only(
self, configuration_setting, read_only=True, **kwargs
): # type: (ConfigurationSetting, Optional[bool], dict) -> ConfigurationSetting
"""Set a configuration setting read only
:param configuration_setting: the ConfigurationSetting to be set read only
:type configuration_setting: :class:`ConfigurationSetting`
:param read_only: set the read only setting if true, else clear the read only setting
:type read_only: bool
:keyword ~azure.core.MatchConditions match_condition: the match condition to use upon the etag
:keyword dict headers: if "headers" exists, its value (a dict) will be added to the http request header
:return: The ConfigurationSetting returned from the service
:rtype: ~azure.appconfiguration.ConfigurationSetting
:raises: :class:`HttpResponseError`, :class:`ClientAuthenticationError`, :class:`ResourceNotFoundError`
Example
.. code-block:: python
config_setting = client.get_configuration_setting(
key="MyKey", label="MyLabel"
)
read_only_config_setting = client.set_read_only(config_setting)
read_only_config_setting = client.set_read_only(config_setting, read_only=False)
"""
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError}
match_condition = kwargs.pop("match_condition", MatchConditions.Unconditionally)
if match_condition == MatchConditions.IfNotModified:
error_map[412] = ResourceModifiedError
if match_condition == MatchConditions.IfModified:
error_map[412] = ResourceNotModifiedError
if match_condition == MatchConditions.IfPresent:
error_map[412] = ResourceNotFoundError
if match_condition == MatchConditions.IfMissing:
error_map[412] = ResourceExistsError
try:
if read_only:
key_value = self._impl.put_lock(
key=configuration_setting.key,
label=configuration_setting.label,
if_match=prep_if_match(configuration_setting.etag, match_condition),
if_none_match=prep_if_none_match(
configuration_setting.etag, match_condition
),
error_map=error_map,
**kwargs
)
else:
key_value = self._impl.delete_lock(
key=configuration_setting.key,
label=configuration_setting.label,
if_match=prep_if_match(configuration_setting.etag, match_condition),
if_none_match=prep_if_none_match(
configuration_setting.etag, match_condition
),
error_map=error_map,
**kwargs
)
return ConfigurationSetting._from_generated(key_value)
except ErrorException as error:
raise HttpResponseError(message=error.message, response=error.response)
except binascii.Error:
raise binascii.Error("Connection string secret has incorrect padding")
def update_sync_token(self, token):
# type: (str) -> None
"""Add a sync token to the internal list of tokens.
:param token: The sync token to be added to the internal list of tokens
:type token: str
"""
if not self._sync_token_policy:
raise AttributeError(
"Client has no sync token policy, possibly because it was not provided during instantiation."
)
self._sync_token_policy.add_token(token)
def close(self):
# type: (...) -> None
"""Close all connections made by the client"""
self._impl._client.close()