From d7343f697be8c2b32a288f901b28b9027940dc01 Mon Sep 17 00:00:00 2001 From: "Prathamesh Govekar (MINDTREE LIMITED)" Date: Mon, 25 Feb 2019 18:40:10 -0800 Subject: [PATCH] Added New EventHubsManagementSample project --- ...Core.sln => EventHubsManagementSample.sln} | 2 +- .../EventHubsManagementSample.cs | 10 +-- .../EventHubsManagementSample.csproj} | 6 ++ .../Program.cs | 2 +- .../appsettings.json | 0 samples/Management/DotNet/README.md | 73 +++++++++++++++++++ 6 files changed, 85 insertions(+), 8 deletions(-) rename samples/Management/DotNet/{EventHubsDotNetCore.sln => EventHubsManagementSample.sln} (83%) rename samples/Management/DotNet/{EventHubsDotNetCore => EventHubsManagementSample}/EventHubsManagementSample.cs (96%) rename samples/Management/DotNet/{EventHubsDotNetCore/EventHubsDotNetCore.csproj => EventHubsManagementSample/EventHubsManagementSample.csproj} (88%) rename samples/Management/DotNet/{EventHubsDotNetCore => EventHubsManagementSample}/Program.cs (81%) rename samples/Management/DotNet/{EventHubsDotNetCore => EventHubsManagementSample}/appsettings.json (100%) create mode 100644 samples/Management/DotNet/README.md diff --git a/samples/Management/DotNet/EventHubsDotNetCore.sln b/samples/Management/DotNet/EventHubsManagementSample.sln similarity index 83% rename from samples/Management/DotNet/EventHubsDotNetCore.sln rename to samples/Management/DotNet/EventHubsManagementSample.sln index e6bd8dfa..28f146dd 100644 --- a/samples/Management/DotNet/EventHubsDotNetCore.sln +++ b/samples/Management/DotNet/EventHubsManagementSample.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.28307.271 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHubsDotNetCore", "EventHubsDotNetCore\EventHubsDotNetCore.csproj", "{1FB31C38-A397-4E82-ADFE-22132F33E0C2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventHubsManagementSample", "EventHubsManagementSample\EventHubsManagementSample.csproj", "{1FB31C38-A397-4E82-ADFE-22132F33E0C2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/samples/Management/DotNet/EventHubsDotNetCore/EventHubsManagementSample.cs b/samples/Management/DotNet/EventHubsManagementSample/EventHubsManagementSample.cs similarity index 96% rename from samples/Management/DotNet/EventHubsDotNetCore/EventHubsManagementSample.cs rename to samples/Management/DotNet/EventHubsManagementSample/EventHubsManagementSample.cs index 2428c244..375bb54e 100644 --- a/samples/Management/DotNet/EventHubsDotNetCore/EventHubsManagementSample.cs +++ b/samples/Management/DotNet/EventHubsManagementSample/EventHubsManagementSample.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -namespace EventHubsDotNetCore +namespace EventHubsManagementSample { using System; using System.Threading.Tasks; @@ -27,11 +27,9 @@ namespace EventHubsDotNetCore static EventHubManagementSample() { - var builder = new ConfigurationBuilder(); - builder.AddJsonFile("appsettings.json", true, true); - builder.AddUserSecrets(); - - SettingsCache = builder.Build(); + SettingsCache = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", true, true) + .Build(); } public static void Run() diff --git a/samples/Management/DotNet/EventHubsDotNetCore/EventHubsDotNetCore.csproj b/samples/Management/DotNet/EventHubsManagementSample/EventHubsManagementSample.csproj similarity index 88% rename from samples/Management/DotNet/EventHubsDotNetCore/EventHubsDotNetCore.csproj rename to samples/Management/DotNet/EventHubsManagementSample/EventHubsManagementSample.csproj index c49be0f5..6474f99d 100644 --- a/samples/Management/DotNet/EventHubsDotNetCore/EventHubsDotNetCore.csproj +++ b/samples/Management/DotNet/EventHubsManagementSample/EventHubsManagementSample.csproj @@ -18,4 +18,10 @@ + + + PreserveNewest + + + diff --git a/samples/Management/DotNet/EventHubsDotNetCore/Program.cs b/samples/Management/DotNet/EventHubsManagementSample/Program.cs similarity index 81% rename from samples/Management/DotNet/EventHubsDotNetCore/Program.cs rename to samples/Management/DotNet/EventHubsManagementSample/Program.cs index cc50b847..31599c07 100644 --- a/samples/Management/DotNet/EventHubsDotNetCore/Program.cs +++ b/samples/Management/DotNet/EventHubsManagementSample/Program.cs @@ -1,6 +1,6 @@ using System; -namespace EventHubsDotNetCore +namespace EventHubsManagementSample { class Program { diff --git a/samples/Management/DotNet/EventHubsDotNetCore/appsettings.json b/samples/Management/DotNet/EventHubsManagementSample/appsettings.json similarity index 100% rename from samples/Management/DotNet/EventHubsDotNetCore/appsettings.json rename to samples/Management/DotNet/EventHubsManagementSample/appsettings.json diff --git a/samples/Management/DotNet/README.md b/samples/Management/DotNet/README.md new file mode 100644 index 00000000..912b5be9 --- /dev/null +++ b/samples/Management/DotNet/README.md @@ -0,0 +1,73 @@ +# Event Hubs Management Library Sample + +This sample uses the .NET Standard Event Hubs management library to show how users can dynamically create Event Hub namespaces as well as entities. The management library can be consumed by both the full .NET Framework and .NET Core applications. For more information on .NET Standard see [.NET Platforms Support](https://docs.microsoft.com/en-us/dotnet/articles/standard/library#net-platforms-support). + +The management library allows Create, Read, Update, and Delete operations on the following: + +* Namespaces +* Event Hub Entities +* Consumer Groups + +## Prerequisites + +In order to get started using the Event Hub management libraries, you must authenticate with Azure Active Directory (AAD). AAD requires that you authenticate as a Service Principal which provides access to your Azure resources. For information on creating a Service Principal follow one of these articles: + +* [Use the Azure Portal to create Active Directory application and service principal that can access resources](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal) +* [Use Azure PowerShell to create a service principal to access resources](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authenticate-service-principal) +* [Use Azure CLI to create a service principal to access resources](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authenticate-service-principal-cli) + +The above tutorials will provide you with an `AppId` (Client ID), `TenantId`, and `ClientSecret` (Authentication Key), all of which will be used to authenticate by the management libraries. You must have 'Owner' permissions under *Role* for the resource group that you wish to run the sample on. Finally, when creating your Active Directory application, if you do not have a sign-on URL to input in the create step, simply input any URL format string e.g. https://contoso.org/exampleapp. + +You will also have to install DotNet Core in order to run the sample. + +## Running the sample +Populate the `appsettings.json` file with the appropriate values obtained from Azure Active Directory, and run the app using Visual Studio or `dotnet run`. You may find your SubscriptionId by clicking *More services* -> *Subscriptions* in the left hand nav of the Azure Portal. + +```json +{ + "TenantId": "", + "ClientId": "", + "ClientSecret": "", + "SubscriptionId": "", + "DataCenterLocation": "East US" +} +``` + +## Required NuGet packages +In order to use the `Microsoft.Azure.Management.EventHub` package, you will also need: + +* `Microsoft.Azure.Management.ResourceManager` - used to perform operations on resource groups, a required 'container' for Azure resources. +* `Microsoft.IdentityModel.Clients.ActiveDirectory` - used to authenticate with Azure Active Directory. + +## Programming pattern + +The pattern to manipulate any Event Hubs resource is similar and follows a common protocol: + +1. Obtain a token from Azure Active Directory using the `Microsoft.IdentityModel.Clients.ActiveDirectory` library + ```csharp + var context = new AuthenticationContext($"https://login.windows.net/{tenantId}"); + + var result = await context.AcquireTokenAsync( + "https://management.core.windows.net/", + new ClientCredential(clientId, clientSecret) + ); + ``` + +1. Create the `EventHubManagementClient` object + ```csharp + var creds = new TokenCredentials(token); + var ehClient = new EventHubManagementClient(creds) + { + SubscriptionId = SettingsCache["SubscriptionId"] + }; + ``` + +1. Set the CreateOrUpdate parameters to your specified values + ```csharp + var ehParams = new Eventhub() { }; + ``` + +1. Execute the call + ```csharp + await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, EventHubName, ehParams); + ```