Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable ASPIREAZURE003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Azure;
using Aspire.Hosting.Azure.CognitiveServices;
Expand Down Expand Up @@ -38,6 +40,11 @@ public static IResourceBuilder<AzureOpenAIResource> AddAzureOpenAI(this IDistrib

var configureInfrastructure = (AzureResourceInfrastructure infrastructure) =>
{
var azureResource = (AzureOpenAIResource)infrastructure.AspireResource;

// Check if this Azure OpenAI has a private endpoint (via annotation)
var hasPrivateEndpoint = azureResource.HasAnnotationOfType<PrivateEndpointTargetAnnotation>();

var cogServicesAccount = AzureProvisioningResource.CreateExistingOrNewProvisionableResource(infrastructure,
(identifier, name) =>
{
Expand All @@ -55,7 +62,9 @@ public static IResourceBuilder<AzureOpenAIResource> AddAzureOpenAI(this IDistrib
Properties = new CognitiveServicesAccountProperties()
{
CustomSubDomainName = ToLower(Take(Concat(infrastructure.AspireResource.Name, GetUniqueString(GetResourceGroup().Id)), 24)),
PublicNetworkAccess = ServiceAccountPublicNetworkAccess.Enabled,
PublicNetworkAccess = hasPrivateEndpoint
? ServiceAccountPublicNetworkAccess.Disabled
: ServiceAccountPublicNetworkAccess.Enabled,
// Disable local auth for AOAI since managed identity is used
DisableLocalAuth = true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Aspire.Hosting.ApplicationModel;
[AspireExport]
public class AzureOpenAIResource(string name, Action<AzureResourceInfrastructure> configureInfrastructure)
: AzureProvisioningResource(name, configureInfrastructure),
IResourceWithConnectionString, IAzureNspAssociationTarget
IResourceWithConnectionString, IAzurePrivateEndpointTarget, IAzureNspAssociationTarget
{
[Obsolete("Use AzureOpenAIDeploymentResource instead.")]
private readonly List<AzureOpenAIDeployment> _deployments = [];
Expand Down Expand Up @@ -111,6 +111,10 @@ public override ProvisionableResource AddAsExistingResource(AzureResourceInfrast
return account;
}

IEnumerable<string> IAzurePrivateEndpointTarget.GetPrivateLinkGroupIds() => ["account"];

string IAzurePrivateEndpointTarget.GetPrivateDnsZoneName() => "privatelink.openai.azure.com";

IEnumerable<KeyValuePair<string, ReferenceExpression>> IResourceWithConnectionString.GetConnectionProperties()
{
yield return new("Uri", UriExpression);
Expand Down
11 changes: 10 additions & 1 deletion src/Aspire.Hosting.Foundry/FoundryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable ASPIREAZURE003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Azure;
using Aspire.Hosting.Foundry;
Expand Down Expand Up @@ -408,6 +410,11 @@ await rns.PublishUpdateAsync(deployment, state => state with

private static void ConfigureInfrastructure(AzureResourceInfrastructure infrastructure)
{
var azureResource = (FoundryResource)infrastructure.AspireResource;

// Check if this Foundry resource has a private endpoint (via annotation)
var hasPrivateEndpoint = azureResource.HasAnnotationOfType<PrivateEndpointTargetAnnotation>();

var cogServicesAccount = AzureProvisioningResource.CreateExistingOrNewProvisionableResource(infrastructure,
(identifier, name) =>
{
Expand All @@ -427,7 +434,9 @@ private static void ConfigureInfrastructure(AzureResourceInfrastructure infrastr
// Until this bug is fixed, CustomSubDomainName must be set to the
// account's name: https://msdata.visualstudio.com/Vienna/_workitems/edit/4866592
CustomSubDomainName = ToLower(Take(Concat(infrastructure.AspireResource.Name, GetUniqueString(GetResourceGroup().Id)), 24)),
PublicNetworkAccess = ServiceAccountPublicNetworkAccess.Enabled,
PublicNetworkAccess = hasPrivateEndpoint
? ServiceAccountPublicNetworkAccess.Disabled
: ServiceAccountPublicNetworkAccess.Enabled,
DisableLocalAuth = true,
AllowProjectManagement = true
},
Expand Down
6 changes: 5 additions & 1 deletion src/Aspire.Hosting.Foundry/FoundryResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Aspire.Hosting.Foundry;
/// <param name="configureInfrastructure">Configures the underlying Azure resource using Azure.Provisioning.</param>
[AspireExport]
public class FoundryResource(string name, Action<AzureResourceInfrastructure> configureInfrastructure) :
AzureProvisioningResource(name, configureInfrastructure), IResourceWithEndpoints, IResourceWithConnectionString, IAzureNspAssociationTarget
AzureProvisioningResource(name, configureInfrastructure), IResourceWithEndpoints, IResourceWithConnectionString, IAzurePrivateEndpointTarget, IAzureNspAssociationTarget
{
internal Uri? EmulatorServiceUri { get; set; }

Expand Down Expand Up @@ -132,6 +132,10 @@ IEnumerable<KeyValuePair<string, ReferenceExpression>> IResourceWithConnectionSt
yield return new("Key", ReferenceExpression.Create($"{ApiKey}"));
}
}

IEnumerable<string> IAzurePrivateEndpointTarget.GetPrivateLinkGroupIds() => ["account"];

string IAzurePrivateEndpointTarget.GetPrivateDnsZoneName() => "privatelink.cognitiveservices.azure.com";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Foundry is going to need more than 1. I will make this refactoring in a separate change.

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,36 @@ public async Task AddAzureWebPubSub_WithPrivateEndpoint_GeneratesCorrectBicep()

await Verify(manifest.BicepText, extension: "bicep");
}

[Fact]
public async Task AddAzureOpenAI_WithPrivateEndpoint_GeneratesCorrectBicep()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);

var vnet = builder.AddAzureVirtualNetwork("myvnet");
var subnet = vnet.AddSubnet("pesubnet", "10.0.1.0/24");
var openai = builder.AddAzureOpenAI("openai");

subnet.AddPrivateEndpoint(openai);

var manifest = await AzureManifestUtils.GetManifestWithBicep(openai.Resource);

await Verify(manifest.BicepText, extension: "bicep");
}

[Fact]
public async Task AddFoundry_WithPrivateEndpoint_GeneratesCorrectBicep()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);

var vnet = builder.AddAzureVirtualNetwork("myvnet");
var subnet = vnet.AddSubnet("pesubnet", "10.0.1.0/24");
var foundry = builder.AddFoundry("foundry");

subnet.AddPrivateEndpoint(foundry);

var manifest = await AzureManifestUtils.GetManifestWithBicep(foundry.Resource);

await Verify(manifest.BicepText, extension: "bicep");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

resource openai 'Microsoft.CognitiveServices/accounts@2025-09-01' = {
name: take('openai-${uniqueString(resourceGroup().id)}', 64)
location: location
kind: 'OpenAI'
properties: {
customSubDomainName: toLower(take(concat('openai', uniqueString(resourceGroup().id)), 24))
publicNetworkAccess: 'Disabled'
disableLocalAuth: true
}
sku: {
name: 'S0'
}
tags: {
'aspire-resource-name': 'openai'
}
}

output connectionString string = 'Endpoint=${openai.properties.endpoint}'

output endpoint string = openai.properties.endpoint

output name string = openai.name

output id string = openai.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

resource foundry 'Microsoft.CognitiveServices/accounts@2025-09-01' = {
name: take('foundry-${uniqueString(resourceGroup().id)}', 64)
location: location
identity: {
type: 'SystemAssigned'
}
kind: 'AIServices'
properties: {
customSubDomainName: toLower(take(concat('foundry', uniqueString(resourceGroup().id)), 24))
publicNetworkAccess: 'Disabled'
disableLocalAuth: true
allowProjectManagement: true
}
sku: {
name: 'S0'
}
tags: {
'aspire-resource-name': 'foundry'
}
}

resource foundry_caphost 'Microsoft.CognitiveServices/accounts/capabilityHosts@2025-10-01-preview' = {
name: 'foundry-caphost'
properties: {
capabilityHostKind: 'Agents'
enablePublicHostingEnvironment: true
}
parent: foundry
}

output aiFoundryApiEndpoint string = foundry.properties.endpoints['AI Foundry API']

output endpoint string = foundry.properties.endpoint

output name string = foundry.name

output id string = foundry.id
Loading