2020import java .util .Map ;
2121
2222import javax .inject .Inject ;
23- import javax .naming .ConfigurationException ;
2423
2524import org .apache .cloudstack .api .ApiConstants ;
2625import org .apache .cloudstack .api .command .user .iso .DeleteIsoCmd ;
4645import com .cloud .hypervisor .Hypervisor .HypervisorType ;
4746import com .cloud .org .Grouping ;
4847import com .cloud .storage .GuestOS ;
49- import com .cloud .storage .VMTemplateVO ;
5048import com .cloud .storage .Storage .ImageFormat ;
5149import com .cloud .storage .Storage .TemplateType ;
5250import com .cloud .storage .TemplateProfile ;
51+ import com .cloud .storage .VMTemplateVO ;
5352import com .cloud .storage .dao .VMTemplateDao ;
5453import com .cloud .storage .dao .VMTemplateHostDao ;
5554import com .cloud .storage .dao .VMTemplateZoneDao ;
@@ -80,6 +79,7 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
8079 protected @ Inject HostDao _hostDao ;
8180 protected @ Inject ResourceLimitService _resourceLimitMgr ;
8281 protected @ Inject DataStoreManager storeMgr ;
82+ @ Inject TemplateManager templateMgr ;
8383
8484 @ Override
8585 public boolean stop () {
@@ -211,10 +211,16 @@ public TemplateProfile prepare(boolean isIso, long userId, String name, String d
211211 }
212212 }
213213
214+ DataStore imageStore = this .templateMgr .getImageStore (imageStoreUuid , zoneId );
215+ if (imageStore == null ) {
216+ throw new IllegalArgumentException ("Cann't find an image store" );
217+ }
218+ Long imageStoreId = imageStore .getId ();
219+
214220 Long id = _tmpltDao .getNextInSequence (Long .class , "id" );
215221 UserContext .current ().setEventDetails ("Id: " +id + " name: " + name );
216222 return new TemplateProfile (id , userId , name , displayText , bits , passwordEnabled , requiresHVM , url , isPublic ,
217- featured , isExtractable , imgfmt , guestOSId , zoneId , hypervisorType , templateOwner .getAccountName (), templateOwner .getDomainId (), templateOwner .getAccountId (), chksum , bootable , templateTag , details , sshkeyEnabled , imageStoreUuid );
223+ featured , isExtractable , imgfmt , guestOSId , zoneId , hypervisorType , templateOwner .getAccountName (), templateOwner .getDomainId (), templateOwner .getAccountId (), chksum , bootable , templateTag , details , sshkeyEnabled , imageStoreId );
218224 }
219225
220226 @ Override
@@ -224,7 +230,7 @@ public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocatio
224230 Account owner = _accountMgr .getAccount (cmd .getEntityOwnerId ());
225231 _accountMgr .checkAccess (caller , null , true , owner );
226232
227-
233+
228234
229235 return prepare (false , UserContext .current ().getCallerUserId (), cmd .getTemplateName (), cmd .getDisplayText (),
230236 cmd .getBits (), cmd .isPasswordEnabled (), cmd .getRequiresHvm (), cmd .getUrl (), cmd .isPublic (), cmd .isFeatured (),
@@ -251,7 +257,7 @@ protected VMTemplateVO persistTemplate(TemplateProfile profile) {
251257 profile .getPasswordEnabled (), profile .getGuestOsId (), profile .getBootable (), profile .getHypervisorType (), profile .getTemplateTag (),
252258 profile .getDetails (), profile .getSshKeyEnabled ());
253259
254-
260+ template . setImageDataStoreId ( profile . getImageStoreId ());
255261 if (zoneId == null || zoneId .longValue () == -1 ) {
256262 List <DataCenterVO > dcs = _dcDao .listAll ();
257263
0 commit comments