|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Net.Sockets; |
| 5 | +using Aspire.Hosting.ApplicationModel; |
| 6 | +using Aspire.Hosting.Publishing; |
| 7 | + |
| 8 | +namespace Aspire.Hosting; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Provides extension methods for adding Oracle Database resources to an <see cref="IDistributedApplicationBuilder"/>. |
| 12 | +/// </summary> |
| 13 | +public static class OracleDatabaseBuilderExtensions |
| 14 | +{ |
| 15 | + private const string PasswordEnvVarName = "ORACLE_PWD"; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Adds a Oracle Database container to the application model. The default image is "database/free" and the tag is "latest". |
| 19 | + /// </summary> |
| 20 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 21 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 22 | + /// <param name="port">The host port for Oracle Database.</param> |
| 23 | + /// <param name="password">The password for the Oracle Database container. Defaults to a random password.</param> |
| 24 | + /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseContainerResource}"/>.</returns> |
| 25 | + public static IResourceBuilder<OracleDatabaseContainerResource> AddOracleDatabaseContainer(this IDistributedApplicationBuilder builder, string name, int? port = null, string? password = null) |
| 26 | + { |
| 27 | + password = password ?? Guid.NewGuid().ToString("N").Substring(0, 30); |
| 28 | + var oracleDatabaseContainer = new OracleDatabaseContainerResource(name, password); |
| 29 | + return builder.AddResource(oracleDatabaseContainer) |
| 30 | + .WithManifestPublishingCallback(context => WriteOracleDatabaseContainerResourceToManifest(context, oracleDatabaseContainer)) |
| 31 | + .WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 1521)) |
| 32 | + .WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "latest", Registry = "container-registry.oracle.com" }) |
| 33 | + .WithEnvironment(context => |
| 34 | + { |
| 35 | + if (context.PublisherName == "manifest") |
| 36 | + { |
| 37 | + context.EnvironmentVariables.Add(PasswordEnvVarName, $"{{{oracleDatabaseContainer.Name}.inputs.password}}"); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + context.EnvironmentVariables.Add(PasswordEnvVarName, oracleDatabaseContainer.Password); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Adds a Oracle Database resource to the application model. A container is used for local development. |
| 48 | + /// </summary> |
| 49 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 50 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 51 | + /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseServerResource}"/>.</returns> |
| 52 | + public static IResourceBuilder<OracleDatabaseServerResource> AddOracleDatabase(this IDistributedApplicationBuilder builder, string name) |
| 53 | + { |
| 54 | + var password = Guid.NewGuid().ToString("N").Substring(0, 30); |
| 55 | + var oracleDatabaseServer = new OracleDatabaseServerResource(name, password); |
| 56 | + return builder.AddResource(oracleDatabaseServer) |
| 57 | + .WithManifestPublishingCallback(WriteOracleDatabaseContainerToManifest) |
| 58 | + .WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, containerPort: 1521)) |
| 59 | + .WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "latest", Registry = "container-registry.oracle.com" }) |
| 60 | + .WithEnvironment(PasswordEnvVarName, () => oracleDatabaseServer.Password); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Adds a Oracle Database database to the application model. |
| 65 | + /// </summary> |
| 66 | + /// <param name="builder">The Oracle Database server resource builder.</param> |
| 67 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 68 | + /// <returns>A reference to the <see cref="IResourceBuilder{OracleDatabaseResource}"/>.</returns> |
| 69 | + public static IResourceBuilder<OracleDatabaseResource> AddDatabase(this IResourceBuilder<IOracleDatabaseParentResource> builder, string name) |
| 70 | + { |
| 71 | + var oracleDatabase = new OracleDatabaseResource(name, builder.Resource); |
| 72 | + return builder.ApplicationBuilder.AddResource(oracleDatabase) |
| 73 | + .WithManifestPublishingCallback(context => WriteOracleDatabaseToManifest(context, oracleDatabase)); |
| 74 | + } |
| 75 | + |
| 76 | + private static void WriteOracleDatabaseContainerToManifest(ManifestPublishingContext context) |
| 77 | + { |
| 78 | + context.Writer.WriteString("type", "oracle.server.v0"); |
| 79 | + } |
| 80 | + |
| 81 | + private static void WriteOracleDatabaseToManifest(ManifestPublishingContext context, OracleDatabaseResource oracleDatabase) |
| 82 | + { |
| 83 | + context.Writer.WriteString("type", "oracle.database.v0"); |
| 84 | + context.Writer.WriteString("parent", oracleDatabase.Parent.Name); |
| 85 | + } |
| 86 | + |
| 87 | + private static void WriteOracleDatabaseContainerResourceToManifest(ManifestPublishingContext context, OracleDatabaseContainerResource resource) |
| 88 | + { |
| 89 | + context.WriteContainer(resource); |
| 90 | + context.Writer.WriteString( // "connectionString": "...", |
| 91 | + "connectionString", |
| 92 | + $"user id=system;password={{{resource.Name}.inputs.password}};data source={{{resource.Name}.bindings.tcp.host}}:{{{resource.Name}.bindings.tcp.port}};"); |
| 93 | + context.Writer.WriteStartObject("inputs"); // "inputs": { |
| 94 | + context.Writer.WriteStartObject("password"); // "password": { |
| 95 | + context.Writer.WriteString("type", "string"); // "type": "string", |
| 96 | + context.Writer.WriteBoolean("secret", true); // "secret": true, |
| 97 | + context.Writer.WriteStartObject("default"); // "default": { |
| 98 | + context.Writer.WriteStartObject("generate"); // "generate": { |
| 99 | + context.Writer.WriteNumber("minLength", 10); // "minLength": 10, |
| 100 | + context.Writer.WriteEndObject(); // } |
| 101 | + context.Writer.WriteEndObject(); // } |
| 102 | + context.Writer.WriteEndObject(); // } |
| 103 | + context.Writer.WriteEndObject(); // } |
| 104 | + } |
| 105 | +} |
0 commit comments