Skip to content

Commit

Permalink
Add option to specify the tenantId, useful if using a "guest" user on…
Browse files Browse the repository at this point in the history
… several tenants
  • Loading branch information
schuettecarsten authored and pohhsu committed May 31, 2024
1 parent 5a76ce3 commit 4d1a371
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions migrationTool/GlobalOptionsBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ internal class GlobalOptionsBinder : BinderBase<GlobalOptions>
description: @"The directory where the logs are written. Defaults to the working directory"
);

private static readonly Option<string> _tenant = new Option<string>(
aliases: new[] { "--tenant" },
description: "The azure tenant to use")
{
Arity = ArgumentArity.ExactlyOne
};

private static readonly Option<string> _subscription = new Option<string>(
aliases: new[] { "--subscription", "-s" },
description: "The azure subscription to use")
Expand Down Expand Up @@ -51,6 +58,7 @@ public Command GetCommand()
var command = new RootCommand("Azure Media Services migration tool");
command.AddGlobalOption(_logLevel);
command.AddGlobalOption(_logDirectory);
command.AddGlobalOption(_tenant);
command.AddGlobalOption(_subscription);
command.AddGlobalOption(_resourceGroup);

Expand All @@ -60,6 +68,7 @@ public Command GetCommand()
public static GlobalOptions GetValue(BindingContext bindingContext)
{
return new GlobalOptions(
bindingContext.ParseResult.GetValueForOption(_tenant)!,
bindingContext.ParseResult.GetValueForOption(_subscription)!,
bindingContext.ParseResult.GetValueForOption(_resourceGroup)!,
CloudType.Azure, //TODO: add an option.
Expand Down
16 changes: 15 additions & 1 deletion migrationTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,22 @@ static void SetupServices(IServiceCollection collection, GlobalOptions options)
.WriteTo.File(options.LogFile)
.CreateLogger();

string tenantId = options.TenantId;
if (string.IsNullOrEmpty(tenantId))
{
collection
.AddSingleton<TokenCredential>(new DefaultAzureCredential(includeInteractiveCredentials: true));
}
else
{
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions()
{
TenantId = tenantId,
});
collection.AddSingleton<TokenCredential>(credential);
}

collection
.AddSingleton<TokenCredential>(new DefaultAzureCredential(includeInteractiveCredentials: true))
.AddSingleton(options)
.AddSingleton(console)
.AddSingleton<IMigrationTracker<BlobContainerClient, AssetMigrationResult>, AssetMigrationTracker>()
Expand Down
1 change: 1 addition & 0 deletions migrationTool/contracts/GlobalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AMSMigrate.Contracts
{
public record GlobalOptions(
string TenantId,
string SubscriptionId,
string ResourceGroup,
CloudType CloudType,
Expand Down

0 comments on commit 4d1a371

Please sign in to comment.