From d1785ce67ef5b00f755800e3d7a6f674cd6e3d57 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Thu, 7 Jan 2021 12:03:54 -0600 Subject: [PATCH 1/5] Added support for AAD auth --- .../CHANGELOG.md | 5 ++-- .../README.md | 9 +++++++ .../_phone_number_administration_client.py | 5 ++-- ...hone_number_administration_client_async.py | 5 ++-- ...test_phone_number_administration_client.py | 24 +++++++++++++++++- ...hone_number_administration_client_async.py | 25 +++++++++++++++++++ 6 files changed, 64 insertions(+), 9 deletions(-) diff --git a/sdk/communication/azure-communication-administration/CHANGELOG.md b/sdk/communication/azure-communication-administration/CHANGELOG.md index 3ab815d73c1b..63bca17b0a86 100644 --- a/sdk/communication/azure-communication-administration/CHANGELOG.md +++ b/sdk/communication/azure-communication-administration/CHANGELOG.md @@ -24,9 +24,10 @@ Caller must provide one of the following: - Added `MicrosoftTeamsUserIdentifier` ##### `IdentityClient` -- Added support for Azure Active Directory authentication for the Identity client - +- Added support for Azure Active Directory authentication +#### `PhoneNumberAdministrationClient` +- Added support for Azure Active Directory authentication ## 1.0.0b3 (2020-11-16) ### Breaking Changes diff --git a/sdk/communication/azure-communication-administration/README.md b/sdk/communication/azure-communication-administration/README.md index 4e22a0500161..665ccf65f3a2 100644 --- a/sdk/communication/azure-communication-administration/README.md +++ b/sdk/communication/azure-communication-administration/README.md @@ -24,9 +24,18 @@ pip install azure-communication-administration # You can find your endpoint and access token from your resource in the Azure Portal import os from azure.communication.administration import PhoneNumberAdministrationClient +from azure.identity import DefaultAzureCredential connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING') +endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT') + +# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have +# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables. +phone_number_administration_client = PhoneNumberAdministrationClient(endpoint, DefaultAzureCredential()) + +# You can also authenticate using your connection string phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str) + ``` ### Phone plans overview diff --git a/sdk/communication/azure-communication-administration/azure/communication/administration/_phone_number_administration_client.py b/sdk/communication/azure-communication-administration/azure/communication/administration/_phone_number_administration_client.py index f34a07d5b0e4..d4ac20b2bab3 100644 --- a/sdk/communication/azure-communication-administration/azure/communication/administration/_phone_number_administration_client.py +++ b/sdk/communication/azure-communication-administration/azure/communication/administration/_phone_number_administration_client.py @@ -31,8 +31,7 @@ UpdatePhoneNumberCapabilitiesResponse ) -from ._shared.utils import parse_connection_str -from ._shared.policy import HMACCredentialsPolicy +from ._shared.utils import parse_connection_str, get_authentication_policy from ._version import SDK_MONIKER class PhoneNumberAdministrationClient(object): @@ -64,7 +63,7 @@ def __init__( self._endpoint = endpoint self._phone_number_administration_client = PhoneNumberAdministrationClientGen( self._endpoint, - authentication_policy=HMACCredentialsPolicy(endpoint, credential), + authentication_policy=get_authentication_policy(endpoint, credential), sdk_moniker=SDK_MONIKER, **kwargs) diff --git a/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py b/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py index dd18ec52f302..4f8f20b3f0af 100644 --- a/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py +++ b/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py @@ -38,8 +38,7 @@ UpdatePhoneNumberCapabilitiesResponse ) -from .._shared.utils import parse_connection_str -from .._shared.policy import HMACCredentialsPolicy +from .._shared.utils import parse_connection_str, get_authentication_policy class PhoneNumberAdministrationClient(object): """Azure Communication Services Phone Number Management client. @@ -70,7 +69,7 @@ def __init__( self._endpoint = endpoint self._phone_number_administration_client = PhoneNumberAdministrationClientGen( self._endpoint, - authentication_policy=HMACCredentialsPolicy(endpoint, credential), + authentication_policy=get_authentication_policy(endpoint, credential), sdk_moniker=SDK_MONIKER, **kwargs) diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py index 85823a26f162..4fd87b62562e 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py @@ -12,15 +12,23 @@ NumberUpdateCapabilities, CreateSearchOptions ) +from azure.core.credentials import AccessToken from phone_number_helper import PhoneNumberUriReplacer from phone_number_testcase import PhoneNumberCommunicationTestCase from _shared.testcase import BodyReplacerProcessor +from azure.identity import DefaultAzureCredential +from azure.communication.administration._shared.utils import parse_connection_str SKIP_PHONE_NUMBER_TESTS = True PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet" -class PhoneNumberAdministrationClientTest(PhoneNumberCommunicationTestCase): +class FakeTokenCredential(object): + def __init__(self): + self.token = AccessToken("Fake Token", 0) + def get_token(self, *args): + return self.token +class PhoneNumberAdministrationClientTest(PhoneNumberCommunicationTestCase): def setUp(self): super(PhoneNumberAdministrationClientTest, self).setUp() self.recording_processors.extend([ @@ -125,6 +133,20 @@ def setUp(self): self.capabilities_id = "capabilities_id" self.release_id = "release_id" + @pytest.mark.live_test_only + @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) + def test_list_all_phone_numbers_from_managed_identity(self, connection_string): + endpoint, access_key = parse_connection_str(connection_string) + from devtools_testutils import is_live + if not is_live(): + credential = FakeTokenCredential() + else: + credential = DefaultAzureCredential() + phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) + user = identity_client.create_user() + + assert user.identifier is not None + @pytest.mark.live_test_only @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) def test_list_all_phone_numbers(self): diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py index 43802c4ca8e0..eca537e48f57 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py @@ -5,7 +5,9 @@ # license information. # -------------------------------------------------------------------------- import pytest +from azure.core.credentials import AccessToken from azure.communication.administration.aio import PhoneNumberAdministrationClient +from azure.communication.administration._shared.utils import parse_connection_str from azure.communication.administration import ( PstnConfiguration, NumberUpdateCapabilities, @@ -14,11 +16,18 @@ from phone_number_helper import PhoneNumberUriReplacer from phone_number_testcase_async import AsyncPhoneNumberCommunicationTestCase from _shared.testcase import BodyReplacerProcessor, ResponseReplacerProcessor +from azure.identity import DefaultAzureCredential import os SKIP_PHONE_NUMBER_TESTS = True PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet" +class FakeTokenCredential(object): + def __init__(self): + self.token = AccessToken("Fake Token", 0) + + def get_token(self, *args): + return self.token class PhoneNumberAdministrationClientTestAsync(AsyncPhoneNumberCommunicationTestCase): def setUp(self): @@ -126,6 +135,22 @@ def setUp(self): self.capabilities_id = "capabilities_id" self.release_id = "release_id" + @AsyncPhoneNumberCommunicationTestCase.await_prepared_test + @pytest.mark.live_test_only + @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) + async def test_create_user_from_managed_identity(self, connection_string): + endpoint, access_key = parse_connection_str(connection_string) + from devtools_testutils import is_live + if not is_live(): + credential = FakeTokenCredential() + else: + credential = DefaultAzureCredential() + identity_client = CommunicationIdentityClient(endpoint, credential) + async with identity_client: + user = await identity_client.create_user() + + assert user.identifier is not None + @AsyncPhoneNumberCommunicationTestCase.await_prepared_test @pytest.mark.live_test_only @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) From 45ffafb29fcfc7a0e320dd6241187005c8e2c5f7 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Tue, 26 Jan 2021 12:59:00 -0600 Subject: [PATCH 2/5] added successful test recording --- ...hone_number_administration_client_async.py | 2 +- ...l_phone_numbers_from_managed_identity.yaml | 34 +++++++++++++++++++ ...l_phone_numbers_from_managed_identity.yaml | 25 ++++++++++++++ ...test_phone_number_administration_client.py | 9 +++-- ...hone_number_administration_client_async.py | 16 +++++---- 5 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml create mode 100644 sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml diff --git a/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py b/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py index 4f8f20b3f0af..86939133891b 100644 --- a/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py +++ b/sdk/communication/azure-communication-administration/azure/communication/administration/aio/_phone_number_administration_client_async.py @@ -69,7 +69,7 @@ def __init__( self._endpoint = endpoint self._phone_number_administration_client = PhoneNumberAdministrationClientGen( self._endpoint, - authentication_policy=get_authentication_policy(endpoint, credential), + authentication_policy=get_authentication_policy(endpoint, credential, is_async=True), sdk_moniker=SDK_MONIKER, **kwargs) diff --git a/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml new file mode 100644 index 000000000000..7a3f1dbb56cc --- /dev/null +++ b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml @@ -0,0 +1,34 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-communication-administration/1.0.0b4 Python/3.8.5 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://sanitized.communication.azure.com/administration/phonenumbers/phonenumbers?locale=en-US&skip=0&take=100&api-version=2020-07-20-preview1 + response: + body: + string: '{"phoneNumbers": "sanitized", "nextLink": null}' + headers: + content-type: + - application/json; charset=utf-8 + date: + - Tue, 26 Jan 2021 18:56:12 GMT + ms-cv: + - fqvIVn7QTU2rhudQ0Xygaw.0 + request-context: + - appId= + transfer-encoding: + - chunked + x-processing-time: + - 824ms + status: + code: 200 + message: OK +version: 1 diff --git a/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml new file mode 100644 index 000000000000..1fba205218af --- /dev/null +++ b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml @@ -0,0 +1,25 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/json + User-Agent: + - azsdk-python-communication-administration/1.0.0b4 Python/3.8.5 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://sanitized.communication.azure.com/administration/phonenumbers/phonenumbers?locale=en-US&skip=0&take=100&api-version=2020-07-20-preview1 + response: + body: + string: '{"phoneNumbers": "sanitized", "nextLink": null}' + headers: + content-type: application/json; charset=utf-8 + date: Tue, 26 Jan 2021 19:56:21 GMT + ms-cv: zqvTNGtq40i4vqZgCLIwLQ.0 + request-context: appId= + transfer-encoding: chunked + x-processing-time: 1732ms + status: + code: 200 + message: OK + url: https://sanitized.communication.azure.com/administration/phonenumbers/phonenumbers?locale=en-US&skip=0&take=100&api-version=2020-07-20-preview1 +version: 1 diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py index 4fd87b62562e..30b3480ec193 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py @@ -135,17 +135,16 @@ def setUp(self): @pytest.mark.live_test_only @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) - def test_list_all_phone_numbers_from_managed_identity(self, connection_string): - endpoint, access_key = parse_connection_str(connection_string) + def test_list_all_phone_numbers_from_managed_identity(self): + endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) - user = identity_client.create_user() - - assert user.identifier is not None + pages = phone_number_client.list_all_phone_numbers() + assert pages.next() @pytest.mark.live_test_only @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py index eca537e48f57..09e60b2ad48d 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py @@ -138,18 +138,20 @@ def setUp(self): @AsyncPhoneNumberCommunicationTestCase.await_prepared_test @pytest.mark.live_test_only @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) - async def test_create_user_from_managed_identity(self, connection_string): - endpoint, access_key = parse_connection_str(connection_string) + async def test_list_all_phone_numbers_from_managed_identity(self): + endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() - identity_client = CommunicationIdentityClient(endpoint, credential) - async with identity_client: - user = await identity_client.create_user() - - assert user.identifier is not None + phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) + async with phone_number_client: + pages = phone_number_client.list_all_phone_numbers() + items = [] + async for item in pages: + items.append(item) + assert len(items) > 0 @AsyncPhoneNumberCommunicationTestCase.await_prepared_test @pytest.mark.live_test_only From b10b68c6d51ac22186c5d21903bf56215fa89a3b Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Thu, 28 Jan 2021 12:10:05 -0600 Subject: [PATCH 3/5] Addressed comments --- .../tests/_shared/fake_token_credential.py | 13 +++++++++++++ .../tests/_shared/utils.py | 14 ++++++++++++++ ...t_all_phone_numbers_from_managed_identity.yaml | 6 +++--- ...t_all_phone_numbers_from_managed_identity.yaml | 6 +++--- .../test_phone_number_administration_client.py | 14 ++------------ ...st_phone_number_administration_client_async.py | 15 ++------------- 6 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py create mode 100644 sdk/communication/azure-communication-administration/tests/_shared/utils.py diff --git a/sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py b/sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py new file mode 100644 index 000000000000..b4fc2e6eaccf --- /dev/null +++ b/sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py @@ -0,0 +1,13 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +from azure.core.credentials import AccessToken + +class FakeTokenCredential(object): + def __init__(self): + self.token = AccessToken("Fake Token", 0) + + def get_token(self, *args): + return self.token \ No newline at end of file diff --git a/sdk/communication/azure-communication-administration/tests/_shared/utils.py b/sdk/communication/azure-communication-administration/tests/_shared/utils.py new file mode 100644 index 000000000000..4fcf96d313ff --- /dev/null +++ b/sdk/communication/azure-communication-administration/tests/_shared/utils.py @@ -0,0 +1,14 @@ +# ------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ------------------------------------------------------------------------- +from .fake_token_credential import FakeTokenCredential +from azure.identity import DefaultAzureCredential + +def create_token_credential(): + # type: () -> FakeTokenCredential or DefaultAzureCredential + from devtools_testutils import is_live + if not is_live(): + return FakeTokenCredential() + return DefaultAzureCredential() diff --git a/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml index 7a3f1dbb56cc..fbfeb1fcc4de 100644 --- a/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml +++ b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client.test_list_all_phone_numbers_from_managed_identity.yaml @@ -19,15 +19,15 @@ interactions: content-type: - application/json; charset=utf-8 date: - - Tue, 26 Jan 2021 18:56:12 GMT + - Thu, 28 Jan 2021 18:48:48 GMT ms-cv: - - fqvIVn7QTU2rhudQ0Xygaw.0 + - /rMw3sZH40i74G71li+lDA.0 request-context: - appId= transfer-encoding: - chunked x-processing-time: - - 824ms + - 1124ms status: code: 200 message: OK diff --git a/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml index 1fba205218af..4ad1a112d86c 100644 --- a/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml +++ b/sdk/communication/azure-communication-administration/tests/recordings/test_phone_number_administration_client_async.test_list_all_phone_numbers_from_managed_identity.yaml @@ -13,11 +13,11 @@ interactions: string: '{"phoneNumbers": "sanitized", "nextLink": null}' headers: content-type: application/json; charset=utf-8 - date: Tue, 26 Jan 2021 19:56:21 GMT - ms-cv: zqvTNGtq40i4vqZgCLIwLQ.0 + date: Thu, 28 Jan 2021 18:49:47 GMT + ms-cv: jqpEP/JsFUKDau9Y0tMhbw.0 request-context: appId= transfer-encoding: chunked - x-processing-time: 1732ms + x-processing-time: 765ms status: code: 200 message: OK diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py index 30b3480ec193..449a70993642 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py @@ -16,18 +16,12 @@ from phone_number_helper import PhoneNumberUriReplacer from phone_number_testcase import PhoneNumberCommunicationTestCase from _shared.testcase import BodyReplacerProcessor -from azure.identity import DefaultAzureCredential +from _shared.utils import create_token_credential from azure.communication.administration._shared.utils import parse_connection_str SKIP_PHONE_NUMBER_TESTS = True PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet" -class FakeTokenCredential(object): - def __init__(self): - self.token = AccessToken("Fake Token", 0) - - def get_token(self, *args): - return self.token class PhoneNumberAdministrationClientTest(PhoneNumberCommunicationTestCase): def setUp(self): super(PhoneNumberAdministrationClientTest, self).setUp() @@ -137,11 +131,7 @@ def setUp(self): @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) def test_list_all_phone_numbers_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) - from devtools_testutils import is_live - if not is_live(): - credential = FakeTokenCredential() - else: - credential = DefaultAzureCredential() + credential = create_token_credential() phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) pages = phone_number_client.list_all_phone_numbers() assert pages.next() diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py index 09e60b2ad48d..dccf1ae64fc9 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py @@ -5,7 +5,6 @@ # license information. # -------------------------------------------------------------------------- import pytest -from azure.core.credentials import AccessToken from azure.communication.administration.aio import PhoneNumberAdministrationClient from azure.communication.administration._shared.utils import parse_connection_str from azure.communication.administration import ( @@ -16,18 +15,12 @@ from phone_number_helper import PhoneNumberUriReplacer from phone_number_testcase_async import AsyncPhoneNumberCommunicationTestCase from _shared.testcase import BodyReplacerProcessor, ResponseReplacerProcessor -from azure.identity import DefaultAzureCredential +from _shared.utils import create_token_credential import os SKIP_PHONE_NUMBER_TESTS = True PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet" -class FakeTokenCredential(object): - def __init__(self): - self.token = AccessToken("Fake Token", 0) - - def get_token(self, *args): - return self.token class PhoneNumberAdministrationClientTestAsync(AsyncPhoneNumberCommunicationTestCase): def setUp(self): @@ -140,11 +133,7 @@ def setUp(self): @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) async def test_list_all_phone_numbers_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) - from devtools_testutils import is_live - if not is_live(): - credential = FakeTokenCredential() - else: - credential = DefaultAzureCredential() + credential = create_token_credential() phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) async with phone_number_client: pages = phone_number_client.list_all_phone_numbers() From 57f878c9d48f109c9ac6a764ca1b8e4fbe446b01 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Tue, 2 Feb 2021 11:48:41 -0600 Subject: [PATCH 4/5] Edited readme --- .../azure-communication-administration/README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sdk/communication/azure-communication-administration/README.md b/sdk/communication/azure-communication-administration/README.md index 665ccf65f3a2..10bc2d9c65be 100644 --- a/sdk/communication/azure-communication-administration/README.md +++ b/sdk/communication/azure-communication-administration/README.md @@ -26,17 +26,26 @@ import os from azure.communication.administration import PhoneNumberAdministrationClient from azure.identity import DefaultAzureCredential -connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING') endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT') -# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have +# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have your # AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables. phone_number_administration_client = PhoneNumberAdministrationClient(endpoint, DefaultAzureCredential()) -# You can also authenticate using your connection string +``` +### Initializing Phone Number Client Using Connection String +Connection string authentication is also available for Phone Number Client. + +```python +# You can find your endpoint and access token from your resource in the Azure Portal +import os +from azure.communication.administration import PhoneNumberAdministrationClient + +connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING') phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str) ``` + ### Phone plans overview Phone plans come in two types; Geographic and Toll-Free. Geographic phone plans are phone plans associated with a location, whose phone numbers' area codes are associated with the area code of a geographic location. Toll-Free phone plans are phone plans not associated location. For example, in the US, toll-free numbers can come with area codes such as 800 or 888. From 5913854e64554cd5e5f382790e2275a6f23ce1cb Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Wed, 3 Feb 2021 10:32:46 -0600 Subject: [PATCH 5/5] Fixed README --- sdk/communication/azure-communication-administration/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/communication/azure-communication-administration/README.md b/sdk/communication/azure-communication-administration/README.md index 10bc2d9c65be..9b9ca27c8d5f 100644 --- a/sdk/communication/azure-communication-administration/README.md +++ b/sdk/communication/azure-communication-administration/README.md @@ -43,7 +43,6 @@ from azure.communication.administration import PhoneNumberAdministrationClient connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING') phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str) - ``` ### Phone plans overview