1
0
mirror of https://github.com/bitwarden/server synced 2025-12-14 07:13:39 +00:00

Split scene service and query service

rename instances of `recipe` to `scene`
This commit is contained in:
Matt Gibson
2025-10-30 09:08:36 -07:00
parent 2d50d05587
commit 3f22adcbf2
13 changed files with 217 additions and 86 deletions

22
.vscode/launch.json vendored
View File

@@ -69,6 +69,28 @@
"preLaunchTask": "buildFullServer",
"stopAll": true
},
{
"name": "Full Server with Seeder API",
"configurations": [
"run-Admin",
"run-API",
"run-Events",
"run-EventsProcessor",
"run-Identity",
"run-Sso",
"run-Icons",
"run-Billing",
"run-Notifications",
"run-SeederAPI"
],
"presentation": {
"hidden": false,
"group": "AA_compounds",
"order": 6
},
"preLaunchTask": "buildFullServerWithSeederApi",
"stopAll": true
},
{
"name": "Self Host: Bit",
"configurations": [

16
.vscode/tasks.json vendored
View File

@@ -54,6 +54,22 @@
"buildNotifications"
],
},
{
"label": "buildFullServerWithSeederApi",
"hide": true,
"dependsOrder": "sequence",
"dependsOn": [
"buildAdmin",
"buildAPI",
"buildEventsProcessor",
"buildIdentity",
"buildSso",
"buildIcons",
"buildBilling",
"buildNotifications",
"buildSeederAPI"
],
},
{
"label": "buildSelfHostBit",
"hide": true,

View File

@@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc;
namespace Bit.SeederApi.Controllers;
[Route("query")]
public class QueryController(ILogger<QueryController> logger, ISeedService recipeService)
public class QueryController(ILogger<QueryController> logger, IQueryService queryService)
: Controller
{
[HttpPost]
@@ -15,15 +15,15 @@ public class QueryController(ILogger<QueryController> logger, ISeedService recip
try
{
var result = recipeService.ExecuteQuery(request.Template, request.Arguments);
var result = queryService.ExecuteQuery(request.Template, request.Arguments);
return Json(new { Result = result });
return Json(result);
}
catch (RecipeNotFoundException ex)
catch (SceneNotFoundException ex)
{
return NotFound(new { Error = ex.Message });
}
catch (RecipeExecutionException ex)
catch (SceneExecutionException ex)
{
logger.LogError(ex, "Error executing query: {Query}", request.Template);
return BadRequest(new

View File

@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Mvc;
namespace Bit.SeederApi.Controllers;
[Route("seed")]
public class SeedController(ILogger<SeedController> logger, ISeedService recipeService)
public class SeedController(ILogger<SeedController> logger, ISceneService sceneService)
: Controller
{
[HttpPost]
@@ -16,15 +16,15 @@ public class SeedController(ILogger<SeedController> logger, ISeedService recipeS
try
{
SceneResponseModel response = recipeService.ExecuteScene(request.Template, request.Arguments);
SceneResponseModel response = sceneService.ExecuteScene(request.Template, request.Arguments);
return Json(response);
}
catch (RecipeNotFoundException ex)
catch (SceneNotFoundException ex)
{
return NotFound(new { Error = ex.Message });
}
catch (RecipeExecutionException ex)
catch (SceneExecutionException ex)
{
logger.LogError(ex, "Error executing scene: {Template}", request.Template);
return BadRequest(new
@@ -48,7 +48,7 @@ public class SeedController(ILogger<SeedController> logger, ISeedService recipeS
{
try
{
await recipeService.DestroyRecipe(seedId);
await sceneService.DestroyScene(seedId);
}
catch (Exception ex)
{
@@ -79,11 +79,11 @@ public class SeedController(ILogger<SeedController> logger, ISeedService recipeS
try
{
var result = await recipeService.DestroyRecipe(seedId);
var result = await sceneService.DestroyScene(seedId);
return Json(result);
}
catch (RecipeExecutionException ex)
catch (SceneExecutionException ex)
{
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
return BadRequest(new
@@ -101,7 +101,7 @@ public class SeedController(ILogger<SeedController> logger, ISeedService recipeS
logger.LogInformation("Deleting all seeded data");
// Pull all Seeded Data ids
var seededData = recipeService.GetAllSeededData();
var seededData = sceneService.GetAllSeededData();
var aggregateException = new AggregateException();
@@ -111,7 +111,7 @@ public class SeedController(ILogger<SeedController> logger, ISeedService recipeS
{
try
{
await recipeService.DestroyRecipe(sd.Id);
await sceneService.DestroyScene(sd.Id);
}
catch (Exception ex)
{

View File

@@ -19,7 +19,7 @@ builder.Services.AddScoped<Microsoft.AspNetCore.Identity.IPasswordHasher<Bit.Cor
// Seeder services
builder.Services.AddSingleton<Bit.RustSDK.RustSdkService>();
builder.Services.AddScoped<Bit.Seeder.Factories.UserSeeder>();
builder.Services.AddScoped<ISeedService, SeedService>();
builder.Services.AddScoped<ISceneService, SceneService>();
builder.Services.AddScoped<MangleId>(_ => new MangleId());
builder.Services.AddScenes();
builder.Services.AddQueries();

View File

@@ -0,0 +1,17 @@
using System.Text.Json;
namespace Bit.SeederApi.Services;
public interface IQueryService
{
/// <summary>
/// Executes a query with the given query name and arguments.
/// Queries are read-only and do not track entities or create seed IDs.
/// </summary>
/// <param name="queryName">The name of the query (e.g., "EmergencyAccessInviteQuery")</param>
/// <param name="arguments">Optional JSON arguments to pass to the query's Execute method</param>
/// <returns>The result of the query execution</returns>
/// <exception cref="SceneNotFoundException">Thrown when the query is not found</exception>
/// <exception cref="SceneExecutionException">Thrown when there's an error executing the query</exception>
object ExecuteQuery(string queryName, JsonElement? arguments);
}

View File

@@ -1,38 +0,0 @@
using System.Text.Json;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.SeederApi.Models.Response;
namespace Bit.SeederApi.Services;
public interface ISeedService
{
/// <summary>
/// Executes a scene with the given template name and arguments.
/// </summary>
/// <param name="templateName">The name of the scene template (e.g., "SingleUserScene")</param>
/// <param name="arguments">Optional JSON arguments to pass to the scene's Seed method</param>
/// <returns>A tuple containing the result and optional seed ID for tracked entities</returns>
/// <exception cref="RecipeNotFoundException">Thrown when the scene template is not found</exception>
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the scene</exception>
SceneResponseModel ExecuteScene(string templateName, JsonElement? arguments);
/// <summary>
/// Destroys data created by a scene using the seeded data ID.
/// </summary>
/// <param name="seedId">The ID of the seeded data to destroy</param>
/// <returns>The result of the destroy operation</returns>
/// <exception cref="RecipeExecutionException">Thrown when there's an error destroying the seeded data</exception>
Task<object?> DestroyRecipe(Guid seedId);
List<SeededData> GetAllSeededData();
/// <summary>
/// Executes a query with the given query name and arguments.
/// Queries are read-only and do not track entities or create seed IDs.
/// </summary>
/// <param name="queryName">The name of the query (e.g., "EmergencyAccessInviteQuery")</param>
/// <param name="arguments">Optional JSON arguments to pass to the query's Execute method</param>
/// <returns>The result of the query execution</returns>
/// <exception cref="RecipeNotFoundException">Thrown when the query is not found</exception>
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the query</exception>
object ExecuteQuery(string queryName, JsonElement? arguments);
}

View File

@@ -0,0 +1,27 @@
using System.Text.Json;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.SeederApi.Models.Response;
namespace Bit.SeederApi.Services;
public interface ISceneService
{
/// <summary>
/// Executes a scene with the given template name and arguments.
/// </summary>
/// <param name="templateName">The name of the scene template (e.g., "SingleUserScene")</param>
/// <param name="arguments">Optional JSON arguments to pass to the scene's Seed method</param>
/// <returns>A tuple containing the result and optional seed ID for tracked entities</returns>
/// <exception cref="SceneNotFoundException">Thrown when the scene template is not found</exception>
/// <exception cref="SceneExecutionException">Thrown when there's an error executing the scene</exception>
SceneResponseModel ExecuteScene(string templateName, JsonElement? arguments);
/// <summary>
/// Destroys data created by a scene using the seeded data ID.
/// </summary>
/// <param name="seedId">The ID of the seeded data to destroy</param>
/// <returns>The result of the destroy operation</returns>
/// <exception cref="SceneExecutionException">Thrown when there's an error destroying the seeded data</exception>
Task<object?> DestroyScene(Guid seedId);
List<SeededData> GetAllSeededData();
}

View File

@@ -0,0 +1,10 @@
namespace Bit.SeederApi.Services;
public class QueryNotFoundException(string query) : Exception($"Query '{query}' not found");
public class QueryExecutionException : Exception
{
public QueryExecutionException(string message) : base(message) { }
public QueryExecutionException(string message, Exception innerException)
: base(message, innerException) { }
}

View File

@@ -0,0 +1,77 @@
using System.Text.Json;
using Bit.Seeder;
namespace Bit.SeederApi.Services;
public class QueryService(
ILogger<QueryService> logger,
IServiceProvider serviceProvider)
: IQueryService
{
private static readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public object ExecuteQuery(string queryName, JsonElement? arguments)
{
try
{
var query = serviceProvider.GetKeyedService<IQuery>(queryName)
?? throw new QueryNotFoundException(queryName);
var requestType = query.GetRequestType();
// Deserialize the arguments into the request model
object? requestModel;
if (arguments == null)
{
// Try to create an instance with default values
try
{
requestModel = Activator.CreateInstance(requestType);
if (requestModel == null)
{
throw new QueryExecutionException(
$"Arguments are required for query '{queryName}'");
}
}
catch
{
throw new QueryExecutionException(
$"Arguments are required for query '{queryName}'");
}
}
else
{
try
{
requestModel = JsonSerializer.Deserialize(arguments.Value.GetRawText(), requestType, _jsonOptions);
if (requestModel == null)
{
throw new QueryExecutionException(
$"Failed to deserialize request model for query '{queryName}'");
}
}
catch (JsonException ex)
{
throw new QueryExecutionException(
$"Failed to deserialize request model for query '{queryName}': {ex.Message}", ex);
}
}
var result = query.Execute(requestModel);
logger.LogInformation("Successfully executed query: {QueryName}", queryName);
return result;
}
catch (Exception ex) when (ex is not QueryNotFoundException and not QueryExecutionException)
{
logger.LogError(ex, "Unexpected error executing query: {QueryName}", queryName);
throw new QueryExecutionException(
$"An unexpected error occurred while executing query '{queryName}'",
ex.InnerException ?? ex);
}
}
}

View File

@@ -1,10 +0,0 @@
namespace Bit.SeederApi.Services;
public class RecipeNotFoundException(string recipe) : Exception($"Recipe '{recipe}' not found");
public class RecipeExecutionException : Exception
{
public RecipeExecutionException(string message) : base(message) { }
public RecipeExecutionException(string message, Exception innerException)
: base(message, innerException) { }
}

View File

@@ -0,0 +1,10 @@
namespace Bit.SeederApi.Services;
public class SceneNotFoundException(string scene) : Exception($"Scene '{scene}' not found");
public class SceneExecutionException : Exception
{
public SceneExecutionException(string message) : base(message) { }
public SceneExecutionException(string message, Exception innerException)
: base(message, innerException) { }
}

View File

@@ -7,13 +7,13 @@ using Bit.SeederApi.Models.Response;
namespace Bit.SeederApi.Services;
public class SeedService(
public class SceneService(
DatabaseContext databaseContext,
ILogger<SeedService> logger,
ILogger<SceneService> logger,
IServiceProvider serviceProvider,
IUserRepository userRepository,
IOrganizationRepository organizationRepository)
: ISeedService
: ISceneService
{
private static readonly JsonSerializerOptions _jsonOptions = new()
{
@@ -46,7 +46,7 @@ public class SeedService(
databaseContext.Add(seededData);
databaseContext.SaveChanges();
logger.LogInformation("Saved seeded data with ID {SeedId} for scene {RecipeName}",
logger.LogInformation("Saved seeded data with ID {SeedId} for scene {SceneName}",
seededData.Id, templateName);
return SceneResponseModel.FromSceneResult(result, seededData.Id);
@@ -57,7 +57,7 @@ public class SeedService(
try
{
var query = serviceProvider.GetKeyedService<IQuery>(queryName)
?? throw new RecipeNotFoundException(queryName);
?? throw new SceneNotFoundException(queryName);
var requestType = query.GetRequestType();
@@ -71,13 +71,13 @@ public class SeedService(
requestModel = Activator.CreateInstance(requestType);
if (requestModel == null)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Arguments are required for query '{queryName}'");
}
}
catch
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Arguments are required for query '{queryName}'");
}
}
@@ -88,13 +88,13 @@ public class SeedService(
requestModel = JsonSerializer.Deserialize(arguments.Value.GetRawText(), requestType, _jsonOptions);
if (requestModel == null)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Failed to deserialize request model for query '{queryName}'");
}
}
catch (JsonException ex)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Failed to deserialize request model for query '{queryName}': {ex.Message}", ex);
}
}
@@ -104,16 +104,16 @@ public class SeedService(
logger.LogInformation("Successfully executed query: {QueryName}", queryName);
return result;
}
catch (Exception ex) when (ex is not RecipeNotFoundException and not RecipeExecutionException)
catch (Exception ex) when (ex is not SceneNotFoundException and not SceneExecutionException)
{
logger.LogError(ex, "Unexpected error executing query: {QueryName}", queryName);
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"An unexpected error occurred while executing query '{queryName}'",
ex.InnerException ?? ex);
}
}
public async Task<object?> DestroyRecipe(Guid seedId)
public async Task<object?> DestroyScene(Guid seedId)
{
var seededData = databaseContext.SeededData.FirstOrDefault(s => s.Id == seedId);
if (seededData == null)
@@ -125,7 +125,7 @@ public class SeedService(
var trackedEntities = JsonSerializer.Deserialize<Dictionary<string, List<Guid>>>(seededData.Data);
if (trackedEntities == null)
{
throw new RecipeExecutionException($"Failed to deserialize tracked entities for seed ID {seedId}");
throw new SceneExecutionException($"Failed to deserialize tracked entities for seed ID {seedId}");
}
// Delete in reverse order to respect foreign key constraints
@@ -152,7 +152,7 @@ public class SeedService(
}
if (aggregateException.InnerExceptions.Count > 0)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"One or more errors occurred while deleting organizations for seed ID {seedId}",
aggregateException);
}
@@ -161,10 +161,10 @@ public class SeedService(
databaseContext.Remove(seededData);
databaseContext.SaveChanges();
logger.LogInformation("Successfully destroyed seeded data with ID {SeedId} for scene {RecipeName}",
logger.LogInformation("Successfully destroyed seeded data with ID {SeedId} for scene {SceneName}",
seedId, seededData.RecipeName);
return new { SeedId = seedId, RecipeName = seededData.RecipeName };
return new { SeedId = seedId, SceneName = seededData.RecipeName };
}
private SceneResult<object?> ExecuteSceneMethod(string templateName, JsonElement? arguments, string methodName)
@@ -172,7 +172,7 @@ public class SeedService(
try
{
var scene = serviceProvider.GetKeyedService<IScene>(templateName)
?? throw new RecipeNotFoundException(templateName);
?? throw new SceneNotFoundException(templateName);
var requestType = scene.GetRequestType();
@@ -186,13 +186,13 @@ public class SeedService(
requestModel = Activator.CreateInstance(requestType);
if (requestModel == null)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Arguments are required for scene '{templateName}'");
}
}
catch
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Arguments are required for scene '{templateName}'");
}
}
@@ -203,13 +203,13 @@ public class SeedService(
requestModel = JsonSerializer.Deserialize(arguments.Value.GetRawText(), requestType, _jsonOptions);
if (requestModel == null)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Failed to deserialize request model for scene '{templateName}'");
}
}
catch (JsonException ex)
{
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"Failed to deserialize request model for scene '{templateName}': {ex.Message}", ex);
}
}
@@ -219,10 +219,10 @@ public class SeedService(
logger.LogInformation("Successfully executed {MethodName} on scene: {TemplateName}", methodName, templateName);
return result;
}
catch (Exception ex) when (ex is not RecipeNotFoundException and not RecipeExecutionException)
catch (Exception ex) when (ex is not SceneNotFoundException and not SceneExecutionException)
{
logger.LogError(ex, "Unexpected error executing {MethodName} on scene: {TemplateName}", methodName, templateName);
throw new RecipeExecutionException(
throw new SceneExecutionException(
$"An unexpected error occurred while executing {methodName} on scene '{templateName}'",
ex.InnerException ?? ex);
}