Skip to content
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

chore: adding validation for seeder settings #126

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker/Dockerfile-provisioning-migrations
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ await scope.ServiceProvider.GetRequiredService<IDatabaseInitializer>()
.InitializeDatabasesAsync(cancellationToken);
}

public static IServiceCollection AddDatabaseInitializer<TDbContext>(this IServiceCollection services, IConfigurationSection section) where TDbContext : DbContext
{
services.AddOptions<SeederSettings>()
.Bind(section);

return services
public static IServiceCollection AddDatabaseInitializer<TDbContext>(this IServiceCollection services, IConfigurationSection section) where TDbContext : DbContext =>
services
.ConfigureSeederSettings(section)
.AddTransient<IDatabaseInitializer, DatabaseInitializer<TDbContext>>()
.AddTransient<DbInitializer<TDbContext>>()
.AddTransient<DbSeeder>()
.AddServices(typeof(ICustomSeeder), ServiceLifetime.Transient)
.AddTransient<CustomSeederRunner>();
}

private static IServiceCollection AddServices(this IServiceCollection services, Type interfaceType, ServiceLifetime lifetime)
{
Expand Down
3 changes: 3 additions & 0 deletions src/framework/Framework.Seeding/Framework.Seeding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Framework.ErrorHandling.Library\Framework.ErrorHandling.Library.csproj" />
<ProjectReference Include="..\Framework.Models\Framework.Models.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions src/framework/Framework.Seeding/SeederSettings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,3 +45,19 @@ public SeederSettings()
[Required]
public IEnumerable<string> TestDataEnvironments { get; set; }
}

public static class SeederSettingsExtensions
{
public static IServiceCollection ConfigureSeederSettings(
this IServiceCollection services,
IConfigurationSection section)
{
services.AddOptions<SeederSettings>()
.Bind(section)
.ValidateDataAnnotations()
.ValidateEnumEnumeration(section)
.ValidateDistinctValues(section)
.ValidateOnStart();
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"DeleteIntervalInDays": 80,
"Seeding":{
"DataPaths":[],
"TestDataEnvironments": []
}
}