-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
1191e99
commit e7c9111
Showing
6 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
Daybreak/Services/Startup/Actions/CredentialsOptionsMigrator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Daybreak.Attributes; | ||
using Daybreak.Configuration.Options; | ||
using Daybreak.Services.Options; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Core.Extensions; | ||
using System.Reflection; | ||
|
||
namespace Daybreak.Services.Startup.Actions; | ||
internal sealed class CredentialsOptionsMigrator : StartupActionBase | ||
{ | ||
private const string OldOptionsKey = "CredentialManagerOptions"; | ||
|
||
private readonly IOptionsProvider optionsProvider; | ||
private readonly ILogger<CredentialsOptionsMigrator> logger; | ||
|
||
public CredentialsOptionsMigrator( | ||
IOptionsProvider optionsProvider, | ||
ILogger<CredentialsOptionsMigrator> logger) | ||
{ | ||
this.optionsProvider = optionsProvider.ThrowIfNull(); | ||
this.logger = logger.ThrowIfNull(); | ||
} | ||
|
||
public override void ExecuteOnStartup() | ||
{ | ||
if (this.optionsProvider.TryGetKeyedOptions(OldOptionsKey) is not JObject options) | ||
{ | ||
return; | ||
} | ||
|
||
var newOptionsKey = GetNewOptionsKey(); | ||
this.logger.LogInformation($"Found [{OldOptionsKey}]. Migrating options to [{newOptionsKey}]"); | ||
this.optionsProvider.SaveRegisteredOptions(newOptionsKey, options); | ||
} | ||
|
||
private static string GetNewOptionsKey() | ||
{ | ||
return typeof(CredentialManagerOptions).GetCustomAttribute<OptionsNameAttribute>()?.Name ?? throw new InvalidOperationException("Unable to retrieve new credentials options key"); | ||
} | ||
} |