From 4d1a37174a69306593eee0492781c882e219e894 Mon Sep 17 00:00:00 2001 From: Carsten Schuette Date: Fri, 31 May 2024 10:31:04 +0200 Subject: [PATCH] Add option to specify the tenantId, useful if using a "guest" user on several tenants --- migrationTool/GlobalOptionsBinder.cs | 9 +++++++++ migrationTool/Program.cs | 16 +++++++++++++++- migrationTool/contracts/GlobalOptions.cs | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/migrationTool/GlobalOptionsBinder.cs b/migrationTool/GlobalOptionsBinder.cs index 1643bd3..9568f97 100644 --- a/migrationTool/GlobalOptionsBinder.cs +++ b/migrationTool/GlobalOptionsBinder.cs @@ -24,6 +24,13 @@ internal class GlobalOptionsBinder : BinderBase description: @"The directory where the logs are written. Defaults to the working directory" ); + private static readonly Option _tenant = new Option( + aliases: new[] { "--tenant" }, + description: "The azure tenant to use") + { + Arity = ArgumentArity.ExactlyOne + }; + private static readonly Option _subscription = new Option( aliases: new[] { "--subscription", "-s" }, description: "The azure subscription to use") @@ -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); @@ -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. diff --git a/migrationTool/Program.cs b/migrationTool/Program.cs index 563470f..7dde926 100644 --- a/migrationTool/Program.cs +++ b/migrationTool/Program.cs @@ -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(new DefaultAzureCredential(includeInteractiveCredentials: true)); + } + else + { + var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions() + { + TenantId = tenantId, + }); + collection.AddSingleton(credential); + } + collection - .AddSingleton(new DefaultAzureCredential(includeInteractiveCredentials: true)) .AddSingleton(options) .AddSingleton(console) .AddSingleton, AssetMigrationTracker>() diff --git a/migrationTool/contracts/GlobalOptions.cs b/migrationTool/contracts/GlobalOptions.cs index 8bee85c..f258a34 100644 --- a/migrationTool/contracts/GlobalOptions.cs +++ b/migrationTool/contracts/GlobalOptions.cs @@ -3,6 +3,7 @@ namespace AMSMigrate.Contracts { public record GlobalOptions( + string TenantId, string SubscriptionId, string ResourceGroup, CloudType CloudType,