Skip to content

[Az.RecoveryServices.Backup] Fix AFS Restore Command Bug #28302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,71 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
targetFolder = "/";
}

GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(recoveryPoint.ContainerName.Split(';')[2]);
GenericResource storageAccountResource = null;
string sourceResourceId = null;
string storageAccountLocation = vaultLocation; // Default to vault location
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption that the storage account location should default to vault location may not always be correct. Consider adding a comment explaining why this is a reasonable fallback or implement logic to determine the actual storage account location.

Suggested change
string storageAccountLocation = vaultLocation; // Default to vault location
// Default to vault location as a fallback. This assumption is based on the expectation
// that in most cases, the storage account and the vault are in the same location.
// However, this may not always be true. If the storage account location cannot be retrieved,
// we log a warning and proceed with the fallback. Ensure that this fallback is safe for your scenario.
string storageAccountLocation = vaultLocation;

Copilot uses AI. Check for mistakes.


try
{
storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(recoveryPoint.ContainerName.Split(';')[2]);
if (storageAccountResource != null)
{
sourceResourceId = storageAccountResource.Id;
storageAccountLocation = storageAccountResource.Location;
Logger.Instance.WriteDebug("Successfully retrieved storage account resource");
}
}
catch (Exception ex)
{
Logger.Instance.WriteDebug($"Failed to get storage account resource: {ex.Message}. Will attempt to retrieve from protected item.");
storageAccountResource = null;
}

// If storage account retrieval failed, fall back to getting it from protected item
if (storageAccountResource == null || string.IsNullOrEmpty(sourceResourceId))
{
try
{
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(recoveryPoint.Id);
string containerUri = HelperUtils.GetContainerUri(keyValueDict, recoveryPoint.Id);
string protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, recoveryPoint.Id);

var protectedItemResponse = ServiceClientAdapter.GetProtectedItem(
containerUri,
protectedItemUri,
null,
vaultName: vaultName,
resourceGroupName: resourceGroupName);

if (protectedItemResponse?.Body?.Properties is AzureFileshareProtectedItem afsProtectedItem)
{
sourceResourceId = afsProtectedItem.SourceResourceId;
Logger.Instance.WriteDebug("Retrieved source resource ID from protected item");

// Create a minimal resource object with the source resource ID
if (!string.IsNullOrEmpty(sourceResourceId))
{
storageAccountResource = new GenericResource
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new GenericResource with minimal properties could lead to issues if other parts of the code expect additional properties to be populated. Consider documenting this limitation or ensuring all required properties are set.

Copilot uses AI. Check for mistakes.

{
Id = sourceResourceId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initiliazing this way might not work. please confirm

Location = storageAccountLocation
};
}
}
}
catch (Exception ex)
{
Logger.Instance.WriteDebug($"Failed to get protected item: {ex.Message}");
}
}

// Validate that we have a source resource ID
if (storageAccountResource == null || string.IsNullOrEmpty(storageAccountResource.Id))
{
throw new ArgumentException(string.Format(Resources.AzureFileShareNotFound,
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses 'AzureFileShareNotFound' resource which may not accurately describe the actual problem - missing source resource ID rather than a missing file share. Consider using a more specific error message that indicates the source storage account resource ID could not be retrieved.

Copilot uses AI. Check for mistakes.

recoveryPoint.ContainerName));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test case for this. also have we thoroughly tested this change?

GenericResource targetStorageAccountResource = null;
string targetStorageAccountLocation = null;
if (targetStorageAccountName != null)
Expand Down
1 change: 1 addition & 0 deletions src/RecoveryServices/RecoveryServices/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->
## Upcoming Release
* Added new optional parameter CVMOsDiskEncryptionSetId in Restore-AzRecoveryServicesBackupItem.
* Fixed bug in Restore-AzRecoveryServicesBackupItem cmdlet for Azure File Share restore.

## Version 7.7.2
* Added fix for resume protection with AzureFileshare.
Expand Down
Loading