Skip to content

Commit ef1e0b6

Browse files
authored
Merge pull request Azure#3123 from krkhan/vmbackup_stgaccts_fix
Add support for multiple resource groups in VMBackup
2 parents f57f5d4 + fd0ffa0 commit ef1e0b6

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/ResourceManager/Compute/Commands.Compute/Extension/AzureVMBackup/AzureVMBackupExtensionProtectedSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Compute.StorageServices;
1516
using System.Collections.Generic;
1617
namespace Microsoft.Azure.Commands.Compute.Extension.AzureDiskEncryption
1718
{
@@ -20,8 +21,12 @@ public class AzureVMBackupBlobSasUris
2021
public AzureVMBackupBlobSasUris()
2122
{
2223
blobSASUri = new List<string>();
24+
pageBlobUri = new List<string>();
25+
storageCredentialsFactory = new List<StorageCredentialsFactory>();
2326
}
2427
public List<string> blobSASUri { get; set; }
28+
public List<string> pageBlobUri { get; set; }
29+
public List<StorageCredentialsFactory> storageCredentialsFactory { get; set; }
2530
}
2631

2732
public class AzureVMBackupExtensionProtectedSettings

src/ResourceManager/Compute/Commands.Compute/Extension/AzureVMBackup/AzureVMBackupExtensionUtil.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
using Newtonsoft.Json;
2929
using System;
3030
using System.Collections.Generic;
31+
using System.Linq;
32+
using System.Text.RegularExpressions;
3133
using System.Threading;
3234
using System.Threading.Tasks;
3335

@@ -76,16 +78,16 @@ private string GetBase64Encoding(object obj)
7678
/// <param name="taskId"></param>
7779
/// <param name="storageCredentialsFactory"></param>
7880
/// <returns></returns>
79-
public List<CloudPageBlob> FindSnapshot(List<string> blobUris, Dictionary<string, string> snapshotQuery, StorageCredentialsFactory storageCredentialsFactory)
81+
public List<CloudPageBlob> FindSnapshot(AzureContext azContext, List<string> blobUris, List<StorageCredentialsFactory> storageCredentialsFactory, Dictionary<string, string> snapshotQuery)
8082
{
8183
List<CloudPageBlob> snapshots = new List<CloudPageBlob>();
8284
for (int i = 0; i < blobUris.Count; i++)
8385
{
8486
BlobUri blobUri = null;
8587
if (BlobUri.TryParseUri(new Uri(blobUris[i]), out blobUri))
8688
{
87-
StorageCredentials sc = storageCredentialsFactory.Create(blobUri);
88-
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(sc, true);
89+
StorageCredentials sc = storageCredentialsFactory[i].Create(blobUri);
90+
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(sc, azContext.Environment.GetEndpointSuffix(AzureEnvironment.Endpoint.StorageEndpointSuffix), true);
8991
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
9092
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobUri.BlobContainerName);
9193
IEnumerable<IListBlobItem> blobs = blobContainer.ListBlobs(null, true, BlobListingDetails.All);
@@ -119,9 +121,19 @@ public List<CloudPageBlob> FindSnapshot(List<string> blobUris, Dictionary<string
119121
}
120122
return snapshots;
121123
}
124+
internal string GetResourceGroupFromId(string id)
125+
{
126+
var matcher = new Regex("/subscriptions/([^/]+)/resourceGroups/([^/]+)/providers/(\\w+)");
127+
var result = matcher.Match(id);
128+
if (!result.Success || result.Groups == null || result.Groups.Count < 3)
129+
{
130+
throw new InvalidOperationException(string.Format("Cannot find resource group name and storage account name from resource identity {0}", id));
131+
}
122132

133+
return result.Groups[2].Value;
134+
}
123135

124-
public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, CloudPageBlobObjectFactory cloudPageBlobObjectFactory)
136+
public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, AzureContext azContext)
125137
{
126138
AzureVMBackupBlobSasUris blobSASUris = new AzureVMBackupBlobSasUris();
127139

@@ -131,6 +143,18 @@ public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, Cloud
131143
BlobUri osBlobUri = null;
132144
if (BlobUri.TryParseUri(new Uri(blobUri), out osBlobUri))
133145
{
146+
StorageManagementClient storageClient = AzureSession.ClientFactory.CreateArmClient<StorageManagementClient>(azContext, AzureEnvironment.Endpoint.ResourceManager);
147+
148+
// Need to convert osBlobUri.StorageAccountName into corresponding resource group name
149+
150+
var listResponse = storageClient.StorageAccounts.List();
151+
var account = listResponse.First(accTemp => accTemp.Name.Equals(osBlobUri.StorageAccountName, StringComparison.InvariantCultureIgnoreCase));
152+
string resourceGroupName = GetResourceGroupFromId(account.Id);
153+
154+
StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(resourceGroupName, storageClient, azContext.Subscription);
155+
156+
CloudPageBlobObjectFactory cloudPageBlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1));
157+
134158
CloudPageBlob pageBlob = cloudPageBlobObjectFactory.Create(osBlobUri);
135159

136160
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
@@ -139,6 +163,8 @@ public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, Cloud
139163

140164
string sasUri = osBlobUri.Uri + pageBlob.GetSharedAccessSignature(sasConstraints);
141165
blobSASUris.blobSASUri.Add(sasUri);
166+
blobSASUris.pageBlobUri.Add(blobUri);
167+
blobSASUris.storageCredentialsFactory.Add(storageCredentialsFactory);
142168
}
143169
else
144170
{
@@ -167,10 +193,11 @@ public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, Vir
167193
StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription);
168194

169195
List<string> blobUris = this.GetDiskBlobUris(virtualMachineResponse.Body);
196+
AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(blobUris, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context);
170197

171198
Dictionary<string, string> snapshotQuery = new Dictionary<string, string>();
172199
snapshotQuery.Add(backupExtensionMetadataName, snapshotTag);
173-
List <CloudPageBlob> snapshots = this.FindSnapshot(blobUris, snapshotQuery, storageCredentialsFactory);
200+
List<CloudPageBlob> snapshots = this.FindSnapshot(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, blobSASUris.pageBlobUri, blobSASUris.storageCredentialsFactory, snapshotQuery);
174201
if (snapshots == null || snapshots.Count == 0)
175202
{
176203
throw new AzureVMBackupException(AzureVMBackupErrorCodes.NoSnapshotFound, "snapshot with the tag not found.");
@@ -200,15 +227,10 @@ public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshot
200227
var virtualMachine = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(
201228
vmConfig.ResourceGroupName,
202229
vmConfig.VMName);
203-
StorageManagementClient storageClient = AzureSession.ClientFactory.CreateArmClient<StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager);
204-
205-
StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription);
206-
207-
CloudPageBlobObjectFactory cloudPageBlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1));
208230

209231
List<string> vmPageBlobUris = this.GetDiskBlobUris(virtualMachine.Body);
210232

211-
AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(vmPageBlobUris, cloudPageBlobObjectFactory);
233+
AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(vmPageBlobUris, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context);
212234

213235
string taskId = Guid.NewGuid().ToString();
214236

@@ -262,7 +284,7 @@ public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshot
262284
int i = 0;
263285
for (; i < loopingTimes; i++)
264286
{
265-
List<CloudPageBlob> snapshotsFound = this.FindSnapshot(vmPageBlobUris, snapshotQuery, storageCredentialsFactory);
287+
List<CloudPageBlob> snapshotsFound = this.FindSnapshot(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, blobSASUris.pageBlobUri, blobSASUris.storageCredentialsFactory, snapshotQuery);
266288
if (snapshotsFound.Count == vmPageBlobUris.Count)
267289
{
268290
break;

0 commit comments

Comments
 (0)