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 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); } }
static async Task Main(string[] args) { // User Identity var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); var communicationServiceClient = new CommunicationManagementClient(subscriptionId, new InteractiveBrowserCredential()); // System assigned Managed Identity //var subscriptionId = "AZURE_SUBSCRIPTION_ID"; //var acsClient = new CommunicationManagementClient(subscriptionId, new ManagedIdentityCredential()); // User assigned Managed Identity /*var subscriptionId = "AZURE_SUBSCRIPTION_ID";*/ /*var managedIdentityCredential = new ManagedIdentityCredential("AZURE_CLIENT_ID"); * var acsClient = new CommunicationManagementClient(subscriptionId, managedIdentityCredential);*/ // Using Service Principal /*var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); * var acsClient = new CommunicationManagementClient(subscriptionId, new EnvironmentCredential());*/ // Create a Communication Services resource var resourceGroupName = "myResourceGroupName"; var resourceName = "myResource"; var resource = new CommunicationServiceResource { Location = "Global", DataLocation = "UnitedStates" }; var operation = await communicationServiceClient.CommunicationService.StartCreateOrUpdateAsync(resourceGroupName, resourceName, resource); await operation.WaitForCompletionAsync(); }
public async Task LinkNotificationHub() { // Setup resource group for the test. This resource group is deleted by CleanupResourceGroupsAsync after the test ends Subscription sub = await ResourcesManagementClient.GetDefaultSubscriptionAsync(); var lro = await sub.GetResourceGroups().CreateOrUpdateAsync( NotificationHubsResourceGroupName, new ResourceGroupData(Location)); ResourceGroup rg = lro.Value; CommunicationManagementClient acsClient = GetCommunicationManagementClient(); var resourceName = Recording.GenerateAssetName("sdk-test-link-notif-hub-"); // Create a new resource with a our test parameters CommunicationServiceCreateOrUpdateOperation result = await acsClient.CommunicationService.StartCreateOrUpdateAsync( rg.Data.Name, resourceName, new CommunicationServiceResource { Location = ResourceLocation, DataLocation = ResourceDataLocation }); await result.WaitForCompletionAsync(); // Check that our resource has been created successfully Assert.IsTrue(result.HasCompleted); Assert.IsTrue(result.HasValue); CommunicationServiceResource resource = result.Value; // Retrieve var resourceRetrieved = await acsClient.CommunicationService.GetAsync(rg.Data.Name, resourceName); Assert.AreEqual( resourceName, resourceRetrieved.Value.Name); Assert.AreEqual( "Succeeded", resourceRetrieved.Value.ProvisioningState.ToString()); // Link NotificationHub var linkNotificationHubResponse = await acsClient.CommunicationService.LinkNotificationHubAsync( rg.Data.Name, resourceName, new LinkNotificationHubParameters(NotificationHubsResourceId, NotificationHubsConnectionString)); Assert.AreEqual(NotificationHubsResourceId, linkNotificationHubResponse.Value.ResourceId); // Delete CommunicationServiceDeleteOperation deleteResult = await acsClient.CommunicationService.StartDeleteAsync(rg.Data.Name, resourceName); await deleteResult.WaitForCompletionAsync(); // Check that our resource has been deleted successfully Assert.IsTrue(deleteResult.HasCompleted); Assert.IsTrue(deleteResult.HasValue); }
public async Task CheckNameUniqueness() { // Setup resource group for the test. This resource group is deleted by CleanupResourceGroupsAsync after the test ends Subscription sub = await ResourcesManagementClient.GetDefaultSubscriptionAsync(); var lro = await sub.GetResourceGroups().CreateOrUpdateAsync( Recording.GenerateAssetName(ResourceGroupPrefix), new ResourceGroupData(Location)); ResourceGroup rg = lro.Value; CommunicationManagementClient acsClient = GetCommunicationManagementClient(); var resourceName = Recording.GenerateAssetName("sdk-test-name-availablity-"); // Check if name is unique Response <NameAvailability> nameAvailabilityResult = await acsClient.CommunicationService.CheckNameAvailabilityAsync(new NameAvailabilityParameters("Microsoft.Communication/CommunicationServices", resourceName)); Assert.IsTrue(nameAvailabilityResult.Value.NameAvailable); // Create a new resource with a our test parameters CommunicationServiceCreateOrUpdateOperation result = await acsClient.CommunicationService.StartCreateOrUpdateAsync( rg.Data.Name, resourceName, new CommunicationServiceResource { Location = ResourceLocation, DataLocation = ResourceDataLocation }); await result.WaitForCompletionAsync(); // Check that our resource has been created successfully Assert.IsTrue(result.HasCompleted); Assert.IsTrue(result.HasValue); CommunicationServiceResource resource = result.Value; // Retrieve var resourceRetrieved = await acsClient.CommunicationService.GetAsync(rg.Data.Name, resourceName); Assert.AreEqual( resourceName, resourceRetrieved.Value.Name); Assert.AreEqual( "Succeeded", resourceRetrieved.Value.ProvisioningState.ToString()); // Check if name is unique nameAvailabilityResult = await acsClient.CommunicationService.CheckNameAvailabilityAsync(new NameAvailabilityParameters("Microsoft.Communication/CommunicationServices", resourceName)); Assert.IsFalse(nameAvailabilityResult.Value.NameAvailable); }
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 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); } }
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 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 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); } }
public async Task ListByRg() { // Setup resource group for the test. This resource group is deleted by CleanupResourceGroupsAsync after the test ends Subscription sub = await ResourcesManagementClient.GetDefaultSubscriptionAsync(); var lro = await sub.GetResourceGroups().CreateOrUpdateAsync( Recording.GenerateAssetName(ResourceGroupPrefix), new ResourceGroupData(Location)); ResourceGroup rg = lro.Value; // Create a new resource with the test parameters CommunicationManagementClient acsClient = GetCommunicationManagementClient(); var resourceName = Recording.GenerateAssetName("sdk-test-list-by-rg-"); var testResource = new CommunicationServiceResource { Location = ResourceLocation, DataLocation = ResourceDataLocation }; CommunicationServiceCreateOrUpdateOperation result = await acsClient.CommunicationService.StartCreateOrUpdateAsync( rg.Data.Name, resourceName, testResource); await result.WaitForCompletionAsync(); Assert.IsTrue(result.HasCompleted); Assert.IsTrue(result.HasValue); // Verify that the resource we just created is in the list var resources = acsClient.CommunicationService.ListByResourceGroupAsync(rg.Data.Name); bool resourceFound = false; await foreach (var resource in resources) { if (resource.Name.Equals(resourceName)) { resourceFound = true; break; } } Assert.True(resourceFound); }
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 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); } }
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); }
public async Task CrudSimpleResource() { // Setup resource group for the test. This resource group is deleted by CleanupResourceGroupsAsync after the test ends ResourceGroup rg = await ResourcesManagementClient.ResourceGroups.CreateOrUpdateAsync( Recording.GenerateAssetName(ResourceGroupPrefix), new ResourceGroup(Location)); CommunicationManagementClient acsClient = GetCommunicationManagementClient(); var resourceName = Recording.GenerateAssetName("sdk-test-crud-simple-"); // Create a new resource with a our test parameters CommunicationServiceCreateOrUpdateOperation result = await acsClient.CommunicationService.StartCreateOrUpdateAsync( rg.Name, resourceName, new CommunicationServiceResource { Location = ResourceLocation, DataLocation = ResourceDataLocation }); await result.WaitForCompletionAsync(); // Check that our resource has been created successfully Assert.IsTrue(result.HasCompleted); Assert.IsTrue(result.HasValue); CommunicationServiceResource resource = result.Value; // Check that the keys are there. // Note: These values have been sanitized. var keys = await acsClient.CommunicationService.ListKeysAsync(rg.Name, resourceName); Assert.NotNull(keys.Value.PrimaryKey); Assert.NotNull(keys.Value.SecondaryKey); Assert.NotNull(keys.Value.PrimaryConnectionString); Assert.NotNull(keys.Value.SecondaryConnectionString); keys = await acsClient.CommunicationService.RegenerateKeyAsync(rg.Name, resourceName, new RegenerateKeyParameters { KeyType = KeyType.Primary }); Assert.NotNull(keys.Value.PrimaryKey); Assert.Null(keys.Value.SecondaryKey); Assert.NotNull(keys.Value.PrimaryConnectionString); Assert.Null(keys.Value.SecondaryConnectionString); keys = await acsClient.CommunicationService.RegenerateKeyAsync(rg.Name, resourceName, new RegenerateKeyParameters { KeyType = KeyType.Secondary }); Assert.Null(keys.Value.PrimaryKey); Assert.NotNull(keys.Value.SecondaryKey); Assert.Null(keys.Value.PrimaryConnectionString); Assert.NotNull(keys.Value.SecondaryConnectionString); // Retrieve var resourceRetrieved = await acsClient.CommunicationService.GetAsync(rg.Name, resourceName); Assert.AreEqual( resourceName, resourceRetrieved.Value.Name); Assert.AreEqual( "Succeeded", resourceRetrieved.Value.ProvisioningState.ToString()); // Update CommunicationServiceResource emptyResource = new CommunicationServiceResource(); resource = await acsClient.CommunicationService.UpdateAsync( rg.Name, resourceName, emptyResource); Assert.True(resource.Tags.Count == 0); // Delete CommunicationServiceDeleteOperation deleteResult = await acsClient.CommunicationService.StartDeleteAsync(rg.Name, resourceName); await deleteResult.WaitForCompletionAsync(); // Check that our resource has been deleted successfully Assert.IsTrue(deleteResult.HasCompleted); Assert.IsTrue(deleteResult.HasValue); }
public async Task CrudResourceWithTags() { // Setup resource group for the test. This resource group is deleted by CleanupResourceGroupsAsync after the test ends ResourceGroup rg = await ResourcesManagementClient.ResourceGroups.CreateOrUpdateAsync( Recording.GenerateAssetName(ResourceGroupPrefix), new ResourceGroup(Location)); CommunicationManagementClient acsClient = GetCommunicationManagementClient(); var resourceName = Recording.GenerateAssetName("sdk-test-crud-with-tags-"); // Create a new resource with a our test parameters CommunicationServiceResource serviceResource = new CommunicationServiceResource { Location = ResourceLocation, DataLocation = ResourceDataLocation }; serviceResource.Tags.Add("tag1", "tag1val"); serviceResource.Tags.Add("tag2", "tag2val"); CommunicationServiceCreateOrUpdateOperation result = await acsClient.CommunicationService.StartCreateOrUpdateAsync( rg.Name, resourceName, serviceResource); await result.WaitForCompletionAsync(); // Check that our resource has been created successfully Assert.IsTrue(result.HasCompleted); Assert.IsTrue(result.HasValue); CommunicationServiceResource resource = result.Value; Assert.AreEqual("tag1val", resource.Tags["tag1"]); Assert.AreEqual("tag2val", resource.Tags["tag2"]); Assert.IsFalse(resource.Tags.ContainsKey("tag3")); // Check that the keys are there. // Note: These values have been sanitized. var keys = await acsClient.CommunicationService.ListKeysAsync(rg.Name, resourceName); Assert.NotNull(keys.Value.PrimaryKey); Assert.NotNull(keys.Value.SecondaryKey); Assert.NotNull(keys.Value.PrimaryConnectionString); Assert.NotNull(keys.Value.SecondaryConnectionString); // Retrieve var resourceRetrieved = await acsClient.CommunicationService.GetAsync(rg.Name, resourceName); Assert.AreEqual( resourceName, resourceRetrieved.Value.Name); Assert.AreEqual( "Succeeded", resourceRetrieved.Value.ProvisioningState.ToString());; Assert.AreEqual( resource.Tags as IEnumerable <KeyValuePair <string, string> >, resourceRetrieved.Value.Tags as IEnumerable <KeyValuePair <string, string> >); // Update resource.Tags.Remove("tag1"); resource.Tags["tag2"] = "tag2newval"; resource.Tags.Add("tag3", "tag3val"); resource = await acsClient.CommunicationService.UpdateAsync( rg.Name, resourceName, resource); Assert.False(resource.Tags.ContainsKey("tag1")); Assert.AreEqual("tag2newval", resource.Tags["tag2"]); Assert.AreEqual("tag3val", resource.Tags["tag3"]); // Delete CommunicationServiceDeleteOperation deleteResult = await acsClient.CommunicationService.StartDeleteAsync(rg.Name, resourceName); await deleteResult.WaitForCompletionAsync(); // Check that our resource has been deleted successfully Assert.IsTrue(deleteResult.HasCompleted); Assert.IsTrue(deleteResult.HasValue); }