28
28
using Newtonsoft . Json ;
29
29
using System ;
30
30
using System . Collections . Generic ;
31
+ using System . Linq ;
32
+ using System . Text . RegularExpressions ;
31
33
using System . Threading ;
32
34
using System . Threading . Tasks ;
33
35
@@ -76,16 +78,16 @@ private string GetBase64Encoding(object obj)
76
78
/// <param name="taskId"></param>
77
79
/// <param name="storageCredentialsFactory"></param>
78
80
/// <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 )
80
82
{
81
83
List < CloudPageBlob > snapshots = new List < CloudPageBlob > ( ) ;
82
84
for ( int i = 0 ; i < blobUris . Count ; i ++ )
83
85
{
84
86
BlobUri blobUri = null ;
85
87
if ( BlobUri . TryParseUri ( new Uri ( blobUris [ i ] ) , out blobUri ) )
86
88
{
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 ) ;
89
91
CloudBlobClient blobClient = cloudStorageAccount . CreateCloudBlobClient ( ) ;
90
92
CloudBlobContainer blobContainer = blobClient . GetContainerReference ( blobUri . BlobContainerName ) ;
91
93
IEnumerable < IListBlobItem > blobs = blobContainer . ListBlobs ( null , true , BlobListingDetails . All ) ;
@@ -119,9 +121,19 @@ public List<CloudPageBlob> FindSnapshot(List<string> blobUris, Dictionary<string
119
121
}
120
122
return snapshots ;
121
123
}
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
+ }
122
132
133
+ return result . Groups [ 2 ] . Value ;
134
+ }
123
135
124
- public AzureVMBackupBlobSasUris GenerateBlobSasUris ( List < string > blobUris , CloudPageBlobObjectFactory cloudPageBlobObjectFactory )
136
+ public AzureVMBackupBlobSasUris GenerateBlobSasUris ( List < string > blobUris , AzureContext azContext )
125
137
{
126
138
AzureVMBackupBlobSasUris blobSASUris = new AzureVMBackupBlobSasUris ( ) ;
127
139
@@ -131,6 +143,18 @@ public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, Cloud
131
143
BlobUri osBlobUri = null ;
132
144
if ( BlobUri . TryParseUri ( new Uri ( blobUri ) , out osBlobUri ) )
133
145
{
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
+
134
158
CloudPageBlob pageBlob = cloudPageBlobObjectFactory . Create ( osBlobUri ) ;
135
159
136
160
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy ( ) ;
@@ -139,6 +163,8 @@ public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, Cloud
139
163
140
164
string sasUri = osBlobUri . Uri + pageBlob . GetSharedAccessSignature ( sasConstraints ) ;
141
165
blobSASUris . blobSASUri . Add ( sasUri ) ;
166
+ blobSASUris . pageBlobUri . Add ( blobUri ) ;
167
+ blobSASUris . storageCredentialsFactory . Add ( storageCredentialsFactory ) ;
142
168
}
143
169
else
144
170
{
@@ -167,10 +193,11 @@ public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, Vir
167
193
StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory ( vmConfig . ResourceGroupName , storageClient , virtualMachineExtensionBaseCmdlet . DefaultProfile . Context . Subscription ) ;
168
194
169
195
List < string > blobUris = this . GetDiskBlobUris ( virtualMachineResponse . Body ) ;
196
+ AzureVMBackupBlobSasUris blobSASUris = this . GenerateBlobSasUris ( blobUris , virtualMachineExtensionBaseCmdlet . DefaultProfile . Context ) ;
170
197
171
198
Dictionary < string , string > snapshotQuery = new Dictionary < string , string > ( ) ;
172
199
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 ) ;
174
201
if ( snapshots == null || snapshots . Count == 0 )
175
202
{
176
203
throw new AzureVMBackupException ( AzureVMBackupErrorCodes . NoSnapshotFound , "snapshot with the tag not found." ) ;
@@ -200,15 +227,10 @@ public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshot
200
227
var virtualMachine = virtualMachineExtensionBaseCmdlet . ComputeClient . ComputeManagementClient . VirtualMachines . GetWithInstanceView (
201
228
vmConfig . ResourceGroupName ,
202
229
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 ) ) ;
208
230
209
231
List < string > vmPageBlobUris = this . GetDiskBlobUris ( virtualMachine . Body ) ;
210
232
211
- AzureVMBackupBlobSasUris blobSASUris = this . GenerateBlobSasUris ( vmPageBlobUris , cloudPageBlobObjectFactory ) ;
233
+ AzureVMBackupBlobSasUris blobSASUris = this . GenerateBlobSasUris ( vmPageBlobUris , virtualMachineExtensionBaseCmdlet . DefaultProfile . Context ) ;
212
234
213
235
string taskId = Guid . NewGuid ( ) . ToString ( ) ;
214
236
@@ -262,7 +284,7 @@ public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshot
262
284
int i = 0 ;
263
285
for ( ; i < loopingTimes ; i ++ )
264
286
{
265
- List < CloudPageBlob > snapshotsFound = this . FindSnapshot ( vmPageBlobUris , snapshotQuery , storageCredentialsFactory ) ;
287
+ List < CloudPageBlob > snapshotsFound = this . FindSnapshot ( virtualMachineExtensionBaseCmdlet . DefaultProfile . Context , blobSASUris . pageBlobUri , blobSASUris . storageCredentialsFactory , snapshotQuery ) ;
266
288
if ( snapshotsFound . Count == vmPageBlobUris . Count )
267
289
{
268
290
break ;
0 commit comments