|
40 | 40 |
|
41 | 41 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
42 | 42 | import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd; |
| 43 | +import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoCmd; |
43 | 44 | import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoPermissionsCmd; |
44 | 45 | import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd; |
45 | 46 | import org.apache.cloudstack.api.command.user.iso.ExtractIsoCmd; |
46 | 47 | import org.apache.cloudstack.api.command.user.iso.ListIsoPermissionsCmd; |
47 | 48 | import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd; |
| 49 | +import org.apache.cloudstack.api.command.user.iso.UpdateIsoCmd; |
48 | 50 | import org.apache.cloudstack.api.command.user.iso.UpdateIsoPermissionsCmd; |
49 | 51 | import org.apache.cloudstack.api.command.user.template.CopyTemplateCmd; |
50 | 52 | import org.apache.cloudstack.api.command.user.template.CreateTemplateCmd; |
51 | 53 | import org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd; |
52 | 54 | import org.apache.cloudstack.api.command.user.template.ExtractTemplateCmd; |
53 | 55 | import org.apache.cloudstack.api.command.user.template.ListTemplatePermissionsCmd; |
54 | 56 | import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd; |
| 57 | +import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd; |
55 | 58 | import org.apache.cloudstack.api.command.user.template.UpdateTemplatePermissionsCmd; |
56 | 59 | import org.apache.cloudstack.engine.subsystem.api.storage.CommandResult; |
57 | 60 | import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
|
177 | 180 | import com.cloud.user.dao.UserAccountDao; |
178 | 181 | import com.cloud.user.dao.UserDao; |
179 | 182 | import com.cloud.uservm.UserVm; |
| 183 | +import com.cloud.utils.EnumUtils; |
180 | 184 | import com.cloud.utils.NumbersUtil; |
181 | 185 | import com.cloud.utils.Pair; |
182 | 186 | import com.cloud.utils.component.AdapterBase; |
@@ -2225,5 +2229,97 @@ public List<DataStore> getImageStoreByTemplate(long templateId, Long zoneId) { |
2225 | 2229 | return stores; |
2226 | 2230 | } |
2227 | 2231 |
|
| 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); |
2228 | 2278 |
|
| 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 | + } |
2229 | 2325 | } |
0 commit comments