feat(floci): model Cosmos API as a child resource - #1546
Conversation
Adds a WithCosmosReference<TDestination>(...) extension (TS binding
withFlociAzureCosmosReference) that injects a Cosmos DB connection string
(ConnectionStrings__{connectionName}, default 'cosmos') pointing at the
floci-az emulator's Cosmos SQL/NoSQL API, so a dependent can consume it via
the standard AddAzureCosmosClient(name) flow. connectionName and accountName
(default devstoreaccount1) are configurable; uses the well-known Cosmos
emulator key. Additive/composable with WithReference. Includes tests, docs,
and the regenerated API surface.
Closes CommunityToolkit#1545
There was a problem hiding this comment.
Doing the review and I feel like this kind of has to work around some of the design of how Aspire works since we're not really treating the floci resource as something that can provide a Cosmos-formatted connection string, and bypassing the WithReference design for that.
I wonder if this as an API wouldn't be better:
var azure = builder.AddFlociAzure("floci-az");
var cosmos = azure.WithCosmos();
builder.AddProject<Projects.Api>("api")
.WithReference(azure)
.WithReference(cosmos)
.WaitFor(azure);In this case the WithCosmos() method would return a IResourceBuilder<FlociAzureCosmosResource>, and then we treat it as a dedicated resource on its own which overrides the connection string creation to return a Cosmos formatted one.
This then models the CosmosDB part of Floci as a resource in its own right, making it visualise what the resource graph looks like, unlike your current proposal which would show the resource graph as not containing a CosmosDB resource since the resource doesn't exist it's just something teased out of another resource. Then, since it's modelled as a resource, you can handle deployments easier by doing something like:
IResourceBuilder<IResourceWithConnectionString> cosmos = builder.IsDeployment ?
builder.AddAzureCosmos(...) :
flociAzure.WithCosmos();(yes, that code isn't 100% right, I'm doing it from memory 😅).
Does that make sense?
Use Aspire's standard WithReference flow and expose Cosmos in the resource graph. Revert the generated API surface because CI owns it.
|
Addressed review feedback in 088933e:
Validation: |
|
Thanks @thomhurst - looks right to go. |
Closes #1545
Overview
Models the Cosmos DB API exposed by the Floci Azure emulator as a dedicated Aspire child resource. Dependents now use Aspire's standard
WithReferenceflow, so Cosmos appears in the resource graph and can be swapped with anotherIResourceWithConnectionStringfor deployment.Usage
App side remains the normal flow:
What's included
WithCosmos(name = "cosmos", accountName = null)returningIResourceBuilder<FlociAzureCosmosResource>.FlociAzureCosmosResourceimplementsIResourceWithConnectionStringandIResourceWithParent<FlociAzureContainerResource>.WithReference(cosmos)injectsConnectionStrings__{resourceName}.Testing
WithReferenceTests: 11/11 passed.net8.0,net9.0, andnet10.0; 0 warnings, 0 errors.