|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.api.command.user.vm; |
18 | 18 |
|
19 | | -import com.cloud.dc.DataCenter; |
20 | | -import com.cloud.dc.DataCenter.NetworkType; |
21 | | -import com.cloud.event.EventTypes; |
22 | | -import com.cloud.exception.ConcurrentOperationException; |
23 | | -import com.cloud.exception.InsufficientCapacityException; |
24 | | -import com.cloud.exception.InsufficientServerCapacityException; |
25 | | -import com.cloud.exception.InvalidParameterValueException; |
26 | | -import com.cloud.exception.ResourceAllocationException; |
27 | | -import com.cloud.exception.ResourceUnavailableException; |
28 | | -import com.cloud.hypervisor.Hypervisor.HypervisorType; |
29 | | -import com.cloud.network.Network; |
30 | | -import com.cloud.network.Network.IpAddresses; |
31 | | -import com.cloud.offering.DiskOffering; |
32 | | -import com.cloud.offering.ServiceOffering; |
33 | | -import com.cloud.template.VirtualMachineTemplate; |
34 | | -import com.cloud.user.Account; |
35 | | -import com.cloud.uservm.UserVm; |
36 | | -import com.cloud.utils.net.NetUtils; |
37 | | -import com.cloud.vm.VirtualMachine; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
38 | 27 | import org.apache.cloudstack.acl.RoleType; |
39 | 28 | import org.apache.cloudstack.affinity.AffinityGroupResponse; |
40 | 29 | import org.apache.cloudstack.api.ACL; |
|
59 | 48 | import org.apache.cloudstack.context.CallContext; |
60 | 49 | import org.apache.log4j.Logger; |
61 | 50 |
|
62 | | -import java.util.ArrayList; |
63 | | -import java.util.Collection; |
64 | | -import java.util.HashMap; |
65 | | -import java.util.Iterator; |
66 | | -import java.util.LinkedHashMap; |
67 | | -import java.util.List; |
68 | | -import java.util.Map; |
| 51 | +import com.cloud.event.EventTypes; |
| 52 | +import com.cloud.exception.ConcurrentOperationException; |
| 53 | +import com.cloud.exception.InsufficientCapacityException; |
| 54 | +import com.cloud.exception.InsufficientServerCapacityException; |
| 55 | +import com.cloud.exception.InvalidParameterValueException; |
| 56 | +import com.cloud.exception.ResourceAllocationException; |
| 57 | +import com.cloud.exception.ResourceUnavailableException; |
| 58 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 59 | +import com.cloud.network.Network; |
| 60 | +import com.cloud.network.Network.IpAddresses; |
| 61 | +import com.cloud.uservm.UserVm; |
| 62 | +import com.cloud.utils.net.NetUtils; |
| 63 | +import com.cloud.vm.VirtualMachine; |
69 | 64 |
|
70 | 65 | @APICommand(name = "deployVirtualMachine", description = "Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted, entityType = {VirtualMachine.class}, |
71 | 66 | requestHasSensitiveInfo = false, responseHasSensitiveInfo = true) |
72 | | -public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd { |
| 67 | +public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd implements SecurityGroupAction { |
73 | 68 | public static final Logger s_logger = Logger.getLogger(DeployVMCmd.class.getName()); |
74 | 69 |
|
75 | 70 | private static final String s_name = "deployvirtualmachineresponse"; |
@@ -257,26 +252,12 @@ public boolean isDisplay() { |
257 | 252 | return displayVm; |
258 | 253 | } |
259 | 254 |
|
260 | | - public List<Long> getSecurityGroupIdList() { |
261 | | - if (securityGroupNameList != null && securityGroupIdList != null) { |
262 | | - throw new InvalidParameterValueException("securitygroupids parameter is mutually exclusive with securitygroupnames parameter"); |
263 | | - } |
| 255 | + public List<String> getSecurityGroupNameList() { |
| 256 | + return securityGroupNameList; |
| 257 | + } |
264 | 258 |
|
265 | | - //transform group names to ids here |
266 | | - if (securityGroupNameList != null) { |
267 | | - List<Long> securityGroupIds = new ArrayList<Long>(); |
268 | | - for (String groupName : securityGroupNameList) { |
269 | | - Long groupId = _responseGenerator.getSecurityGroupId(groupName, getEntityOwnerId()); |
270 | | - if (groupId == null) { |
271 | | - throw new InvalidParameterValueException("Unable to find group by name " + groupName); |
272 | | - } else { |
273 | | - securityGroupIds.add(groupId); |
274 | | - } |
275 | | - } |
276 | | - return securityGroupIds; |
277 | | - } else { |
278 | | - return securityGroupIdList; |
279 | | - } |
| 259 | + public List<Long> getSecurityGroupIdList() { |
| 260 | + return securityGroupIdList; |
280 | 261 | } |
281 | 262 |
|
282 | 263 | public Long getServiceOfferingId() { |
@@ -328,7 +309,7 @@ public boolean getStartVm() { |
328 | 309 | return startVm == null ? true : startVm; |
329 | 310 | } |
330 | 311 |
|
331 | | - private Map<Long, IpAddresses> getIpToNetworkMap() { |
| 312 | + public Map<Long, IpAddresses> getIpToNetworkMap() { |
332 | 313 | if ((networkIds != null || ipAddress != null || getIp6Address() != null) && ipToNetworkList != null) { |
333 | 314 | throw new InvalidParameterValueException("NetworkIds and ipAddress can't be specified along with ipToNetworkMap parameter"); |
334 | 315 | } |
@@ -363,6 +344,10 @@ private Map<Long, IpAddresses> getIpToNetworkMap() { |
363 | 344 | return ipToNetworkMap; |
364 | 345 | } |
365 | 346 |
|
| 347 | + public String getIpAddress() { |
| 348 | + return ipAddress; |
| 349 | + } |
| 350 | + |
366 | 351 | public String getIp6Address() { |
367 | 352 | if (ip6Address == null) { |
368 | 353 | return null; |
@@ -392,6 +377,11 @@ public List<Long> getAffinityGroupIdList() { |
392 | 377 | } |
393 | 378 | } |
394 | 379 |
|
| 380 | + public String getKeyboard() { |
| 381 | + // TODO Auto-generated method stub |
| 382 | + return keyboard; |
| 383 | + } |
| 384 | + |
395 | 385 | ///////////////////////////////////////////////////// |
396 | 386 | /////////////// API Implementation/////////////////// |
397 | 387 | ///////////////////////////////////////////////////// |
@@ -478,137 +468,11 @@ public void execute() { |
478 | 468 | } |
479 | 469 | } |
480 | 470 |
|
481 | | - // this is an opportunity to verify that parameters that came in via the Details Map are OK |
482 | | - // for example, minIops and maxIops should either both be specified or neither be specified and, |
483 | | - // if specified, minIops should be <= maxIops |
484 | | - private void verifyDetails() { |
485 | | - Map<String, String> map = getDetails(); |
486 | | - |
487 | | - if (map != null) { |
488 | | - String minIops = (String)map.get("minIops"); |
489 | | - String maxIops = (String)map.get("maxIops"); |
490 | | - |
491 | | - verifyMinAndMaxIops(minIops, maxIops); |
492 | | - |
493 | | - minIops = (String)map.get("minIopsDo"); |
494 | | - maxIops = (String)map.get("maxIopsDo"); |
495 | | - |
496 | | - verifyMinAndMaxIops(minIops, maxIops); |
497 | | - } |
498 | | - } |
499 | | - |
500 | | - private void verifyMinAndMaxIops(String minIops, String maxIops) { |
501 | | - if ((minIops != null && maxIops == null) || (minIops == null && maxIops != null)) { |
502 | | - throw new InvalidParameterValueException("Either 'Min IOPS' and 'Max IOPS' must both be specified or neither be specified."); |
503 | | - } |
504 | | - |
505 | | - long lMinIops; |
506 | | - |
507 | | - try { |
508 | | - if (minIops != null) { |
509 | | - lMinIops = Long.parseLong(minIops); |
510 | | - } |
511 | | - else { |
512 | | - lMinIops = 0; |
513 | | - } |
514 | | - } |
515 | | - catch (NumberFormatException ex) { |
516 | | - throw new InvalidParameterValueException("'Min IOPS' must be a whole number."); |
517 | | - } |
518 | | - |
519 | | - long lMaxIops; |
520 | | - |
521 | | - try { |
522 | | - if (maxIops != null) { |
523 | | - lMaxIops = Long.parseLong(maxIops); |
524 | | - } |
525 | | - else { |
526 | | - lMaxIops = 0; |
527 | | - } |
528 | | - } |
529 | | - catch (NumberFormatException ex) { |
530 | | - throw new InvalidParameterValueException("'Max IOPS' must be a whole number."); |
531 | | - } |
532 | | - |
533 | | - if (lMinIops > lMaxIops) { |
534 | | - throw new InvalidParameterValueException("'Min IOPS' must be less than or equal to 'Max IOPS'."); |
535 | | - } |
536 | | - } |
537 | 471 |
|
538 | 472 | @Override |
539 | 473 | public void create() throws ResourceAllocationException { |
540 | 474 | try { |
541 | | - //Verify that all objects exist before passing them to the service |
542 | | - Account owner = _accountService.getActiveAccountById(getEntityOwnerId()); |
543 | | - |
544 | | - verifyDetails(); |
545 | | - |
546 | | - DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId); |
547 | | - if (zone == null) { |
548 | | - throw new InvalidParameterValueException("Unable to find zone by id=" + zoneId); |
549 | | - } |
550 | | - |
551 | | - ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId); |
552 | | - if (serviceOffering == null) { |
553 | | - throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId); |
554 | | - } |
555 | | - |
556 | | - if(!serviceOffering.isDynamic()) { |
557 | | - for(String detail: getDetails().keySet()) { |
558 | | - if(detail.equalsIgnoreCase("cpuNumber") || detail.equalsIgnoreCase("cpuSpeed") || detail.equalsIgnoreCase("memory")) { |
559 | | - throw new InvalidParameterValueException("cpuNumber or cpuSpeed or memory should not be specified for static service offering"); |
560 | | - } |
561 | | - } |
562 | | - } |
563 | | - |
564 | | - VirtualMachineTemplate template = _entityMgr.findById(VirtualMachineTemplate.class, templateId); |
565 | | - // Make sure a valid template ID was specified |
566 | | - if (template == null) { |
567 | | - throw new InvalidParameterValueException("Unable to find the template " + templateId); |
568 | | - } |
569 | | - |
570 | | - DiskOffering diskOffering = null; |
571 | | - if (diskOfferingId != null) { |
572 | | - diskOffering = _entityMgr.findById(DiskOffering.class, diskOfferingId); |
573 | | - if (diskOffering == null) { |
574 | | - throw new InvalidParameterValueException("Unable to find disk offering " + diskOfferingId); |
575 | | - } |
576 | | - } |
577 | | - |
578 | | - if (!zone.isLocalStorageEnabled()) { |
579 | | - if (serviceOffering.getUseLocalStorage()) { |
580 | | - throw new InvalidParameterValueException("Zone is not configured to use local storage but service offering " + serviceOffering.getName() + " uses it"); |
581 | | - } |
582 | | - if (diskOffering != null && diskOffering.getUseLocalStorage()) { |
583 | | - throw new InvalidParameterValueException("Zone is not configured to use local storage but disk offering " + diskOffering.getName() + " uses it"); |
584 | | - } |
585 | | - } |
586 | | - |
587 | | - UserVm vm = null; |
588 | | - IpAddresses addrs = new IpAddresses(ipAddress, getIp6Address()); |
589 | | - if (zone.getNetworkType() == NetworkType.Basic) { |
590 | | - if (getNetworkIds() != null) { |
591 | | - throw new InvalidParameterValueException("Can't specify network Ids in Basic zone"); |
592 | | - } else { |
593 | | - vm = _userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, getSecurityGroupIdList(), owner, name, displayName, diskOfferingId, |
594 | | - size, group, getHypervisor(), getHttpMethod(), userData, sshKeyPairName, getIpToNetworkMap(), addrs, displayVm, keyboard, getAffinityGroupIdList(), |
595 | | - getDetails(), getCustomId()); |
596 | | - } |
597 | | - } else { |
598 | | - if (zone.isSecurityGroupEnabled()) { |
599 | | - vm = _userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, getNetworkIds(), getSecurityGroupIdList(), owner, name, |
600 | | - displayName, diskOfferingId, size, group, getHypervisor(), getHttpMethod(), userData, sshKeyPairName, getIpToNetworkMap(), addrs, displayVm, keyboard, |
601 | | - getAffinityGroupIdList(), getDetails(), getCustomId()); |
602 | | - |
603 | | - } else { |
604 | | - if (getSecurityGroupIdList() != null && !getSecurityGroupIdList().isEmpty()) { |
605 | | - throw new InvalidParameterValueException("Can't create vm with security groups; security group feature is not enabled per zone"); |
606 | | - } |
607 | | - vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, getNetworkIds(), owner, name, displayName, diskOfferingId, size, group, |
608 | | - getHypervisor(), getHttpMethod(), userData, sshKeyPairName, getIpToNetworkMap(), addrs, displayVm, keyboard, getAffinityGroupIdList(), getDetails(), |
609 | | - getCustomId()); |
610 | | - } |
611 | | - } |
| 475 | + UserVm vm = _userVmService.createVirtualMachine(this); |
612 | 476 |
|
613 | 477 | if (vm != null) { |
614 | 478 | setEntityId(vm.getId()); |
|
0 commit comments