-
Notifications
You must be signed in to change notification settings - Fork 854
Add OracleManagedDataAccess Aspire Component #1004
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
Closed
Closed
Changes from all commits
Commits
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
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 IOracleDatabaseResource : IResourceWithConnectionString | ||
| { | ||
| } |
81 changes: 81 additions & 0 deletions
81
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,81 @@ | ||
| // 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 System.Text.Json; | ||
| using Aspire.Hosting.ApplicationModel; | ||
|
|
||
| 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_DATABASE_PASSWORD"; | ||
|
|
||
| /// <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"); | ||
| var oracleDatabaseContainer = new OracleDatabaseContainerResource(name, password); | ||
| return builder.AddResource(oracleDatabaseContainer) | ||
| .WithAnnotation(new ManifestPublishingCallbackAnnotation(WriteOracleDatabaseContainerToManifest)) | ||
| .WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, port: port, containerPort: 1521)) | ||
| .WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "latest", Registry = "container-registry.oracle.com" }) | ||
| .WithEnvironment(PasswordEnvVarName, () => oracleDatabaseContainer.Password); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a Oracle Database connection to the application model. Connection strings can also be read from the connection string section of the configuration using the name of the resource. | ||
| /// </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="connectionString">The Oracle Database connection string (optional).</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseConnectionResource}"/>.</returns> | ||
| public static IResourceBuilder<OracleDatabaseConnectionResource> AddOracleDatabaseConnection(this IDistributedApplicationBuilder builder, string name, string? connectionString = null) | ||
| { | ||
| var oracleDatabaseConnection = new OracleDatabaseConnectionResource(name, connectionString); | ||
|
|
||
| return builder.AddResource(oracleDatabaseConnection) | ||
| .WithAnnotation(new ManifestPublishingCallbackAnnotation((json) => WriteOracleDatabaseConnectionToManifest(json, oracleDatabaseConnection))); | ||
| } | ||
|
|
||
| /// <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<OracleDatabaseContainerResource> builder, string name) | ||
| { | ||
| var oracleDatabase = new OracleDatabaseResource(name, builder.Resource); | ||
| return builder.ApplicationBuilder.AddResource(oracleDatabase) | ||
| .WithAnnotation(new ManifestPublishingCallbackAnnotation( | ||
| (json) => WriteOracleDatabaseToManifest(json, oracleDatabase))); | ||
| } | ||
|
|
||
| private static void WriteOracleDatabaseConnectionToManifest(Utf8JsonWriter jsonWriter, OracleDatabaseConnectionResource oracleDatabaseConnection) | ||
| { | ||
| jsonWriter.WriteString("type", "oracle.connection.v0"); | ||
| jsonWriter.WriteString("connectionString", oracleDatabaseConnection.GetConnectionString()); | ||
| } | ||
|
|
||
| private static void WriteOracleDatabaseContainerToManifest(Utf8JsonWriter jsonWriter) | ||
| { | ||
| jsonWriter.WriteString("type", "oracle.server.v0"); | ||
| } | ||
|
|
||
| private static void WriteOracleDatabaseToManifest(Utf8JsonWriter json, OracleDatabaseResource oracleDatabase) | ||
| { | ||
| json.WriteString("type", "oracle.database.v0"); | ||
| json.WriteString("parent", oracleDatabase.Parent.Name); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/Aspire.Hosting/Oracle/OracleDatabaseConnectionResource.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,20 @@ | ||
| // 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 connection. | ||
| /// </summary> | ||
| /// <param name="name">The name of the resource.</param> | ||
| /// <param name="connectionString">The Oracle Database connection string.</param> | ||
| public class OracleDatabaseConnectionResource(string name, string? connectionString) : Resource(name), IOracleDatabaseResource | ||
| { | ||
| private readonly string? _connectionString = connectionString; | ||
|
|
||
| /// <summary> | ||
| /// Gets the connection string for the Oracle Database server. | ||
| /// </summary> | ||
| /// <returns>The specified connection string.</returns> | ||
| public string? GetConnectionString() => _connectionString; | ||
| } |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
| // 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 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), IOracleDatabaseResource | ||
| { | ||
| 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={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="oracleContainer">The Oracle Database server resource associated with this database.</param> | ||
| public class OracleDatabaseResource(string name, OracleDatabaseContainerResource oracleContainer) : Resource(name), IOracleDatabaseResource, IResourceWithParent<OracleDatabaseContainerResource> | ||
| { | ||
| public OracleDatabaseContainerResource Parent { get; } = oracleContainer; | ||
|
|
||
| /// <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."); | ||
| } | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
...mponents/Aspire.Oracle.ManagedDataAccess.Core/Aspire.Oracle.ManagedDataAccess.Core.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,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(NetCurrent)</TargetFramework> | ||
| <IsPackable>true</IsPackable> | ||
| <PackageTags>$(ComponentDatabasePackageTags) oracle odp managed data access core sql</PackageTags> | ||
| <Description>A Oracle® client that integrates with Aspire, including health checks, metrics, logging, and telemetry.</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="..\Common\HealthChecksExtensions.cs" Link="HealthChecksExtensions.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="AspNetCore.HealthChecks.Oracle" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||
| <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" /> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" /> | ||
| <PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
| <PackageReference Include="Oracle.ManagedDataAccess.Core" /> | ||
| <PackageReference Include="Oracle.ManagedDataAccess.OpenTelemetry" /> | ||
| </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.
Uh oh!
There was an error while loading. Please reload this page.