-
Notifications
You must be signed in to change notification settings - Fork 715
Adds Aspire Oracle EntityFrameworkCore Database component. #1295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
92a7922
prepare directory
andrevlins 0f2f3f8
Adds Aspire Oracle EntityFrameworkCore Database component.
andrevlins 12ee396
AppModel apis
andrevlins 0f384e8
Merge branch 'main' into aspire-oracle-ef
andrevlins 773b457
appmodel apis
andrevlins 06d7a4c
tests for oracle hosting
andrevlins 4db7868
adjusting to reviews
andrevlins 824ed28
Merge branch 'dotnet:main' into aspire-oracle-ef
andrevlins 91e0b7b
Oracle has a 30 character password limit
andrevlins 74a10a0
functional test
andrevlins ae61db3
Merge branch 'main' into aspire-oracle-ef
andrevlins 9ac8eab
Merge branch 'dotnet:main' into aspire-oracle-ef
andrevlins e04cee1
adjusting to reviews
andrevlins 8b84294
Merge branch 'main' into aspire-oracle-ef
andrevlins 5d99c70
configurationschema was outdated
andrevlins 7c09cbd
adjusting to reviews
andrevlins 9abcdd8
Merge branch 'dotnet:main' into aspire-oracle-ef
andrevlins 5f0b689
updated with the latest bits
andrevlins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Aspire.Hosting/Oracle/IOracleDatabaseParentResource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// Represents a Oracle Database resource that requires a connection string. | ||
| /// </summary> | ||
| public interface IOracleDatabaseParentResource : IResourceWithConnectionString, IResourceWithEnvironment | ||
| { | ||
| } |
105 changes: 105 additions & 0 deletions
105
src/Aspire.Hosting/Oracle/OracleDatabaseBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net.Sockets; | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Publishing; | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for adding Oracle Database resources to an <see cref="IDistributedApplicationBuilder"/>. | ||
| /// </summary> | ||
| public static class OracleDatabaseBuilderExtensions | ||
| { | ||
| private const string PasswordEnvVarName = "ORACLE_PWD"; | ||
|
|
||
| /// <summary> | ||
| /// Adds a Oracle Database container to the application model. The default image is "database/free" and the tag is "latest". | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> | ||
| /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> | ||
| /// <param name="port">The host port for Oracle Database.</param> | ||
| /// <param name="password">The password for the Oracle Database container. Defaults to a random password.</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseContainerResource}"/>.</returns> | ||
| public static IResourceBuilder<OracleDatabaseContainerResource> AddOracleDatabaseContainer(this IDistributedApplicationBuilder builder, string name, int? port = null, string? password = null) | ||
| { | ||
| password = password ?? Guid.NewGuid().ToString("N").Substring(0, 30); | ||
| var oracleDatabaseContainer = new OracleDatabaseContainerResource(name, password); | ||
| return builder.AddResource(oracleDatabaseContainer) | ||
| .WithManifestPublishingCallback(context => WriteOracleDatabaseContainerResourceToManifest(context, oracleDatabaseContainer)) | ||
| .WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 1521)) | ||
| .WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "latest", Registry = "container-registry.oracle.com" }) | ||
| .WithEnvironment(context => | ||
| { | ||
| if (context.PublisherName == "manifest") | ||
| { | ||
| context.EnvironmentVariables.Add(PasswordEnvVarName, $"{{{oracleDatabaseContainer.Name}.inputs.password}}"); | ||
| } | ||
| else | ||
| { | ||
| context.EnvironmentVariables.Add(PasswordEnvVarName, oracleDatabaseContainer.Password); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a Oracle Database resource to the application model. A container is used for local development. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> | ||
| /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseServerResource}"/>.</returns> | ||
| public static IResourceBuilder<OracleDatabaseServerResource> AddOracleDatabase(this IDistributedApplicationBuilder builder, string name) | ||
| { | ||
| var password = Guid.NewGuid().ToString("N").Substring(0, 30); | ||
| var oracleDatabaseServer = new OracleDatabaseServerResource(name, password); | ||
| return builder.AddResource(oracleDatabaseServer) | ||
| .WithManifestPublishingCallback(WriteOracleDatabaseContainerToManifest) | ||
| .WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, containerPort: 1521)) | ||
| .WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "latest", Registry = "container-registry.oracle.com" }) | ||
| .WithEnvironment(PasswordEnvVarName, () => oracleDatabaseServer.Password); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a Oracle Database database to the application model. | ||
| /// </summary> | ||
| /// <param name="builder">The Oracle Database server resource builder.</param> | ||
| /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseResource}"/>.</returns> | ||
| public static IResourceBuilder<OracleDatabaseResource> AddDatabase(this IResourceBuilder<IOracleDatabaseParentResource> builder, string name) | ||
| { | ||
| var oracleDatabase = new OracleDatabaseResource(name, builder.Resource); | ||
| return builder.ApplicationBuilder.AddResource(oracleDatabase) | ||
| .WithManifestPublishingCallback(context => WriteOracleDatabaseToManifest(context, oracleDatabase)); | ||
| } | ||
|
|
||
| private static void WriteOracleDatabaseContainerToManifest(ManifestPublishingContext context) | ||
| { | ||
| context.Writer.WriteString("type", "oracle.server.v0"); | ||
| } | ||
|
|
||
| private static void WriteOracleDatabaseToManifest(ManifestPublishingContext context, OracleDatabaseResource oracleDatabase) | ||
| { | ||
| context.Writer.WriteString("type", "oracle.database.v0"); | ||
| context.Writer.WriteString("parent", oracleDatabase.Parent.Name); | ||
| } | ||
|
|
||
| private static void WriteOracleDatabaseContainerResourceToManifest(ManifestPublishingContext context, OracleDatabaseContainerResource resource) | ||
| { | ||
| context.WriteContainer(resource); | ||
| context.Writer.WriteString( // "connectionString": "...", | ||
| "connectionString", | ||
| $"user id=system;password={{{resource.Name}.inputs.password}};data source={{{resource.Name}.bindings.tcp.host}}:{{{resource.Name}.bindings.tcp.port}};"); | ||
| context.Writer.WriteStartObject("inputs"); // "inputs": { | ||
| context.Writer.WriteStartObject("password"); // "password": { | ||
| context.Writer.WriteString("type", "string"); // "type": "string", | ||
| context.Writer.WriteBoolean("secret", true); // "secret": true, | ||
| context.Writer.WriteStartObject("default"); // "default": { | ||
| context.Writer.WriteStartObject("generate"); // "generate": { | ||
| context.Writer.WriteNumber("minLength", 10); // "minLength": 10, | ||
| context.Writer.WriteEndObject(); // } | ||
| context.Writer.WriteEndObject(); // } | ||
| context.Writer.WriteEndObject(); // } | ||
| context.Writer.WriteEndObject(); // } | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
src/Aspire.Hosting/Oracle/OracleDatabaseContainerResource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Utils; | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// A resource that represents a Oracle Database container. | ||
| /// </summary> | ||
| /// <param name="name">The name of the resource.</param> | ||
| /// <param name="password">The Oracle Database server password.</param> | ||
| public class OracleDatabaseContainerResource(string name, string password) : ContainerResource(name), IOracleDatabaseParentResource | ||
| { | ||
| public string Password { get; } = password; | ||
|
|
||
| /// <summary> | ||
| /// Gets the connection string for the Oracle Database server. | ||
| /// </summary> | ||
| /// <returns>A connection string for the Oracle Database server in the form "user id=system;password=password;data source=localhost:port".</returns> | ||
| public string? GetConnectionString() | ||
| { | ||
| if (!this.TryGetAllocatedEndPoints(out var allocatedEndpoints)) | ||
| { | ||
| throw new DistributedApplicationException("Expected allocated endpoints!"); | ||
| } | ||
|
|
||
| var allocatedEndpoint = allocatedEndpoints.Single(); // We should only have one endpoint for Oracle Database. | ||
|
|
||
| var connectionString = $"user id=system;password={PasswordUtil.EscapePassword(Password)};data source={allocatedEndpoint.Address}:{allocatedEndpoint.Port}"; | ||
| return connectionString; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// A resource that represents a Oracle Database database. This is a child resource of a <see cref="OracleDatabaseContainerResource"/>. | ||
| /// </summary> | ||
| /// <param name="name">The name of the resource.</param> | ||
| /// <param name="oracleParentResource">The Oracle Database parent resource associated with this database.</param> | ||
| public class OracleDatabaseResource(string name, IOracleDatabaseParentResource oracleParentResource) : Resource(name), IResourceWithParent<IOracleDatabaseParentResource>, IResourceWithConnectionString | ||
| { | ||
| public IOracleDatabaseParentResource Parent { get; } = oracleParentResource; | ||
|
|
||
| /// <summary> | ||
| /// Gets the connection string for the Oracle Database. | ||
| /// </summary> | ||
| /// <returns>A connection string for the Oracle Database.</returns> | ||
| public string? GetConnectionString() | ||
| { | ||
| if (Parent.GetConnectionString() is { } connectionString) | ||
| { | ||
| return $"{connectionString}/{Name}"; | ||
| } | ||
| else | ||
| { | ||
| throw new DistributedApplicationException("Parent resource connection string was null."); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Utils; | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// A resource that represents a Oracle Database container. | ||
| /// </summary> | ||
| /// <param name="name">The name of the resource.</param> | ||
| /// <param name="password">The Oracle Database server password.</param> | ||
| public class OracleDatabaseServerResource(string name, string password) : Resource(name), IOracleDatabaseParentResource | ||
| { | ||
| public string Password { get; } = password; | ||
|
|
||
| /// <summary> | ||
| /// Gets the connection string for the Oracle Database server. | ||
| /// </summary> | ||
| /// <returns>A connection string for the Oracle Database server in the form "user id=system;password=password;data source=host:port".</returns> | ||
| public string? GetConnectionString() | ||
| { | ||
| if (!this.TryGetAllocatedEndPoints(out var allocatedEndpoints)) | ||
| { | ||
| throw new DistributedApplicationException("Expected allocated endpoints!"); | ||
| } | ||
|
|
||
| var allocatedEndpoint = allocatedEndpoints.Single(); // We should only have one endpoint for Oracle. | ||
|
|
||
| var connectionString = $"user id=system;password={PasswordUtil.EscapePassword(Password)};data source={allocatedEndpoint.Address}:{allocatedEndpoint.Port}"; | ||
| return connectionString; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/Components/Aspire.Oracle.EntityFrameworkCore/Aspire.Oracle.EntityFrameworkCore.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(NetCurrent)</TargetFramework> | ||
| <IsPackable>true</IsPackable> | ||
| <PackageTags>$(ComponentEfCorePackageTags) oracle sql</PackageTags> | ||
| <Description>An Oracle Database provider for Entity Framework Core that integrates with Aspire, including connection pooling, health check, logging, and telemetry.</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="..\Common\HealthChecksExtensions.cs" Link="HealthChecksExtensions.cs" /> | ||
| <Compile Include="..\Common\ConfigurationSchemaAttributes.cs" Link="ConfigurationSchemaAttributes.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||
| <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" /> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" /> | ||
| <PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.EventCounters" /> | ||
| <PackageReference Include="Oracle.EntityFrameworkCore" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should centralize this generated password logic into
PasswordUtils@mitchdennyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that it is different than SqlServer.
aspire/src/Aspire.Hosting/SqlServer/SqlServerBuilderExtensions.cs
Line 25 in bc220e3
and MySql
aspire/src/Aspire.Hosting/MySql/MySqlBuilderExtensions.cs
Line 27 in bc220e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably just come up with a decent password generator that we can use. What I did for SQL Server was half baked and really only appropriate for inner loop (which luckily is where it is used). I'd be happy to see this go in without change this password generation logic and instead see an issue created where we properly develop out PasswordUtils than can meet our various use cases (needs to be flexible enough to side step various limitations around what the containerized tool can used, and what we can escape in the connection string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know an issue hasn't been created yet, but I saw this comment and opened a PR that might address it: #1469.