Skip to content

Commit 8a657e7

Browse files
Merge from sb_control_client main feature branch.
2 parents 4319e44 + 825e668 commit 8a657e7

File tree

3 files changed

+266
-224
lines changed

3 files changed

+266
-224
lines changed

sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py

+64-34
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
5-
5+
from contextlib import contextmanager
66
from copy import copy
77
from typing import TYPE_CHECKING, Dict, Any, Union, List, cast, Type
88
from xml.etree.ElementTree import ElementTree, Element
99

10-
from azure.core.exceptions import ResourceNotFoundError
10+
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
1111
from azure.core.pipeline import Pipeline
1212
from azure.core.pipeline.policies import HttpLoggingPolicy, DistributedTracingPolicy, ContentDecodePolicy, \
1313
RequestIdPolicy, BearerTokenCredentialPolicy
@@ -29,6 +29,19 @@
2929
from azure.core.credentials import TokenCredential # pylint:disable=ungrouped-imports
3030

3131

32+
@contextmanager
33+
def _handle_response_error():
34+
try:
35+
yield
36+
except HttpResponseError as response_error:
37+
new_response_error = HttpResponseError(
38+
message=response_error.model.detail,
39+
response=response_error.response,
40+
model=response_error.model
41+
)
42+
raise new_response_error
43+
44+
3245
def _convert_xml_to_object(queue_name, et, class_):
3346
# type: (str, Union[Element, ElementTree], Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
3447
content_ele = cast(ElementTree, et).find(constants.CONTENT_TAG)
@@ -97,17 +110,40 @@ def _get_queue_object(self, queue_name, class_):
97110

98111
if not queue_name:
99112
raise ValueError("queue_name must be a non-empty str")
100-
et = cast(
101-
ElementTree,
102-
self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
103-
)
113+
114+
with _handle_response_error():
115+
et = cast(
116+
ElementTree,
117+
self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
118+
)
104119
return _convert_xml_to_object(queue_name, et, class_)
105120

121+
def _list_queues(self, skip, max_count, class_):
122+
# type: (int, int, Type[Model]) -> Union[List[QueueDescription], List[QueueRuntimeInfo])
123+
with _handle_response_error():
124+
et = cast(
125+
ElementTree,
126+
self._impl.list_entities(
127+
entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION
128+
)
129+
)
130+
entries = et.findall(constants.ENTRY_TAG)
131+
queues = []
132+
for entry in entries:
133+
entity_name = entry.find(constants.TITLE_TAG).text # type: ignore
134+
queue_description = _convert_xml_to_object(
135+
entity_name, # type: ignore
136+
cast(Element, entry),
137+
class_
138+
)
139+
queues.append(queue_description)
140+
return queues
141+
106142
def get_queue(self, queue_name):
107143
# type: (str) -> QueueDescription
108144
return self._get_queue_object(queue_name, QueueDescription)
109145

110-
def get_queue_metrics(self, queue_name):
146+
def get_queue_runtime_info(self, queue_name):
111147
# type: (str) -> QueueRuntimeInfo
112148
return self._get_queue_object(queue_name, QueueRuntimeInfo)
113149

@@ -131,10 +167,11 @@ def create_queue(self, queue):
131167
)
132168
request_body = create_entity_body.serialize(is_xml=True)
133169
try:
134-
et = cast(
135-
ElementTree,
136-
self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
137-
)
170+
with _handle_response_error():
171+
et = cast(
172+
ElementTree,
173+
self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
174+
)
138175
except ValidationError as e:
139176
# post-hoc try to give a somewhat-justifiable failure reason.
140177
if isinstance(queue, str) or (isinstance(queue, QueueDescription) and isinstance(queue.queue_name, str)):
@@ -157,15 +194,16 @@ def update_queue(self, queue_description):
157194
)
158195
)
159196
request_body = create_entity_body.serialize(is_xml=True)
160-
et = cast(
161-
ElementTree,
162-
self._impl.queue.put(
163-
queue_description.queue_name, # type: ignore
164-
request_body,
165-
api_version=constants.API_VERSION,
166-
if_match="*"
197+
with _handle_response_error():
198+
et = cast(
199+
ElementTree,
200+
self._impl.queue.put(
201+
queue_description.queue_name, # type: ignore
202+
request_body,
203+
api_version=constants.API_VERSION,
204+
if_match="*"
205+
)
167206
)
168-
)
169207
return _convert_xml_to_object(queue_description.queue_name, et, QueueDescription)
170208

171209
def delete_queue(self, queue_name):
@@ -174,21 +212,13 @@ def delete_queue(self, queue_name):
174212

175213
if not queue_name:
176214
raise ValueError("queue_name must not be None or empty")
177-
self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
215+
with _handle_response_error():
216+
self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
178217

179218
def list_queues(self, skip=0, max_count=100):
180219
# type: (int, int) -> List[QueueDescription]
181-
et = cast(
182-
ElementTree,
183-
self._impl.list_entities(entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION)
184-
)
185-
entries = et.findall(constants.ENTRY_TAG)
186-
queue_descriptions = []
187-
for entry in entries:
188-
entity_name = entry.find(constants.TITLE_TAG).text # type: ignore
189-
queue_description = _convert_xml_to_object(
190-
entity_name, # type: ignore
191-
cast(Element, entry),
192-
QueueDescription)
193-
queue_descriptions.append(queue_description)
194-
return queue_descriptions
220+
return self._list_queues(skip, max_count, QueueDescription)
221+
222+
def list_queues_runtime_info(self, skip=0, max_count=100):
223+
# type: (int, int) -> List[QueueRuntimeInfo]
224+
return self._list_queues(skip, max_count, QueueRuntimeInfo)

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

+46-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
5-
65
from copy import copy
76
from typing import TYPE_CHECKING, Dict, Any, Union, List, Type, cast
87
from xml.etree.ElementTree import ElementTree, Element
@@ -22,7 +21,7 @@
2221
from .._generated.aio._service_bus_management_client_async import ServiceBusManagementClient \
2322
as ServiceBusManagementClientImpl
2423
from .. import _constants as constants
25-
from .._management_client import _convert_xml_to_object
24+
from .._management_client import _convert_xml_to_object, _handle_response_error
2625
from ._shared_key_policy_async import AsyncServiceBusSharedKeyCredentialPolicy
2726

2827

@@ -88,17 +87,39 @@ async def _get_queue_object(self, queue_name, clazz):
8887
# type: (str, Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
8988
if not queue_name:
9089
raise ValueError("queue_name must be a non-empty str")
91-
et = cast(
92-
ElementTree,
93-
await self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
94-
)
90+
with _handle_response_error():
91+
et = cast(
92+
ElementTree,
93+
await self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
94+
)
9595
return _convert_xml_to_object(queue_name, et, clazz)
9696

97+
async def _list_queues(self, skip, max_count, clazz):
98+
# type: (int, int, Type[Model]) -> Union[List[QueueDescription], List[QueueRuntimeInfo])
99+
with _handle_response_error():
100+
et = cast(
101+
ElementTree,
102+
await self._impl.list_entities(
103+
entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION
104+
)
105+
)
106+
entries = et.findall(constants.ENTRY_TAG)
107+
queues = []
108+
for entry in entries:
109+
entity_name = entry.find(constants.TITLE_TAG).text # type: ignore
110+
queue_description = _convert_xml_to_object(
111+
entity_name, # type: ignore
112+
cast(Element, entry),
113+
clazz
114+
)
115+
queues.append(queue_description)
116+
return queues
117+
97118
async def get_queue(self, queue_name):
98119
# type: (str) -> QueueDescription
99120
return await self._get_queue_object(queue_name, QueueDescription)
100121

101-
async def get_queue_metrics(self, queue_name):
122+
async def get_queue_runtime_info(self, queue_name):
102123
# type: (str) -> QueueRuntimeInfo
103124
return await self._get_queue_object(queue_name, QueueRuntimeInfo)
104125

@@ -120,10 +141,11 @@ async def create_queue(self, queue):
120141
)
121142
)
122143
request_body = create_entity_body.serialize(is_xml=True)
123-
et = cast(
124-
ElementTree,
125-
await self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
126-
)
144+
with _handle_response_error():
145+
et = cast(
146+
ElementTree,
147+
await self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
148+
)
127149
return _convert_xml_to_object(queue_name, et, QueueDescription)
128150

129151
async def update_queue(self, queue_description):
@@ -140,12 +162,13 @@ async def update_queue(self, queue_description):
140162
)
141163
)
142164
request_body = create_entity_body.serialize(is_xml=True)
143-
et = cast(
144-
ElementTree,
145-
await self._impl.queue.put(
146-
queue_description.queue_name, request_body, api_version=constants.API_VERSION, if_match="*"
165+
with _handle_response_error():
166+
et = cast(
167+
ElementTree,
168+
await self._impl.queue.put(
169+
queue_description.queue_name, request_body, api_version=constants.API_VERSION, if_match="*"
170+
)
147171
)
148-
)
149172
return _convert_xml_to_object(queue_description.queue_name, et, QueueDescription)
150173

151174
async def delete_queue(self, queue_name):
@@ -154,24 +177,13 @@ async def delete_queue(self, queue_name):
154177

155178
if not queue_name:
156179
raise ValueError("queue_name must not be None or empty")
157-
await self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
180+
with _handle_response_error():
181+
await self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
158182

159183
async def list_queues(self, skip=0, max_count=100):
160184
# type: (int, int) -> List[QueueDescription]
161-
et = cast(
162-
ElementTree,
163-
await self._impl.list_entities(
164-
entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION
165-
)
166-
)
167-
entries = et.findall(constants.ENTRY_TAG)
168-
queue_descriptions = []
169-
for entry in entries:
170-
entity_name = entry.find(constants.TITLE_TAG).text # type: ignore
171-
queue_description = _convert_xml_to_object(
172-
entity_name, # type: ignore
173-
cast(Element, entry),
174-
QueueDescription
175-
)
176-
queue_descriptions.append(queue_description)
177-
return queue_descriptions
185+
return await self._list_queues(skip, max_count, QueueDescription)
186+
187+
async def list_queues_runtime_info(self, skip=0, max_count=100):
188+
# type: (int, int) -> List[QueueRuntimeInfo]
189+
return await self._list_queues(skip, max_count, QueueRuntimeInfo)

0 commit comments

Comments
 (0)