2525import java .util .concurrent .ExecutionException ;
2626import java .util .concurrent .ExecutorService ;
2727import java .util .concurrent .Executors ;
28+
2829import javax .ejb .Local ;
2930import javax .inject .Inject ;
3031import javax .naming .ConfigurationException ;
3132
32- import com .cloud .utils .DateUtil ;
3333import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
3434import org .apache .cloudstack .api .BaseListTemplateOrIsoPermissionsCmd ;
3535import org .apache .cloudstack .api .BaseUpdateTemplateOrIsoCmd ;
4848import org .apache .cloudstack .api .command .user .template .RegisterTemplateCmd ;
4949import org .apache .cloudstack .api .command .user .template .UpdateTemplateCmd ;
5050import org .apache .cloudstack .api .command .user .template .UpdateTemplatePermissionsCmd ;
51- import org .apache .cloudstack .engine .subsystem .api .storage .*;
51+ import org .apache .cloudstack .engine .subsystem .api .storage .DataObject ;
52+ import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
53+ import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
54+ import org .apache .cloudstack .engine .subsystem .api .storage .EndPoint ;
55+ import org .apache .cloudstack .engine .subsystem .api .storage .EndPointSelector ;
56+ import org .apache .cloudstack .engine .subsystem .api .storage .Scope ;
57+ import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotDataFactory ;
58+ import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotInfo ;
59+ import org .apache .cloudstack .engine .subsystem .api .storage .StorageCacheManager ;
60+ import org .apache .cloudstack .engine .subsystem .api .storage .TemplateDataFactory ;
61+ import org .apache .cloudstack .engine .subsystem .api .storage .TemplateInfo ;
62+ import org .apache .cloudstack .engine .subsystem .api .storage .TemplateService ;
5263import org .apache .cloudstack .engine .subsystem .api .storage .TemplateService .TemplateApiResult ;
64+ import org .apache .cloudstack .engine .subsystem .api .storage .VolumeDataFactory ;
65+ import org .apache .cloudstack .engine .subsystem .api .storage .VolumeInfo ;
66+ import org .apache .cloudstack .engine .subsystem .api .storage .ZoneScope ;
5367import org .apache .cloudstack .framework .async .AsyncCallFuture ;
5468import org .apache .cloudstack .storage .command .AttachCommand ;
5569import org .apache .cloudstack .storage .command .CommandResult ;
6781import com .cloud .agent .api .Answer ;
6882import com .cloud .agent .api .Command ;
6983import com .cloud .agent .api .ComputeChecksumCommand ;
70-
7184import com .cloud .agent .api .storage .DestroyCommand ;
7285import com .cloud .agent .api .to .DataTO ;
7386import com .cloud .agent .api .to .DiskTO ;
8396import com .cloud .dc .DataCenterVO ;
8497import com .cloud .dc .dao .ClusterDao ;
8598import com .cloud .dc .dao .DataCenterDao ;
99+ import com .cloud .domain .Domain ;
86100import com .cloud .domain .dao .DomainDao ;
87101import com .cloud .event .ActionEvent ;
88102import com .cloud .event .EventTypes ;
101115import com .cloud .hypervisor .HypervisorGuruManager ;
102116import com .cloud .projects .Project ;
103117import com .cloud .projects .ProjectManager ;
104-
105118import com .cloud .resource .ResourceManager ;
106119import com .cloud .server .ConfigurationServer ;
120+ import com .cloud .storage .DataStoreRole ;
107121import com .cloud .storage .GuestOSVO ;
108122import com .cloud .storage .LaunchPermissionVO ;
123+ import com .cloud .storage .ScopeType ;
109124import com .cloud .storage .Snapshot ;
110125import com .cloud .storage .SnapshotVO ;
111126import com .cloud .storage .Storage ;
112-
113127import com .cloud .storage .Storage .ImageFormat ;
114128import com .cloud .storage .Storage .TemplateType ;
115- import com .cloud .storage .DataStoreRole ;
116- import com .cloud .storage .ScopeType ;
117129import com .cloud .storage .StorageManager ;
118130import com .cloud .storage .StoragePool ;
119131import com .cloud .storage .StoragePoolHostVO ;
120132import com .cloud .storage .StoragePoolStatus ;
121133import com .cloud .storage .TemplateProfile ;
122134import com .cloud .storage .Upload ;
123- import com .cloud .storage .VMTemplateZoneVO ;
124-
125135import com .cloud .storage .VMTemplateHostVO ;
126136import com .cloud .storage .VMTemplateStoragePoolVO ;
127137import com .cloud .storage .VMTemplateStorageResourceAssoc ;
128138import com .cloud .storage .VMTemplateStorageResourceAssoc .Status ;
129139import com .cloud .storage .VMTemplateVO ;
140+ import com .cloud .storage .VMTemplateZoneVO ;
130141import com .cloud .storage .Volume ;
131142import com .cloud .storage .VolumeManager ;
132143import com .cloud .storage .VolumeVO ;
145156import com .cloud .storage .secondary .SecondaryStorageVmManager ;
146157import com .cloud .storage .upload .UploadMonitor ;
147158import com .cloud .template .TemplateAdapter .TemplateAdapterType ;
148-
149159import com .cloud .user .Account ;
150160import com .cloud .user .AccountManager ;
151161import com .cloud .user .AccountService ;
157167import com .cloud .user .dao .UserAccountDao ;
158168import com .cloud .user .dao .UserDao ;
159169import com .cloud .uservm .UserVm ;
170+ import com .cloud .utils .DateUtil ;
160171import com .cloud .utils .EnumUtils ;
161172import com .cloud .utils .NumbersUtil ;
162173import com .cloud .utils .Pair ;
163174import com .cloud .utils .component .AdapterBase ;
164175import com .cloud .utils .component .ManagerBase ;
165176import com .cloud .utils .concurrency .NamedThreadFactory ;
166- import com .cloud .utils .db .*;
177+ import com .cloud .utils .db .DB ;
178+ import com .cloud .utils .db .Transaction ;
167179import com .cloud .utils .exception .CloudRuntimeException ;
168180import com .cloud .vm .UserVmManager ;
169181import com .cloud .vm .UserVmVO ;
@@ -385,9 +397,7 @@ public String extract(ExtractTemplateCmd cmd) {
385397 if (template == null ) {
386398 throw new InvalidParameterValueException ("unable to find template with id " + templateId );
387399 }
388- TemplateAdapter adapter = getAdapter (template .getHypervisorType ());
389- TemplateProfile profile = adapter .prepareExtractTemplate (cmd );
390-
400+
391401 return extract (caller , templateId , url , zoneId , mode , eventId , false );
392402 }
393403
@@ -711,7 +721,7 @@ public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUn
711721 throw new InvalidParameterValueException ("There is no template " + templateId + " in zone " + sourceZoneId );
712722 }
713723 if (srcSecStore .getScope ().getScopeType () == ScopeType .REGION ) {
714- s_logger .debug ("Template " + templateId + " is in region-wide secondary storage " + dstSecStore .getName () + " , don't need to copy" );
724+ s_logger .debug ("Template " + templateId + " is in region-wide secondary storage " + srcSecStore .getName () + " , don't need to copy" );
715725 return template ;
716726 }
717727
@@ -809,9 +819,6 @@ public boolean stop() {
809819
810820 @ Override
811821 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
812-
813- final Map <String , String > configs = _configDao .getConfiguration ("AgentManager" , params );
814-
815822 String value = _configDao .getValue (Config .PrimaryStorageDownloadWait .toString ());
816823 _primaryStorageDownloadWait = NumbersUtil .parseInt (value , Integer .parseInt (Config .PrimaryStorageDownloadWait .getDefaultValue ()));
817824
@@ -1237,8 +1244,8 @@ public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissions
12371244 }
12381245 }
12391246
1240- Long accountId = template .getAccountId ();
1241- if (accountId == null ) {
1247+ Long ownerId = template .getAccountId ();
1248+ if (ownerId == null ) {
12421249 // if there is no owner of the template then it's probably already a
12431250 // public template (or domain private template) so
12441251 // publishing to individual users is irrelevant
@@ -1270,12 +1277,19 @@ public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissions
12701277 }
12711278
12721279 _tmpltDao .update (template .getId (), updatedTemplate );
1280+
1281+ //when operation is add/remove, accountNames can not be null
1282+ if (("add" .equalsIgnoreCase (operation ) || "remove" .equalsIgnoreCase (operation )) && accountNames == null ) {
1283+ throw new InvalidParameterValueException ("Operation " + operation + " requires accounts or projectIds to be passed in" );
1284+ }
12731285
1274- Long domainId = caller .getDomainId ();
1286+ //Derive the domain id from the template owner as updateTemplatePermissions is not cross domain operation
1287+ Account owner = _accountMgr .getAccount (ownerId );
1288+ Domain domain = _domainDao .findById (owner .getDomainId ());
12751289 if ("add" .equalsIgnoreCase (operation )) {
12761290 txn .start ();
12771291 for (String accountName : accountNames ) {
1278- Account permittedAccount = _accountDao .findActiveAccount (accountName , domainId );
1292+ Account permittedAccount = _accountDao .findActiveAccount (accountName , domain . getId () );
12791293 if (permittedAccount != null ) {
12801294 if (permittedAccount .getId () == caller .getId ()) {
12811295 continue ; // don't grant permission to the template
@@ -1288,15 +1302,15 @@ public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissions
12881302 }
12891303 } else {
12901304 txn .rollback ();
1291- throw new InvalidParameterValueException ("Unable to grant a launch permission to account " + accountName
1305+ throw new InvalidParameterValueException ("Unable to grant a launch permission to account " + accountName + " in domain id=" + domain . getUuid ()
12921306 + ", account not found. " + "No permissions updated, please verify the account names and retry." );
12931307 }
12941308 }
12951309 txn .commit ();
12961310 } else if ("remove" .equalsIgnoreCase (operation )) {
12971311 List <Long > accountIds = new ArrayList <Long >();
12981312 for (String accountName : accountNames ) {
1299- Account permittedAccount = _accountDao .findActiveAccount (accountName , domainId );
1313+ Account permittedAccount = _accountDao .findActiveAccount (accountName , domain . getId () );
13001314 if (permittedAccount != null ) {
13011315 accountIds .add (permittedAccount .getId ());
13021316 }
@@ -1314,10 +1328,6 @@ public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissions
13141328 return true ;
13151329 }
13161330
1317- private String getRandomPrivateTemplateName () {
1318- return UUID .randomUUID ().toString ();
1319- }
1320-
13211331 @ Override
13221332 @ DB
13231333 @ ActionEvent (eventType = EventTypes .EVENT_TEMPLATE_CREATE , eventDescription = "creating template" , async = true )
@@ -1526,7 +1536,6 @@ public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd, Account t
15261536 }
15271537
15281538 volume = this ._volumeDao .findById (snapshot .getVolumeId ());
1529- VolumeVO snapshotVolume = this ._volumeDao .findByIdIncludingRemoved (snapshot .getVolumeId ());
15301539
15311540 // check permissions
15321541 _accountMgr .checkAccess (caller , null , true , snapshot );
0 commit comments