Skip to content

Support referencing existing Service Bus resources in references#7249

Merged
captainsafia merged 11 commits intomainfrom
safia/existing-resources-1
Feb 3, 2025
Merged

Support referencing existing Service Bus resources in references#7249
captainsafia merged 11 commits intomainfrom
safia/existing-resources-1

Conversation

@captainsafia
Copy link
Copy Markdown
Contributor

Opening this PR to sanity check the approach before adding it to other resource types. Some things of note:

  • Support for referencing existing resources in other subscription IDs/resource groups isn't implemented due to a gap in the underlying provisioning APIs. See [FEATURE REQ] FromExisting methods should support setting scopes Azure/azure-sdk-for-net#47980. There are workarounds for the but the juice might not be worth the squeeze.
  • Separate APIs for publishing/running with an existing resource backed by an annotation and explicit checks in the ConfigureInfrastructure calls for each resource.

@davidfowl
Copy link
Copy Markdown
Contributor

Opening this PR to sanity check the approach before adding it to other resource types. Some things of note:

This should be a draft right?

Comment thread src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs Outdated
Comment thread src/Aspire.Hosting/ApplicationModel/ExistingResourceAnnotation.cs Outdated
Comment thread src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs Outdated
Comment thread src/Aspire.Hosting/ApplicationModel/ExistingResourceAnnotation.cs Outdated
Comment thread src/Aspire.Hosting.Azure/Provisioning/Provisioners/AzureProvisioner.cs Outdated
Comment thread src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs Outdated
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs Outdated
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs Outdated
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs Outdated
Comment thread tests/Aspire.Hosting.Azure.Tests/ExistingAzureResourceTests.cs Outdated
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs Outdated
}

[Fact]
public async Task RequiresPublishAsExistingInPublishMode()
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.

(nit) Is this test name correct? the test calls RunAsExisting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the test name is correct although confusing. I was intending to say that you need to call PublishAsExisting in publish-mode otherwise the API effectively no-ops.

Comment thread tests/Aspire.Hosting.Azure.Tests/ExistingAzureResourceTests.cs Outdated
Comment thread src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs Outdated
@@ -86,7 +96,7 @@ public static IResourceBuilder<AzureServiceBusResource> AddAzureServiceBus(this
infrastructure.Add(cdkRule);
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.

Does generating these Queues and Topics work with an existing resource?

Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceAnnotation.cs Outdated
@danmoseley danmoseley requested review from Copilot and removed request for Copilot January 28, 2025 16:42
Copy link
Copy Markdown
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

Looks really good. Nice work @captainsafia.

Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs Outdated
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs Outdated
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceExtensions.cs
Comment thread src/Aspire.Hosting.Azure/ExistingAzureResourceAnnotation.cs Outdated
Comment thread src/Aspire.Hosting.Azure/AzureBicepResource.cs
Comment thread src/Aspire.Hosting.Azure/AzureProvisioningResourceExtensions.cs Outdated
Comment thread src/Aspire.Hosting.Azure/AzureBicepResource.cs Outdated
Comment thread src/Aspire.Hosting.Azure/Provisioning/Provisioners/BicepProvisioner.cs Outdated
Comment thread src/Aspire.Hosting.Azure/Provisioning/Provisioners/BicepProvisioner.cs Outdated
// This could be done as a bicep template that imports the other bicep template but this is
// quick and dirty for now
var keyVaults = context.ResourceGroup.GetKeyVaults();
var keyVaults = resourceGroup.GetKeyVaults();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Testing this code path is interesting...

Comment on lines +67 to +70
if (builder.Resource.IsExisting())
{
throw new InvalidOperationException($"Resource {builder.Resource.Name} is already marked as an existing resource.");
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why throw instead of replace?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I feel like it makes it clearer to reason through the scope a resource is grouped to if we only support one XAsExisting invocation on a resource.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to think about this like a pipeline. It should not throw

@captainsafia captainsafia enabled auto-merge (squash) February 3, 2025 21:16
/// </summary>
/// <remarks>
/// The scope is a weakly-typed collection of key-value pairs that can be used to configure
/// the Bicep scope that is emitted by AZD in the module definition for a resource.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove azd from the docs

/// versions azure.bicep.v1. Only the `resourceGroup` key is respected as a sub-property although
/// more keys may be supported in the future, such as a subscriptionId.
/// </remarks>
public Dictionary<string, object> Scope { get; } = [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why isn't this a type?

@captainsafia captainsafia merged commit 4e1a0f2 into main Feb 3, 2025
@captainsafia captainsafia deleted the safia/existing-resources-1 branch February 3, 2025 22:21
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 6, 2025
@github-actions github-actions Bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Mar 10, 2025
@captainsafia captainsafia review requested due to automatic review settings March 24, 2026 00:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants