1
0
mirror of https://github.com/bitwarden/server synced 2025-12-31 15:43:16 +00:00

Add support for destroying seeded data

This commit is contained in:
Hinton
2025-10-07 14:59:17 -07:00
parent 2b340b72da
commit 79f5d8f147
4 changed files with 83 additions and 13 deletions

View File

@@ -13,4 +13,14 @@ public interface IRecipeService
/// <exception cref="RecipeNotFoundException">Thrown when the recipe template is not found</exception>
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the recipe</exception>
object? ExecuteRecipe(string templateName, JsonElement? arguments);
/// <summary>
/// Destroys data created by a recipe with the given template name and arguments.
/// </summary>
/// <param name="templateName">The name of the recipe template (e.g., "OrganizationWithUsersRecipe")</param>
/// <param name="arguments">Optional JSON arguments to pass to the recipe's Destroy method</param>
/// <returns>The result returned by the recipe's Destroy method</returns>
/// <exception cref="RecipeNotFoundException">Thrown when the recipe template is not found</exception>
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the recipe</exception>
object? DestroyRecipe(string templateName, JsonElement? arguments);
}

View File

@@ -16,24 +16,34 @@ public class RecipeService : IRecipeService
}
public object? ExecuteRecipe(string templateName, JsonElement? arguments)
{
return ExecuteRecipeMethod(templateName, arguments, "Seed");
}
public object? DestroyRecipe(string templateName, JsonElement? arguments)
{
return ExecuteRecipeMethod(templateName, arguments, "Destroy");
}
private object? ExecuteRecipeMethod(string templateName, JsonElement? arguments, string methodName)
{
try
{
var recipeType = LoadRecipeType(templateName);
var seedMethod = GetSeedMethod(recipeType, templateName);
var method = GetRecipeMethod(recipeType, templateName, methodName);
var recipeInstance = Activator.CreateInstance(recipeType, _databaseContext)!;
var methodArguments = ParseMethodArguments(seedMethod, arguments);
var result = seedMethod.Invoke(recipeInstance, methodArguments);
var methodArguments = ParseMethodArguments(method, arguments);
var result = method.Invoke(recipeInstance, methodArguments);
_logger.LogInformation("Successfully executed recipe: {TemplateName}", templateName);
_logger.LogInformation("Successfully executed {MethodName} on recipe: {TemplateName}", methodName, templateName);
return result;
}
catch (Exception ex) when (ex is not RecipeNotFoundException and not RecipeExecutionException)
{
_logger.LogError(ex, "Unexpected error executing recipe: {TemplateName}", templateName);
_logger.LogError(ex, "Unexpected error executing {MethodName} on recipe: {TemplateName}", methodName, templateName);
throw new RecipeExecutionException(
$"An unexpected error occurred while executing recipe '{templateName}'",
$"An unexpected error occurred while executing {methodName} on recipe '{templateName}'",
ex.InnerException ?? ex);
}
}
@@ -48,10 +58,10 @@ public class RecipeService : IRecipeService
return recipeType ?? throw new RecipeNotFoundException(templateName);
}
private static MethodInfo GetSeedMethod(Type recipeType, string templateName)
private static MethodInfo GetRecipeMethod(Type recipeType, string templateName, string methodName)
{
var seedMethod = recipeType.GetMethod("Seed");
return seedMethod ?? throw new RecipeExecutionException($"Seed method not found in recipe '{templateName}'");
var method = recipeType.GetMethod(methodName);
return method ?? throw new RecipeExecutionException($"{methodName} method not found in recipe '{templateName}'");
}
private static object?[] ParseMethodArguments(MethodInfo seedMethod, JsonElement? arguments)