-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure CLI commands for communication (#4206)
* Azure CLI commands for communication * Resolving lint and style errors * Lint errors fix * More descriptive about the communication commands * Adding tests for communication extension * Made changes for sanatization * Incorporated review comments * Azure CLI commands for communication * Resolving lint and style errors * Lint errors fix * More descriptive about the communication commands * Adding tests for communication extension * Made changes for sanatization * Incorporated review comments * Removing hard coded ACS resource id * Made changes to run the sample in playback mode * Change azure_devtools with azure.cli.testsdk imports, as per latest azure-cli changes * Add communication resource preparer in communication extension * Add license information in preparers.py * Updated the version and added rease history. Updated the Readme file. Co-authored-by: maulinasharma <[email protected]> Co-authored-by: Mohammad Irfan <[email protected]> Co-authored-by: maulinasharma <[email protected]> Co-authored-by: Mohammad Irfan <[email protected]>
- Loading branch information
1 parent
6ac7b05
commit 72de9b7
Showing
21 changed files
with
1,332 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/communication/azext_communication/manual/_client_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def cf_communication_identity(cli_ctx, kwargs): | ||
from azure.communication.identity import CommunicationIdentityClient | ||
connection_string = kwargs.pop('connection_string', None) | ||
client = CommunicationIdentityClient.from_connection_string(connection_string) | ||
return client | ||
|
||
|
||
def cf_communication_sms(cli_ctx, kwargs): | ||
from azure.communication.sms import SmsClient | ||
connection_string = kwargs.pop('connection_string', None) | ||
client = SmsClient.from_connection_string(connection_string) | ||
return client | ||
|
||
|
||
def cf_communication_phonenumbers(cli_ctx, kwargs): | ||
from azure.communication.phonenumbers import PhoneNumbersClient | ||
connection_string = kwargs.pop('connection_string', None) | ||
client = PhoneNumbersClient.from_connection_string(connection_string) | ||
return client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/communication/azext_communication/manual/_validators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
|
||
def get_config_value(cmd, section, key, default): | ||
return cmd.cli_ctx.config.get(section, key, default) | ||
|
||
|
||
def validate_client_parameters(cmd, namespace): | ||
""" Retrieves communication connection parameters from environment variables | ||
and parses out connection string into account name and key """ | ||
n = namespace | ||
|
||
if not n.connection_string: | ||
n.connection_string = get_config_value(cmd, 'communication', 'connection_string', None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
# pylint: disable=line-too-long | ||
from azext_communication.manual._client_factory import cf_communication_identity | ||
from azext_communication.manual._client_factory import cf_communication_sms | ||
from azext_communication.manual._client_factory import cf_communication_phonenumbers | ||
|
||
|
||
def load_command_table(self, _): | ||
|
||
with self.command_group('communication identity', client_factory=cf_communication_identity) as g: | ||
g.communication_custom_command('issue-access-token', "issue_access_token", client_factory=cf_communication_identity) | ||
|
||
with self.command_group('communication sms', client_factory=cf_communication_sms) as g: | ||
g.communication_custom_command('send-sms', 'communication_send_sms') | ||
|
||
with self.command_group('communication phonenumbers', client_factory=cf_communication_phonenumbers) as g: | ||
g.communication_custom_command('list-phonenumbers', 'communication_list_phonenumbers') | ||
g.communication_custom_command('show-phonenumber', 'communication_show_phonenumber') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
|
||
from azure.communication.identity import CommunicationUserIdentifier | ||
|
||
|
||
def issue_access_token(client, scopes, userid=None): | ||
user_token_data = {"user_id": userid, "token": "", "expires_on": ""} | ||
if userid is not None: | ||
user = CommunicationUserIdentifier(userid) | ||
token_data = client.get_token(user, scopes) | ||
user_token_data["token"] = token_data.token | ||
user_token_data["expires_on"] = str(token_data.expires_on) | ||
else: | ||
identity_token_result = client.create_user_and_token(scopes) | ||
if len(identity_token_result) >= 2: | ||
user_token_data["user_id"] = identity_token_result[0].properties['id'] | ||
user_token_data["token"] = identity_token_result[1].token | ||
user_token_data["expires_on"] = str(identity_token_result[1].expires_on) | ||
|
||
return user_token_data | ||
|
||
|
||
def communication_send_sms(client, sender, recipient, message): | ||
return client.send(from_=sender, to=recipient, message=message) | ||
|
||
|
||
def communication_list_phonenumbers(client): | ||
return client.list_purchased_phone_numbers() | ||
|
||
|
||
def communication_show_phonenumber(client, phonenumber): | ||
return client.get_purchased_phone_number(phonenumber) |
66 changes: 66 additions & 0 deletions
66
src/communication/azext_communication/tests/latest/preparers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# | ||
# Code generated by Microsoft (R) AutoRest Code Generator. | ||
# Changes may cause incorrect behavior and will be lost if the code is | ||
# regenerated. | ||
# -------------------------------------------------------------------------- | ||
|
||
from azure.cli.testsdk.scenario_tests import SingleValueReplacer | ||
from azure.cli.testsdk.preparers import NoTrafficRecordingPreparer, ResourceGroupPreparer | ||
from azure.cli.testsdk.exceptions import CliTestError | ||
from azure.cli.testsdk.reverse_dependency import get_dummy_cli | ||
import os | ||
|
||
# Communication resource Preparer and its shorthand decorator | ||
# pylint: disable=too-many-instance-attributes | ||
class CommunicationResourcePreparer(NoTrafficRecordingPreparer, SingleValueReplacer): | ||
def __init__(self, name_prefix='clitest', location='Global', data_location='United States', length=24, | ||
parameter_name='communication_resource', resource_group_parameter_name='resource_group', skip_delete=True, | ||
dev_setting_name='AZURE_CLI_TEST_DEV_COMMUNICATION_RESOURCE_NAME', key='cr'): | ||
super(CommunicationResourcePreparer, self).__init__(name_prefix, length) | ||
self.cli_ctx = get_dummy_cli() | ||
self.location = location | ||
self.data_location = data_location | ||
self.resource_group_parameter_name = resource_group_parameter_name | ||
self.skip_delete = skip_delete | ||
self.parameter_name = parameter_name | ||
self.key = key | ||
self.dev_setting_name = os.environ.get(dev_setting_name, None) | ||
|
||
def create_resource(self, name, **kwargs): | ||
group = self._get_resource_group(**kwargs) | ||
|
||
if not self.dev_setting_name: | ||
|
||
template = 'az communication create --name {} --location {} --data-location "{}" --resource-group {} ' | ||
self.live_only_execute(self.cli_ctx, template.format( | ||
name, self.location, self.data_location, group)) | ||
else: | ||
name = self.dev_setting_name | ||
|
||
try: | ||
account_key = self.live_only_execute(self.cli_ctx, | ||
'az communication list-key --name {} --resource-group {} --query "primaryConnectionString" -otsv' | ||
.format(name, group)).output.strip() | ||
except AttributeError: # live only execute returns None if playing from record | ||
account_key = None | ||
|
||
self.test_class_instance.kwargs[self.key] = name | ||
return {self.parameter_name: name, | ||
self.parameter_name + '_info': (name, account_key or 'endpoint=https://sanitized.communication.azure.com/;accesskey=fake===')} | ||
|
||
def remove_resource(self, name, **kwargs): | ||
if not self.skip_delete and not self.dev_setting_name: | ||
group = self._get_resource_group(**kwargs) | ||
self.live_only_execute(self.cli_ctx, 'az communication delete --name {} --resource-group {} --yes'.format(name, group)) | ||
|
||
def _get_resource_group(self, **kwargs): | ||
try: | ||
return kwargs.get(self.resource_group_parameter_name) | ||
except KeyError: | ||
template = 'To create a communication resource a resource group is required. Please add ' \ | ||
'decorator @{} in front of this communication resource preparer.' | ||
raise CliTestError(template.format(ResourceGroupPreparer.__name__)) |
Oops, something went wrong.