@@ -239,52 +239,15 @@ private void ImportTemplateImage()
239
239
240
240
private string GetAzureVmSasUri ( string vmImageName )
241
241
{
242
- ComputeManagementClient computeClient = new ComputeManagementClient ( this . Client . Credentials , this . Client . BaseUri ) ;
243
- VirtualMachineOSImageGetResponse imageGetResponse = computeClient . VirtualMachineOSImages . Get ( vmImageName ) ;
242
+ string mediaLinkUri = null ;
244
243
Uri uri = null ;
245
244
StorageManagementClient storageClient = null ;
246
245
string storageAccountName = null ;
247
246
StorageAccountGetKeysResponse getKeysResponse = null ;
248
247
ErrorRecord er = null ;
249
248
250
- if ( imageGetResponse == null || string . IsNullOrEmpty ( imageGetResponse . Name ) )
251
- {
252
- er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
253
- string . Format ( "Image with name {0} not found." , vmImageName ) ,
254
- String . Empty ,
255
- Client . TemplateImages ,
256
- ErrorCategory . InvalidArgument
257
- ) ;
258
-
259
- ThrowTerminatingError ( er ) ;
260
- }
261
-
262
- if ( string . Compare ( imageGetResponse . OperatingSystemType , "Windows" , true ) != 0 )
263
- {
264
- er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
265
- String . Format ( "Invalid Argument: OS Image type is {0}. It must be Windows." , imageGetResponse . OperatingSystemType ) ,
266
- String . Empty ,
267
- Client . TemplateImages ,
268
- ErrorCategory . InvalidArgument
269
- ) ;
270
-
271
- ThrowTerminatingError ( er ) ;
272
- }
273
-
274
- if ( imageGetResponse . MediaLinkUri == null || string . IsNullOrEmpty ( imageGetResponse . MediaLinkUri . AbsoluteUri ) )
275
- {
276
- er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
277
- String . Format ( "Invalid Argument: Cannot use {0} because it is an Azure Gallery image. Only uploaded images can be used." , vmImageName ) ,
278
- String . Empty ,
279
- Client . TemplateImages ,
280
- ErrorCategory . InvalidArgument
281
- ) ;
282
-
283
- ThrowTerminatingError ( er ) ;
284
- }
285
-
286
- // If reached so far, the image is good
287
- uri = new Uri ( imageGetResponse . MediaLinkUri . AbsoluteUri ) ;
249
+ mediaLinkUri = GetImageMediaLinkUri ( vmImageName ) ;
250
+ uri = new Uri ( mediaLinkUri ) ;
288
251
storageClient = new StorageManagementClient ( this . Client . Credentials , this . Client . BaseUri ) ;
289
252
storageAccountName = uri . Authority . Split ( '.' ) [ 0 ] ;
290
253
getKeysResponse = storageClient . StorageAccounts . GetKeys ( storageAccountName ) ;
@@ -306,12 +269,12 @@ private string GetAzureVmSasUri(string vmImageName)
306
269
307
270
if ( sas != null )
308
271
{
309
- return imageGetResponse . MediaLinkUri . AbsoluteUri + sas ;
272
+ return mediaLinkUri + sas ;
310
273
}
311
274
else
312
275
{
313
276
er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
314
- String . Format ( "Couldn't get Sas for template imge uri. Error {0}" , imageGetResponse . StatusCode . ToString ( ) ) ,
277
+ "Couldn't get Sas for template image uri." ,
315
278
String . Empty ,
316
279
Client . TemplateImages ,
317
280
ErrorCategory . ConnectionError
@@ -335,6 +298,116 @@ private string GetAzureVmSasUri(string vmImageName)
335
298
return null ;
336
299
}
337
300
301
+ public string GetImageMediaLinkUri ( string vmImageName )
302
+ {
303
+ ComputeManagementClient computeClient = new ComputeManagementClient ( this . Client . Credentials , this . Client . BaseUri ) ;
304
+ VirtualMachineOSImageGetResponse imageGetResponse = null ;
305
+ VirtualMachineVMImageListResponse vmList = null ;
306
+ string osType = null ;
307
+ string mediaLinkUri = null ;
308
+ ErrorRecord er = null ;
309
+
310
+ try
311
+ {
312
+ imageGetResponse = computeClient . VirtualMachineOSImages . Get ( vmImageName ) ;
313
+
314
+ if ( imageGetResponse != null )
315
+ {
316
+ osType = imageGetResponse . OperatingSystemType ;
317
+
318
+ if ( imageGetResponse . MediaLinkUri != null )
319
+ {
320
+ mediaLinkUri = imageGetResponse . MediaLinkUri . AbsoluteUri ;
321
+ }
322
+ }
323
+ }
324
+ catch ( Hyak . Common . CloudException cloudEx )
325
+ {
326
+ if ( cloudEx . Error . Code == "ResourceNotFound" )
327
+ {
328
+ try
329
+ {
330
+ vmList = computeClient . VirtualMachineVMImages . List ( ) ;
331
+
332
+ foreach ( VirtualMachineVMImageListResponse . VirtualMachineVMImage image in vmList . VMImages )
333
+ {
334
+ if ( string . Compare ( image . Name , vmImageName , true ) == 0 )
335
+ {
336
+ if ( image . OSDiskConfiguration != null )
337
+ {
338
+ osType = image . OSDiskConfiguration . OperatingSystem ;
339
+
340
+ if ( image . OSDiskConfiguration . MediaLink != null )
341
+ {
342
+ mediaLinkUri = image . OSDiskConfiguration . MediaLink . AbsoluteUri ;
343
+ }
344
+
345
+ break ;
346
+ }
347
+ else
348
+ {
349
+ er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
350
+ string . Format ( "No OSDiskConfiguration found for image {0}." , vmImageName ) ,
351
+ String . Empty ,
352
+ Client . TemplateImages ,
353
+ ErrorCategory . InvalidArgument
354
+ ) ;
355
+
356
+ ThrowTerminatingError ( er ) ;
357
+ }
358
+ }
359
+ }
360
+ }
361
+ catch
362
+ {
363
+ throw ;
364
+ }
365
+ }
366
+ else
367
+ {
368
+ throw ;
369
+ }
370
+ }
371
+ catch ( Exception ex )
372
+ {
373
+ er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
374
+ ex . Message ,
375
+ String . Empty ,
376
+ Client . TemplateImages ,
377
+ ErrorCategory . InvalidArgument
378
+ ) ;
379
+
380
+ ThrowTerminatingError ( er ) ;
381
+ }
382
+
383
+ if ( string . Compare ( osType , "Windows" , true ) != 0 )
384
+ {
385
+ er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
386
+ osType == null ? String . Format ( "Couldn't find image with name {0}" , vmImageName ) :
387
+ String . Format ( "Invalid Argument: OS Image type is {0}. It must be Windows." , osType ) ,
388
+ String . Empty ,
389
+ Client . TemplateImages ,
390
+ ErrorCategory . InvalidArgument
391
+ ) ;
392
+
393
+ ThrowTerminatingError ( er ) ;
394
+ }
395
+
396
+ if ( string . IsNullOrEmpty ( mediaLinkUri ) )
397
+ {
398
+ er = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
399
+ String . Format ( "Invalid Argument: Cannot use {0} because it is an Azure Gallery image. Only uploaded images can be used." , vmImageName ) ,
400
+ String . Empty ,
401
+ Client . TemplateImages ,
402
+ ErrorCategory . InvalidArgument
403
+ ) ;
404
+
405
+ ThrowTerminatingError ( er ) ;
406
+ }
407
+
408
+ return mediaLinkUri ;
409
+ }
410
+
338
411
public override void ExecuteCmdlet ( )
339
412
{
340
413
// register the subscription for this service if it has not been before
0 commit comments