Skip to content

Commit a98ffd5

Browse files
committed
Reference Foundry project from hosted agent target
1 parent 4cfc99d commit a98ffd5

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ private static void AddProjectReferenceForRunMode<T>(
144144
}
145145
}
146146

147+
private static void AddProjectReferenceForPublishMode(
148+
IDistributedApplicationBuilder applicationBuilder,
149+
IResource target,
150+
IResourceBuilder<AzureCognitiveServicesProjectResource> project)
151+
{
152+
if (target is not IResourceWithEnvironment targetWithEnvironment)
153+
{
154+
throw new InvalidOperationException($"Unable to reference Foundry project from resource '{target.Name}' because it does not support environment variables.");
155+
}
156+
157+
applicationBuilder.CreateResourceBuilder(targetWithEnvironment)
158+
.WithReference(project);
159+
}
160+
147161
private static IResourceBuilder<AzureCognitiveServicesProjectResource> ResolveProjectBuilderForPublish<T>(IResourceBuilder<T> builder)
148162
where T : IResourceWithEndpoints, IResourceWithEnvironment, IComputeResource
149163
{
@@ -308,7 +322,8 @@ private static void ConfigurePublishMode<T>(
308322
{
309323
// Ensure we have a container resource to deploy.
310324
// ExecutableResource needs PublishAsDockerFile() to convert it into a container resource at this stage.
311-
builder.ApplicationBuilder.CreateResourceBuilder(executableResource).PublishAsDockerFile();
325+
builder.ApplicationBuilder.CreateResourceBuilder(executableResource)
326+
.PublishAsDockerFile();
312327

313328
if (builder.ApplicationBuilder.TryCreateResourceBuilder(resource.Name, out containerResourceBuilder))
314329
{
@@ -328,6 +343,8 @@ private static void ConfigurePublishMode<T>(
328343
throw new InvalidOperationException($"Unable to create hosted agent for resource '{resource.Name}' because it is not a container, executable, or project resource.");
329344
}
330345

346+
AddProjectReferenceForPublishMode(builder.ApplicationBuilder, target, project);
347+
331348
// Create a separate agent resource to host the deployment.
332349
var hostedAgent = new AzureHostedAgentResource(agentName, target, configure);
333350

@@ -340,7 +357,6 @@ private static void ConfigurePublishMode<T>(
340357

341358
builder.ApplicationBuilder.AddResource(hostedAgent)
342359
.WithIconName("Agents")
343-
.WithReferenceRelationship(target)
344-
.WithReference(project);
360+
.WithReferenceRelationship(target);
345361
}
346362
}

tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,29 @@ public void AsHostedAgent_InPublishMode_CreatesHostedAgentResource()
142142
Assert.Equal("agent-ha", hostedAgent.Name);
143143
}
144144

145+
[Fact]
146+
public void AsHostedAgent_InPublishMode_AddsProjectReferenceToDeploymentTarget()
147+
{
148+
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
149+
var project = builder.AddFoundry("account")
150+
.AddProject("my-project");
151+
152+
builder.AddProject<Project>("agent", launchProfileName: null)
153+
.AsHostedAgent(project);
154+
155+
builder.Build();
156+
157+
var hostedAgent = Assert.Single(builder.Resources.OfType<AzureHostedAgentResource>());
158+
159+
Assert.True(hostedAgent.Target.TryGetAnnotationsOfType<ResourceRelationshipAnnotation>(out var relationships));
160+
Assert.Contains(relationships, r =>
161+
r.Type == "Reference" &&
162+
ReferenceEquals(r.Resource, project.Resource));
163+
Assert.DoesNotContain(hostedAgent.Annotations.OfType<ResourceRelationshipAnnotation>(), r =>
164+
r.Type == "Reference" &&
165+
ReferenceEquals(r.Resource, project.Resource));
166+
}
167+
145168
[Fact]
146169
public void AsHostedAgent_WithOptions_AppliesAllPropertiesToConfiguration()
147170
{
@@ -282,4 +305,9 @@ public async Task FoundryProject_DefaultRegistryDoesNotAddGlobalRegistryTargets(
282305

283306
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ExecuteBeforeStartHooksAsync")]
284307
private static extern Task ExecuteBeforeStartHooksAsync(DistributedApplication app, CancellationToken cancellationToken);
308+
309+
private sealed class Project : IProjectMetadata
310+
{
311+
public string ProjectPath => "project";
312+
}
285313
}

0 commit comments

Comments
 (0)