Skip to content

Commit

Permalink
Update/claim check sample4 (#156)
Browse files Browse the repository at this point in the history
* Update libraries

* improvements

* Update claim-check/code-samples/sample-4/readme.md

Co-authored-by: Chad Kittel <[email protected]>

* Update claim-check/code-samples/sample-4/readme.md

Co-authored-by: Chad Kittel <[email protected]>

* Fix bicep

---------

Co-authored-by: Federico Arambarri <v-fearam>
Co-authored-by: Chad Kittel <[email protected]>
  • Loading branch information
v-fearam and ckittel authored Sep 30, 2024
1 parent 9e97926 commit eb46898
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.21.2" />
<PackageReference Include="Confluent.Kafka" Version="2.5.3" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.2" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace Pnp.Samples.ClaimCheckPattern
{
public interface ISampleBlobDataMover
{
Task<string> DownloadAsync(Uri blobUri, bool deleteAfter = true);
Task<string> UploadAsync(string blobUri, string containerName, string content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace Pnp.Samples.ClaimCheckPattern
{
public interface ISampleKafkaProducer
{
Task SendMessageAsync(string message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Console.WriteLine("Sending Claim Check message...");
var kafkaWorker = new SampleKafkaProducer(configuration);

await kafkaWorker.SendMessage(JsonSerializer.Serialize(new { payloadUri = newBlobUri }));
await kafkaWorker.SendMessageAsync(JsonSerializer.Serialize(new { payloadUri = newBlobUri }));
Console.WriteLine();

Console.WriteLine("Done.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Pnp.Samples.ClaimCheckPattern
/// <summary>
/// A sample Storage Blob uploader/downloader. Uses Azure Entra ID for authentication.
/// </summary>
public class SampleBlobDataMover(ILoggerFactory loggerFactory)
public class SampleBlobDataMover(ILoggerFactory loggerFactory) : ISampleBlobDataMover
{
readonly ILogger _logger = loggerFactory.CreateLogger<SampleBlobDataMover>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using Azure.Core;
using Azure.Identity;
using Azure.Identity;
using Confluent.Kafka;
using Microsoft.Extensions.Configuration;
using System.IdentityModel.Tokens.Jwt;

namespace Pnp.Samples.ClaimCheckPattern
{
/// <summary>
/// Implements the Kafka producer to send messages to a Kafka-enabled Event Hub namespace
/// Uses Azure Entra ID to authenticate.
/// </summary>
public class SampleKafkaProducer(IConfiguration configuration)
public class SampleKafkaProducer(IConfiguration configuration) : ISampleKafkaProducer
{
readonly string _eventHubEndpoint = configuration.GetSection("AppSettings:eventHubEndpoint").Value!;
readonly string _kafkaFqdn = configuration.GetSection("AppSettings:eventHubFqdn").Value!;
readonly string _topic = configuration.GetSection("AppSettings:eventHubName").Value!;

public async Task SendMessage(string message)
public async Task SendMessageAsync(string message)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Pnp.Samples.ClaimCheckPattern
/// <summary>
/// Sample function illustrating the processing of Event Hub events containing claim-check style events.
/// </summary>
public class FunctionConsumer4(ILoggerFactory loggerFactory)
public class FunctionConsumer4(ILoggerFactory loggerFactory, ISampleBlobDataMover sampleBlobDataMover)
{
readonly ILogger _logger = loggerFactory.CreateLogger<FunctionConsumer4>();
readonly SampleBlobDataMover _downloader = new(loggerFactory);
readonly ISampleBlobDataMover _downloader = sampleBlobDataMover;

[Function(nameof(FunctionConsumer4))]
public async Task RunAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.11.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" Version="6.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.11.5" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" Version="6.3.5" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.3.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.21.2" />
<PackageReference Include="Azure.Identity" Version="1.12.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace Pnp.Samples.ClaimCheckPattern
{
public interface ISampleBlobDataMover
{
Task<string> DownloadAsync(Uri blobUri, bool deleteAfter = true);
Task<string> UploadAsync(string blobUri, string containerName, string content);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Pnp.Samples.ClaimCheckPattern;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddScoped<ISampleBlobDataMover, SampleBlobDataMover>();
})
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Pnp.Samples.ClaimCheckPattern
/// <summary>
/// A sample Storage Blob uploader/downloader. Uses Azure Entra ID for authentication.
/// </summary>
public class SampleBlobDataMover(ILoggerFactory loggerFactory)
public class SampleBlobDataMover(ILoggerFactory loggerFactory) : ISampleBlobDataMover
{
readonly ILogger _logger = loggerFactory.CreateLogger<SampleBlobDataMover>();

Expand Down
45 changes: 44 additions & 1 deletion claim-check/code-samples/sample-4/bicep/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ resource eventHubDataOwnwerRole 'Microsoft.Authorization/roleDefinitions@2022-04

/*** NEW RESOURCES ***/

resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: 'la-${namePrefix}'
location: location
properties: {
retentionInDays: 30
features: {
searchVersion: 1
}
sku: {
name: 'PerGB2018'
}
}
}

@description('The Azure Storage account which will be where authorized clients upload large blobs to. The Azure Function will hand out scoped, time-limited SaS tokens for this blobs in this account.')
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
name: 'st${namePrefix}cc'
Expand All @@ -41,7 +55,7 @@ resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
accessTier: 'Hot'
allowBlobPublicAccess: false
allowCrossTenantReplication: false
allowSharedKeyAccess: true
allowSharedKeyAccess: false //Ony managed identiy allowed
isLocalUserEnabled: false
isHnsEnabled: false
isNfsV3Enabled: false
Expand Down Expand Up @@ -112,6 +126,35 @@ resource eventHubNamespace 'Microsoft.EventHub/namespaces@2023-01-01-preview' =
}
}

@description('Diagnostic settings for the Event Hub namespace.')
resource eventHubDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
name: 'evhns-diagnostics'
scope: eventHubNamespace
properties: {
logs: [
{
category: 'OperationalLogs'
enabled: true
retentionPolicy: {
enabled: false
days: 0
}
}
]
metrics: [
{
category: 'AllMetrics'
enabled: true
retentionPolicy: {
enabled: false
days: 0
}
}
]
workspaceId: logAnalytics.id
}
}

@description('Set permissions to give the user principal access to Event Hub')
resource userEventHubDataOwnwerRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(eventHubNamespace.id, eventHubDataOwnwerRole.id, principalId)
Expand Down
35 changes: 19 additions & 16 deletions claim-check/code-samples/sample-4/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,36 @@ Install the prerequisites and follow the steps to deploy and run the examples.

1. Clone this repository to your workstation and navigate to the working directory.

```shell
```bash
git clone https://github.com/mspnp/cloud-design-patterns
cd cloud-design-patterns/claim-check/code-samples/sample-4
```

1. Log into Azure and create an empty resource group.

```azurecli
az login
az account set -s <Name or ID of subscription>
```bash
az login
az account set -s <Name or ID of subscription>

NAME_PREFIX=<unique value between three to five characters of length>
az group create -n "rg-${NAME_PREFIX}" -l eastus2
```
NAME_PREFIX=<unique value between three to five characters of length>
LOCATION=eastus2
RESOURCE_GROUP_NAME="rg-${NAME_PREFIX}-${LOCATION}"

az group create -n "${RESOURCE_GROUP_NAME}" -l ${LOCATION}
```

1. Deploy the supporting Azure resources.

```azurecli
```bash
CURRENT_USER_OBJECT_ID=$(az ad signed-in-user show -o tsv --query id)

# This could take a few minutes
az deployment group create -n deploy-claim-check -f bicep/main.bicep -g "rg-${NAME_PREFIX}" -p namePrefix=$NAME_PREFIX principalId=$CURRENT_USER_OBJECT_ID
az deployment group create -n deploy-claim-check -f bicep/main.bicep -g "${RESOURCE_GROUP_NAME}" -p namePrefix=$NAME_PREFIX principalId=$CURRENT_USER_OBJECT_ID
```

1. Configure the samples to use the created Azure resources.

```shell
```bash
sed "s/{EVENT_HUBS_NAMESPACE}/evhns-${NAME_PREFIX}/g" ClientProducer4/appsettings.json.template >ClientProducer4/appsettings.json
sed -i "s/{STORAGE_ACCOUNT_NAME}/st${NAME_PREFIX}cc/g" ClientProducer4/appsettings.json

Expand All @@ -75,10 +78,10 @@ Install the prerequisites and follow the steps to deploy and run the examples.
1. Launch the Function sample that will process the claim check messages as they arrive to Event Hubs.

```bash
cd ./FunctionConsumer4
func start
```
```bash
cd ./FunctionConsumer4
func start
```

> Please note: For demo purposes, the sample application will write the payload content to the the screen. Keep that in mind before you try sending really large payloads.
Expand All @@ -96,6 +99,6 @@ Install the prerequisites and follow the steps to deploy and run the examples.

Remove the resource group that you created when you are done with this sample.

```azurecli
az group delete -n "rg-${NAME_PREFIX}"
```bash
az group delete -n "${RESOURCE_GROUP_NAME}" -y
```

0 comments on commit eb46898

Please sign in to comment.