Skip to content

Commit 17c2f18

Browse files
[ServiceBus] add keyword override support to update_ methods in mgmt module (#18210)
* add keyword override support * updat impl * add test and recordings * version bump, changelog update * bug fix log * fix pylint * fix pylint and mypy * pr review feedback * Update sdk/servicebus/azure-servicebus/CHANGELOG.md Co-authored-by: swathipil <[email protected]> * review feedback * fix mypy and pylint * fix pylint * header bearer should use normalized path to generate token instead of raw input * update code snippets to show update keyword argument usage Co-authored-by: swathipil <[email protected]>
1 parent 701a6f5 commit 17c2f18

File tree

36 files changed

+2984
-1312
lines changed

36 files changed

+2984
-1312
lines changed

sdk/servicebus/azure-servicebus/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The preview features related to AMQPAnnotatedMessage introduced in 7.2.0b1 are n
88

99
* Added support for using `azure.core.credentials.AzureNamedKeyCredential` as credential for authenticating the clients.
1010
* Support for using `azure.core.credentials.AzureSasCredential` as credential for authenticating the clients is now GA.
11+
* `ServiceBusAdministrationClient.update_*` methods now accept keyword arguments to override the properties specified in the model instance.
12+
13+
**Bug Fixes**
14+
15+
* Fixed a bug where `update_queue` and `update_subscription` methods were mutating the properties `forward_to` and `forward_dead_lettered_messages_to` of the model instance when those properties are entities instead of full paths.
1116

1217
**Notes**
1318

@@ -26,6 +31,7 @@ The preview features related to AMQPAnnotatedMessage introduced in 7.2.0b1 are n
2631
- `VALUE`: The body of message consists of one amqp-value section and the section contains a single AMQP value.
2732
* Added new property `body_type` on `azure.servicebus.ServiceBusMessage` and `azure.servicebus.ReceivedMessage` which returns `azure.servicebus.AMQPMessageBodyType`.
2833

34+
2935
## 7.1.1 (2021-04-07)
3036

3137
This version and all future versions will require Python 2.7 or Python 3.6+, Python 3.5 is no longer supported.

sdk/servicebus/azure-servicebus/azure/servicebus/aio/management/_management_client_async.py

+32-59
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# pylint:disable=specify-parameter-names-in-call
77
# pylint:disable=too-many-lines
88
import functools
9+
from copy import deepcopy
910
from typing import TYPE_CHECKING, Any, Union, cast, Mapping
1011
from xml.etree.ElementTree import ElementTree
1112

@@ -72,7 +73,6 @@
7273
)
7374
from ...management._xml_workaround_policy import ServiceBusXMLWorkaroundPolicy
7475
from ...management._handle_response_error import _handle_response_error
75-
from ...management._model_workaround import avoid_timedelta_overflow
7676
from ._utils import extract_data_template, extract_rule_data_template, get_next_template
7777
from ...management._utils import (
7878
deserialize_rule_key_values,
@@ -386,14 +386,14 @@ async def create_queue(self, queue_name: str, **kwargs) -> QueueProperties:
386386
forward_dead_lettered_messages_to=forward_dead_lettered_messages_to,
387387
user_metadata=kwargs.pop("user_metadata", None),
388388
)
389-
to_create = queue._to_internal_entity()
389+
to_create = queue._to_internal_entity(self.fully_qualified_namespace)
390390
create_entity_body = CreateQueueBody(
391391
content=CreateQueueBodyContent(
392392
queue_description=to_create, # type: ignore
393393
)
394394
)
395395
request_body = create_entity_body.serialize(is_xml=True)
396-
await self._create_forward_to_header_tokens(queue, kwargs)
396+
await self._create_forward_to_header_tokens(to_create, kwargs)
397397
with _handle_response_error():
398398
entry_ele = cast(
399399
ElementTree,
@@ -419,39 +419,26 @@ async def update_queue(
419419
Before calling this method, you should use `get_queue`, `create_queue` or `list_queues` to get a
420420
`QueueProperties` instance, then update the properties. Only a portion of properties can
421421
be updated. Refer to https://docs.microsoft.com/en-us/rest/api/servicebus/update-queue.
422+
You could also pass keyword arguments for updating properties in the form of
423+
`<property_name>=<property_value>` which will override whatever was specified in
424+
the `QueueProperties` instance. Refer to ~azure.servicebus.management.QueueProperties for names of properties.
422425
423426
:param queue: The queue that is returned from `get_queue`, `create_queue` or `list_queues` and
424427
has the updated properties.
425428
:type queue: ~azure.servicebus.management.QueueProperties
426429
:rtype: None
427430
"""
428-
429-
queue = create_properties_from_dict_if_needed(queue, QueueProperties)
430-
queue.forward_to = _normalize_entity_path_to_full_path_if_needed(
431-
queue.forward_to, self.fully_qualified_namespace
432-
)
433-
queue.forward_dead_lettered_messages_to = (
434-
_normalize_entity_path_to_full_path_if_needed(
435-
queue.forward_dead_lettered_messages_to,
436-
self.fully_qualified_namespace,
437-
)
438-
)
439-
to_update = queue._to_internal_entity()
440-
441-
to_update.default_message_time_to_live = avoid_timedelta_overflow(
442-
to_update.default_message_time_to_live
443-
)
444-
to_update.auto_delete_on_idle = avoid_timedelta_overflow(
445-
to_update.auto_delete_on_idle
446-
)
431+
# we should not mutate the input, making a copy first for update
432+
queue = deepcopy(create_properties_from_dict_if_needed(queue, QueueProperties))
433+
to_update = queue._to_internal_entity(self.fully_qualified_namespace, kwargs)
447434

448435
create_entity_body = CreateQueueBody(
449436
content=CreateQueueBodyContent(
450437
queue_description=to_update,
451438
)
452439
)
453440
request_body = create_entity_body.serialize(is_xml=True)
454-
await self._create_forward_to_header_tokens(queue, kwargs)
441+
await self._create_forward_to_header_tokens(to_update, kwargs)
455442
with _handle_response_error():
456443
await self._impl.entity.put(
457444
queue.name, # type: ignore
@@ -660,22 +647,18 @@ async def update_topic(
660647
Before calling this method, you should use `get_topic`, `create_topic` or `list_topics` to get a
661648
`TopicProperties` instance, then update the properties. Only a portion of properties can be updated.
662649
Refer to https://docs.microsoft.com/en-us/rest/api/servicebus/update-topic.
650+
You could also pass keyword arguments for updating properties in the form of
651+
`<property_name>=<property_value>` which will override whatever was specified in
652+
the `TopicProperties` instance. Refer to ~azure.servicebus.management.TopicProperties for names of properties.
663653
664654
:param topic: The topic that is returned from `get_topic`, `create_topic`, or `list_topics`
665655
and has the updated properties.
666656
:type topic: ~azure.servicebus.management.TopicProperties
667657
:rtype: None
668658
"""
669659

670-
topic = create_properties_from_dict_if_needed(topic, TopicProperties)
671-
to_update = topic._to_internal_entity()
672-
673-
to_update.default_message_time_to_live = avoid_timedelta_overflow( # type: ignore
674-
to_update.default_message_time_to_live
675-
)
676-
to_update.auto_delete_on_idle = avoid_timedelta_overflow( # type: ignore
677-
to_update.auto_delete_on_idle
678-
)
660+
topic = deepcopy(create_properties_from_dict_if_needed(topic, TopicProperties))
661+
to_update = topic._to_internal_entity(kwargs)
679662

680663
create_entity_body = CreateTopicBody(
681664
content=CreateTopicBodyContent(
@@ -849,6 +832,7 @@ async def create_subscription(
849832
:type auto_delete_on_idle: Union[~datetime.timedelta, str]
850833
:rtype: ~azure.servicebus.management.SubscriptionProperties
851834
"""
835+
# pylint:disable=protected-access
852836
_validate_entity_name_type(topic_name, display_name="topic_name")
853837
forward_to = _normalize_entity_path_to_full_path_if_needed(
854838
kwargs.pop("forward_to", None), self.fully_qualified_namespace
@@ -882,15 +866,15 @@ async def create_subscription(
882866
auto_delete_on_idle=kwargs.pop("auto_delete_on_idle", None),
883867
availability_status=None,
884868
)
885-
to_create = subscription._to_internal_entity() # type: ignore # pylint:disable=protected-access
869+
to_create = subscription._to_internal_entity(self.fully_qualified_namespace) # type: ignore
886870

887871
create_entity_body = CreateSubscriptionBody(
888872
content=CreateSubscriptionBodyContent(
889873
subscription_description=to_create, # type: ignore
890874
)
891875
)
892876
request_body = create_entity_body.serialize(is_xml=True)
893-
await self._create_forward_to_header_tokens(subscription, kwargs)
877+
await self._create_forward_to_header_tokens(to_create, kwargs)
894878
with _handle_response_error():
895879
entry_ele = cast(
896880
ElementTree,
@@ -919,6 +903,10 @@ async def update_subscription(
919903
920904
Before calling this method, you should use `get_subscription`, `update_subscription` or `list_subscription`
921905
to get a `SubscriptionProperties` instance, then update the properties.
906+
You could also pass keyword arguments for updating properties in the form of
907+
`<property_name>=<property_value>` which will override whatever was specified in
908+
the `SubscriptionProperties` instance.
909+
Refer to ~azure.servicebus.management.SubscriptionProperties for names of properties.
922910
923911
:param str topic_name: The topic that owns the subscription.
924912
:param ~azure.servicebus.management.SubscriptionProperties subscription: The subscription that is returned
@@ -927,35 +915,17 @@ async def update_subscription(
927915
"""
928916

929917
_validate_entity_name_type(topic_name, display_name="topic_name")
930-
931-
subscription = create_properties_from_dict_if_needed(
932-
subscription, SubscriptionProperties
933-
)
934-
subscription.forward_to = _normalize_entity_path_to_full_path_if_needed(
935-
subscription.forward_to, self.fully_qualified_namespace
936-
)
937-
subscription.forward_dead_lettered_messages_to = (
938-
_normalize_entity_path_to_full_path_if_needed(
939-
subscription.forward_dead_lettered_messages_to,
940-
self.fully_qualified_namespace,
941-
)
942-
)
943-
to_update = subscription._to_internal_entity()
944-
945-
to_update.default_message_time_to_live = avoid_timedelta_overflow( # type: ignore
946-
to_update.default_message_time_to_live
947-
)
948-
to_update.auto_delete_on_idle = avoid_timedelta_overflow( # type: ignore
949-
to_update.auto_delete_on_idle
950-
)
918+
# we should not mutate the input, making a copy first for update
919+
subscription = deepcopy(create_properties_from_dict_if_needed(subscription, SubscriptionProperties))
920+
to_update = subscription._to_internal_entity(self.fully_qualified_namespace, kwargs)
951921

952922
create_entity_body = CreateSubscriptionBody(
953923
content=CreateSubscriptionBodyContent(
954924
subscription_description=to_update,
955925
)
956926
)
957927
request_body = create_entity_body.serialize(is_xml=True)
958-
await self._create_forward_to_header_tokens(subscription, kwargs)
928+
await self._create_forward_to_header_tokens(to_update, kwargs)
959929
with _handle_response_error():
960930
await self._impl.subscription.put(
961931
topic_name,
@@ -1130,6 +1100,9 @@ async def update_rule(
11301100
11311101
Before calling this method, you should use `get_rule`, `create_rule` or `list_rules` to get a `RuleProperties`
11321102
instance, then update the properties.
1103+
You could also pass keyword arguments for updating properties in the form of
1104+
`<property_name>=<property_value>` which will override whatever was specified in
1105+
the `RuleProperties` instance. Refer to ~azure.servicebus.management.RuleProperties for names of properties.
11331106
11341107
:param str topic_name: The topic that owns the subscription.
11351108
:param str subscription_name: The subscription that
@@ -1140,9 +1113,9 @@ async def update_rule(
11401113
:rtype: None
11411114
"""
11421115
_validate_topic_and_subscription_types(topic_name, subscription_name)
1143-
1144-
rule = create_properties_from_dict_if_needed(rule, RuleProperties)
1145-
to_update = rule._to_internal_entity()
1116+
# we should not mutate the input, making a copy first for update
1117+
rule = deepcopy(create_properties_from_dict_if_needed(rule, RuleProperties))
1118+
to_update = rule._to_internal_entity(kwargs)
11461119

11471120
create_entity_body = CreateRuleBody(
11481121
content=CreateRuleBodyContent(

0 commit comments

Comments
 (0)