Skip to content

Commit e5b56d8

Browse files
authored
Cleanup parser code, use bool to determine if request is cross sub or not (#17584)
1 parent 371a678 commit e5b56d8

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/Sql/Sql/Database Backup/Cmdlet/RestoreAzureRMSqlDatabase.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,11 @@ protected override AzureSqlDatabaseModel GetEntity()
400400
}
401401

402402
/// get auth headers for cross-sub and cross-tenant restore operations
403-
string targetSubscriptionId = ModelAdapter.Context?.Subscription.Id;
404-
string sourceSubscriptionId = ParseSourceSubscriptionIdFromResourceId(ResourceId);
403+
string targetSubscriptionId = ModelAdapter.Context?.Subscription?.Id;
404+
string sourceSubscriptionId = ParseSubscriptionIdFromResourceId(ResourceId);
405+
bool isCrossSubscriptionRestore = false;
405406
Dictionary<string, List<string>> auxAuthHeader = null;
406-
if (!string.IsNullOrEmpty(ResourceId) && targetSubscriptionId!=sourceSubscriptionId)
407+
if (!string.IsNullOrEmpty(sourceSubscriptionId) && !string.IsNullOrEmpty(targetSubscriptionId) && !targetSubscriptionId.Equals(sourceSubscriptionId, StringComparison.OrdinalIgnoreCase))
407408
{
408409
List<string> resourceIds = new List<string>();
409410
resourceIds.Add(ResourceId);
@@ -412,32 +413,34 @@ protected override AzureSqlDatabaseModel GetEntity()
412413
{
413414
auxAuthHeader = new Dictionary<string, List<string>>(auxHeaderDictionary);
414415
}
416+
417+
isCrossSubscriptionRestore = true;
415418
}
416419

417-
return ModelAdapter.RestoreDatabase(this.ResourceGroupName, restorePointInTime, ResourceId, model, sourceSubscriptionId, auxAuthHeader);
420+
return ModelAdapter.RestoreDatabase(this.ResourceGroupName, restorePointInTime, ResourceId, model, isCrossSubscriptionRestore, auxAuthHeader);
418421
}
419422

420423
/// <summary>
421-
/// Parse source subscription id from ResourceId
424+
/// Parse subscription id from ResourceId
422425
/// </summary>
423-
/// <returns>Source Subscription Id</returns>
424-
private string ParseSourceSubscriptionIdFromResourceId(string resourceId)
426+
/// <returns>Subscription Id</returns>
427+
private string ParseSubscriptionIdFromResourceId(string resourceId)
425428
{
426-
if (string.IsNullOrEmpty(resourceId))
429+
string subscriptionId = null;
430+
if (!string.IsNullOrEmpty(resourceId))
427431
{
428-
return null;
429-
}
430-
431-
string[] words = resourceId.Split('/');
432-
string sourceSubscriptionId = "";
433-
for (int i = 0; i < words.Length; i++)
434-
{
435-
if (words[i] == "subscriptions")
432+
string[] words = resourceId.Split('/');
433+
for (int i = 0; i < words.Length - 1; i++)
436434
{
437-
sourceSubscriptionId = words[i + 1];
435+
if ("subscriptions".Equals(words[i], StringComparison.OrdinalIgnoreCase))
436+
{
437+
subscriptionId = words[i + 1];
438+
break;
439+
}
438440
}
439441
}
440-
return sourceSubscriptionId;
442+
443+
return subscriptionId;
441444
}
442445
}
443446
}

src/Sql/Sql/Database Backup/Services/AzureSqlDatabaseBackupAdapter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ internal AzureSqlDatabaseGeoBackupPolicyModel SetDatabaseGeoBackupPolicy(
581581
/// <param name="restorePointInTime">A point to time to restore to (for PITR and dropped DB restore)</param>
582582
/// <param name="resourceId">The resource ID of the DB to restore (live, geo backup, deleted database, long term retention backup, etc.)</param>
583583
/// <param name="model">An object modeling the database to create via restore</param>
584-
/// <param name="sourceSubscriptionId">Source Subscription Id</param>
584+
/// <param name="isCrossSubscriptionRestore">Is cross subscription restore</param>
585585
/// <param name="customHeaders">Custom headers</param>
586586
/// <returns>Restored database object</returns>
587-
internal AzureSqlDatabaseModel RestoreDatabase(string resourceGroup, DateTime restorePointInTime, string resourceId, AzureSqlDatabaseModel model, string sourceSubscriptionId, Dictionary<string, List<string>> customHeaders = null)
587+
internal AzureSqlDatabaseModel RestoreDatabase(string resourceGroup, DateTime restorePointInTime, string resourceId, AzureSqlDatabaseModel model, bool isCrossSubscriptionRestore, Dictionary<string, List<string>> customHeaders = null)
588588
{
589589
// Construct the ARM resource Id of the pool
590590
string elasticPoolId = string.IsNullOrWhiteSpace(model.ElasticPoolName) ? null : AzureSqlDatabaseModel.PoolIdTemplate.FormatInvariant(
@@ -613,16 +613,16 @@ internal AzureSqlDatabaseModel RestoreDatabase(string resourceGroup, DateTime re
613613
};
614614

615615
// check if restore operation is cross subscription or same subscription
616-
if (_subscription.Id != sourceSubscriptionId)
616+
if (isCrossSubscriptionRestore)
617617
{
618618
// cross subscription path
619619
if (dbModel.CreateMode != Management.Sql.Models.CreateMode.Recovery
620620
&& dbModel.CreateMode != Management.Sql.Models.CreateMode.Restore
621-
&& dbModel.CreateMode != Management.Sql.Models.CreateMode.PointInTimeRestore
622-
&& dbModel.CreateMode != Management.Sql.Models.CreateMode.RestoreLongTermRetentionBackup)
621+
&& dbModel.CreateMode != Management.Sql.Models.CreateMode.PointInTimeRestore)
623622
{
624-
throw new ArgumentException("Restore mode not supported");
623+
throw new ArgumentException("Restore mode not supported for cross subscription restore. Supported restore modes for cross subscription are Recovery, Restore, PointInTimeRestore.");
625624
}
625+
626626
dbModel.SourceResourceId = resourceId;
627627
}
628628
else

0 commit comments

Comments
 (0)