diff --git a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py index bccb6ce98c95..30c2fe492511 100644 --- a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py +++ b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py @@ -206,9 +206,10 @@ def set_trunks( if x.fqdn not in [o.fqdn for o in trunks]: config.trunks[x.fqdn] = None - self._rest_service.patch_sip_configuration( - body=config, **kwargs - ) + if len(config.trunks) > 0: + self._rest_service.patch_sip_configuration( + body=config, **kwargs + ) @distributed_trace def set_routes( diff --git a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py index de813269457d..ffc1acd1d731 100644 --- a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py +++ b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py @@ -210,9 +210,10 @@ async def set_trunks( if x.fqdn not in [o.fqdn for o in trunks]: config.trunks[x.fqdn] = None - await self._rest_service.patch_sip_configuration( - body=config, **kwargs - ) + if len(config.trunks) > 0: + await self._rest_service.patch_sip_configuration( + body=config, **kwargs + ) @distributed_trace_async async def set_routes( diff --git a/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json new file mode 100644 index 000000000000..d1f95b38ad69 --- /dev/null +++ b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json @@ -0,0 +1,65 @@ +{ + "Entries": [ + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "Connection": "keep-alive", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "182ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + }, + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "Connection": "keep-alive", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "279ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + } + ], + "Variables": {} +} diff --git a/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json new file mode 100644 index 000000000000..2ea199e26dcf --- /dev/null +++ b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json @@ -0,0 +1,65 @@ +{ + "Entries": [ + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "Content-Length": "0", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "270ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + }, + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "Content-Length": "0", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "76ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + } + ], + "Variables": {} +} diff --git a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py index f2ea6a947b52..2e7276a774d8 100644 --- a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py +++ b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py @@ -3,7 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- - +from azure.core.exceptions import HttpResponseError from phone_numbers_testcase import PhoneNumbersTestCase from _shared.utils import create_token_credential, get_http_logging_policy from sip_routing_helper import get_user_domain, assert_trunks_are_equal, assert_routes_are_equal @@ -79,6 +79,16 @@ def test_set_trunks_from_managed_identity(self, **kwargs): assert result_trunks is not None, "No trunks were returned." assert_trunks_are_equal(result_trunks,[self.additional_trunk]) + @recorded_by_proxy + def test_set_trunks_empty_list(self, **kwargs): + """Verification of bug fix. SDK shouldn't send empty PATCH, otherwise it will receive exception. + This situation occurs, when sending empty trunks list to already empty trunk configuration.""" + try: + self._sip_routing_client.set_trunks([]) + self._sip_routing_client.set_trunks([]) + except HttpResponseError as exception: + assert False, "Trying to set empty trunks list returned Http error: " + str(exception.status_code) + ", message: " + exception.message + @recorded_by_proxy def test_set_routes(self, **kwargs): self._prepare_test() diff --git a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py index 84cc0c70174a..5764edf79f90 100644 --- a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py +++ b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py @@ -3,8 +3,8 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- - import pytest +from azure.core.exceptions import HttpResponseError from phone_numbers_testcase import PhoneNumbersTestCase from devtools_testutils.aio import recorded_by_proxy_async from _shared.utils import async_create_token_credential, get_http_logging_policy @@ -88,6 +88,17 @@ async def test_set_trunks_from_managed_identity(self): result_trunks = await self._sip_routing_client.get_trunks() assert result_trunks is not None, "No trunks were returned." assert_trunks_are_equal(result_trunks,[self.additional_trunk]), "Trunks are not equal." + + @recorded_by_proxy_async + async def test_set_trunks_empty_list(self): + """Verification of bug fix. SDK shouldn't send empty PATCH, otherwise it will receive exception. + This situation occurs, when sending empty trunks list to already empty trunk configuration.""" + async with self._sip_routing_client: + try: + await self._sip_routing_client.set_trunks([]) + await self._sip_routing_client.set_trunks([]) + except HttpResponseError as exception: + assert False, "Trying to set empty trunks list returned Http error: " + str(exception.status_code) + ", message: " + exception.message @recorded_by_proxy_async async def test_set_routes(self):