Skip to content

Commit 1a74dfa

Browse files
committed
Move UpdateTemplate and UpdateIso methods from ManagerServerImpl to
TemplateManagerImpl.
1 parent fe4f53b commit 1a74dfa

7 files changed

Lines changed: 110 additions & 105 deletions

File tree

api/src/com/cloud/server/ManagementService.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,12 @@ public interface ManagementService {
123123
/**
124124
* Searches for servers by the specified search criteria Can search by: "name", "type", "state", "dataCenterId",
125125
* "podId"
126-
*
126+
*
127127
* @param cmd
128128
* @return List of Hosts
129129
*/
130130
Pair<List<? extends Host>, Integer> searchForServers(ListHostsCmd cmd);
131131

132-
/**
133-
* Creates a new template
134-
*
135-
* @param cmd
136-
* @return updated template
137-
*/
138-
VirtualMachineTemplate updateTemplate(UpdateIsoCmd cmd);
139-
140-
VirtualMachineTemplate updateTemplate(UpdateTemplateCmd cmd);
141132

142133

143134

api/src/com/cloud/template/TemplateApiService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
2525
import org.apache.cloudstack.api.command.user.iso.ExtractIsoCmd;
2626
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
27+
import org.apache.cloudstack.api.command.user.iso.UpdateIsoCmd;
2728
import org.apache.cloudstack.api.command.user.template.CopyTemplateCmd;
2829
import org.apache.cloudstack.api.command.user.template.CreateTemplateCmd;
2930
import org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd;
3031
import org.apache.cloudstack.api.command.user.template.ExtractTemplateCmd;
3132
import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd;
33+
import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd;
3234

3335
import com.cloud.exception.InternalErrorException;
3436
import com.cloud.exception.ResourceAllocationException;
@@ -90,11 +92,14 @@ public interface TemplateApiService {
9092
List<String> listTemplatePermissions(BaseListTemplateOrIsoPermissionsCmd cmd);
9193

9294
boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissionsCmd cmd);
93-
95+
9496
VirtualMachineTemplate createPrivateTemplateRecord(CreateTemplateCmd cmd,
9597
Account templateOwner) throws ResourceAllocationException;
9698

9799
VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command)
98100
throws CloudRuntimeException;
99101

102+
VirtualMachineTemplate updateTemplate(UpdateIsoCmd cmd);
103+
104+
VirtualMachineTemplate updateTemplate(UpdateTemplateCmd cmd);
100105
}

api/src/org/apache/cloudstack/api/command/user/iso/UpdateIsoCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public long getEntityOwnerId() {
6666

6767
@Override
6868
public void execute(){
69-
VirtualMachineTemplate result = _mgr.updateTemplate(this);
69+
VirtualMachineTemplate result = _templateService.updateTemplate(this);
7070
if (result != null) {
7171
TemplateResponse response = _responseGenerator.createIsoResponse(result);
7272
response.setResponseName(getCommandName());

api/src/org/apache/cloudstack/api/command/user/template/UpdateTemplateCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public long getEntityOwnerId() {
6666

6767
@Override
6868
public void execute(){
69-
VirtualMachineTemplate result = _mgr.updateTemplate(this);
69+
VirtualMachineTemplate result = _templateService.updateTemplate(this);
7070
if (result != null) {
7171
TemplateResponse response = _responseGenerator.createIsoResponse(result);
7272
response.setObjectName("template");

server/src/com/cloud/server/ManagementServerImpl.java

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,99 +1275,7 @@ private Set<Pair<Long, Long>> listTemplates(Long templateId, String name, String
12751275
return templateZonePairSet;
12761276
}
12771277

1278-
@Override
1279-
public VMTemplateVO updateTemplate(UpdateIsoCmd cmd) {
1280-
return updateTemplateOrIso(cmd);
1281-
}
1282-
1283-
@Override
1284-
public VMTemplateVO updateTemplate(UpdateTemplateCmd cmd) {
1285-
return updateTemplateOrIso(cmd);
1286-
}
1287-
1288-
private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
1289-
Long id = cmd.getId();
1290-
String name = cmd.getTemplateName();
1291-
String displayText = cmd.getDisplayText();
1292-
String format = cmd.getFormat();
1293-
Long guestOSId = cmd.getOsTypeId();
1294-
Boolean passwordEnabled = cmd.isPasswordEnabled();
1295-
Boolean bootable = cmd.isBootable();
1296-
Integer sortKey = cmd.getSortKey();
1297-
Account account = UserContext.current().getCaller();
1298-
1299-
// verify that template exists
1300-
VMTemplateVO template = _templateDao.findById(id);
1301-
if (template == null || template.getRemoved() != null) {
1302-
InvalidParameterValueException ex = new InvalidParameterValueException("unable to find template/iso with specified id");
1303-
ex.addProxyObject(template, id, "templateId");
1304-
throw ex;
1305-
}
1306-
1307-
// Don't allow to modify system template
1308-
if (id == Long.valueOf(1)) {
1309-
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to update template/iso of specified id");
1310-
ex.addProxyObject(template, id, "templateId");
1311-
throw ex;
1312-
}
1313-
1314-
// do a permission check
1315-
_accountMgr.checkAccess(account, AccessType.ModifyEntry, true, template);
1316-
1317-
boolean updateNeeded = !(name == null && displayText == null && format == null && guestOSId == null && passwordEnabled == null
1318-
&& bootable == null && sortKey == null);
1319-
if (!updateNeeded) {
1320-
return template;
1321-
}
1322-
1323-
template = _templateDao.createForUpdate(id);
1324-
1325-
if (name != null) {
1326-
template.setName(name);
1327-
}
1328-
1329-
if (displayText != null) {
1330-
template.setDisplayText(displayText);
1331-
}
1332-
1333-
if (sortKey != null) {
1334-
template.setSortKey(sortKey);
1335-
}
1336-
1337-
ImageFormat imageFormat = null;
1338-
if (format != null) {
1339-
try {
1340-
imageFormat = ImageFormat.valueOf(format.toUpperCase());
1341-
} catch (IllegalArgumentException e) {
1342-
throw new InvalidParameterValueException("Image format: " + format + " is incorrect. Supported formats are "
1343-
+ EnumUtils.listValues(ImageFormat.values()));
1344-
}
1345-
1346-
template.setFormat(imageFormat);
1347-
}
1348-
1349-
if (guestOSId != null) {
1350-
GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
13511278

1352-
if (guestOS == null) {
1353-
throw new InvalidParameterValueException("Please specify a valid guest OS ID.");
1354-
} else {
1355-
template.setGuestOSId(guestOSId);
1356-
}
1357-
}
1358-
1359-
if (passwordEnabled != null) {
1360-
template.setEnablePassword(passwordEnabled);
1361-
}
1362-
1363-
if (bootable != null) {
1364-
template.setBootable(bootable);
1365-
}
1366-
1367-
_templateDao.update(id, template);
1368-
1369-
return _templateDao.findById(id);
1370-
}
13711279

13721280
@Override
13731281
public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(ListPublicIpAddressesCmd cmd) {

server/src/com/cloud/template/TemplateManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.apache.cloudstack.api.command.user.iso.UpdateIsoCmd;
22+
import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd;
2123
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
2224
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
2325
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
@@ -125,4 +127,7 @@ VMTemplateHostVO getTemplateHostRef(long zoneId, long tmpltId,
125127

126128
List<DataStore> getImageStoreByTemplate(long templateId, Long zoneId);
127129

130+
131+
132+
128133
}

server/src/com/cloud/template/TemplateManagerImpl.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@
4040

4141
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4242
import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd;
43+
import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoCmd;
4344
import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoPermissionsCmd;
4445
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
4546
import org.apache.cloudstack.api.command.user.iso.ExtractIsoCmd;
4647
import org.apache.cloudstack.api.command.user.iso.ListIsoPermissionsCmd;
4748
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
49+
import org.apache.cloudstack.api.command.user.iso.UpdateIsoCmd;
4850
import org.apache.cloudstack.api.command.user.iso.UpdateIsoPermissionsCmd;
4951
import org.apache.cloudstack.api.command.user.template.CopyTemplateCmd;
5052
import org.apache.cloudstack.api.command.user.template.CreateTemplateCmd;
5153
import org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd;
5254
import org.apache.cloudstack.api.command.user.template.ExtractTemplateCmd;
5355
import org.apache.cloudstack.api.command.user.template.ListTemplatePermissionsCmd;
5456
import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd;
57+
import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd;
5558
import org.apache.cloudstack.api.command.user.template.UpdateTemplatePermissionsCmd;
5659
import org.apache.cloudstack.engine.subsystem.api.storage.CommandResult;
5760
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
@@ -177,6 +180,7 @@
177180
import com.cloud.user.dao.UserAccountDao;
178181
import com.cloud.user.dao.UserDao;
179182
import com.cloud.uservm.UserVm;
183+
import com.cloud.utils.EnumUtils;
180184
import com.cloud.utils.NumbersUtil;
181185
import com.cloud.utils.Pair;
182186
import com.cloud.utils.component.AdapterBase;
@@ -2225,5 +2229,97 @@ public List<DataStore> getImageStoreByTemplate(long templateId, Long zoneId) {
22252229
return stores;
22262230
}
22272231

2232+
@Override
2233+
public VMTemplateVO updateTemplate(UpdateIsoCmd cmd) {
2234+
return updateTemplateOrIso(cmd);
2235+
}
2236+
2237+
@Override
2238+
public VMTemplateVO updateTemplate(UpdateTemplateCmd cmd) {
2239+
return updateTemplateOrIso(cmd);
2240+
}
2241+
2242+
private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
2243+
Long id = cmd.getId();
2244+
String name = cmd.getTemplateName();
2245+
String displayText = cmd.getDisplayText();
2246+
String format = cmd.getFormat();
2247+
Long guestOSId = cmd.getOsTypeId();
2248+
Boolean passwordEnabled = cmd.isPasswordEnabled();
2249+
Boolean bootable = cmd.isBootable();
2250+
Integer sortKey = cmd.getSortKey();
2251+
Account account = UserContext.current().getCaller();
2252+
2253+
// verify that template exists
2254+
VMTemplateVO template = _tmpltDao.findById(id);
2255+
if (template == null || template.getRemoved() != null) {
2256+
InvalidParameterValueException ex = new InvalidParameterValueException("unable to find template/iso with specified id");
2257+
ex.addProxyObject(template, id, "templateId");
2258+
throw ex;
2259+
}
2260+
2261+
// Don't allow to modify system template
2262+
if (id == Long.valueOf(1)) {
2263+
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to update template/iso of specified id");
2264+
ex.addProxyObject(template, id, "templateId");
2265+
throw ex;
2266+
}
2267+
2268+
// do a permission check
2269+
_accountMgr.checkAccess(account, AccessType.ModifyEntry, true, template);
2270+
2271+
boolean updateNeeded = !(name == null && displayText == null && format == null && guestOSId == null && passwordEnabled == null
2272+
&& bootable == null && sortKey == null);
2273+
if (!updateNeeded) {
2274+
return template;
2275+
}
2276+
2277+
template = _tmpltDao.createForUpdate(id);
22282278

2279+
if (name != null) {
2280+
template.setName(name);
2281+
}
2282+
2283+
if (displayText != null) {
2284+
template.setDisplayText(displayText);
2285+
}
2286+
2287+
if (sortKey != null) {
2288+
template.setSortKey(sortKey);
2289+
}
2290+
2291+
ImageFormat imageFormat = null;
2292+
if (format != null) {
2293+
try {
2294+
imageFormat = ImageFormat.valueOf(format.toUpperCase());
2295+
} catch (IllegalArgumentException e) {
2296+
throw new InvalidParameterValueException("Image format: " + format + " is incorrect. Supported formats are "
2297+
+ EnumUtils.listValues(ImageFormat.values()));
2298+
}
2299+
2300+
template.setFormat(imageFormat);
2301+
}
2302+
2303+
if (guestOSId != null) {
2304+
GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
2305+
2306+
if (guestOS == null) {
2307+
throw new InvalidParameterValueException("Please specify a valid guest OS ID.");
2308+
} else {
2309+
template.setGuestOSId(guestOSId);
2310+
}
2311+
}
2312+
2313+
if (passwordEnabled != null) {
2314+
template.setEnablePassword(passwordEnabled);
2315+
}
2316+
2317+
if (bootable != null) {
2318+
template.setBootable(bootable);
2319+
}
2320+
2321+
_tmpltDao.update(id, template);
2322+
2323+
return _tmpltDao.findById(id);
2324+
}
22292325
}

0 commit comments

Comments
 (0)