From 17bd708116d2f4bee4476226ba1e50c571facf4b Mon Sep 17 00:00:00 2001 From: Phil Schneider Date: Thu, 6 Jul 2023 09:10:37 +0200 Subject: [PATCH] chore: adding validation for seeder settings --- docker/Dockerfile-provisioning-migrations | 2 + .../DatabaseInitializerExtensions.cs | 10 ++--- .../Framework.Seeding.csproj | 3 ++ .../Framework.Seeding/SeederSettings.cs | 39 +++++++++++++++++++ .../PortalBackend.Migrations/appsettings.json | 1 + 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/docker/Dockerfile-provisioning-migrations b/docker/Dockerfile-provisioning-migrations index aee7bcf00e..5d8d208419 100644 --- a/docker/Dockerfile-provisioning-migrations +++ b/docker/Dockerfile-provisioning-migrations @@ -26,6 +26,8 @@ COPY /src/provisioning /src/provisioning COPY /src/framework/Framework.ErrorHandling.Library /src/framework/Framework.ErrorHandling.Library COPY /src/framework/Framework.BaseDependencies /src/framework/Framework.BaseDependencies COPY /src/framework/Framework.Seeding /src/framework/Framework.Seeding +COPY /src/framework/Framework.Models /src/framework/Framework.Models +COPY /src/framework/Framework.Linq /src/framework/Framework.Linq WORKDIR /src/provisioning/Provisioning.Migrations RUN dotnet build "Provisioning.Migrations.csproj" -c Release -o /migrations/build diff --git a/src/framework/Framework.Seeding/DependencyInjection/DatabaseInitializerExtensions.cs b/src/framework/Framework.Seeding/DependencyInjection/DatabaseInitializerExtensions.cs index c3b42a66f9..62e51cfb4a 100644 --- a/src/framework/Framework.Seeding/DependencyInjection/DatabaseInitializerExtensions.cs +++ b/src/framework/Framework.Seeding/DependencyInjection/DatabaseInitializerExtensions.cs @@ -35,18 +35,14 @@ await scope.ServiceProvider.GetRequiredService() .InitializeDatabasesAsync(cancellationToken); } - public static IServiceCollection AddDatabaseInitializer(this IServiceCollection services, IConfigurationSection section) where TDbContext : DbContext - { - services.AddOptions() - .Bind(section); - - return services + public static IServiceCollection AddDatabaseInitializer(this IServiceCollection services, IConfigurationSection section) where TDbContext : DbContext => + services + .ConfigureSeederSettings(section) .AddTransient>() .AddTransient>() .AddTransient() .AddServices(typeof(ICustomSeeder), ServiceLifetime.Transient) .AddTransient(); - } private static IServiceCollection AddServices(this IServiceCollection services, Type interfaceType, ServiceLifetime lifetime) { diff --git a/src/framework/Framework.Seeding/Framework.Seeding.csproj b/src/framework/Framework.Seeding/Framework.Seeding.csproj index 4033f522d5..c63c3ea3cd 100644 --- a/src/framework/Framework.Seeding/Framework.Seeding.csproj +++ b/src/framework/Framework.Seeding/Framework.Seeding.csproj @@ -35,12 +35,15 @@ + + + diff --git a/src/framework/Framework.Seeding/SeederSettings.cs b/src/framework/Framework.Seeding/SeederSettings.cs index 3339841282..60cc165ea2 100644 --- a/src/framework/Framework.Seeding/SeederSettings.cs +++ b/src/framework/Framework.Seeding/SeederSettings.cs @@ -1,3 +1,26 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 BMW Group AG + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Validation; using System.ComponentModel.DataAnnotations; namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding; @@ -22,3 +45,19 @@ public SeederSettings() [Required] public IEnumerable TestDataEnvironments { get; set; } } + +public static class SeederSettingsExtensions +{ + public static IServiceCollection ConfigureSeederSettings( + this IServiceCollection services, + IConfigurationSection section) + { + services.AddOptions() + .Bind(section) + .ValidateDataAnnotations() + .ValidateEnumEnumeration(section) + .ValidateDistinctValues(section) + .ValidateOnStart(); + return services; + } +} diff --git a/src/portalbackend/PortalBackend.Migrations/appsettings.json b/src/portalbackend/PortalBackend.Migrations/appsettings.json index 46387a085b..f3e92a274c 100644 --- a/src/portalbackend/PortalBackend.Migrations/appsettings.json +++ b/src/portalbackend/PortalBackend.Migrations/appsettings.json @@ -26,6 +26,7 @@ }, "DeleteIntervalInDays": 80, "Seeding":{ + "DataPaths":[], "TestDataEnvironments": [] } }