Skip to content

Commit

Permalink
fix wild cards handling (#9066)
Browse files Browse the repository at this point in the history
* fix wild cards handling

* update tests

* rename keys/labels to key_filter/label_filter
  • Loading branch information
xiangyan99 authored Dec 13, 2019
1 parent 93f511f commit 3b4cdf9
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from ._azure_appconfiguration_credential import AppConfigConnectionStringCredential
from ._utils import (
get_endpoint_from_connection_string,
escape_and_tostr,
prep_if_match,
prep_if_none_match,
)
Expand Down Expand Up @@ -145,18 +144,18 @@ def _create_appconfig_pipeline(self, credential, base_url=None, aad_mode=False,

@distributed_trace
def list_configuration_settings(
self, keys=None, labels=None, **kwargs
): # type: (Optional[list], Optional[list], dict) -> azure.core.paging.ItemPaged[ConfigurationSetting]
self, key_filter=None, label_filter=None, **kwargs
): # type: (Optional[str], Optional[str], dict) -> azure.core.paging.ItemPaged[ConfigurationSetting]

"""List the configuration settings stored in the configuration service, optionally filtered by
label and accept_datetime
:param keys: filter results based on their keys. '*' can be
:param key_filter: filter results based on their keys. '*' can be
used as wildcard in the beginning or end of the filter
:type keys: list[str]
:param labels: filter results based on their label. '*' can be
: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 labels: list[str]
: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
Expand All @@ -177,24 +176,22 @@ def list_configuration_settings(
pass # do something
filtered_listed = client.list_configuration_settings(
labels=["*Labe*"], keys=["*Ke*"], accept_datetime=accept_datetime
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]
encoded_labels = escape_and_tostr(labels)
encoded_keys = escape_and_tostr(keys)
error_map = {
401: ClientAuthenticationError
}

try:
return self._impl.get_key_values(
label=encoded_labels,
key=encoded_keys,
label=label_filter,
key=key_filter,
select=select,
cls=lambda objs: [ConfigurationSetting._from_key_value(x) for x in objs],
error_map=error_map,
Expand Down Expand Up @@ -437,18 +434,18 @@ def delete_configuration_setting(

@distributed_trace
def list_revisions(
self, keys=None, labels=None, **kwargs
): # type: (Optional[list], Optional[list], dict) -> azure.core.paging.ItemPaged[ConfigurationSetting]
self, key_filter=None, label_filter=None, **kwargs
): # type: (Optional[str], Optional[str], dict) -> azure.core.paging.ItemPaged[ConfigurationSetting]

"""
Find the ConfigurationSetting revision history.
:param keys: filter results based on their keys. '*' can be
:param key_filter: filter results based on their keys. '*' can be
used as wildcard in the beginning or end of the filter
:type keys: list[str]
:param labels: filter results based on their label. '*' can be
: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 labels: list[str]
: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
Expand All @@ -469,24 +466,22 @@ def list_revisions(
pass # do something
filtered_revisions = client.list_revisions(
labels=["*Labe*"], keys=["*Ke*"], accept_datetime=accept_datetime
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]
encoded_labels = escape_and_tostr(labels)
encoded_keys = escape_and_tostr(keys)
error_map = {
401: ClientAuthenticationError
}

try:
return self._impl.get_revisions(
label=encoded_labels,
key=encoded_keys,
label=label_filter,
key=key_filter,
select=select,
cls=lambda objs: [ConfigurationSetting._from_key_value(x) for x in objs],
error_map=error_map,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,8 @@
# -------------------------------------------------------------------------

from datetime import datetime
import re
from azure.core import MatchConditions

def escape_reserved(value):
"""
Reserved characters are star(*), comma(,) and backslash(\\)
If a reserved character is part of the value, then it must be escaped using \\{Reserved Character}.
Non-reserved characters can also be escaped.
"""
if value is None:
return None
if value == "":
return "\0" # '\0' will be encoded to %00 in the url.
if isinstance(value, list):
return [escape_reserved(s) for s in value]
value = str(value) # value is unicode for Python 2.7
# precede all reserved characters with a backslash.
# But if a * is at the beginning or the end, don't add the backslash
return re.sub(r"((?!^)\*(?!$)|\\|,)", r"\\\1", value)

def escape_and_tostr(value):
if value is None:
return None
if value == [None]:
return None
value = escape_reserved(value)
return ','.join(value)

def quote_etag(etag):
if not etag or etag == "*":
return etag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from .._azure_appconfiguration_error import ResourceReadOnlyError
from .._utils import (
get_endpoint_from_connection_string,
escape_and_tostr,
prep_if_match,
prep_if_none_match,
)
Expand Down Expand Up @@ -155,18 +154,18 @@ def _create_appconfig_pipeline(self, credential, base_url=None, aad_mode=False,

@distributed_trace
def list_configuration_settings(
self, keys=None, labels=None, **kwargs
): # type: (list, list, dict) -> azure.core.paging.ItemPaged[ConfigurationSetting]
self, key_filter=None, label_filter=None, **kwargs
): # type: (Optional[str], Optional[str], dict) -> azure.core.paging.ItemPaged[ConfigurationSetting]

"""List the configuration settings stored in the configuration service, optionally filtered by
label and accept_datetime
:param keys: filter results based on their keys. '*' can be
:param key_filter: filter results based on their keys. '*' can be
used as wildcard in the beginning or end of the filter
:type keys: list[str]
:param labels: filter results based on their label. '*' can be
: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 labels: list[str]
: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
Expand All @@ -187,24 +186,22 @@ def list_configuration_settings(
pass # do something
filtered_listed = async_client.list_configuration_settings(
labels=["*Labe*"], keys=["*Ke*"], accept_datetime=accept_datetime
label_filter="*Labe*", key_filter="*Ke*", accept_datetime=accept_datetime
)
async 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]
encoded_labels = escape_and_tostr(labels)
encoded_keys = escape_and_tostr(keys)
error_map = {
401: ClientAuthenticationError
}

try:
return self._impl.get_key_values(
label=encoded_labels,
key=encoded_keys,
label=label_filter,
key=key_filter,
select=select,
cls=lambda objs: [ConfigurationSetting._from_key_value(x) for x in objs],
error_map=error_map,
Expand Down Expand Up @@ -453,18 +450,18 @@ async def delete_configuration_setting(

@distributed_trace
def list_revisions(
self, keys=None, labels=None, **kwargs
): # type: (Optional[list], Optional[list], dict) -> azure.core.paging.AsyncItemPaged[ConfigurationSetting]
self, key_filter=None, label_filter=None, **kwargs
): # type: (Optional[str], Optional[str], dict) -> azure.core.paging.AsyncItemPaged[ConfigurationSetting]

"""
Find the ConfigurationSetting revision history.
:param keys: filter results based on their keys. '*' can be
:param key_filter: filter results based on their keys. '*' can be
used as wildcard in the beginning or end of the filter
:type keys: list[str]
:param labels: filter results based on their label. '*' can be
: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 labels: list[str]
: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
Expand All @@ -486,24 +483,22 @@ def list_revisions(
pass # do something
filtered_revisions = async_client.list_revisions(
labels=["*Labe*"], keys=["*Ke*"], accept_datetime=accept_datetime
label_filter="*Labe*", key_filter="*Ke*", accept_datetime=accept_datetime
)
async 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]
encoded_labels = escape_and_tostr(labels)
encoded_keys = escape_and_tostr(keys)
error_map = {
401: ClientAuthenticationError
}

try:
return self._impl.get_revisions(
label=encoded_labels,
key=encoded_keys,
label=label_filter,
key=key_filter,
select=select,
cls=lambda objs: [ConfigurationSetting._from_key_value(x) for x in objs],
error_map=error_map,
Expand Down
12 changes: 6 additions & 6 deletions sdk/appconfiguration/azure-appconfiguration/tests/async_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ def delete_configuration_setting(self, key, label=None, etag=None, **kwargs):
)

def list_configuration_settings(
self, labels=None, keys=None, accept_datetime=None, fields=None, **kwargs
self, label_filter=None, key_filter=None, accept_datetime=None, fields=None, **kwargs
):
paged = self.obj.list_configuration_settings(
labels=labels,
keys=keys,
label_filter=label_filter,
key_filter=key_filter,
accept_datetime=accept_datetime,
fields=fields,
**kwargs
)
return _to_list(paged)

def list_revisions(
self, labels=None, keys=None, accept_datetime=None, fields=None, **kwargs
self, label_filter=None, key_filter=None, accept_datetime=None, fields=None, **kwargs
):
paged = self.obj.list_revisions(
labels=labels,
keys=keys,
label_filter=label_filter,
key_filter=key_filter,
accept_datetime=accept_datetime,
fields=fields,
**kwargs
Expand Down
Loading

0 comments on commit 3b4cdf9

Please sign in to comment.