Skip to content

Commit

Permalink
Add Azure Communication Service sample (#5)
Browse files Browse the repository at this point in the history
* Add initial project

* Add draft README

* Use utilities

* Make simpler to match other samples

* Update README

* Remove unused CommandLine package

* Remove unused function

* Update READMEs with PR feedback
  • Loading branch information
memontic-ms authored Sep 26, 2020
1 parent 82db6ca commit 432602f
Show file tree
Hide file tree
Showing 7 changed files with 910 additions and 460 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interested in. Each folder name is based on the Azure service, eg. Compute / Net
### Samples:

- [App Configuration](https://github.com/Azure-Samples/azure-samples-net-management/tree/master/samples/appconfiguration)
- [Communication](https://github.com/Azure-Samples/azure-samples-net-management/tree/master/samples/communication)
- [Compute](https://github.com/Azure-Samples/azure-samples-net-management/tree/master/samples/compute)
- [Event Hub](https://github.com/Azure-Samples/azure-samples-net-management/tree/master/samples/eventhub)
- [Key Vault](https://github.com/Azure-Samples/azure-samples-net-management/tree/master/samples/keyvault/manage-key-vault)
Expand Down
12 changes: 12 additions & 0 deletions samples/Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CreateStorageSample", "stor
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CreateAppConfigurationSample", "appconfiguration\create-app-configuration\CreateAppConfigurationSample.csproj", "{41A79A15-3552-4BB3-826D-1B3C89F83609}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "communication", "communication", "{FDDBE618-720B-4676-8CA7-441CA0DC18FF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "manage-communication", "manage-communication", "{40A26974-0915-4CFC-A04C-4F7A44A8EE55}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ManageCommunication", "communication\manage-communication\ManageCommunication.csproj", "{920BDCC4-62AE-4156-964F-43C67CC84C3B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -151,6 +157,10 @@ Global
{41A79A15-3552-4BB3-826D-1B3C89F83609}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41A79A15-3552-4BB3-826D-1B3C89F83609}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41A79A15-3552-4BB3-826D-1B3C89F83609}.Release|Any CPU.Build.0 = Release|Any CPU
{920BDCC4-62AE-4156-964F-43C67CC84C3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{920BDCC4-62AE-4156-964F-43C67CC84C3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{920BDCC4-62AE-4156-964F-43C67CC84C3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{920BDCC4-62AE-4156-964F-43C67CC84C3B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -187,6 +197,8 @@ Global
{0DB07A48-919C-4939-87E4-EA13A2DBB5E0} = {D8B0A9A1-3FBF-49C5-B218-8C71A6928A10}
{DB85FCDE-06DF-4563-9BE9-4B9988B8B29D} = {E3936643-EE14-4588-9986-47884419CE2C}
{41A79A15-3552-4BB3-826D-1B3C89F83609} = {0DB07A48-919C-4939-87E4-EA13A2DBB5E0}
{40A26974-0915-4CFC-A04C-4F7A44A8EE55} = {FDDBE618-720B-4676-8CA7-441CA0DC18FF}
{920BDCC4-62AE-4156-964F-43C67CC84C3B} = {40A26974-0915-4CFC-A04C-4F7A44A8EE55}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F80A83A1-2D7B-4467-BE1F-E61821187505}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.2.2" />
<PackageReference Include="Azure.ResourceManager.Communication" Version="1.0.0-beta.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\utilities\Utilities.csproj" />
</ItemGroup>

</Project>
250 changes: 250 additions & 0 deletions samples/communication/manage-communication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager.Communication;
using Azure.ResourceManager.Communication.Models;
using Samples.Utilities;

namespace ManageCommunication
{
public class Program
{
public static async Task Main(string[] args)
{
try
{
// Authenticate
var credential = new DefaultAzureCredential();

await RunSample(credential);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}

public static async Task RunSample(DefaultAzureCredential credential)
{
String resourceGroupName = Utilities.RandomResourceName("rg-manage-comm-", 24);
String resourceName = Utilities.RandomResourceName("manage-comm-", 24);
String region = "westus";

CommunicationManagementClient acsClient = CreateCommunicationManagementClient(credential);
await ResourceGroupHelper.CreateOrUpdateResourceGroup(resourceGroupName, region);

await CreateCommunicationServiceAsync(acsClient, resourceGroupName, resourceName);
await GetCommunicationServiceAsync(acsClient, resourceGroupName, resourceName);
await UpdateCommunicationServiceAsync(acsClient, resourceGroupName, resourceName);

ListCommunicationServiceBySubscription(acsClient);
ListCommunicationServiceByResourceGroup(acsClient, resourceGroupName);

await ListKeysAsync(acsClient, resourceGroupName, resourceName);
await RegenerateKeyAsync(acsClient, resourceGroupName, resourceName, KeyType.Primary);
await RegenerateKeyAsync(acsClient, resourceGroupName, resourceName, KeyType.Secondary);

await LinkNotificationHubAsync(acsClient, resourceGroupName, resourceName);

await DeleteCommunicationServiceAsync(acsClient, resourceGroupName, resourceName);
}

private static CommunicationManagementClient CreateCommunicationManagementClient(TokenCredential tokenCredential)
{
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
return new CommunicationManagementClient(subscriptionId, tokenCredential);
}

private static async Task CreateCommunicationServiceAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName)
{
try
{
Utilities.Log("\nCommunicationService Create...");

// Set up a CommunicationServiceResource with attributes of the resource we intend to create
var resource = new CommunicationServiceResource { Location = "global", DataLocation = "UnitedStates" };

// Create a resource in the specificed resource group and waits for a response
Utilities.Log("Waiting for acsClient.CommunicationService.StartCreateOrUpdateAsync");
var operation = await acsClient.CommunicationService.StartCreateOrUpdateAsync(resourceGroupName, resourceName, resource);

Utilities.Log("Gained the CommunicationServiceCreateOrUpdateOperation. Waiting for it to complete...");
Response<CommunicationServiceResource> response = await operation.WaitForCompletionAsync();
Utilities.Log("\tresponse: " + response.ToString());
Utilities.Print(response.Value);
}
catch (Exception e)
{
Utilities.Log("CreateCommunicationServiceAsync encountered: " + e.Message);
}
}

private static async Task GetCommunicationServiceAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName)
{
try
{
Utilities.Log("\nCommunicationService Fetch...");

// Fetch a previously created CommunicationServiceResource
Utilities.Log("Waiting for acsClient.CommunicationService.StartCreateOrUpdateAsync");
Response<CommunicationServiceResource> response = await acsClient.CommunicationService.GetAsync(resourceGroupName, resourceName);
Utilities.Log("\tresponse: " + response.ToString());
Utilities.Print(response.Value);
}
catch (Exception e)
{
Utilities.Log("GetCommunicationServiceAsync encountered: " + e.Message);
}
}

private static async Task UpdateCommunicationServiceAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName)
{
try
{
Utilities.Log("\nCommunicationService Update...");

// Create a CommunicationServiceResource with the updated resource attributes
var resource = new CommunicationServiceResource { Location = "global", DataLocation = "UnitedStates" };

var tags = new Dictionary<string, string>();
tags.Add("ExampleTagName1", "ExampleTagValue1");
tags.Add("ExampleTagName2", "ExampleTagValue2");

// Update an existing resource in Azure with the attributes in `resource` and wait for a response
Utilities.Log("Waiting for acsClient.CommunicationService.StartCreateOrUpdateAsync");
CommunicationServiceCreateOrUpdateOperation operation = await acsClient.CommunicationService.StartCreateOrUpdateAsync(resourceGroupName, resourceName, resource);

Utilities.Log("Gained the communicationServiceCreateOrUpdateOperation. Waiting for it to complete...");
Response<CommunicationServiceResource> response = await operation.WaitForCompletionAsync();
Utilities.Log("\tresponse: " + response.ToString());
Utilities.Print(response.Value);
}
catch (Exception e)
{
Utilities.Log("UpdateCommunicationServiceAsync encountered: " + e.Message);
}
}

private static async Task DeleteCommunicationServiceAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName)
{
try
{
Utilities.Log("\nCommunicationService Delete...");

CommunicationServiceDeleteOperation operation = await acsClient.CommunicationService.StartDeleteAsync(resourceGroupName, resourceName);

Utilities.Log("Gained the CommunicationServiceDeleteOperation. Waiting for it to complete...");
Response<Response> response = await operation.WaitForCompletionAsync();
Utilities.Log("\tresponse: " + response.ToString());
}
catch (Exception e)
{
Utilities.Log("DeleteCommunicationServiceAsync encountered: " + e.Message);
}
}

private static void ListCommunicationServiceBySubscription(CommunicationManagementClient acsClient)
{
try
{
Utilities.Log("\nCommunicationService List by Subscription...");

// Fetch all Azure Communication Services resources in the subscription
var resources = acsClient.CommunicationService.ListBySubscription();
Utilities.Log("Found number of resources: " + resources.ToArray().Length);

foreach (var resource in resources)
{
Utilities.Print(resource);
}
}
catch (Exception e)
{
Utilities.Log("ListCommunicationServiceBySubscription encountered: " + e.Message);
}
}

private static void ListCommunicationServiceByResourceGroup(CommunicationManagementClient acsClient, string resourceGroupName)
{
try
{
Utilities.Log("\nCommunicationService List by Resource Group...");

var resources = acsClient.CommunicationService.ListByResourceGroup(resourceGroupName);
Utilities.Log("Found number of resources: " + resources.ToArray().Length);
foreach (var resource in resources)
{
Utilities.Print(resource);
}
}
catch (Exception e)
{
Utilities.Log("ListCommunicationServiceByResourceGroup encountered: " + e.Message);
}
}

private static async Task ListKeysAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName)
{
try
{
Utilities.Log("\nCommunicationService List Keys...");

Response<CommunicationServiceKeys> response = await acsClient.CommunicationService.ListKeysAsync(resourceGroupName, resourceName);
Utilities.Log("PrimaryKey: " + response.Value.PrimaryKey);
Utilities.Log("SecondaryKey: " + response.Value.SecondaryKey);
Utilities.Log("PrimaryConnectionString: " + response.Value.PrimaryConnectionString);
Utilities.Log("SecondaryConnectionString: " + response.Value.SecondaryConnectionString);
}
catch (Exception e)
{
Utilities.Log("ListKeysAsync encountered: " + e.Message);
}
}

private static async Task RegenerateKeyAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName, KeyType type)
{
try
{
Utilities.Log("\nCommunicationService Regenerate Keys...");

var keyTypeParameters = new RegenerateKeyParameters();
keyTypeParameters.KeyType = type;

Response<CommunicationServiceKeys> response = await acsClient.CommunicationService.RegenerateKeyAsync(resourceGroupName, resourceName, keyTypeParameters);
Utilities.Log("PrimaryKey: " + response.Value.PrimaryKey);
Utilities.Log("SecondaryKey: " + response.Value.SecondaryKey);
Utilities.Log("PrimaryConnectionString: " + response.Value.PrimaryConnectionString);
Utilities.Log("SecondaryConnectionString: " + response.Value.SecondaryConnectionString);
}
catch (Exception e)
{
Utilities.Log("RegenerateKeyAsync encountered: " + e.Message);
}
}

private static async Task LinkNotificationHubAsync(CommunicationManagementClient acsClient, string resourceGroupName, string resourceName)
{
try
{
Utilities.Log("\nCommunicationService Link Notification Hub...");

var notificationHubId = Environment.GetEnvironmentVariable("AZURE_NOTIFICATION_HUB_ID");
var notificationHubConnectionString = Environment.GetEnvironmentVariable("AZURE_NOTIFICATION_HUB_CONNECTION_STRING");

Response<LinkedNotificationHub> response = await acsClient.CommunicationService.LinkNotificationHubAsync(resourceGroupName, resourceName, new LinkNotificationHubParameters(notificationHubId, notificationHubConnectionString));
Utilities.Log("\tresponse: " + response.ToString());
}
catch (Exception e)
{
Utilities.Log("LinkNotificationHubAsync encountered: " + e.Message);
}
}
}
}
Loading

0 comments on commit 432602f

Please sign in to comment.