@@ -436,7 +436,7 @@ public String extract(ExtractTemplateCmd cmd) {
436436 }
437437
438438 @ Override
439- public VirtualMachineTemplate prepareTemplate (long templateId , long zoneId ) {
439+ public VirtualMachineTemplate prepareTemplate (long templateId , long zoneId , Long storageId ) {
440440
441441 VMTemplateVO vmTemplate = _tmpltDao .findById (templateId );
442442 if (vmTemplate == null ) {
@@ -445,7 +445,19 @@ public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId) {
445445
446446 _accountMgr .checkAccess (CallContext .current ().getCallingAccount (), AccessType .OperateEntry , true , vmTemplate );
447447
448- prepareTemplateInAllStoragePools (vmTemplate , zoneId );
448+ if (storageId != null ) {
449+ StoragePoolVO pool = _poolDao .findById (storageId );
450+ if (pool != null ) {
451+ if (pool .getStatus () == StoragePoolStatus .Up && pool .getDataCenterId () == zoneId ) {
452+ prepareTemplateInOneStoragePool (vmTemplate , pool );
453+ } else {
454+ s_logger .warn ("Skip loading template " + vmTemplate .getId () + " into primary storage " + pool .getId () + " as either the pool zone "
455+ + pool .getDataCenterId () + " is different from the requested zone " + zoneId + " or the pool is currently not available." );
456+ }
457+ }
458+ } else {
459+ prepareTemplateInAllStoragePools (vmTemplate , zoneId );
460+ }
449461 return vmTemplate ;
450462 }
451463
@@ -556,28 +568,32 @@ public void prepareIsoForVmProfile(VirtualMachineProfile profile) {
556568 }
557569 }
558570
571+ private void prepareTemplateInOneStoragePool (final VMTemplateVO template , final StoragePoolVO pool ) {
572+ s_logger .info ("Schedule to preload template " + template .getId () + " into primary storage " + pool .getId ());
573+ _preloadExecutor .execute (new ManagedContextRunnable () {
574+ @ Override
575+ protected void runInContext () {
576+ try {
577+ reallyRun ();
578+ } catch (Throwable e ) {
579+ s_logger .warn ("Unexpected exception " , e );
580+ }
581+ }
582+
583+ private void reallyRun () {
584+ s_logger .info ("Start to preload template " + template .getId () + " into primary storage " + pool .getId ());
585+ StoragePool pol = (StoragePool )_dataStoreMgr .getPrimaryDataStore (pool .getId ());
586+ prepareTemplateForCreate (template , pol );
587+ s_logger .info ("End of preloading template " + template .getId () + " into primary storage " + pool .getId ());
588+ }
589+ });
590+ }
591+
559592 public void prepareTemplateInAllStoragePools (final VMTemplateVO template , long zoneId ) {
560593 List <StoragePoolVO > pools = _poolDao .listByStatus (StoragePoolStatus .Up );
561594 for (final StoragePoolVO pool : pools ) {
562595 if (pool .getDataCenterId () == zoneId ) {
563- s_logger .info ("Schedule to preload template " + template .getId () + " into primary storage " + pool .getId ());
564- _preloadExecutor .execute (new ManagedContextRunnable () {
565- @ Override
566- protected void runInContext () {
567- try {
568- reallyRun ();
569- } catch (Throwable e ) {
570- s_logger .warn ("Unexpected exception " , e );
571- }
572- }
573-
574- private void reallyRun () {
575- s_logger .info ("Start to preload template " + template .getId () + " into primary storage " + pool .getId ());
576- StoragePool pol = (StoragePool )_dataStoreMgr .getPrimaryDataStore (pool .getId ());
577- prepareTemplateForCreate (template , pol );
578- s_logger .info ("End of preloading template " + template .getId () + " into primary storage " + pool .getId ());
579- }
580- });
596+ prepareTemplateInOneStoragePool (template , pool );
581597 } else {
582598 s_logger .info ("Skip loading template " + template .getId () + " into primary storage " + pool .getId () + " as pool zone " + pool .getDataCenterId () +
583599 " is different from the requested zone " + zoneId );
0 commit comments